/* Print names, integer and double constants from input stream */ /*============= 1. The definitions section ====================*/ %{ #include #include #include %} /* Macro defining the exponent in double constant */ EXP ((E|e)("+"|"-")?[0-9]+) %% /*============= 2. The rules (regular expressions) section ======*/ [a-zA-Z][a-zA-Z0-9]* { printf("name: %s\n", yytext); } (" "|\t|\n|\r\n)* {} /* Skip space */ /* Integer constant */ [0-9]+ { printf("integer constant: %s\n", yytext); } /* Double constant may be in one of the following 3 forms: */ /* 1) .5, .5e-20, 1.2, 123.456E+10 (mandatory point and fraction) */ /* 2) 1., 123.e+10, 0.2E-20, 35.3 (mandatory integer and point) */ /* 3) 1e+10, 2e20, 1E-12 (mandatory integer and exponent) */ /* The expression for double is the union "|" of 3 subexpressions */ ([0-9]*"."[0-9]+{EXP}?)|([0-9]+"."[0-9]*{EXP}?)|([0-9]+{EXP}) { printf("double constant: %s\n", yytext); } . {} /* Any other character */ %% /*============= The user programs section ===================*/ int main() { yylex(); return 0; } int yywrap() { return 1; } /* Stop at end of file */