%{ // This is for UCSD CSE 141L, Spring 2006. -Wei and JSampson // // documentation on flex found in: // // http://www.gnu.org/software/flex/manual/html_mono/flex.html // // This is a source file for GNU flex. Flex compiles this file // into a text processing tool using the grammar specified below. // Run flex as to generate lex.yy.c: // > flex sample_lex.l // // The makefile adds an option to rename the generated output file. // // The generated lex.yy.c will provide an API to recognize // certain strings. See further comments below #include #include #include "token.h" int size; // The following definitions specifies regular expressions. Regular // expressions are patterns that may match a given string, indicating // some action is to be taken. The action may communicate things like // what is it classified as, along with other information. // Everything between %{ and %} are directly passed to lex.yy.c %} ws [ \t]+ token [a-zA-Z][a-zA-Z0-9_\-]* comment \#[^\n]+ %% \n { /* This section contains several "rules" consisting of an * a "pattern" and "action". If an input string matches * the pattern, then the action executes, often passing * this information externally. In this actions you can see that * it return a macro defined NEWLINE (see token.h). This is * used in parser.C to count line numbers. * The matched string is visible externally as yytext variable. */ return NEWLINE; } {ws} {comment} {token}: { size = strlen(yytext); yytext[size-1]=0; return LABEL; } {token} { return TOKEN; } %%