UNIT 3 - Chapter 1 in Compiler Design
UNIT 3 - Chapter 1 in Compiler Design
o Semantic Analysis Phase verifies the parse tree, whether it’s meaningful or
not. It furthermore produces a verified parse tree. It also does type checking,
Label checking, and Flow control checking.
o We have mentioned some of the semantics errors that the semantic analyzer
is expected to recognize:
Type mismatch
Undeclared variable
Reserved identifier misuse.
Multiple declaration of variable in a scope.
Accessing an out-of-scope variable.
Actual and formal parameter mismatch.
There are two ways to represent the semantic rules associated with grammar symbols.
o Syntax-Directed Definitions (SDD)
o Syntax-Directed Translation Schemes (SDT)
Syntax-Directed Definitions (SDD)
o A syntax-directed definition (SDD) is a context-free grammar together with
attributes and rules. Attributes are associated with grammar symbols and
rules are associated with productions.
Annotated parse tree. (or) The process of computing the attributes values
at the nodes is called annotating of the parse tree.
o In generally, the order of these computations depends on the dependency
graph induced by the semantic rules.
TYPES OF SDT’S
SDT is divided into two subsets known as synthesized and inherited
attributes of grammar.
S --> E
E --> E1 + T
E --> T
T --> T1 * F
T --> F
F --> digit
1. S-attributed SDD:
If an SDD uses only synthesized attributes, it is called as S-attributed
SDD.
S-attributed SDDs are evaluated in bottom-up parsing, as the values of
the parent nodes depend upon the values of the child nodes.
Semantic actions are always placed at rightmost place of RHS.
For eg. Let’s say A -> BC is a production of a grammar, and A’s attribute is
dependent on B’s attributes or C’s attributes then it will be synthesized
attribute.
2. L-attributed SDD:
If an SDD uses both synthesized attributes and inherited attributes with
a restriction that inherited attribute can inherit values from left siblings
only, it is called as L-attributed SDD.
Attributes in L-attributed SDDs are evaluated by depth-first and left-to-
right parsing manner.
Semantic actions are placed anywhere in RHS.
For example,
A -> XYZ {Y.S = A.S, Y.S = X.S}
is an L-attributed grammar.
For example,
A -> XYZ {Y.S = A.S, Y.S = X.S, Y.S = Z.S}
is not an L-attributed grammar
since Y.S = A.S and Y.S = X.S are allowed but Y.S = Z.S violates the L-attributed
SDD definition as attributed is inheriting the value from its right sibling.
Note – If a definition is S-attributed, then it is also L-attributed but NOT vice-versa.
Dependency Graph
A dependency graph is used to represent the flow of information among the attributes
in a parse tree.
In a parse tree, a dependency graph basically helps to determine the evaluation order
for the attributes.
The main aim of the dependency graphs is to help the compiler to check for various
types of dependencies between statements in order to prevent them from being
executed in the incorrect sequence, i.e. in a way that affects the program’s meaning.
Example of Dependency Graph:
Design dependency graph for the following grammar:
E → E(1) + E(2) {E. VAL = Node (+, E(1) . VAL, E(2). VAL)}
E → E(1) ∗ E(2) {E. VAL = Node (∗, E(1). VAL, E(2). VAL)})
Node (+, E(1). VAL, E(2). VAL) will create a node labeled +.
E(1). VAL &E(2). VAL are left & right children of this node.
Similarly, Node (∗, E(1). VAL, E(2). VAL) will make the syntax as −
Function UNARY (−, E(1). VAL) will make a node – (unary minus) & E(1). VAL will be the only child
of it.
Function LEAF (id) will create a Leaf node with label id.
Example 1 : construct a syntax tree for an expression a − 4 + c.
In this sequence, p1, p2, …. . p5 are pointers to the symbol table entries for identifier 'a'
and 'c' respectively.
The tree is generated in a bottom-up fashion. The function calls mkleaf (id, entry a) and mkleaf
(num 4) construct the leaves for a and 4. The pointers to these nodes are stored using p 1and p2.
The call mknodes (′−′, p1, p2 ) then make the interior node with the leaves for a and 4 as
children. The syntax tree will be
Example 2 − Draw Syntax Tree for the string a + b ∗ c − d.
Postfix Notation
abc+*d2/-
Example 7: Syntax Tree for the string a – b ∗ c + d is:
o The synthesized attributes in the annotated parse tree of Figure can be evaluated by
an LR parser during a bottom-up parse of the input line 3* 5+4n.
o The following figure shows the sequence of moves made by the parser on input 3*5+4n.
o The contents of the state and val fields of the parsing stack are shown after each move.
o We again take the liberty of replacing stack states by their corresponding grammar
symbols.
o We take the further liberty of showing, instead of token digit, the actual input digit.
o Consider the sequence of events on seeing the input symbol 3.
o In the first move, the parser shifts the state corresponding to the token digit (whose
attribute value is 3) onto the stack. (The state is represented by 3 and the value 3 is in the
val field,)
o On the second move, the parser reduces by the production F -> digit and implements the
semantic rule F.val := digit.lexval.
o On -the third move the parser reduces by T->F.
o No code fragment is associated with this production, so the val array is left unchanged.
o Note that after each reduction the top of the val stack contains the attribute value
associated with the left side of the reducing production.
o Note:
At each shift of digit, we also push digit.lexval into val-stack. • At all other shifts, we do
not put anything into val-stack because other terminals do not have attributes (but we
increment the stack pointer for val-stack).
Figure: Moves made by translator on input 3*5+4 n.
ADDITIONAL Examples
o Example 1:
o Consider the grammar and Semantic Action and Construct Syntax tree, parse tree
and annotated tree for the string 5*6+7;
o For the Non-terminals E,T and F the values can be obtained using the attribute “Val”.
o The taken digit has synthesized attribute “lexval”.
o In S→EN, symbol S is the start symbol. This rule is to print the final answer of
expressed.
o The corresponding annotated parse tree is shown below for the string 5*6+7;
o Example 2:
o PROBLEM: Consider the grammar that is used for Simple desk calculator.
o Obtain the Semantic action and also the annotated parse tree for the string 3*5+4n.
L→En
E→E1+T
E→T
T→T1*F
T→F
F→ (E)
F→digit
o Solution:
o The corresponding annotated parse tree shown below, for the string 3*5+4n.
o Example 3:
o Implementation of Syntax directed translation.
o Syntax direct translation is implemented by constructing a parse tree and performing the
actions in a left to right depth first order.
o SDT is implementing by parse the input and produce a parse tree as a result.