syntactic analysis tools natawut nupairoj, ph.d. department of computer engineering chulalongkorn...

11
Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Upload: kory-mckinney

Post on 14-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Syntactic Analysis Tools

Natawut Nupairoj, Ph.D.

Department of Computer EngineeringChulalongkorn University

Page 2: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Outline

Overview. Yacc Specification Format. Examples.

Page 3: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

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

Page 4: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Yacc Specification

A Yacc source program has three parts:declarations

%%

translation rules

%%

supporting C-routines

Page 5: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

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;

%%

Page 6: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

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;

}

Page 7: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

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

Page 8: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

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;

%%

Page 9: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Example: Yacc with Lex (Yacc)yyerror(char* errmsg){

fprintf(stderr, "%s\n", errmsg);}

main(int argc, char** argv){

yyparse();return 0;

}

Page 10: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

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]; }%%

Page 11: Syntactic Analysis Tools Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Compile Yacc and Lex

byacc –d calc.y

flex calc.l

gcc –o calc y.tab.c lex.yy.c -lfl