Compiler Design (16CS303) : Welcome
Compiler Design (16CS303) : Welcome
COMPILER DESIGN
(16CS303)
by
S. Shivaprasad
Assistant Professor
Department of CSE
VFSTR Deemed to be University
INTERMEDIATE CODE GENERATION
Intermediate code eliminates the need of a new full compiler for every
unique machine
GRAPHICAL REPRESENTATIONS:
= =
+
a + a
* * *
b uminus uminus
uminus b
c c 7
b c
AST
Dept. of CSE, VFSTR University
DAG Compiler Design
EXAMPLE OF THREE-ADDRESS CODE
Example: a:=b*-c+b*-c:
t1:=- c
t1:=- c
t2:=b * t1
t2:=b * t1
t3:=- c
t4:=b * t3 t5:=t2 + t2
t5:=t2 + t4 a:=t5
a:=t5
8
Dept. of CSE, VFSTR University Compiler Design
IMPLEMENTATION
THREE-ADDRESS CODE (QUADRAPLES)
A quadruple is:
x := y op z
where x, y and z are names, constants or compiler-generated
temporaries;
op is any operator.
But we may also the following notation for quadraples (much better
notation
because it looks like a machine code instruction)
op y,z,x
apply operator op to y and z, and store the result in x.
We use the term “three-address code” because each statement usually
contains three addresses (two for operands, one for the result). 11
15
Dept. of CSE, VFSTR University
Compiler Design
THREE-ADDRESS STATEMENTS (CONT.)
Indexed Assignments:
move y[i],,x or x := y[i]
move x,,y[i] or y[i] := x
16
Dept. of CSE, VFSTR University Compiler Design
TYPES OF THREE ADDRESS
CODES
IMPLEMENTATIONS OF 3-ADDRESS STATEMENTS
op arg1 arg2 result
Quadruples
(0) uminus c t1
t1:=- c
(1) * b t1 t2
t2:=b * t1
t3:=- c (2) uminus c
t4:=b * t3 (3) * b t3 t4
t5:=t2 + t4 (4) + t2 t4 t5
a:=t5
(5) := t5 a
op arg1 arg2
(0) []= x i
(1) assign (0) y
op arg1 arg2
(0) []= y i
(1) assign x (0)
21
Dept. of CSE, VFSTR University Compiler Design
IMPLEMENTATIONS OF 3-ADDRESS STATEMENTS
a=b+c+d
t1 = b * c; t1 = b + c;
t2 = b * d;
t2 = a < t1;
ifz t2 goto l0;
t3 = t1 + t2;
t3 = a - c;
a =t3; a = t3;
l0: t4 = b * c;
c = t4;
Write Three Address Code for the following expression-
-(a * b) + (c + d) – (a + b + c + d)
three address code for the given expression is-
(1) t1 = a * b
(2) t2 = uminus t1
(3) t3 = c + d
(4) t4 = t2 + t3
(5) t5 = a + b
(6) t6 = t3 + t5
(7) t7 = t4 – t6
Write Three Address Code for the following expression-
If A < B then 1 else 0
l3: c=a;
t1= y*20
x=a[y, z] t2=t1+z
t3=t2*4
t4=base address of a
x=t4[t3]
ASSIGNMENTS
by
S. Shivaprasad
Assistant Professor
Department of CSE
VFSTR Deemed to be University
SEMANTIC ANALYSIS PHASE
Semantic analysis
float x = 10.1;
float y = x*30;
37
COMPILER NEEDS TO KNOW?
Context-sensitive Grammar
38
CONTEXT-SENSITIVE ANALYSIS
Why is context-sensitive analysis hard?
answers depend on values, not syntax
questions and answers involve non-local information
answers may involve computation
Several alternatives:
abstract syntax tree (attribute grammars): specify
non-local computations; automatic evaluators
symbol tables: central store for facts; express checking
code
language design: simplify language; avoid problems
The plain parse-tree constructed in that phase is generally of no use for
a compiler, as it does not carry any information of how to evaluate the
tree. For exampe
E → E +T
The above CFG production has no semantic rule associated with it, and it cannot help in making any sense of
the production.
Semantics
Semantics of a language provide meaning to its constructs,
like tokens and syntax structure.
Semantics help interpret symbols, their types, and their
relations with each other.
Semantic analysis judges whether the syntax structure constructed
in the source program derives any meaning or not.
CFG + semantic rules = Syntax Directed Definitions
Attribute Grammar
Attribute grammar is a special form of context-free
grammar where some additional information (attributes) are
appended to one or more of its non-terminals in order to
provide context-sensitive information.
Example:
E → E + T { E.value = E.value + T.value }
Based on the way the attributes get their values, they can be broadly divided
into two categories : synthesized attributes and inherited attributes.
Synthesized attributes:
These attributes get values from the attribute values of their child nodes.
S → ABC
• If S is taking values from its child nodes (A,B,C), then it is
said to be a synthesized attribute, as the values of ABC
are synthesized to S.
Inherited attributes:
In contrast to synthesized attributes, inherited attributes can take values from
parent and/or siblings.
S → ABC • A can get values from S, B and C. B can take
values from S, A, and C. Likewise, C can take values
from S, A, and B
WELCOME
COMPILER DESIGN
(16CS303)
by
S. Shivaprasad
Assistant Professor
Department of CSE
VFSTR Deemed to be University
Synthesized attributes
Inherited attributes
Types of SDT
S-attributed SDT
L-attributed SDT
S-attributed SDT
Ex:
If an SDT uses only synthesized
attributes, it is called as S-attributed A-> XY
SDT.
S-attributed SDTs are evaluated in A.a= f(X,Y)
bottom-up parsing, as the values of the
parent nodes depend upon the values
of the child nodes.
Semantic actions are placed in
rightmost place of RHS.
A-> XYZ
L-attributed SDT
A.a=f(X,Y,Z)
If an SDT uses both synthesized Y.b=f(X)
attributes and inherited attributes with Z.c=f(X,Y)
a restriction that inherited attribute
can inherit values from left siblings
only, it is called as L-attributed SDT.
Attributes in L-attributed SDTs are
evaluated by depth-first and left-to-
right parsing manner.
Semantic actions are placed anywhere
in RHS.
A->BC { B.s= A.s)