CD UNIT 3 PART B
CD UNIT 3 PART B
1.Demonstrate the concept of three address code with its types and implementation.
• One operator
Format:
x = y op z
4. Temporary Variables
Most TAC instructions use temporary variables (t1, t2, ...) to hold intermediate results.
Example:
Expression: a + b * c
TAC:
t1 = b * c
t2 = a + t1
A. Quadruples (Quad)
B. Triples
C. Indirect Triples
Expression:
a = (b + c) * (d - e)
Step-by-step TAC:
t1 = b + c
t2 = d - e
t3 = t1 * t2
a = t3
Each instruction here has at most three addresses, and each operation is reduced to a
simple step.
2. Build AST
→ Converts parse tree to a simpler Abstract Syntax Tree (AST).
3. Generate TAC
→ Traverses AST, creates temporary variables, and forms TAC instructions.
4. Store TAC
→ Saves instructions as Quadruples or Triples for further optimization or machine
code generation.
if (a < b) {
x = y + z;
} else {
x = y - z;
TAC:
if a < b goto L1
goto L2
L1: t1 = y + z
x = t1
goto L3
L2: t2 = y - z
x = t2
L3:
Labels and conditional jumps help simulate control flow using TAC.
while (a < b) {
a = a + 1;
TAC:
t1 = a + 1
a = t1
goto L1
L2:
2.Illustrate the translation scheme for declaration in a procedure and nested procedures.
A translation scheme is a set of rules and actions used by the compiler to process parts of a
program (like variable declarations or functions) while building symbol tables and generating
code.
In simple terms, it tells the compiler what to do when it sees things like:
In a single procedure, all variable declarations can be processed sequentially. The compiler
maintains:
• An offset: indicating the memory location (relative address) for the next variable.
• A symbol table: storing information about each variable (name, type, offset).
The compiler:
• Sets offset = 0
x Integer 0
y Real 4
z Array[3]int 12
In languages with nested procedures, a procedure can be defined inside another. Each
nested procedure gets its own symbol table, which points back to the enclosing procedure’s
table.
Extended Grammar
P→D
D→D;D
| id : T
| proc id ; D ; S
Key Operations:
Scope Tree
1. Introduction
This approach makes it easier to convert high-level source code into low-level instructions
that can later be translated into machine code.
E->E or T|T
F->not F|(E)|True|False
Identify the semantic rules and explain the process of converting the Boolean
Videolinks:
1. https://youtu.be/zWKlmk1POPg?si=jLAtTXQWEoPz81x4
2. https://youtu.be/5H0Kp7TwvG0?si=0Z2Gb0l-Fo9s-o1F
For expression asked in question:
• Use labels (like L1, L2, etc.) to mark points where control jumps.
• Use:
o goto Lfalse
• Replace structured control (like if) with goto and label-based control.
Video link:https://youtu.be/75Tme7scP2k?si=Ld20v8JENNuvGMGo
(write rules given in the video)
if (a > b)
x = a + b;
else
x = a - b;
Step-by-step Translation:
3. If false, execute x = a - b
4. After executing either branch, jump to a common point to continue
o x = t1 — Assignment to x
o t2 = a - b — Subtraction result
o x = t2 — Assignment to x
6 Develop the syntax directed translation scheme for case statements and
switch(ch)
case 1:
c-=a+b;
break;
case 2:
c=a-b;
break;
Introduction
switch(ch) {
case 1:
c -= a + b;
break;
case 2:
c = a - b;
break;
}
Explanation:
2. Conditional Jumps:
o goto L3: If none of the above conditions are met, jump to L3 (end of the
switch).
3. Case 1 (L1):
o goto L3: After executing case 1, jump to the end of the switch.
4. Case 2 (L2):
o goto L3: After executing case 2, jump to the end of the switch.
o Marks the point where control resumes after the switch statement.
8 Explain in detail about the back patching procedure to replace the goto statements by its
target address in intermediate code conversion.