BCSE307L_COMPILER DESIGN
Semantics Dr. B.V. Baiju,
SCOPE,Assistant Professor
Analysis
VIT, Vellore
Semantic Analysis
• Semantic Analysis makes sure that declarations and statements of program are
semantically correct.
• It uses syntax tree and symbol table to check whether the given program is
semantically consistent with language definition.
• It gathers type information and stores it in either syntax tree or symbol table.
Semantic Errors:
• Errors recognized by semantic analyzer are as follows:
– 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.
Syntax Directed Definition (SDD)
• A syntax-directed definition (SDD) is a context-free grammar together with
attributes and rules.
Attributes + CFG + Semantic Rules = Syntax Directed Definition
• Attributes are associated with grammar symbols (i.e. nodes of the parse tree)
– The attribute of a grammar symbol can be numbers, types, table
references, or a string.
• Rules are associated with productions.
• If X is a symbol and a is one of its attributes, then we write
X.a
• This denotes the value a at a node in the parse-tree with the label X.
Production Semantic Rules
E → E1 + T [Link] = [Link]+[Link] val is attribute
E→T [Link] = [Link]
• Semantic rules setup dependencies between attributes which can
represented by a dependency graph
• The dependency graph determines the evaluation order of these
semantic rules
• Evaluation of a semantic rule defines the value of an attribute.
Inherited and Synthesised Attribute
• There are two kinds of attributes for the grammar symbol : for a non-terminal A, at a
parse tree node N
a. Synthesized attribute (S- attribute)
• A non-terminal A at node N in a parse tree is defined by a semantic rule that is
associated with the production at N.
• Synthesized attributes get values from the attribute values of their child nodes.
• Bottom up manner (Child to parent node)
• Terminals can have synthesized
attribute.
• Attributes can have lexical values and
these values supplied by lexical analyzer
• Semantic rules can be applied only
for non terminals for computing
value of its attribute
Example : S → ABC
S is said to be a synthesized attribute if it takes values from its child node (A, B, C).
• A syntax directed definition that uses synthesized
Production Semantic rules
attribute exclusively is said to be S-attribute
definition. L En Print ([Link])
n : Used to terminate the evaluation E E1+T [Link] = [Link] + [Link]
Lexval : Lexical value, value for this terminal
ET [Link] = [Link]
is passed by lexical analayzer
L En : Augmented Grammar T T1*F [Link] = [Link] * [Link]
val : Attribute
[Link] = [Link] : [Link] used to define [Link] TF [Link] = [Link]
[Link] = [Link] + [Link] : E. val can be derived F (E) [Link] = [Link]
from adding [Link] with [Link]
F digit [Link] = [Link]
Syntax directed definition of simple
desk calculator which evaluates
expressions terminated by an
endmarker, n
Evaluating an SDD at the Nodes of a Parse Tree
• To determine the value of attributes at nodes in a parse tree, do the following:
– We must first construct a parse tree.
– Then we must use SDD rules to assess the values of all the nodes in the parse
tree's attributes.
– An annotated parse tree is formed via this improvised parse tree.
Construct Annotated Parse Tree
• Annotated Parse tree contains the values and attributes at each node.
• Before analyzing a node's attribute value, first evaluate all the elements that
determine its value.
– To evaluate a node's synthesized attribute, parse the tree from the bottom up.
• Its value is determined by the value of the child attributes of the concerned
node and the node itself.
– To determine a node's inherited attribute, parse the tree from the top down.
• Its value is determined by its parent, siblings, and the node itself.
– Synthesized and inherited attributes may coexist at some nodes in a parse tree. In
this scenario, we're not sure if there is even a single order in which the nodes'
characteristics may be evaluated.
Example: Synthesized attributes Production Semantic rules
String: 3*5+4n; L En Print ([Link])
E E1+T [Link] = [Link] + [Link]
L
ET [Link] = [Link]
n
[Link]=19 T T1*F [Link] = [Link] * [Link]
The process of TF [Link] = [Link]
+ [Link]=4 computing the
[Link]=15 F (E) [Link] = [Link]
attribute
values at the F digit [Link] = digit . lexval
[Link]=15 [Link]=4
node is called
annotating or
* [Link]=4 decorating the
[Link]=3 [Link]=5 parse tree
[Link]=3 [Link]=5
[Link]=3 parse tree showing the value
of the attributes at each node
is called Annotated parse tree
Annotated parse tree for 3*5+4n
Exercise
Draw Annotated Parse tree for following using the SDD:
1. (3+4)*(5+6)n
2. 1*2*3*(4+5)n
Production Semantic rules
3. (9+8*(7+6)+5)*4n
L En Print ([Link])
E E1+T [Link] = [Link] + [Link]
ET [Link] = [Link]
T T1*F [Link] = [Link] * [Link]
TF [Link] = [Link]
F (E) [Link] = [Link]
F digit [Link] = digit . lexval
(3+4)∗(5+6)n
1*2*3*(4+5)n
Solution2
Inherited Attribute
(3+4)*(5+6)n Solution3
(9+8∗(7+6)+5)∗4n
b. Inherited attribute (I-attribute)
• For a nonterminal B at a parse-tree node N is defined by a semantic rule associated
with the production at the parent of N.
• The value of inherited attribute is computed from the values of attributes at the siblings
and parent of that node.
• The value of an inherited attribute for a non-terminal symbol (node) N can be defined as
– The attribute values of N’s parent.
– Attribute values of N’s Siblings. Example;
– Total attribute values of N itself. S → ABC
• Top down manner (Parent to child)
A can get its values from S, B and C
B can get its values from S, A and C
C can get its values from A, B and S
Syntax Directed Definitions are used to specify syntax directed translation.
• There are 2 types of SDD
S – Attributed Definitions
L - Attributed Definitions
S – Attributed Definitions
• Only synthesized attributes used in syntax directed definition.
• S-attributed grammars interact well with LR(K) parsers, since the evaluation of
attributes is bottom-up.
L - Attributed Definitions
• Uses both synthesized attribute and inherited attribute
• An inherited value at a node in a parse tree is computed from the value of attributes at
the parent and/or only left siblings of the node.
• L-attributed grammars interact well with LL(K) parsers
L-Attributed Definitions
Definition: An SDD is L-Attributed if each attribute is either
[Link].
[Link] from the left, and hence the name L-attributed.
If the production is A → X1X2...Xn, then the inherited attributes for Xj can depend only on
[Link] attributes of A, the LHS.
[Link] attribute of X1, ..., Xj-1, i.e. only on symbols to the left of Xj.
[Link] of Xj, *BUT* you must guarantee (separately) that these attributes do not by
themselves cause a cycle.
Example for synthesized attribute:
Production Semantic Rules
E → E1 * E2 {[Link] = [Link] * [Link]}
E → E1 + E2 {[Link] = [Link] +[Link]}
E → int {[Link] = [Link]}
String: 3*2+5
An SDD based on a grammar suitable for top-down parsing
TT*F
TF
F digit
• Grammar contains 3 non terminal T, F, T’
• digit has attribute value (i.e lexval)
• val : Synthesized attribute of non terminal T, F (T. val and [Link])
• T’ has 2 attribute
• inh inherited attribute
• syn synthesized attribute
T FT’ ( T’ can be inherited from F, T can have the value from T’ )
T’ *FT’ (T’ can be inherited from F)
Construct Annotated parse tree for 3 * 5
• An inherited attribute distributes type information to the various identifiers in a
declaration.
• Consider the production D T V which is used for a single declaration such as
D stands for declaration
int sum T stands for type (int)
V stands for variable (Sum)
Production Semantic Rules
DTV [Link] = [Link]
Example for Inherited Attributes:
• The keyword int or real followed by a list
Production Semantic Rules of identifiers.
D → TL [Link] = [Link] • Symbol T is associated with a
T → int [Link] = integer synthesized attribute type
T → real [Link] = real • Symbol L is associated with an inherited
L → L1, id [Link] = [Link]; attribute [Link].
addtype([Link], [Link]) • Rules associated with L call for procedure
L → id addtype([Link], [Link]) add type to the type of each identifier to its
entry in symbol table
Input: int id, id
• The nonterminal T has a
synthesized attribute
whose value is received
by the inherited
attribute of L via the
semantic rule
[Link] := [Link].
• Then the type [Link] is
passed down a parse
tree via the semantic
rule
[Link] := [Link].
Evaluation Orders for SDD's
• Dependency graphs are a useful tool for determining an evaluation order for the
attribute instances in a given parse tree.
• Semantic rules set up dependencies between attributes which can be represented by a
dependency graph.
• While an annotated parse tree shows the values of attributes, a dependency graph
helps us determine how those values can be computed.
• It is a Directed Graph
• If an attribute b at a node depends on an attribute c, then the semantic rule for
b at that node must be evaluated after the semantic rule that defines c.
• Construction:
– Put each semantic rule into the form b=f(c1,…,ck) by introducing dummy
synthesized attribute b for every semantic rule that consists of a procedure call.
– E.g.,
LEn print([Link])
Becomes: dummy = print([Link])
– The graph has a node for each attribute and an edge to the node for b from the
node for c if attribute b depends on attribute c.
• A dependency graph indicate the flow of information among the instances of attributes
in a given parse tree.
• An edge from one attribute instance to another means that the value of the first is
needed to compute the second.
• Edges express constraints implied by the semantic rules
For each parse-tree node, say a node labeled by grammar symbol X, the dependency
graph has a node for each attribute associated with X.
Suppose that a semantic rule associated with a production p defines the value
of synthesized attribute A.b in terms of the value of X.c (the rule may define A.b
in terms of other attributes in addition to X.c) . Then, the dependency graph has
an edge from X.c to A.b. More precisely, at every node N labeled as A where
production p is applied, create an edge to attribute b at N, from the attribute c
at the child of N corresponding to this instance of the symbol X in the body of
the production1 .
Suppose that a semantic rule associated with a production p defines the value
of inherited attribute B.i in terms of the value of X.a. Then, the dependency
graph has an edge from X.a to B.i. For each node N labeled B that corresponds
to an occurrence of this B in the body of production p, create an edge to
attribute i at N from the attribute a at the node M that corresponds to this
occurrence of X. Note that M could be either the parent or a sibling of N.
• Example
Production Semantic Rule
E→E1 + E2 [Link] = [Link] + [Link]
• [Link] is synthesized from [Link] and [Link]
• The dotted lines represent the parse tree that is
not part of the dependency graph.
Dependency Graph Construction
for each node n in the parse tree do
for each attribute a of the grammar symbol at n do
construct a node in the dependency graph for a
for each node n in the parse tree do
for each semantic rule b = f(c1,…,cn)
associated with the production used at n do
for i= 1 to n do
construct an edge from the node ci to the node b
Evaluation order
• A topological sort of a directed acyclic graph is any ordering 𝑚1, 𝑚2, … … … . . , 𝑚𝑘 of the
nodes of the graph such that edges go from nodes earlier in the ordering to later nodes.
• If 𝑚𝑖𝑚𝑗 is an edge from 𝑚𝑖 to 𝑚𝑗 then 𝑚𝑖 appears before 𝑚𝑗 in the ordering.
1 [Link]=real [Link]=real 2
real 3 ,
[Link]=real id3 4
,
5 [Link]=real id2 6
7 id1
Example:
Construct a Dependency graph for the annotated parse tree, whose SDD is given in
Table . The input string is 4 * 6
The nodes of the dependency graph,
represented by the numbers 1 through 9
• Nodes 1 and 2 represent the attribute lexval associated with the two leaves labeled digit.
• Nodes 3 and 4 represent the attribute val (i.e., 4 and 6) associated with the two nodes
labeled F.
• The edges to node 3 from 1 and to node 4 from 2 result from the semantic rule that defines
[Link] in terms of digit.lexua1.
• In fact, [Link] equals [Link], but the edge represents dependence, not equality.
• Nodes 5 and 6 represent the inherited attribute T ′ .inh associated with each of the
occurrences of nonterminal T ′ (T ′ → ∗F T ′ 1 and T ′ → ε).
• The edge from 3 to 5 is due to the rule T ′ .inh = [Link], which defines T ′ .inh at the right
child of the root from F.va1 at the left child.
• We see edges to node 6 from node 5 for T ′ .inh and from node 4 for [Link], because these
values are multiplied to evaluate the attribute inh at node 6.
• Nodes 7 and 8 represent the synthesized attribute syn associated with the occurrences of T ′
• The edge to node 7 from 6 is due to the semantic rule T ′ .syn = T ′ .inh associated with
production T ′ → ε.
• The edge to node 8 from 7 is due to a semantic rule associated with production 2.
• Finally, node 9 represents the attribute [Link]. The edge from 8 to 9 is due to the semantic
rule, [Link] = T ′ .syn, associated with production
Expression 5 + 8 *10
Expression 3 * 5 *2
• Attribute grammar
• This is a special case of context free grammar where additional
information is appended to one or more non-terminals in-order to provide
context-sensitive information.
The right side contains semantic rules
that specify how the grammar should be
• Example interpreted.
Given the CFG below; The non-terminal values of E and T are
added and their result copied to the non-
E → E + T { [Link] = [Link] + [Link] }
terminal E.
• Consider the grammar for signed binary numbers
number → signlist
sign → + | −
list → listbit | bit
bit → 0 | 1
• We want to build an
attribute grammar that
Attribute Grammar
annotates Number with the
value it represents.
• First we associate attributes
with grammar symbols.
Symbol Attributes
number val
sign neg
list pos, val
bit pos, val