1 / 16

YACC Example

YACC Example. Taken from LEX & YACC Simple calculator a = 4 + 6 a a=10 b = 7 c = a + b c c = 17 pressure = (78 + 34) * 16.4 $. Grammar. expression ::= expression '+' term | expression '-' term | term term ::= term '*' factor |

samira
Download Presentation

YACC Example

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. YACC Example • Taken from LEX & YACC • Simple calculator a = 4 + 6 a a=10 b = 7 c = a + b c c = 17 pressure = (78 + 34) * 16.4 $

  2. Grammar expression ::= expression '+' term | expression '-' term | term term ::= term '*' factor | term '/' factor | factor factor ::= '(' expression ')' | '-' factor | NUMBER | NAME

  3. parser.h

  4. 0 name value 1 name value 2 name value /* * Header for calculator program */ #define NSYMS 20 /* maximum number of symbols */ struct symtab { char *name; double value; } symtab[NSYMS]; struct symtab *symlook(); 3 name value 4 name value 5 name value 6 name value 7 name value 8 name value 9 name value 10 name value 11 name value 12 name value 13 name value 14 name value parser.h

  5. parser.y

  6. %{ #include "parser.h" #include <string.h> %} %union { double dval; struct symtab *symp; } %token <symp> NAME %token <dval> NUMBER %type <dval> expression %type <dval> term %type <dval> factor %% parser.y

  7. statement_list: statement '\n' | statement_list statement '\n‘ ; statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } ; expression: expression '+' term { $$ = $1 + $3; } | expression '-' term { $$ = $1 - $3; } term ; parser.y

  8. term: term '*' factor { $$ = $1 * $3; } | term '/' factor { if($3 == 0.0) yyerror("divide by zero"); else $$ = $1 / $3; } | factor ; factor: '(' expression ')' { $$ = $2; } | '-' factor { $$ = -$2; } | NUMBER | NAME { $$ = $1->value; } ; %% parser.y

  9. /* look up a symbol table entry, add if not present */ struct symtab *symlook(char *s) { char *p; struct symtab *sp; for(sp = symtab; sp < &symtab[NSYMS]; sp++) { /* is it already here? */ if(sp->name && !strcmp(sp->name, s)) return sp; if(!sp->name) { /* is it free */ sp->name = strdup(s); return sp; } /* otherwise continue to next */ } yyerror("Too many symbols"); exit(1); /* cannot continue */ } /* symlook */ parser.y

  10. yyerror(char *s) { printf( "yyerror: %s\n", s); } parser.y

  11. typedef union { double dval; struct symtab *symp; } YYSTYPE; extern YYSTYPE yylval; # define NAME 257 # define NUMBER 258 y.tab.h

  12. calclexer.l

  13. %{ #include "y.tab.h" #include "parser.h" #include <math.h> %} %% calclexer.l

  14. %% ([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) { yylval.dval = atof(yytext); return NUMBER; } [ \t] ; /* ignore white space */ [A-Za-z][A-Za-z0-9]* { /* return symbol pointer */ yylval.symp = symlook(yytext); return NAME; } "$" { return 0; /* end of input */ } \n |. return yytext[0]; %% calclexer.l

  15. Makefile

  16. Makefile LEX = lex YACC = yacc CC = gcc calcu: y.tab.olex.yy.o $(CC) -ocalcuy.tab.olex.yy.o -ly -ll y.tab.cy.tab.h: parser.y $(YACC) -dparser.y y.tab.o: y.tab.cparser.h $(CC) -cy.tab.c lex.yy.o: y.tab.hlex.yy.c $(CC) -clex.yy.c lex.yy.c: calclexer.lparser.h $(LEX) calclexer.l clean: rm *.o rm *.c rm calcu

More Related