syntactic analysis tools
DESCRIPTION
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 %% - PowerPoint PPT PresentationTRANSCRIPT
Syntactic Analysis Tools
Natawut Nupairoj, Ph.D.
Department of Computer EngineeringChulalongkorn University
Outline
Overview. Yacc Specification Format. Examples.
Parser Generator
Yaccspecificationtranslate.y
Yacccom p ile r
Ccom p ile r
a .ou t
y.tab .c
y.tab .c a.out
input output
cc y.tab.c -ly
Yacc Specification
A Yacc source program has three parts:declarations
%%
translation rules
%%
supporting C-routines
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;
%%
Example: Calculator Programyylex(){
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;
}
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 definitionLex definition can get them through “y.tab.h”.
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;
%%
Example: Yacc with Lex (Yacc)yyerror(char* errmsg){
fprintf(stderr, "%s\n", errmsg);}
main(int argc, char** argv){
yyparse();return 0;
}
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 \nnumber [0-9]+symbol [\+\*\(\)]
%%{ws} {/* no action and no return */}{eol} { return EOL; }{number} { yylval = atoi(yytext); return(NUMBER); }{symbol} { return yytext[0]; }%%
Compile Yacc and Lex
byacc –d calc.y
flex calc.l
gcc –o calc y.tab.c lex.yy.c -lfl