![Page 1: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/1.jpg)
1
YACC
Yet Another Compiler Compiler
![Page 2: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/2.jpg)
2
Yacc is a parser generator:
Input: A Grammar
Output: A parser for the grammar
(Reminder: a parser finds derivations)
![Page 3: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/3.jpg)
3
Example grammar:
The yacc code: expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | - expr | INT ;
expr -> ( expr ) | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | - expr | INT ;
![Page 4: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/4.jpg)
4
Exampe Input:
10 * 3 + 4
Yacc Derivation:
expr => expr + expr => expr * expr + expr => 10*3 + 4
![Page 5: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/5.jpg)
5
Resolving Ambiguities
%left '+', '-'%left '*', '/'%left UMINUS%%
expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | '-' expr %prec UMINUS | INT ;
![Page 6: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/6.jpg)
6
Actions
%left '+', '-'%left '*', '/'%left UMINUS%%
expr : '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} ;
![Page 7: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/7.jpg)
7
A Complete Yacc program
%union{ int int_val;}
%left '+', '-'%left '*', '/'%left UMINUS
%token <int_val> INT%type <int_val> expr
%start program
%%
![Page 8: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/8.jpg)
8
program : expr {printf("Expr value = %d \n", $1);} | error {printf("YACC: syntax error near line %d \n", linenum); abort();} ;
expr : '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} ;
%%
#include "lex.yy.c"
![Page 9: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/9.jpg)
9
Execution Example
10 + 20*(3 - 4 + 25)Input:
Output: Expr value = 490
![Page 10: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/10.jpg)
10
The Lex Code%{int linenum=1;int temp_int;%}%%
\n {linenum++;}
[\t ] /* skip spaces */;\/\/[^\n]* /* ignore comments */;
"+" {return '+';}"-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';}"(" {return '(';}
![Page 11: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/11.jpg)
11
[0-9]+ {sscanf(yytext, "%d", &temp_int); yylval.int_val = temp_int; return INT;}
. {printf("LEX: unknown input string found in line %d \n", linenum); abort();}
![Page 12: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/12.jpg)
12
Compiling:
yacc YaccFilelex LexFilecc y.tab.c -ly -ll -o MyParser
Executable: MyParser
![Page 13: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/13.jpg)
13
Another Yacc Program
%union{ int int_val;}
%left '+', '-'%left '*', '/'%left UMINUS
%token <int_val> INT%type <int_val> expr
%start program
%%
![Page 14: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/14.jpg)
14
program : stmt_list | error {printf("YACC: syntax error near line %d \n", linenum); abort();} ;
stmt_list : stmt_list stmt | stmt ;
stmt : expr ';' {printf("Expr value = %d \n", $1);} ;
![Page 15: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/15.jpg)
15
expr : '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} ;
%%
#include "lex.yy.c"
![Page 16: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/16.jpg)
16
Execution Example
10 + 20*(30 -67) / 4;
34 * 35 - 123 + -001;
17*8/6;
Input:
Output: Expr value = -175 Expr value = 1066 Expr value = 22
![Page 17: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/17.jpg)
17
Lex Code
%{int linenum=1;int temp_int;%}%%
\n {linenum++;}
[\t ] /* skip spaces */;\/\/[^\n]* /* ignore comments */;
![Page 18: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/18.jpg)
18
"+" {return '+';}"-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';}"(" {return '(';}";" {return ';';}
[0-9]+ {sscanf(yytext, "%d", &temp_int); yylval.int_val = temp_int; return INT;}
. {printf("LEX: unknown input string found in line %d \n", linenum); abort();}
![Page 19: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/19.jpg)
19
Another Yacc Program%union{ int int_val; char *str_val;}
%left '+', '-'%left '*', '/'%left UMINUS
%token PRINT%token NEWLINE%token <str_val> STRING%token <int_val> INT%type <int_val> expr
%start program%%
![Page 20: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/20.jpg)
20
program : stmt_list | error {printf("YACC: syntax error near line %d \n", linenum); abort();} ;
stmt_list : stmt_list stmt | stmt ;
stmt : expr ';' {printf("expression found\n");} | PRINT expr ';' {printf("%d", $2);} | PRINT STRING ';' {printf("%s", $2);} | PRINT NEWLINE ';' {printf("\n");} ;
![Page 21: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/21.jpg)
21
expr : '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} ;
%%
#include "lex.yy.c"
![Page 22: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/22.jpg)
22
Execution Example
Input: print "The value of expression 123 * 25 is ";print 123 * 25; print newline;10 + 5 * 8;print "end of program";print newline;
Output: The value of expression 123 * 25 is 3075expression foundend of program
![Page 23: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/23.jpg)
23
Lex Code
%{int linenum=1;int temp_int;char temp_str[200];%}%%
\n {linenum++;}
[\t ] /* skip spaces */;\/\/[^\n]* /* ignore comments */;
![Page 24: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/24.jpg)
24
"+" {return '+';}"-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';}"(" {return '(';}";" {return ';';}"print" {return PRINT;}"newline" {return NEWLINE;}
![Page 25: 1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d6d5503460f94a4d567/html5/thumbnails/25.jpg)
25
[0-9]+ {sscanf(yytext, "%d", &temp_int); yylval.int_val = temp_int; return INT;}
\"[^"\n]*\" {strncpy(temp_str, &(yytext[1]), strlen(yytext)-2); temp_str[strlen(yytext)-2] = (char) 0; yylval.str_val = temp_str; return STRING;}
. {printf("LEX: unknown input string found in line %d \n", linenum); abort();}