0% found this document useful (0 votes)
19 views60 pages

Copch 4

Uploaded by

Abigail Tekle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views60 pages

Copch 4

Uploaded by

Abigail Tekle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

CHAPTER FOUR:

SEMANTIC ANALYSIS

1
SEMANTIC ANALYSIS

2
SEMANTIC ANALYSIS(…)

 In semantic analysis we check whether they form a


sensible set of instructions in the programing language.
 It accepts the parse tree from the syntax analysis phase
and adds the semantic information to the parse tree and
performs certain checks based on the information.
 It also helps constructing the symbol table with
appropriate information

3
SEMANTIC ANALYSIS(…)

• Save information in the symbol table


• Helps on intermediate code generation 4

• Display appropriate error message


SEMANTIC ANALYSIS(…)
 There are two notations for associating semantic rules
with productions that used describe the semantic language
very easily.

1. Syntax-Directed Definitions(SDD) and

2. Syntax-Directed Translation (SDT)


 Syntax-directed definitions are high-level specifications
for translations.
 They hide many implementation details and free the
user from having to specify explicitly the order in 5

which translation takes place.


SEMANTIC ANALYSIS(…)
 Translation
schemes indicate the order in which
semantic rules are to be evaluated.

In other words, translation schemes give more

information about implementation details.

 We use both notations for specifying semantic

checking, particularly the determination of types, and

for generating intermediate code.


6
SEMANTIC ANALYSIS(…)

 Conceptually, with both SDD and translation


schemes:
we parse the input token stream,
 build the parse tree, and
 then traverse the tree as needed to evaluate the
semantic rules at the parse-tree nodes.

7
1. SYNTAX-DIRECTED DEFINITIONS

 Is a generalization of a context-free grammar


 Is a CFG that is attributes and semantic rules.
i.e CFG + semantic rules = Syntax Directed Definitions
 The attributes are associated with grammar symbols(non
terminal)where as the semantic rules are associated with
productions.
 The semantic rules are used to compute the attribute
value. 8
1. SYNTAX-DIRECTED DEFINITIONS(…)

 Observe that a semantic rule is associated with


production where the attribute name val is associated
with each non terminal used in the rule. 9
SYNTAX-DIRECTED DEFINITIONS(…)
Semantic rule

 On the above given table on the production. E→E1+T


E.val=E1.val+T.val //semantic rule

10
SYNTAX-DIRECTED DEFINITIONS(…)
Attributes

structure. 11
ATTRIBUTE(…)

 Attributes examples as

12
ATTRIBUTE(…)

13
Attribute (…)

 The attribute value for a node in the parse tree may be


depend on information from its children nodes or its
sibling nodes or parent nodes.
 Based on the way the attributes get their values, they
can be broadly divided into two categories :
Synthesized attributes (S- attributes) and
 Inherited attributes(I- attributes)

14
SYNTHESIZED AND INHERITED ATTRIBUTES

1. Synthesized attributes :-These attributes get values from


the attribute values of their child or itself nodes.
• Synthesized attributes never take values from their
parent nodes or any sibling nodes.
• To illustrate, assume the following production:

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

ABC are synthesized to S.


SYNTHESIZED ATTRIBUTES

A parse tree of an S-attributed definition is annotated


with a single bottom-up traversal, from the leaves to
the root.
 In Bottom-up parser, the values of the parent nodes
depend upon the values of the child nodes and it uses
depth first traversal.

16
SYNTHESIZED ATTRIBUTES…

17
SYNTHESIZED ATTRIBUTES…..

18
INHERITED ATTRIBUTES
2. Inherited attributes:- it can take values from parent

and/or siblings or itself in a top-down manner.


As in the following production,
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.

19
INHERITED ATTRIBUTES

20
INHERITED ATTRIBUTES…

21
INHERITED ATTRIBUTES…

 Since attribute value for V is obtained from its


sibling, its inherited attributed its attribute is denoted
by inh.
 On similar line, the value int stored on V.inh is
transformed to its chiled id.entry and hence entry is
inherited attribute of id and attribute value is
denoted by id.entry.
22
INHERITED ATTRIBUTES….
 Inherited attributes are suitable for expressing the dependence of a
programming language construct on the context in which it
appears.
 E.g, we can use an inherited attribute to keep track of
whether
 An identifier appears on the left or right side of an
assignment,
 Let us consider an example that an inherited attribute that
distributes type information to the various identifiers in 23a
declaration.
INHERITED ATTRIBUTES(…)
 Example 3: A declaration generated by the nonterminal D in
the syntax-directed definition in the table below consists of
the keyword int or real, followed by a list of identifiers.
 The nonterminal T has a synthesized attribute type, whose
value is determined by the keyword in the declaration.

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(…)

 L-nodes gives the type of the identifiers id1 , id2 and


id3.
 The values are determined by computing the value of
the attribute T.type at the left child of the root and then
evaluating L.in top-down at the three L-nodes in the
right subtree of the root.
 At each L-node we also call the procedure addtype to
insert into the symbol table the fact that the identifier at
27

the right child of this node has type real.


SYNTHESIZED AND INHERITED ATTRIBUTES…

Annotated Parse Tree


 A parse tree showing the values of attributes at each node.
 annotating (or decorating) is the process of computing
the attributes values at the nodes.
 The terminal on the Annotated Parse Tree can have only
synthesized attribute values and they obtained directly
from the lexical analyzer.
 So, there are no semantic rules in SDD to get the lexical
values in to terminals of the Annotated Parse Tree. 28
ANNOTATED PARSE TREE…

 Findingevery node (non terminal)value is called


