Translation Scheme - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Translation scheme, Three address code, Destructive operators, Modern processor, Syntax directed translation scheme, Assignment statement, Symbol table, Hash table are the points from this lecture. You can find series of lecture notes for compiler construction here.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

asim.amjid
asim.amjid ๐Ÿ‡ต๐Ÿ‡ฐ

4.4

(47)

41 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sohail Aslam Compiler Construction Notes Set:6-104
L
Le
ec
ct
tu
ur
re
e
3
36
6
Three-address code is attractive for several reasons. 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. Many modern processors
implement three-address operations; a three-address code models their properties well
We now consider syntax-directed translation schemes using three-address code for
various programming constructs. We start with the assignment statement.
Assignment Statement
Production translation scheme
S โ†’ id = E { p = lookup(id.name); emit( p, โ€˜=โ€™, E.place); }
E โ†’ E1 + E2 { E.place = newtemp();
emit( E.place, โ€˜=โ€™, E1.place,โ€˜+โ€™, E2.place); }
E โ†’ E1 โˆ— E2 { E.place = newtemp();
emit( E.place, โ€˜=โ€™, E1.place, โ€˜โˆ—โ€™, E2.place); }
E โ†’ โ€“ E1 { E.place = newtemp();
emit( E.place, โ€˜=โ€™, โ€˜โ€“โ€™ ,E1.place); }
E โ†’ ( E1 ) { E.place = E1.place; }
E โ†’ id { p = lookup(id.name); emit( E.place, โ€˜=โ€™, p ); }
The translation scheme uses a symbol table for identifiers and temporaries. Every time
the parser encounters an identifier, it installs it in the symbol table. The symbol table can
be implemented as a hash table or using some other efficient data structure for table. 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. The routine newtemp() returns a
new temporary in response to successive calls. Temporaries can be placed in the symbol
table. The routine emit() generates a three-address statement which can either be held
in memory or written to a file. The attribute E.place records the symbol table location.

Partial preview of the text

Download Translation Scheme - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Sohail Aslam Compiler Construction Notes Set:6-

Le Leccttuurree 3 366

Three-address code is attractive for several reasons. 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. Many modern processors implement three-address operations; a three-address code models their properties well

We now consider syntax-directed translation schemes using three-address code for various programming constructs. We start with the assignment statement.

Assignment Statement

Production translation scheme S โ†’ id = E { p = lookup(id.name); emit( p, โ€˜=โ€™, E.place); } E โ†’ E1 + E2 { E.place = newtemp(); emit( E.place, โ€˜=โ€™, E1.place,โ€˜+โ€™, E2.place); } E โ†’ E1 โˆ— E2 { E.place = newtemp(); emit( E.place, โ€˜=โ€™, E1.place, โ€˜โˆ—โ€™, E2.place); } E โ†’ โ€“ E1 { E.place = newtemp(); emit( E.place, โ€˜=โ€™, โ€˜โ€“โ€™ ,E1.place); } E โ†’ ( E1 ) { E.place = E1.place; } E โ†’ id { p = lookup(id.name); emit( E.place, โ€˜=โ€™, p ); }

The translation scheme uses a symbol table for identifiers and temporaries. Every time the parser encounters an identifier, it installs it in the symbol table. The symbol table can be implemented as a hash table or using some other efficient data structure for table. 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. The routine newtemp() returns a new temporary in response to successive calls. Temporaries can be placed in the symbol table. The routine emit() generates a three-address statement which can either be held in memory or written to a file. The attribute E.place records the symbol table location.