Lecture 36 Z
‘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
iplement 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
‘translation scheme
{p= lookup(id.name), emit( p,
{ E;place = newtemp0;
emit( E.place, ‘=", El.place,“+’, E2.place); }
{ E.place = newtempQs
emit( E.place, ‘=", El
Production
i , E:place); }
E>-El {E place = newtemp();
emit( E.place, ‘=’, ‘-’ El place);
E—(El) | _{E,place = El.place; }
E> id {p= lookup(id.name); emii( E:place, =, p)s}
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, Ifthe
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 toa file, The attribute E,place records the symbol table location.
Sohail Aslam ‘Compiler Construction Notes Set:6-104