Presentation is loading. Please wait.

Presentation is loading. Please wait.

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 $

Similar presentations


Presentation on theme: "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 $"— 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 /* *Header for calculator program */ #define NSYMS 20/* maximum number of symbols */ struct symtab { char *name; double value; } symtab[NSYMS]; struct symtab *symlook(); parser.h namevalue 0 namevalue 1 namevalue 2 namevalue 3 namevalue 4 namevalue 5 namevalue 6 namevalue 7 namevalue 8 namevalue 9 namevalue 10 namevalue 11 namevalue 12 namevalue 13 namevalue 14

5 parser.y

6 %{ #include "parser.h" #include %} %union { double dval; struct symtab *symp; } %token NAME %token NUMBER %type expression %type term %type 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 %} % 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 LEX = lex YACC = yacc CC = gcc calcu:y.tab.o lex.yy.o $(CC) -o calcu y.tab.o lex.yy.o -ly -ll y.tab.c y.tab.h: parser.y $(YACC)-d parser.y y.tab.o: y.tab.c parser.h $(CC) -c y.tab.c lex.yy.o: y.tab.h lex.yy.c $(CC) -c lex.yy.c lex.yy.c: calclexer.l parser.h $(LEX) calclexer.l clean: rm *.o rm *.c rm calcu


Download ppt "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 $"

Similar presentations


Ads by Google