prof busch - lsu1 yacc yet another compiler compiler
TRANSCRIPT
![Page 1: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/1.jpg)
Prof Busch - LSU 1
YACC
Yet Another Compiler Compiler
![Page 2: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/2.jpg)
Prof Busch - LSU 2
Yacc is a parser generator
Input: A Grammar
Output: A parser for the grammar
Reminder: a parser finds derivations
![Page 3: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/3.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/4.jpg)
Prof Busch - LSU 4
Exampe Input:
10 * 3 + 4
Yacc Derivation:
expr => expr + expr => expr * expr + expr => 10*3 + 4
![Page 5: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/5.jpg)
Prof Busch - LSU 5
Resolving Ambiguities
%left '+', '-'%left '*', '/'%left UMINUS%%
expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | '-' expr %prec UMINUS | INT ;
![Page 6: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/6.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/7.jpg)
Prof Busch - LSU 7
A Complete Yacc program (EXPR1.y)
%union{ int int_val;}
%left '+', '-'%left '*', '/'%left UMINUS
%token <int_val> INT%type <int_val> expr
%start program
%%
![Page 8: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/8.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/9.jpg)
Prof Busch - LSU 9
Execution Example
10 + 20*(3 - 4 + 25)Input:
Output: Expr value = 490
![Page 10: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/10.jpg)
Prof Busch - LSU 10
The Lex Code (expr1.l)%{int linenum=1;int temp_int;%}%%
\n {linenum++;}
[\t ] /* skip spaces */;\/\/[^\n]* /* ignore comments */;
"+" {return '+';}"-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';}"(" {return '(';}
![Page 11: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/11.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/12.jpg)
Prof Busch - LSU 12
Compiling:
yacc YaccFilelex LexFilecc y.tab.c -ly -ll -o myparser
Executable: myparser
![Page 13: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/13.jpg)
Prof Busch - LSU 13
Another Yacc Program (EXPR2.y)
%union{ int int_val;}
%left '+', '-'%left '*', '/'%left UMINUS
%token <int_val> INT%type <int_val> expr
%start program
%%
![Page 14: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/14.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/15.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/16.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/17.jpg)
Prof Busch - LSU 17
Lex Code (expr2.l)
%{int linenum=1;int temp_int;%}%%
\n {linenum++;}
[\t ] /* skip spaces */;\/\/[^\n]* /* ignore comments */;
![Page 18: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/18.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/19.jpg)
Prof Busch - LSU 19
Another Yacc Program (EXPR3.y)%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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/20.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/21.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/22.jpg)
Prof Busch - LSU 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: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/23.jpg)
Prof Busch - LSU 23
Lex Code (expr3.l)
%{int linenum=1;int temp_int;char temp_str[200];%}%%
\n {linenum++;}
[\t ] /* skip spaces */;\/\/[^\n]* /* ignore comments */;
![Page 24: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/24.jpg)
Prof Busch - LSU 24
"+" {return '+';}"-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';}"(" {return '(';}";" {return ';';}"print" {return PRINT;}"newline" {return NEWLINE;}
![Page 25: Prof Busch - LSU1 YACC Yet Another Compiler Compiler](https://reader036.vdocuments.net/reader036/viewer/2022062422/56649f315503460f94c4c22a/html5/thumbnails/25.jpg)
Prof Busch - LSU 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();}