Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Scanner Generator Lex By J. H. Wang May 10, 2011.

Similar presentations


Presentation on theme: "Using Scanner Generator Lex By J. H. Wang May 10, 2011."— Presentation transcript:

1 Using Scanner Generator Lex By J. H. Wang May 10, 2011

2 Creating a Lexical Analyzer with Lex Lex compiler Lex source program lex.l lex.yy.c C compiler lex.yy.ca.out a.out (The scanner) Input streamSequence of tokens

3 To generate the scanner: –For Lex: lex.l gcc lex.yy.c -o -ll –For flex: flex.l gcc lex.yy.c -o -lfl

4 An Example Patterns for tokens in the grammar – digit  [0-9] digits  digit + number  digits (. digits)? (E [+-]? digits )? letter  [A-Za-z] id  letter ( letter | digit )* if  if then  then else  else relop  | = | = | <> – ws  (blank | tab | newline) +

5 Example Lex Program %{ LT, LE, EQ, NE, GT, GE, IF, THEN, ELSE, ID, NUMBER, RELOP %} delim[ \t\n] ws{delim}+ letter[A-Za-z] digit[0-9] id{letter}({letter}|{digit})* number{digit}+(\.{digit}+)?(E[+-]?{digit}+)? % {ws}{} if{ return(IF); } then{ return(THEN); } else { return(ELSE); }

6 {id}{ yylval = (int) installID(); return (ID); } {number}{ yylval = (int) installNum(); return (NUMBER); } “ “{yylval = NE; return(RELOP); } “>“{yylval = GT; return(RELOP); } “>=“{yylval = GE; return(RELOP); } % int installID() { } int installNum() { }

7 Thanks for Your Attention!


Download ppt "Using Scanner Generator Lex By J. H. Wang May 10, 2011."

Similar presentations


Ads by Google