%{ /* * This is part of the definitions section. It starts with %{ and ends with %}. * Any text placed in this area will be copied verbatim into the lex.yy.c * output file that lex generates, and will be placed near the top of that file. * * Typically this section is used to #include header files that will be needed * in the generated scanner, but any legal C code can be placed here. This * scanner does not need any extra header files, so we don't #include any here. * * Please see https://en.wikipedia.org/wiki/Lex_(software) for further details. */ %} /* * This is also part of the definitions section. Anything after the %} above * until the %% below is interpreted by lex as formal definitions. Things * here won't be copied verbatim into the generated lex.yy.c output file, * but will be analyzed by lex. Some definitions have been placed below. * You may add more definitons here. You may also add comments, but they * must be indented because anything that appears in the leftmost column * is considered the start of a formal definition. */ DIGIT [0-9] HEXD [A-Fa-f] ID [a-z][_a-z0-9]* NUMLIT [0-9][0-9a-fA-F_.#]* %% /* * This is the rules section. It starts with the %% above and continues * until the %% down below. This section contains rules that are analyzed * by lex. Each rule is composed of a pattern and an action. The pattern * of a rule uses UNIX-ish regular expressions to indicate what the lexeme * "looks like", and the action of a rule specifies what is to be done when * a lexeme matching the pattern is encountered. The pattern and the action * of a rule are separated by whitespace. Patterns of a rule can be chained * together using the | operator so that any lexeme matching any of the * chained patterns will provoke the action associated with that rule. An * example of this appears in the "keyword" rule below. * * You may also place comments here, but they also must be indented because * anything that appears in the leftmost column is considered the start of * the pattern of a rule by lex. * * Please see https://en.wikipedia.org/wiki/Lex_(software) for further details. */ {NUMLIT} { printf("%s (num-lit)\n", yytext); } begin | end | function | if | interface | module | procedure | then { /* The action to be taken when we encounter a keyword */ printf("%s (keyword)\n", yytext ); } {ID} printf("%s (identifier)\n", yytext); "+"|";"|"-"|"*"|"/" printf("%s (operator)\n", yytext); [ \t\n]+ /* discard whitespace */ . printf("Unrecognized character: %s\n", yytext); %% /* * This section is the user subroutines section. It starts after the %% above * and continues to the end of the file. Anything appearing in the section is * copied verbatim into the lex.yy.c file generated by lex, and will appear * toward the bottom of that file. * * This section is where subroutines specific to a given scanner are placed. * Any legal C code can be placed here. * * The scanner for this application is not intended to be used by yacc, so * here we define a main function that will "drive" the lexer. */ int main(int argc, char **argv) { if (argc > 1) yyin = fopen(argv[1], "r"); else yyin = stdin; yylex(); } /* * This defines yywrap, a special function used by lex to determine what should * be done when the end of an input file is encountered. You can read more * about this at http://flex.sourceforge.net. */ int yywrap() { return 1; }