1 / 13

lex (1) and flex(1)

lex (1) and flex(1). Lex public interface. FILE * yyin ; /* set before calling yylex () */ int yylex (); /* call once per token */ char yytext []; /* chars matched by yylex () */ int yywrap (); /* end-of-file handler */. .l file format. header %% body %% helper functions.

marlin
Download Presentation

lex (1) and flex(1)

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. lex(1) and flex(1)

  2. Lex public interface • FILE *yyin; /* set before calling yylex() */ • intyylex(); /* call once per token */ • char yytext[]; /* chars matched by yylex() */ • intyywrap(); /* end-of-file handler */

  3. .l file format header %% body %% helper functions

  4. Lex header • C code inside %{ … %} • prototypes for helper functions • #include’s that #define integer token categories • Macro definitions, e.g. letter [a-zA-Z] digit [0-9] ident {letter}({letter}|{digit})* • Warning: macros are fraught with peril

  5. Lex body • Regular expressions with semantic actions “ “ { /* discard */ } {ident} { return IDENT; } “*” { return ASTERISK; } “.” { return PERIOD; } • Match the longest r.e. possible • Break ties with whichever appears first • If it fails to match: copy unmatched to stdout

  6. Lex helper functions • Follows rules of ordinary C code • Compute lexical attributes • Do stuff the regular expressions can’t do • Write a yywrap() to switch files on EOF

  7. Lex regular expressions • \c escapes for most operators • “s” match C string as-is (superescape) • r{m,n} match r between m and n times • r/s match r when s follows • ^r match r when at beginning of line • r$ match r when at end of line

  8. struct token struct token { int category; char *text; intlinenumber; int column; char *filename; union literal value; }

  9. “string removal tool” %% “zap me”

  10. whitespace trimmer %% [ \t]+ putchar(‘ ‘); [ \t]+ /* drop entirely */

  11. string replacement %% username printf(“%s”, getlogin() );

  12. Line/word counter int lines=0, chars=0; %% \n ++lines; ++chars; . ++chars; %% main() { yylex(); printf(“lines: %d chars: %d\n”, lines, chars); }

  13. Example: C reals • Is it: [0-9]*.[0-9]* • Is it: ([0-9]+.[0-9]* | [0-9]*.[0-9]+)

More Related