0% found this document useful (0 votes)
4 views

Unit 3 SDT part 2_

The document discusses the translation of assignment statements and the backpatching technique used in compiler design for handling control flow constructs. It outlines a grammar for assignment statements and provides semantic actions for translating expressions, as well as the need for backpatching in managing jumps in control flow. Additionally, it presents a grammar for flow-of-control constructs and the corresponding translation scheme for statements that alter control flow.

Uploaded by

umairknp2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 3 SDT part 2_

The document discusses the translation of assignment statements and the backpatching technique used in compiler design for handling control flow constructs. It outlines a grammar for assignment statements and provides semantic actions for translating expressions, as well as the need for backpatching in managing jumps in control flow. Additionally, it presents a grammar for flow-of-control constructs and the corresponding translation scheme for statements that alter control flow.

Uploaded by

umairknp2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Translation of Assignment Statements

In the syntax directed translation, assignment statement is mainly deals with expressions. The
expression can be of type real, integer, array and records.

Consider the grammar

S → id := E
E → E1 + E2
E → E1 * E2
E → (E1)
E → id
The translation scheme of above grammar is given below:

Production rule Semantic actions

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 → (E1) {E.place = E1.place}

E → id {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}

o The p returns the entry for id.name in the symbol table.


o The Emit function is used for appending the three address code to the output file.
Otherwise it will report an error.
o The newtemp() is a function used to generate new temporary variables.
o E.place holds the value of E.

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.

Need for Backpatching: Backpatching is mainly used for two purposes:

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.

Statements that alter the flow of control


The goto statement alters the flow of control. If we implement goto statements then we need to
define a LABEL for a statement. A production can be added for this purpose:

S → LABEL : S
LABEL → id
In this production system, semantic action is attached to record the LABEL and its value in the
symbol table.

Following grammar used to incorporate structure flow-of-control constructs:

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.

Translation scheme for statement that alters flow of control

o We introduce the marker non-terminal M as in case of grammar for Boolean expression.


o This M is put before statement in both if then else. In case of while-do, we need to put M before
E as we need to come back to it after executing S.
o In case of if-then-else, if we evaluate E to be true, first S will be executed.
o After this we should ensure that instead of second S, the code after the if-then else will be
executed. Then we place another non-terminal marker N after first S.

The grammar is as follows:


S→ if E then M S
S→ if E then M S else M S
S→ while M E do M S
S→ begin L end
S→ A
L→ L;MS
L→ S
M→ ∈
N→ ∈
The translation scheme for this grammar is as follows:

Production rule Semantic actions

S → if E then M S1 BACKPATCH (E.TRUE, M.QUAD)


S.NEXT = MERGE (E.FALSE,
S1.NEXT)

S → if E then M1 S1 else BACKPATCH (E.TRUE, M1.QUAD)


M2 S2 BACKPATCH (E.FALSE, M2.QUAD)
S.NEXT = MERGE (S1.NEXT,
N.NEXT, S2.NEXT)

S → while M1 E do M2 S1 BACKPATCH (S1,NEXT, M1.QUAD)


BACKPATCH (E.TRUE, M2.QUAD)
S.NEXT = E.FALSE
GEN (goto M1.QUAD)

S → begin L end S.NEXT = L.NEXT

S→A S.NEXT = MAKELIST ()

L→L;MS BACKPATHCH (L1.NEXT, M.QUAD)


L.NEXT = S.NEXT

L→S L.NEXT = S.NEXT

M→∈ M.QUAD = NEXTQUAD

N→ ∈ N.NEXT = MAKELIST (NEXTQUAD)


GEN (goto_)

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:

100: if a < b goto _

101: goto _

102: if c < d goto _

103: goto _

104: if e < f goto _

105: goto _

In the second pass, the same code is re-run to generate the true, false labels by
incorporating short circuit information.

100: if a < b goto TRUE

101: goto 102

102: if c < d goto 104

103: goto FALSE

104: if e < f goto TRUE

105: goto FALSE

You might also like