
Chapter 2
Lexical Analysis
2.1 Introduction
The first step in compiling a program is to break it into tokens. For example, given the j--
program
package pass ;
import java . lang . System ;
public class Factorial {
// Two m e t h o d s and a f ield
public static int f a ctorial ( int n) {
if ( n <= 0)
return 1;
else
return n * fac t o r i a l ( n - 1);
}
public static void m ain ( String [] args ) {
int x = n ;
System . out. println ( x + "! = " + fa c t o r i a l ( x ));
}
static int n = 5;
}
we want to produce the sequence of tokens package, pass, ;, import, java, ., lang, .,
System, ;, public, class, Factorial, {, public, static, int, factorial, (, int, n,),