Download presentation
Presentation is loading. Please wait.
Published byConrad Davidson Modified over 8 years ago
1
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY
2
YACC LALR(1) parser generator Yet another compiler compiler (2013-1) Compiler 2 Parser Generator synta x spec. parser
3
YACC BASICS (1) Input/output Specification file format (2013-1) Compiler 3 Yacc filename.y y.tab.c ytab.c filename.tab.c {definitions} % {rules} % {auxiliary routines}
4
YACC BASICS (2) Definitions Information about tokens, data types, grammar rules C code output file Rules Modified BNF format C code Auxiliary routines Procedure and function declarations main() yyparse() yylex() (2013-1) Compiler 4
5
5 exp exp addop term | term addop + | - term term mulop factor | factor mulop * factor ( exp ) | number Given grammar
6
(2013-1) Compiler 6 %{ #include %} %token NUMBER % command : exp {printf(“%d\n”,$1);} exp : exp ‘+’ term {$$ = $1 + $3;} | exp ‘-’ term {$$ = $1 - $3;} | term {$$ = $1;} ; term : term ‘*’ factor {$$ = $1 * $3;} | factor {$$ = $1;} ; factor : NUMBER {$$ = $1;} | ‘(’ exp ‘)’ {$$ = $2;} ; %
7
(2013-1) Compiler 7 main() { return yyparse(); } int yylex(void) { int c; while((c = getchar()) == ‘ ‘); /* blank 제거 */ if (isdigit(c)) { ungetc(c,stdin); scanf(“%d”,&yylval); return(NUMBER); } if (c == ‘\n’) return 0; /* 파싱 정지 */ return(c); } void yyerror(char *s) { fprintf(stderr,”%s\n”,s); /* 에러메시지 출력 */ return 0; }
8
YACC BASICS (3) Some variables & functions yylval: the value of a token ( == lexeme ) yyparse() Name of Yacc-generated parsing procedure Call lexical analysis procedure, yylex() yyerror() (2013-1) Compiler 8
9
YACC OPTIONS (1) -d Header file generation yacc –d filename.y y.tab.h, ytab.h, filename.tab.h Other file #include y.tab.h Call yylex() (2013-1) Compiler 9
10
YACC OPTIONS (2) -v option Verbose option yacc –v filename.y y.output (2013-1) Compiler 10
11
(2009-1) Compiler 11 state 0 $accept : command $end NUMBER shift 5 ( shift 6. error command goto 1 exp goto 2 term goto 3 factor goto 4 state 1 $accept : command_$end $end accept. error state 2 command : exp_ (1) exp : exp_+ term exp : exp_- term + shift 7 - shift 8. reduce 1 state 3 exp : term_ (4) term : term_* factor * shift 9. reduce 4 state 4 term : factor_ (6). reduce 6
12
(2009-1) Compiler 12 state 7 exp : exp +_term NUMBER shift 5 ( shift 6. error term goto 11 factor goto 4 state 8 exp : exp -_term NUMBER shift 5 ( shift 6. error term goto 12 factor goto 4 state 5 factor : NUMBER_ (7). reduce 7 state 6 factor : (_exp ) NUMBER shift 5 ( shift 6. error exp goto 10 term goto 3 factor goto 4
13
(2009-1) Compiler 13 state 11 exp : exp + term_ (2) term : term_* factor * shift 9. reduce 2 state 12 exp : exp – term_ (3) term : term_* factor * shift 9. reduce 3 state 13 term : term * factor_ (5). reduce 5 state 9 term : term *_factor NUMBER shift 5 ( shift 6. error factor goto 13 state 10 exp : exp_+ term exp : exp_- term factor : ( exp_) + shift 7 - shift 8 ) shift 14. error
14
(2009-1) Compiler 14 state 14 factor : ( exp )_ (8). reduce 8 8/127 terminals, 4/600 nonterminals 9/300 grammar rules, 15/1000 states 0 shift/reduce, 0 reduce/reduce conflicts reported 9/601 working sets used memory: states, etc. 36/2000, parser 11/4000 9/601 distinct lookahead sets 6 extra closures 18 shift entries, 1 exceptions 8 goto entries 4 entries saved by goto default Optimizer space used: input 50/2000, output 218/4000 218 table entries, 202 zero maximum spread: 257, maximum offset: 43
15
(2013-1) Compiler 15
16
(2013-1) Compiler 16 0 $accept : command $end 1 command : exp 2 exp : exp '+' term 3 | exp '-' term 4 | term 5 term : term '*' factor 6 | factor 7 factor : NUMBER 8 | '(' exp ')' state 0 $accept :. command $end (0) NUMBER shift 1 '(' shift 2. error command goto 3 exp goto 4 term goto 5 factor goto 6 state 1 factor : NUMBER. (7). reduce 7
17
(2013-1) Compiler 17 state 2 factor : '('. exp ')' (8) NUMBER shift 1 '(' shift 2. error exp goto 7 term goto 5 factor goto 6 state 3 $accept : command. $end (0) $end accept state 4 command : exp. (1) exp : exp. '+' term (2) exp : exp. '-' term (3) '+' shift 8 '-' shift 9 $end reduce 1 state 5 exp : term. (4) term : term. '*' factor (5) '*' shift 10 $end reduce 4 '+' reduce 4 '-' reduce 4 ')' reduce 4
18
(2013-1) Compiler 18 state 6 term : factor. (6). reduce 6 state 7 exp : exp. '+' term (2) exp : exp. '-' term (3) factor : '(' exp. ')' (8) '+' shift 8 '-' shift 9 ')' shift 11. error state 8 exp : exp '+'. term (2) NUMBER shift 1 '(' shift 2. error term goto 12 factor goto 6 state 9 exp : exp '-'. term (3) NUMBER shift 1 '(' shift 2. error term goto 13 factor goto 6
19
(2013-1) Compiler 19 state 10 term : term '*'. factor (5) NUMBER shift 1 '(' shift 2. error factor goto 14 state 11 factor : '(' exp ')'. (8). reduce 8 state 12 exp : exp '+' term. (2) term : term. '*' factor (5) '*' shift 10 $end reduce 2 '+' reduce 2 '-' reduce 2 ')' reduce 2
20
(2013-1) Compiler 20 state 13 exp : exp '-' term. (3) term : term. '*' factor (5) '*' shift 10 $end reduce 3 '+' reduce 3 '-' reduce 3 ')' reduce 3 state 14 term : term '*' factor. (5). reduce 5 8 terminals, 5 nonterminals 9 grammar rules, 15 states
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.