Professional Documents
Culture Documents
- A software which will read program as input stream and compare them with already
defined patterns and the generate tokens.
How to implement LA ?
By hand
1- from Transition diagram
By tool
3- Using Lex Tools
.......................................
Writing Lexical Analyzer
1- from Transition diagram
}
................................
Source Transition from lexical Specification
(See on board)
int state=0;
file.open();
char ch;
char lexeme[50];
int i=0;
while ((ch=file.getChar())!=eof){
switch (state){
case 0 : if (isletter(ch))
state=1;
else if (isdigit(ch))
state=3;
else if (ch=='+')
state=5;
else if (ch=='{')
state=6;
else if (ch=='}')
state=7;
else if (ch==';')
state=8;
break;
case 1 : if (isdigit(ch)) || (islet(ch))
state=1;
else
state=2;
break;
case 2 : ungetch(ch);
state=0;
i=0;
if (isKeyword(lexeme)){
cout<<" keyword found";
cout<<lexeme;
}else
cout<<" identifier found";
cout<<lexeme;
cout<<"Token = id ";