Example Lexical Analyzer

As you finish up working on the parser, if you’re feeling shaky about your own scanner, you can grab lexer.zip.

This is a scanner generated by Flex, the GNU Fast Lexical Analyzer generator. You need the .c and .h files to compile a working program.

The file example.l is the Flex source file; you won’t need this unless you want to change the scanner. The file lex.yy.c was generated with the command flex -l example.l.

Take a look at main.c to see how the lexical analyzer works. Call yylex() to get the next token. The value returned by yylex() will be one of the token types defined in y.tab.h. When yylex() finishes, the variable yytext will contain the corresponding lexeme.

Comments are closed.