Lex Reserved Words |
If your program has a large collection of reserved words it is more efficient to let lex simply match a string and determine in your own code whether it is a variable or reserved word. For example, instead of coding
"if" return IF; "then" return THEN; "else" return ELSE; {letter}({letter}|{digit})* { yylval.id = symLookup(yytext); return IDENTIFIER; }
where symLookup
returns an index into the symbol table, it is better to detect
reserved words and identifiers simultaneously, as follows:
{letter}({letter}|{digit})* { int i; if ((i = resWord(yytext)) != 0) return (i); yylval.id = symLookup(yytext); return (IDENTIFIER); }
This technique significantly reduces the number of states required, and results in smaller scanner tables.