Docsity

Syntax Directed Translation - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Syntax Directed Translation - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 36

2

Three-Address Code

 Three-address code is attractive for several reasons:

4

Three-Address Code

 absence of destructive operators gives the compiler freedom to reuse names and values  three-address code is reasonably compact: operations are 1 to 2 bytes; addresses are 4 bytes

5

Three-Address Code

 many modern processors implement three-address operations, a three-address code models their properties well

7

Syntax-directed Translation

 We now consider syntax- directed translation schemes using three-address code for various programming constructs

 We start with the assignment statement

8

Production translation scheme S → id = E { p = lookup(id.name); emit( p, ‘=’, E.place); }

E → E 1 + E 2 { E.place = newtemp(); emit( E.place, ‘=’, E 1 .place, ‘+’, E 2 .place); }

E → E 1 ∗ E 2 { E.place = newtemp(); emit( E.place, ‘=’, E 1 .place, ‘∗’, E 2 .place); }

10

Assignment Statement

 The tranlation scheme uses a symbol table for identifiers and temporaries

 Every time the parser encounters an identifier, it installs it in the symbol table.

11

Assignment Statement

 The tranlation scheme uses a symbol table for identifiers and temporaries

 Every time the parser encounters an identifier, it installs it in the symbol table.

13

Assignment Statement

 The routine lookup(name)

checks if there an entry for the name in the symbol table

 If the name is found, the

routine returns a pointer to entry.

14

Assignment Statement

 The routine lookup(name)

checks if there an entry for the name in the symbol table

 If the name is found, the

routine returns a pointer to entry.

16

Assignment Statement

 The routine emit()

generates a three-address statement which can either be held in memory or written to a file

17

Example

Here is the bottom-up parse of the assignment statement

a = b-c + b-c**

and the syntax-directed translation into three-address code