Docsity

Intermediate Representations - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Intermediate Representations - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 34

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

$$ $

Docsity.com

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

5

  1. 3 + 2 × 4 shift 2 3. + 2 × 4 reduce(r8) 3 E. + 2 × 4 shift 4 E +. 2 × 4 shift 5 E + 2. × 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^3 +^2 ×^4

E

E E

val=

val=

E val=

val=

E val=

7

 Compilers are organized as a series of passes

 This creates the need for an intermediate representation for the code being compiled

8

 Compilers are organized as a series of passes

 This creates the need for an intermediate representation for the code being compiled

10

 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

11

 The IR must be expressive enough to record all of the useful facts that might be passed between passes of the compiler

13

 During translation, the compiler derives facts that have no representation in the source code

 For example, the addresses of variables and procedures

14

 Typically, the compiler augments the IR with a set of tables that record additional information

 These tables are considered part of the IR

16

 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

17

 Thus, a source-to-source translator might keep its internal information in a form quite to close to the source