lex flex a lexical analyzer generator
play

Lex (& Flex): A Lexical Analyzer Generator Input: Lex and Yacc - PowerPoint PPT Presentation

Lex (& Flex): A Lexical Analyzer Generator Input: Lex and Yacc Regular exprs defining "tokens" my.l Fragments of C decls & code Output: lex A C program "lex.yy.c" A Quick Tour Use: lex.yy.c


  1. Lex (& Flex): A Lexical Analyzer Generator  Input: Lex and Yacc  Regular exprs defining "tokens" my.l  Fragments of C decls & code  Output: lex  A C program "lex.yy.c" A Quick Tour  Use: lex.yy.c  Compile & link with your main()  Calls to yylex() return successive tokens. Yacc (& Bison & Byacc…): Lex Input: "mylexer.l" A Parser Generator  Input: %{  A context-free grammar #include … Declarations: my.y To front of C  Fragments of C declarations & code int myglobal; program …  Output: Token yacc %}  A C program & some header files code %%  Use: Rules [a-zA-Z]+ {handleit(); return 42; } y.tab.h y.tab.c  Compile & link it with your main() and [ \t\n] {; /* skip whitespace */} Actions  Call yyparse() to parse the entire input file …  yyparse() calls yylex() to get successive tokens %% Subroutines: void handleit() {…} To end of C program … 1

  2. S → E E → E+n | E-n | n Yacc Input: “expr.y” Expression lexer: “expr.l” %{ %{ y.tab.h: C Decls #include … y.tab.c #define NUM 258 #include "y.tab.h" %} #define VAR 259 Yacc %token NUM VAR y.tab.h #define YYSTYPE int %} Decls %% extern YYSTYPE yylval; %% stmt: exp { printf(”%d\n”,$1);} [0-9]+ { yylval = atoi(yytext); return NUM;} ; [ \t] { /* ignore whitespace */ } Rules exp : exp ’+’ NUM { $$ = $1 + $3; } \n { return 0; /* logical EOF */ } and | exp ’-’ NUM { $$ = $1 - $3; } . { return yytext[0]; /* +-*, etc. */ } Actions %% | NUM { $$ = $1; } yyerror(char *msg){printf("%s,%s\n",msg,yytext);} ; int yywrap(){return 1;} %% Subrs … y.tab.c Lex/Yacc Interface: Lex/Yacc Interface: Compile Time Run Time my.y my.l my.c main() yacc lex y.tab.h yyparse() y.tab.c lex.yy.c Token code yylex () yylval gcc Myaction: ... Token value yylval = ... myprog ... return(code) 2

  3. Some C Tidbits More Yacc Declarations Malloc Enums %union { Type of yylval node_t *node; enum kind { root.rchild = (node_t*) title_kind,center_kind}; char *str; } malloc(sizeof(node_t)); typedef struct node_s{ Unions enum kind k; Token %token <str> BHTML BHEAD BTITLE BBODY BCENTER typedef union { struct node_s names & %token <str> EHTML EHEAD ETITLE EBODY ECENTER double d; *lchild,*rchild; types %token <str> P BR LI TEXT int i; char *text; } YYSTYPE; } node_t; Nonterm extern YYSTYPE yylval; %type <node> page head title words body node_t root; names & yylval.d = 3.14; %type <node> heading list center item items types root.k = title_kind; yylval.i = 3; if(root.k==title_kind){…} %start page Start sym Yacc In Action PDA stack: alternates between "states" and symbols from (V ∪ Σ ). initially, push state 0 while not done { let S be the state on top of the stack; let i be the next input symbol (i in Σ ); look at the the action defined in S for i: if "accept", halt and accept; if "error", halt and signal a syntax error; if "shift to state T", push i then T onto the stack; if "reduce via rule r (A → α )", then: pop exactly 2*| α | symbols (the 1st, 3rd, ... will be states, and the 2nd, 4th, ... will be the letters of α ); let T = the state now exposed on top of the stack; T's action for A is "goto state U" for some U; push A, then U onto the stack. } Implementation note: given the tables, it's deterministic, and fast -- just table lookups, push/pop. 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend