1 / 7

Using Scanner Generator Lex

Using Scanner Generator Lex. By J. H. Wang May 10, 2011. Creating a Lexical Analyzer with Lex. Lex compiler. Lex source program lex.l. lex.yy.c. C compiler. lex.yy.c. a.out. a.out (The scanner). Input stream. Sequence of tokens. To generate the scanner:

helena
Download Presentation

Using Scanner Generator Lex

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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.c a.out a.out(The scanner) Input stream Sequence of tokens

  3. To generate the scanner: • For Lex:lex <file>.lgcc lex.yy.c -o <file> -ll • For flex:flex <file>.lgcc lex.yy.c -o <file> -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  ifthen  thenelse  elserelop  < | > | <= | >= | = | <> • 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 = LT; return(RELOP); }“<=“ {yylval = LE; return(RELOP); }“=“ {yylval = EQ; return(RELOP); }“<>“ {yylval = NE; return(RELOP); }“>“ {yylval = GT; return(RELOP); }“>=“ {yylval = GE; return(RELOP); }%%int installID() {}int installNum() {}

  7. Thanks for Your Attention!

More Related