annotating.

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…

 The token digit has a synthesized attribute lexval


whose value is assumed to be supplied by the lexical
analyzer.
 The rule associated with the production L →En for
the starting nonterminal L is just a procedure call that
prints as output the value of the arithmetic expression
generated by E; we can think of this rule as defining a
dummy attribute for nonterminal L.
 In SDD, terminals are assumed to have synthesized
attribute only, as the definition does not provide any
semantic rules for terminals.
31
ANNOTATED PARSE TREE…

 Values for attributes of terminals are usually supplied


by the lexical analyzer.
 Furthermore, the start symbol is assumed not to have
any inherited attributes, unless otherwise stated.
 The output, printed at the root of the tree, is the value
of E.val at the first child of the root.

32
ANNOTATED PARSE TREE…

33
EVALUATING SDD AT THE NODE OF THE PARSE TREE

34
DEPENDENCY GRAPH

35
DEPENDENCY GRAPH…

 An edge from one attribute instance to another


attribute instance indicates that the attribute value
of the first is needed to compute value of the
second.

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…

 Ifthe production A → XY has the semantic rule


X.i := g(A.a, Y.y) associated with it, then there
will be
 an edge to X.i from A.a and also
an edge to X.i from Y.i, since X.i depends on
both A.a and Y.i

38
DEPENDENCY GRAPH…

Example 1

 Obtain the dependency graph for the annotated parse tree.

39
DEPENDENCY GRAPH…

40
DEPENDENCY GRAPH…

41
DEPENDENCY GRAPH…

42
DEPENDENCY GRAPH…

digit.lexval.

43
DEPENDENCY GRAPH…

from f.val at the left child.

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….

 Several methods have been proposed for evaluating


semantic rules:
1. Parse tree methods: At compile time, these
method obtain the evaluation order from a
topological sort of the dependency graph
constructed from the parse tree for each input.
 These method will fail to find an evaluation order
only if the dependency graph for a particular parse
tree under consideration has a cycle.
2. Rule based methods: The order in which the
attributes associated with a production are
48
evaluated is predetermined at compiler
construction time.
EVALUATION ORDER…

For this method, the dependency graph need not


be constructed.
3. Oblivious methods: The evaluation order is
chosen without considering the semantic rules.
This restricts the class of syntax directed
definition that can be used.
Rule based and oblivious methods need not
explicitly construct the dependency graph at
compile time.

49
EVALUATION ORDER…

o we say that an attributed grammar is S-Attributed


when all of its attributes are synthesized; i.e. it
doesn't have inherited attributes.

• Synthesized attributes can be evaluated by a


bottom-up parser as the input is being parsed.
50
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.

 A → Q R {R.h := f4 (A.h), Q.h := f5 (R.s), A.s = f6 (Q.s) }


 The grammar containing this production is not L-
Attributed since Q.h depends on R.s which contradicts
with the rule of L-Attributed grammars.
54
CONSTRUCTION OF SYNTAX TREE
 The syntax-directed definitions can be used to
specify the construction of syntax trees and other
graphical representations of language constructs.
 The use of syntax trees as an intermediate
representation allows translation to be decoupled
from parsing.
 The C compiler constructs a syntax tree for
declarations.

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:

 In a syntax tree, operators and keywords do not appear as


leaves, but rather, are associated with the interior node that
would be the parent of those leaves in the parse tree.
56
CONSTRUCTION OF SYNTAX TREES FOR EXPRESSIONS
 The construction of a syntax tree for an expression is similar to the
translation of the expression into postfix form.
 We construct subtrees of the subexpressions by creating a node for
each operator and operand.
 The children of an operator node are the roots of the node
representing the subexpressions constituting the operands of that
operator.
 Each node in a syntax tree can be implemented as a record with
several fields.
 In the node for an operator, one field identifies the operator and
the remaining fields contain pointers to the nodes for the
operands.
 The operator is often called the label of the node.
 When used for translation, the nodes in a syntax tree may have
57
additional fields to hold the values (or pointers to values) of
attributes attached to the node.
CONSTRUCTION OF SYNTAX TREES FOR
EXPRESSIONS…

 We use the following functions to create the nodes of the


syntax trees for expressions with binary operators.
 Each function returns a pointer to a newly created node.

1. mknode(op, left, right)- creates an operator node with label


op and two fields containing pointers to left and right.

2. mkleaf(id, entry) – creates an identifier node with label id


and a field containing entry, a pointer to the symbol-table
entry for the identifier.
58
3. mkleaf(num, val) – creates a number node with label num
and a field containing val, the value for the number.
CONSTRUCTION OF SYNTAX TREES FOR EXPRESSIONS…

 Example : The following sequence of function calls creates the


syntax tree for the expression a – 4 + c.
 In this sequence, p1, p2, . . . , p5 are pointers to nodes, and
entry a and entry c are pointers to the symbol-table entries for
identifiers a and c, respectively.
p1 := mkleaf(id, entrya);
p2 := mkleaf(num, 4);
p3 := mknode( '-', p1, p2);
p4 := mkleaf(id, entryc);
p5 := mknode( '+', p3, p4);
 The syntax tree for the expression a – 4 + c is constructed
bottom up. 59
CONSTRUCTION OF SYNTAX TREES FOR
EXPRESSIONS…
 The function calls mkleaf(id, entry a) and mkleaf(num, 4)
construct the leaves for a and 4; the pointers to these nodes
are saved using p1 and p2.
 The call mknode('-', p1, p2) then constructs the interior node
with the leaves for a and 4 as children.
 After two more steps, p5 is left pointing to the root.
 The resulting syntax tree for a – 4 + c is:

60

You might also like