Contents    Page-10    Prev    Next    Page+10    Index   

Sample lex Specification[Runnable version of Fig. 3.23 from Aho, Lam, Sethi, and Ullman, Compilers.]


%{ /* lexasu.l  Fig. 3.23 from Aho, Lam, Sethi, and Ullman, Compilers */

#define LT 8      /* Example of use:                                       */
#define LE 9      /* lex /projects/cs375/lexasu.l  compile lexasu.l to C   */
#define EQ 6      /* cc lex.yy.c -ll             Compile lex output with C */
#define NE 7      /* a.out                       Execute C output          */
#define GT 11     /* if switch then 3.14 else 4  Test data                 */
#define GE 10     /* 
#define ID      3
#define NUMBER  5
#define OP      1 /* to avoid returning 0 */
#define IF     13
#define THEN   23
#define ELSE    7
int yylval;       /* type of the returned value */

%}   /* regular definitions */

delim   [ \t\n]
ws      {delim}+
letter  [A-Za-z]
digit   [0-9]
id      {letter}({letter}|{digit})*
number  {digit}+(\.{digit}+)?(E[+\-]?{digit}+)?

%%

{ws}     { /* no action and no return */ }
if       { return(IF); }
then     { return(THEN); }
else     { return(ELSE); }
{id}     { yylval = install_id(); return(ID); }
{number} { yylval = install_num(); return(NUMBER); }
"<"      { yylval = LT; return(OP); }
"<="     { yylval = LE; return(OP); }
"="      { yylval = EQ; return(OP); }
"<>"     { yylval = NE; return(OP); }
">"      { yylval = GT; return(OP); }
">="     { yylval = GE; return(OP); }