Copch 4
Copch 4
SEMANTIC ANALYSIS
1
SEMANTIC ANALYSIS
2
SEMANTIC ANALYSIS(…)
3
SEMANTIC ANALYSIS(…)
7
1. SYNTAX-DIRECTED DEFINITIONS
10
SYNTAX-DIRECTED DEFINITIONS(…)
Attributes
structure. 11
ATTRIBUTE(…)
Attributes examples as
12
ATTRIBUTE(…)
13
Attribute (…)
14
SYNTHESIZED AND INHERITED ATTRIBUTES
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 15
16
SYNTHESIZED ATTRIBUTES…
17
SYNTHESIZED ATTRIBUTES…..
18
INHERITED ATTRIBUTES
2. Inherited attributes:- it can take values from parent
19
INHERITED ATTRIBUTES
20
INHERITED ATTRIBUTES…
21
INHERITED ATTRIBUTES…
24
INHERITED ATTRIBUTES(…)
The semantic rule L.in := T.type, associated with
production D –>T L,
Sets inherited attribute L.in to the type in the
declaration.
The rules then pass this type down the parse tree
using the inherited attribute L.in.
Rules associated with the productions for L call
procedure addtype to add the type of each identifier
to its entry in the symbol table (pointed to by
attribute entry).
25
INHERITED ATTRIBUTES(…)
The following figure shows the annotated parse tree for the
sentence real id1 , id2 , id3 .
26
INHERITED ATTRIBUTES(…)
29
ANNOTATED PARSE TREE…
E.g. Write the SDD for a simple desk calculator and show the
annotated parse tree for the following expression 3*5+4.
30
ANNOTATED PARSE TREE…
32
ANNOTATED PARSE TREE…
33
EVALUATING SDD AT THE NODE OF THE PARSE TREE
34
DEPENDENCY GRAPH
35
DEPENDENCY GRAPH…
36
DEPENDENCY GRAPH…
For example, suppose A.a := f (X.x, Y.y) is a semantic
rule for the production A → XY.
This rule defines a synthesized attribute A.a that
depends on the attributes X.x and Y.y.
If this production is used in the parse tree, then there
will be three nodes A.a, X.x, and Y.y in the dependency
graph with
an edge to A.a from X.x since A.a depends on X.x,
and
an edge to A.a from Y.y since A.a also depends on
Y.y. 37
DEPENDENCY GRAPH…
38
DEPENDENCY GRAPH…
Example 1
39
DEPENDENCY GRAPH…
40
DEPENDENCY GRAPH…
41
DEPENDENCY GRAPH…
42
DEPENDENCY GRAPH…
digit.lexval.
43
DEPENDENCY GRAPH…
44
DEPENDENCY GRAPH…
45
EVALUATION ORDER OF SDD ATRBIUTES
A topological sort of a directed acyclic graph is any ordering m1
, m2 , ..., mk of the nodes of the graph such that edges go from
nodes earlier in the ordering to later nodes;
that is, if mi → mj is an edge from mi to mj , then mi appears
before mj in the ordering.
Any topological sort of a dependency graph gives a valid order
in which the semantic rules associated with the nodes in a parse
tree can be evaluated.
That is, in the topological sort, the dependent attribute c1 ,
c2 , ..., ck in a semantic rule b := f (c1 , c2 , ..., ck ) are
available at a node before f is evaluated.
Evaluation of the semantic rules in this order yields the
translation of the input string.
46
EVALUATION ORDER….
47
EVALUATION ORDER….
49
EVALUATION ORDER…
S-Attributed grammars...
In S-Attributed each semantic rule computes the
attribute value of non terminal that occurs LHS of the
production that represent head of the production with
the help of the attribute values of non terminal on the
RHS of the production that represent body of the
production.
51
EVALUATION ORDER…
52
EVALUATION ORDER..
L-attributed
It is difficult to execute the tasks of the compiler just by
synthesized attributes.
The L-attributed (L stands for left) class of grammar allows
a limited kind of inherited attributes.
B/n the attributes associated with a production
body ,dependency graph edges can go from left to right but
not from right to left(hence L-attributed).
A grammar is L-Attributed if and only if for each rule X0 → X1
X2 . . . Xj . . . XN, all inherited attributes of Xj depend only on:
1. Attributes of X1 X2 . . . Xj-1
2. Inherited attributes of X0
Of course all S-attributed grammars are L-attributed . 53
EVALUATION ORDER…
L-attributed…
A → L M {L.h = f1 (A.h) , M.h = f2 (L.s) , A.s = f3 (M.s) }
This production does not contradict the rules of L-
attributed grammars.
Therefore, the corresponding grammar may be L-
attributed if all of the other productions follow the rule of
L-attributed grammars.
55
CONSTRUCTION OF SYNTAX TREE…
An (abstract) syntax tree is a condensed form of parse tree
useful for representing language constructs.
The productions S→if B then S1 else S2 might appear in a
syntax tree as:
60