/* 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]+) %s CPPCOMM CCOMM %% /*============= 2. The rules (regular expressions) section ======*/ "<" { printf("<"); } ">" { printf(">"); } "//" { printf("//"); BEGIN(CPPCOMM); } . { ECHO; } \n { printf("\n"); BEGIN(INITIAL); } "/*" { printf("/*"); BEGIN(CCOMM); } .|\n { ECHO; } "*/" { printf("*/"); BEGIN(INITIAL); } int|double|char|void|bool|static|const|class|for|while|if|else|return|struct|class|do|this|switch|case|default { printf("%s", yytext); } [a-zA-Z][a-zA-Z0-9]* { // Name ECHO; } (" "|\t|\n|\r\n)* { ECHO; } /* Skip space */ [0-9]+ { // Integer constant // printf("integer constant: %s\n", yytext); printf("%s", 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("%s", yytext); } . { ECHO; } /* Any other character */ %% /*============= The user programs section ===================*/ int main() { printf("\n\n\n\n
```\n");
yylex();
printf("```
\n\n\n"); return 0; } int yywrap() { return 1; } /* Stop at end of file */