Using Lex
Using Lex
main() { yylex(); }
Rules section
Each rule is made up of two parts
A pattern An action
E.g.
[\t ]+ /* ignore white space */ ;
%% main() { yylex(); }
8
Regular Expressions
Regular expressions used by Lex
. * [] ^ $ {} \ + ? | / ()
Example 2-1
%% [\n\t ] ;
-?(([0-9]+)|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) { printf("number\n"); }
11
12
13
14
Another Problem
%{ letter digit }% [A-Za-z] [0-9]
{return (BEGIN);} {return (END);} {return (ASGOP);} {yyval = enter_id(); return(ID);} {yyval = enter_num(); return (NUM);}
%% enter_id() { /* enter the id in the symbol table and returns entry number */} enter_num() { /* enter the number in the constant table and return entry number */}
15