1 / 11

Syntactic Analysis Tools

Syntactic Analysis Tools. Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University. Outline. Overview. Yacc Specification Format. Examples. Parser Generator. Yacc Specification. A Yacc source program has three parts: declarations %% translation rules %%

Download Presentation

Syntactic Analysis Tools

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. Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

  2. Outline • Overview. • Yacc Specification Format. • Examples.

  3. Parser Generator

  4. Yacc Specification • A Yacc source program has three parts: declarations %% translation rules %% supporting C-routines

  5. Example: Calculator Program %{ #include <stdio.h> %} %token DIGIT %% line : expr '\n' { printf("%d\n", $1); } ; expr : expr '+' term { $$ = $1 + $3; } | term ; term : term '*' factor { $$ = $1 * $3; } | factor ; factor : '(' expr ')' { $$ = $2; } | DIGIT ; %%

  6. Example: Calculator Program yylex() { int c; c = getchar(); if(c >= '0' && c <= '9') { yylval = c - '0'; return DIGIT; } return c; } yyerror(char* errmsg) { fprintf(stderr, "%s\n", errmsg); } main(int argc, char** argv) { yyparse(); return 0; }

  7. How to use Yacc with Lex • yyparse calls yylex to get the next token automatically. • yylex returns: • token type or 0 (EOF). • yylval - token attribute. • Tokens are defined in yacc definition • Lex definition can get them through “y.tab.h”.

  8. Example: Yacc with Lex (Yacc) %{ #include <stdio.h> %} %token EOL NUMBER %% line : expr EOL { printf("%d\n", $1); } ; expr : expr '+' term { $$ = $1 + $3; } | term ; term : term '*' factor { $$ = $1 * $3; } | factor ; factor : '(' expr ')' { $$ = $2; } | NUMBER ; %%

  9. Example: Yacc with Lex (Yacc) yyerror(char* errmsg) { fprintf(stderr, "%s\n", errmsg); } main(int argc, char** argv) { yyparse(); return 0; }

  10. Example: Yacc with Lex (Lex) %{ /* define constants for C program here */ #include <stdlib.h> #include "y.tab.h" extern int yylval; %} /* regular definitions */ delim [ \t] ws {delim}+ eol \n number [0-9]+ symbol [\+\*\(\)] %% {ws} {/* no action and no return */} {eol} { return EOL; } {number} { yylval = atoi(yytext); return(NUMBER); } {symbol} { return yytext[0]; } %%

  11. Compile Yacc and Lex byacc –d calc.y flex calc.l gcc –o calc y.tab.c lex.yy.c -lfl

More Related