lex and yacc
play

Lex and Yacc A Quick Tour Lex (& Flex): A Lexical Analyzer - PowerPoint PPT Presentation

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


  1. Lex and Yacc A Quick Tour

  2. Lex (& Flex): A Lexical Analyzer Generator  Input:  Regular exprs defining "tokens" my.l  Fragments of C decls & code  Output: lex  A C program "lex.yy.c"  Use: lex.yy.c  Compile & link with your main()  Calls to yylex() return successive tokens.

  3. Yacc (& Bison & Byacc…): A Parser Generator  Input:  A context-free grammar my.y  Fragments of C declarations & code  Output: yacc  A C program & some header files  Use: y.tab.h y.tab.c  Compile & link it with your main()  Call yyparse() to parse the entire input file  yyparse() calls yylex() to get successive tokens

  4. Lex Input: "mylexer.l" %{ #include … Declarations: To front of C int myglobal; program … Token %} code %% Rules [a-zA-Z]+ {handleit(); return 42; } and [ \t\n] {; /* skip whitespace */} Actions … %% Subroutines: To end of C void handleit() {…} program …

  5. S → E E → E+n | E-n | n Yacc Input: “expr.y” %{ C Decls #include … y.tab.c %} Yacc %token NUM VAR y.tab.h Decls %% stmt: exp { printf(”%d\n”,$1);} ; Rules exp : exp ’+’ NUM { $$ = $1 + $3; } and | exp ’-’ NUM { $$ = $1 - $3; } Actions | NUM { $$ = $1; } ; %% Subrs … y.tab.c

  6. Expression lexer: “expr.l” y.tab.h: %{ #define NUM 258 #include "y.tab.h" #define VAR 259 #define YYSTYPE int %} extern YYSTYPE yylval; %% [0-9]+ { yylval = atoi(yytext); return NUM;} [ \t] { /* ignore whitespace */ } \n { return 0; /* logical EOF */ } . { return yytext[0]; /* +-*, etc. */ } %% yyerror(char *msg){printf("%s,%s\n",msg,yytext);} int yywrap(){return 1;}

  7. Lex/Yacc Interface: Compile Time my.y my.l my.c yacc lex y.tab.h lex.yy.c y.tab.c gcc myprog

  8. Lex/Yacc Interface: Run Time main() yyparse() Token code yylex () yylval Myaction: ... Token value yylval = ... ... return(code)

  9. Some C Tidbits Malloc Enums root.rchild = (node_t*) enum kind { title_kind,center_kind}; malloc(sizeof(node_t)); typedef struct node_s{ Unions enum kind k; typedef union { struct node_s double d; *lchild,*rchild; int i; char *text; } YYSTYPE; } node_t; extern YYSTYPE yylval; node_t root; yylval.d = 3.14; root.k = title_kind; yylval.i = 3; if(root.k==title_kind){…}

  10. More Yacc Declarations %union { Type of yylval node_t *node; char *str; } %token <str> BHTML BHEAD BTITLE BBODY BCENTER Token names & %token <str> EHTML EHEAD ETITLE EBODY ECENTER types %token <str> P BR LI TEXT Nonterm %type <node> page head title words body names & %type <node> heading list center item items types %start page Start sym

  11. 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.

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