TSR - Class Cd-Unit 3
TSR - Class Cd-Unit 3
1/28/2025 1
COURSE CONTENT
UNIT I Introduction to Compilers 9
• Compilers – Interpreters – Analysis of the source program –Cousins of the
Compiler – Grouping of Phases – Phases of a compiler – Compiler construction
tools – Lexical Analysis – Role of Lexical Analyzer – Input Buffering –
Recognition of Tokens – Specification of Tokens. Design of a Lexical Analyzer
Generator.
• Case Study: Lexical Analyzer for separating tokens, Counting whitespace, special
characters and newline character, Pattern Matching of strings – use relevant tool.
1/28/2025 2
Dr. T SAJU RAJ
COURSE CONTENT
UNIT II Syntax Analysis 9
• Need and Role of the parser –Writing Grammars – Context-Free Grammars – Top
Down parsing – Recursive Descent Parsing – Predictive Parsing – Bottom-up
parsing – Shift Reduce Parsing – Operator Precedence Parsing – LR Parsers – SLR
Parser – Canonical LR Parser – LALR Parser.
• Case Study: Specification for desktop calculator, Error recovery of ambiguous
arithmetic grammar, Syntax Analyzer for looping construct - use relevant tool.
•
1/28/2025 6
Dr. T SAJU RAJ
Intermediate Representation
• Graphical IRs:
✓ Abstract Syntax trees
✓ Directed Acyclic Graphs (DAGs)
✓ Control Flow Graphs
• Linear IRs:
➢ Stack based (postfix)
➢ Three address code
(quadruples)
(a – b) * (c + d) + (a – b) is :
ab – cd + *ab -+.
1/28/2025 23
Dr. T SAJU RAJ
Example
Given Expression:
a := (-c * b) + (-c * d)
Three-address code is as follows:
t1 := -c
t2 := b*t1
t3 := -c
t4 := d * t3
t5 := t2 + t4
a := t5
t is used as registers in the target program.
Quadruples:
In quadruples representation, each instruction is splitted into the
following 4 different fields-
op, arg1, arg2, result
Here-
•The op field is used for storing the internal code of the operator.
•The arg1 and arg2 fields are used for storing the two operands used.
•The result field is used for storing the result of the expression.
Exception-02:
To represent the statement like param t1, we place-
Exception-03:
To represent the unconditional and conditional jump statements, we place label of the target in
the result field.
1/28/2025 Dr. T SAJU RAJ 27
Example
a := -b * c + d
Three-address code is as follows:
t1 := -b
t2 := c + d
t3 := t1 * t2
a := t3
t1 := -b
t2 := c + d
t3 := t1 * t2
a := t3
T1 = e ↑ f
T2 = b * c
T3 = T2 / T1
T4 = b *a
T5 = a + T3
T6 = T5 + T4
Solution-
t1 = x + y
t2 = y + z
t3 = t1 * t2
t4 = t1 + z
t5 = t3 + t4
Example:
E → E1 + T { E.val = E1.val + T.val}
Features –
•High level specification
•Hides implementation details
•Explicit order of evaluation is not specified
Inherited Attributes – These are the attributes which derive their values
from their parent or sibling nodes i.e. value of inherited attributes are
computed by value of parent or sibling nodes.
Example:
A→BCD { C.in = A.in, C.type = B.type }
Two attributes:
E.place, a name that will hold the value of E,
E.code, the sequence of three-address statements
evaluating E.
A function gen(…) to produce sequence of three address
statements
– The statements themselves are kept in some data structure,
e.g. list
– SDD operations described using pseudo code 5
When we encounter declarations, we need to lay out storage for the declared
variables.
For every local name in a procedure, we create a ST(Symbol Table) entry containing:
1.The type of the name
2.How much storage the name requires
P→D
D→D;D
D → id : T
T → integer
T → real
1/28/2025 Dr. T SAJU RAJ 59
Declarations (…contd)
The production:
1.S → L := E
2. E → E+E
3. E → (E)
4. E → L
5. L → Elist ]
6. L → id
7. Elist → Elist, E
8. Elist → id[E
For a = b * -c + b * -c
following code is generated
t1 = -c
t2 = b * t1
t3 = -c
t4 = b * t3
t5 = t2 + t4
a = t5
1/28/2025 Dr. T SAJU RAJ 71
Flow of Control Statements
S → while E do S1
Desired Translation is
S. begin :
E.code
if E.place = 0 goto S.after S.begin := newlabel
S1 .code S.after := newlabel
goto S.begin S.code := gen(S.begin:) || E.code ||
S.after : gen(if E.place = 0 goto S.after) ||
S1 .code || gen(goto S.begin) ||
gen(S.after:)
S → if E then S1 else S2
S.else := newlabel
Desired Translation is S.after := newlabel
E.code
S.code = E.code ||
if E.place = 0 goto S.else
gen(if E.place = 0 goto S.else) ||
S1.code
goto S.after S1.code ||
S.else: gen(goto S.after) ||
S2.code gen(S.else :) ||
S.after: S2.code ||
gen(S.after :)
•They are used for computing the logical values true means 1 and false means 0.
E → id relop id
E → true
E → false
Syntax tree:
codeGen_bool(B, trueDst, falseDst):
relop /* base case: B.nodetype == relop */
B.code = E1.code E2.code
newinstr(relop, E1.place, E2.place, trueDst)
newinstr(GOTO, falseDst, NULL, NULL);
E1 E2
Here is the example which generates the three address code using the above
translation scheme:
p>q AND r>s OR u>r
100: if p>q goto 103
101: t1:=0
102: goto 104
103: t1:=1
104: if r>s goto 107
105: t2:=0
106: goto 108
107: t2:=1
108: if u>v goto 111
109: t3:=0
110: goto 112
111: t3:= 1
112: t4:= t1 AND t2
113: t5:= t4 OR t3
Generate three address code for the Three address code for the given code
following code- is-
1.S → LABEL : S
2. LABEL → id
• The run time routines that handle procedure argument passing, calls and returns
are part of the run time support packages
The falling are the actions that taker place in a calling sequence:
•When a procedure call occurs then space is allocated for activation record.
•Evaluate the argument of the called procedure.
•Establish the environment pointers to enable the called procedure to access
data in enclosing blocks.
•Save the state of the calling procedure so that it can resume execution after
the call.
•Also save the return address. It is the address of the location to which the
called routine must transfer after it is finished.
•Finally generate a jump to the beginning of the code for the called procedure.
1/28/2025 Dr. T SAJU RAJ 101
ACTIVATION RECORD
c=0
do
{
if (a < b) then
x++;
else
x–;
c++;
} while (c < 5)
Solution-
Three address code for the given code is-
Generate three address code for the
1.c = 0 following code-
2. if (a < b) goto (4)
3. goto (7) c=0
4. T1 = x + 1 do
5. x = T1 {
6. goto (9) if (a < b) then
7. T2 = x – 1 x++;
8. x = T2 else
9. T3 = c + 1 x–-;
10. c = T3 c++;
11. if (c < 5) goto (2) } while (c < 5)
Solution-
Three address code for the given code is-
1: if (A < C) goto (3)
Generate three address code for the
2: goto (15)
following code-
3: if (B > D) goto (5)
4: goto (15)
while (A < C and B > D) do
5:if (A == 1) goto (7)
if A = 1 then C = C + 1
6:goto (10)
else
7: T1 = c + 1
while A <= D
8: c = T1
do A = A + B
9: goto (1)
10: if (A <= D) goto (12)
11:goto (1)
12:T2 = A + B
13:A = T2 Dr. T SAJU RAJ
1/28/2025 108
14: goto (10)
Practice Problems: 3
Solution-
Three address code for the given code is-
if ch = 1 goto L1
if ch = 2 goto L2 Generate three address code for the
L1: following code-
T1 = a + b switch (ch)
{
c = T1
case 1 : c = a + b;
goto Last
break;
L2: case 2 : c = a – b;
T1 = a – b break;
c = T2 }
goto Last
Last:
1/28/2025 Dr. T SAJU RAJ 110
Practice Problems: 4