CD Chapter 6
CD Chapter 6
3. Three-Address Code
Three address code is a sequence of statements of the general form where x, y,
and z
are names, constants, or compiler-generated temporaries;
op stands for any operator, such as a fixed or floating-point arithmetic operator,
or a
logical operator on Boolean valued data.
The reason for the term "three-address code" is that each statement usually
contains
three addresses, two for the operands and one for the result. Example :
4.1. INTERMEDIATE LANGUAGES
Three address code for the syntax tree a := b* - c + b* -c is
• Advantage: Indirect triples can save some space compared with quadruples, if
the same temporary value is used more than once.
4.3 ASSIGNMENT STATEMENTS
• Expressions can be of type Integer, real, array, and record.
• Translation scheme to produce three address code for assignments
4.3 ASSIGNMENT STATEMENTS
• The translation scheme shows how such symbol-table entries can be found.
• The lexeme for the name represented by id is given by attribute id.name.
• Operation lookup (id.name) checks if here is an entry for this occurrence of the name in
the symbol table. If so, a pointer to the entry is returned; otherwise, lookup returns nil to
indicate that no entry was found.
• E.place - the name that will hold the value of E.
• Newtemp generates a new temporary name each time a temporary is needed.
• Type Conversions within Assignment
• In practice, there would be many different types of Variables and constants, so the
compiler must either reject certain mixed-type operations or generate appropriate type
conversion instructions.
• Consider the grammar for assignment statements as above, Assume there are two types -
real and integer, with integers converted to real when necessary.
• The semantic rule for E.type associated with the Production EE + E is:
4.3 ASSIGNMENT STATEMENTS
• The complete semantic action for a production of the form E E1 + E2 is
4.4 BOOLEAN EXPRESSIONS
• Boolean expressions are composed of the boolean operators (and, or, and
not) applied to elements that are boolean variables or relational
expressions.
• In turn, relational expressions are of the form E1 relop E2 , where E1 and E2
are arithmetic expressions.
• In programming languages, boolean expressions have two primary purposes.
• They are used to compute logical values, but more often they are used as
conditional expressions in statements that alter the flow of control, such as if
then, if-then-else, or while-do statements.
• we consider boolean expressions generated by the following grammar:
4.4 BOOLEAN EXPRESSIONS
Methods of Translating Boolean Expressions
There are two principal methods of representing the value of a boolean
expression.
The first method is to encode true and false numerically.
1 is used to denote true and 0 to denote false.
The second principal method of implementing boolean expressions is by flow of
control, that is, representing the value of a boolean expression by a position
reached in a program
For example, given the expression E l or E2, if we determine that E1 is true, then
we can conclude that the entire expression is true without having to evaluate E2.
Numerical Representations
Expressions will be evaluated completely, from Left to right, in a manner similar
to arithmetic expressions.
For example, the translation for a or b and not c is the three-address sequence
4.4 BOOLEAN EXPRESSIONS
A relational expression such as a < b is equivalent to the conditional
statement if a < b then 1 else 0, which can be translated into the
three address code sequence.