Download presentation
Presentation is loading. Please wait.
1
Compiler Construction
Sohail Aslam Lecture 34 compiler: Bottom-up Parsing
2
prog : expr { cout << $1 << endl;} ;
expr : expr PLUS expr {$$ = $1 + $3;} | expr MINUS expr {$$ = $1 - $3;} | expr TIMES expr {$$ = $1 * $3;} | expr DIVIDE expr {if($3) $$ = $1 / $3;} | LPAREN expr RPAREN {$$ = $2;} | MINUS expr {$$ = -$2;} | NUMBER {$$ = $1;} %% $$ $1 This is a note compiler: Bottom-up Parsing
3
prog : expr { cout << $1 << endl;} ;
expr : expr PLUS expr {$$ = $1 + $3;} | expr MINUS expr {$$ = $1 - $3;} | expr TIMES expr {$$ = $1 * $3;} | expr DIVIDE expr {if($3) $$ = $1 / $3;} | LPAREN expr RPAREN {$$ = $2;} | MINUS expr {$$ = -$2;} | NUMBER {$$ = $1;} %% $$ $1 $2 $3 This is a note compiler: Bottom-up Parsing
4
prog : expr { cout << $1 << endl;} ;
expr : expr PLUS expr {$$ = $1 + $3;} | expr MINUS expr {$$ = $1 - $3;} | expr TIMES expr {$$ = $1 * $3;} | expr DIVIDE expr {if($3) $$ = $1 / $3;} | LPAREN expr RPAREN {$$ = $2;} | MINUS expr {$$ = -$2;} | NUMBER {$$ = $1;} %% This is a note compiler: Bottom-up Parsing
5
E E E E E 3 + 2 4 1 . 3 + 2 4 shift 2 3 . + 2 4 reduce(r8)
5 E 4 reduce(r8) 6 E + E . 4 shift 7 E + E . 4 shift 8 E + E 4 . reduce(r8) 9 E + E E . reduce(r4) 10 E + E . reduce(r2) 11 E . accept E val=11 E E val=3 val=8 E E val=2 val=4 3 + 2 4
6
Intermediate Representations (IR)
7
IR Compilers are organized as a series of passes
This creates the need for an intermediate representation for the code being compiled This is a note compiler: Bottom-up Parsing
8
IR Compilers are organized as a series of passes
This creates the need for an intermediate representation for the code being compiled This is a note compiler: Bottom-up Parsing
9
IR Compilers use some internal form– an IR –to represent the code being analysed and translated Many compilers use more than one IR during the course of compilation This is a note compiler: Bottom-up Parsing
10
IR Compilers use some internal form– an IR –to represent the code being analysed and translated Many compilers use more than one IR during the course of compilation This is a note compiler: Bottom-up Parsing
11
IR The IR must be expressive enough to record all of the useful facts that might be passed between passes of the compiler This is a note compiler: Bottom-up Parsing
12
IR During translation, the compiler derives facts that have no representation in the source code For example, the addresses of variables and procedures This is a note compiler: Bottom-up Parsing
13
IR During translation, the compiler derives facts that have no representation in the source code For example, the addresses of variables and procedures This is a note compiler: Bottom-up Parsing
14
IR Typically, the compiler augments the IR with a set of tables that record additional information These tables are considered part of the IR This is a note compiler: Bottom-up Parsing
15
IR Typically, the compiler augments the IR with a set of tables that record additional information These tables are considered part of the IR This is a note compiler: Bottom-up Parsing
16
IR Selecting an appropriate IR for a compiler project requires an understanding of both the source language and the target machines and the properties of the programs to be compiled This is a note compiler: Bottom-up Parsing
17
IR Thus, a source-to-source translator might keep its internal information in a form quite to close to the source This is a note compiler: Bottom-up Parsing
18
IR In constrast, a compiler that produces assembly code might use a form close to the target machine’s instruction set end of lec 34 compiler: Bottom-up Parsing
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.