Unit 3 SDT part 2_
Unit 3 SDT part 2_
In the syntax directed translation, assignment statement is mainly deals with expressions. The
expression can be of type real, integer, array and records.
S → id := E
E → E1 + E2
E → E1 * E2
E → (E1)
E → id
The translation scheme of above grammar is given below:
S → id :=E {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}
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 → id {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}
Backpatching
Backpatching is a method to deal with jumps in the control flow constructs like if statements,
loops, etc in the intermediate code generation phase of the compiler. Otherwise, as the target of
these jumps may not be known until later in the compilation stages, back patching is a method to
fill in these destinations located elsewhere. Forward jumps are very common in constructs like
the if statements, while loops, switch cases. For example, in a language with goto statements, the
destination of a goto may not be resolved until and unless its label appears following the goto
statement. Forward references i.e; Jumps from lower addresses to higher address it is a
mechanism to maintain and solve these.
1. Boolean Expression Boolean expressions are statements whose results can be either true or
false. A boolean expression which is named for mathematician George Boole is an expression
that evaluates to either true or false.
Let’s look at some common language examples: My favorite color is blue. → true I am afraid
of mathematics. → false 2 is greater than 5. → false
2. Flow of control statements: The flow of control statements needs to be controlled during the
execution of statements in a program. For example:
3. Labels and Goto: The most elementary programming language construct for changing the
flow of control in a program is a label and goto. When a compiler encounters a statement like
goto L, it must check that there is exactly one statement with label L in the scope of this goto
statement. If the label has already appeared, then the symbol table will have an entry giving the
compiler generated label for the first three-address instruction associated with the source
statement labeled L. For the translation, we generate a goto three-address statement with that
compiler generated label as a target.
When a label L is encountered for the first time in the source program, either in a declaration or
as the target of the forward goto, we enter L into the symbol table and generate a symbolic table
for L.
One-pass code generation using backpatching: In a single pass, backpatching may be used to
create a boolean expressions program as well as the flow of control statements. The synthesized
properties truelist and falselist of nonterminal B are used to handle labels in jumping code for
Boolean statements. The label to which control should go if B is true should be added to
B.truelist, which is a list of a jump or conditional jump instructions. B.falselist is the list of
instructions that eventually get the label to which control is assigned when B is false. The jumps
to true and false exist, as well as the label field, are left blank when the program is generated for
B. The lists B.truelist and B.falselist, respectively, contain these early jumps.
S → LABEL : S
LABEL → id
In this production system, semantic action is attached to record the LABEL and its value in the
symbol table.
S→ if E then S
S → if E then S else S
S→ while E do S
S→ begin L end
S→ A
L→ L;S
L→ S
Here, S is a statement, L is a statement-list, A is an assignment statement and E is a Boolean-
valued expression.
Key Functions:
makelist(i): Creates a new list containing a three-address location i.
merge(list1, list2): Combines two lists into one.
backpatch(list, address): Iterates through a list of jump instructions and fills in the
target address address in each instruction.
Consider the Boolean expression “a < b or c < d and e < f”. To generate three-
address code for this, we have already incorporated semantic rules in the previous
module. In backpatching the same code is generated in two passes. In the first pass,
the following would be generated:
101: goto _
103: goto _
105: goto _
In the second pass, the same code is re-run to generate the true, false labels by
incorporating short circuit information.