Lecture 07
Lecture 07
• Type Checking –
Ensures that data types are used in a way
consistent with their definition.
• Label Checking –
A program should contain labels references.
• Flow Control Check –
Keeps a check that control structures are used
in a proper manner.(example: no break
statement outside a loop)
Types of Semantic Errors
1
2
Syntax-Directed Translation
1. We associate information with the programming language
constructs by attaching attributes to grammar symbols.
1
4
Syntax-Directed Definitions and Translation Schemes
1. When we associate semantic rules with productions, we
use two notations:
– Syntax-Directed Definitions (abstract)
– Translation Schemes (detail)
A. Syntax-Directed Definitions:
– give high-level specifications for translations
– hide many implementation details such as order of evaluation of semantic
actions.
– We associate a production rule with a set of semantic actions, and we do not
say when they will be evaluated.
B. Translation Schemes:
– indicate the order of evaluation of semantic actions associated with a
production rule.
– In other words, translation schemes give a little bit information about
implementation details. 1
5
Syntax-Directed Definitions and Translation
Schemes
• Conceptually with both the syntax directed translation and
translation scheme we
– Parse the input token stream
– Build the parse tree
– Traverse the tree to evaluate the semantic rules at the parse tree nodes.
1
6
Syntax-Directed Definitions
1. A syntax-directed definition is a generalization of a context-free
grammar in which:
– Each grammar symbol is associated with a set of attributes.
– This set of attributes for a grammar symbol is partitioned into
two subsets called
• synthesized and
• inherited attributes of that grammar symbol.
– Each production rule is associated with a set of semantic rules.
2. The value of an attribute at a parse tree node is defined by the
semantic rule associated with a production at that node.
3. The value of a synthesized attribute at a node is computed from
the values of attributes at the children in that node of the parse tree
4. The value of an inherited attribute at a node is computed from the
values of attributes at the siblings and parent of that node of the
parse tree 17
Syntax-Directed Definitions
Examples:
Synthesized attribute : E→E1+E2 { E.val =E1.val + E2.val}
Inherited attribute :A→XYZ {Y.val = 2 * A.val}
18
Syntax-Directed Definition
In a syntax-directed definition, each production A→α is
associated with a set of semantic rules of the form:
b=f(c1,c2,…,cn)
where f is a function and b can be one of the followings:
19
Syntax-Directed Definition -- Example
Production Semantic Rules
L→En print(E.val)
E → E1 + T E.val = E1.val + T.val
E→T E.val = T.val
T → T1 * T.val = T1.val * F.val
FT→F T.val = F.val
F→(E) F.val = E.val
F → digit F.val = digit.lexval
1. Symbols E, T, and F are associated with a synthesized attribute val.
2. The token digit has a synthesized attribute lexval (it is assumed that it is
evaluated by the lexical analyzer).
3. Terminals are assumed to have synthesized attributes only. Values for attributes
of terminals are usually supplied by the lexical analyzer.
4. The start symbol does not have any inherited attribute unless otherwise stated.
2
0
Syntax-Directed Definitions (SDD)
Example
2
9
Syntax-Directed Definition – Inherited Attributes
Production Semantic Rules
D→TL L.in = T.type T.type
T → int = integer T.type =
T → real real
L → L1 L1.in = L.in,addtype(id.entry,L.in)
id L → addtype(id.entry,L.in)
id
1. Symbol T is associated with a synthesized attribute type.
3
0
Syntax-Directed Definition -- Example
Production Semantic Rules
L→En print(E.val)
E → E1 + T E.val = E1.val + T.val
E→T E.val = T.val
T → T1 * F T.val = T1.val * F.val
T→F T.val = F.val
F→(E) F.val = E.val
F → digit F.val = digit.lexval
1. Symbols E, T, and F are associated with a synthesized attribute val.
2. The token digit has a synthesized attribute lexval (it is assumed that
it is evaluated by the lexical analyzer).
3. Terminals are assumed to have synthesized attributes only. Values for
attributes of terminals are usually supplied by the lexical analyzer.
4. The start symbol does not have any inherited attribute unless
otherwise stated.
31
Inherited attributes
• An inherited value at a node in a parse tree is defined in
terms of attributes at the parent and/or siblings of the node.
32
Syntax-Directed Definition – Inherited
Attributes
Production Semantic Rules
D→TL L.in = T.type
T → int T.type = integer
T → real T.type = real
L → L1 id L1.in = L.in, addtype(id.entry,L.in)
L → id addtype(id.entry,L.in)
33
Example Attribute Grammar
Production Attribute Rule
𝑆 → if 𝑃 then 𝑆1else 𝑆2
𝐵 𝑆1 𝑆2
CS Swarnendu
335 Biswas
Parse Tree and Abstract
Syntax Tree
Parse Tree Abstract Syntax Tree
𝐸𝑥𝑝𝑟 +
𝐸𝑥𝑝𝑟 + 𝑇𝑒𝑟𝑚
− name
𝐹𝑎𝑐𝑡𝑜𝑟 name
name
CS Swarnendu
335 Biswas
• Abstract Syntax Trees(ASTs).
• These are a reduced form of a parse tree.
They don't check for string membership in the
language of the grammar.
They represent relationships between language
constructs and avoid derivations.
• An example
The parse tree and abstract syntax tree for the
expression
a := b * c + d is.
Abstract Syntax Trees(ASTs).
𝑠𝑖𝑔𝑛 𝑙𝑖𝑠𝑡
− 𝑙𝑖𝑠𝑡 𝑏𝑖𝑡
𝑙𝑖𝑠𝑡 𝑏𝑖𝑡
𝑏𝑖𝑡
1 0 1
CS
335
Annotated Parse Tree
1. A parse tree showing the values of attributes at each
node is called an annotated parse tree.
2. Values of Attributes in nodes of annotated parse-tree are
either,
– initialized to constant values or by the lexical analyzer.
– determined by the semantic-rules.
42
Annotated Parse Tree -- Example
Input: 5+3*4 L
E.val=17 n
E.val=5 + T.val=12
digit.lexval=5 digit.lexval=3
43
Annotated Parse Tree
𝑛𝑢𝑚𝑏𝑒𝑟
attribute(s) is called an
annotated parse tree − 𝑙𝑖𝑠𝑡 𝑏𝑖𝑡
𝑙𝑖𝑠𝑡 𝑏𝑖𝑡
𝑏𝑖𝑡
1 0 1
CS
335
Annotated Parse Tree
𝑛𝑢𝑚𝑏𝑒𝑟
4 𝑝𝑜𝑠 = 2 𝑏𝑖𝑡
1 0 1
CS
335
Annotated Parse Tree
𝑣𝑎𝑙 =?
𝑛𝑢𝑚𝑏𝑒𝑟
𝑝𝑜𝑠 = 2 𝑏𝑖𝑡
1 0 1
CS
335
Annotated Parse Tree
𝑣𝑎𝑙 = −5
𝑛𝑢𝑚𝑏𝑒𝑟
𝑣𝑎𝑙 = 5
• A parse tree showing 𝑛𝑒𝑔 = 𝑡𝑟𝑢𝑒 𝑠𝑖𝑔𝑛 𝑙𝑖𝑠𝑡 𝑝𝑜𝑠 = 0
the value(s) of its
attribute(s) is 𝑣𝑎𝑙 = 1
𝑛𝑒𝑔 = 𝑡𝑟𝑢𝑒 − 𝑝𝑜𝑠 =1 𝑙𝑖𝑠𝑡 𝑣𝑎𝑙 = 4 𝑝𝑜𝑠 = 0 𝑏𝑖𝑡
called an annotated
parse tree 𝑣𝑎𝑙 = 4
𝑣𝑎𝑙 = 0
𝑝𝑜𝑠 = 2 𝑙𝑖𝑠𝑡 𝑝𝑜𝑠 =1 𝑏𝑖𝑡
𝑝𝑜𝑠 = 2 𝑣𝑎𝑙 = 4
𝑏𝑖𝑡
1 0 1
CS
335
Dependency Graph
• If an attribute 𝑏 depends on an attribute 𝑐 then the semantic rule for
𝑏 must be evaluated after the semantic rule for 𝑐
• The dependencies among the nodes are depicted by a directed graph
called dependency graph
CS
335
Dependency Graph
• Suppose 𝐴. 𝑎 = 𝑓 𝑋. 𝑥, 𝑌. 𝑦 is a semantic rule for 𝐴 → 𝑋𝑌
𝐴 𝐴. 𝑎
Parse tree Dependency
graph
𝑋 𝑌 𝑋. 𝑥 𝑌. 𝑦
𝑋 𝑌 𝑋. 𝑥 𝑌. 𝑦
CS
335
Dependency Graph
• Directed Graph
• Shows interdependencies between attributes.
• 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.,
• L→En print(E.val)
• Becomes: dummy = print(E.val)
– 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.
50
Dependency Graph Construction
• Example
• Production Semantic Rule
E→E1 + E2 E.val = E1.val + E2.val
E . val
5
1
Dependency Graph
D → T L L.in =
T.type
T → int T.type = integer
T → real T.type = real
L → L1 L1.in = L.in,
id addtype(id.entry,L.in)
L→ addtype(id.entry,L.in)
id
15
Dependency Graph
Input: 5+3*4 L
E.val=17 n
E.val=5 + T.val=12
digit.lexval=5 digit.lexval=3
53
Dependency Graph
D → T L L.in = T.type
T → int T.type = integer
T → real T.type = real
L → L1 id L1.in = L.in,
addtype(id.entry,L.in)
L → id addtype(id.entry,L.in)
54
Syntax Trees
Syntax-Tree
– an intermediate representation of the compiler’s input.
– A condensed form of the parse tree.
– Syntax tree shows the syntactic structure of the program while
omitting irrelevant details.
– Operators and keywords are associated with the interior nodes.
– Chains of simple productions are collapsed.
Syntax directed translation can be based on
syntax tree as well as parse tree.
55
Syntax Tree-Examples
Expression: if B then S1 else S2
+ if - then - else
5 * B S1 S2
Statement:
3 4 • Node’s label indicates what kind
• Leaves: identifiers or constants of a statement it is
• Internal nodes: labelled with • Children of a node correspond to
operations the components of the
• Children: of a node are its statement
operands
56
Constructing Syntax Tree for
Expressions-
Example: a-4+c +
1. p1:=mkleaf(id,entrya);
2. p2:=mkleaf(num,4); - id
3. p3:=mknode(-,p1,p2)
4. p4:=mkleaf(id,entryc); to entry for c
bottom up.
57
A syntax Directed Definition for
Constructing Syntax Tree
1. It uses underlying productions of the grammar to schedule the
calls of the functions mkleaf and mknode to construct the syntax
tree
2. Employment of the synthesized attribute nptr (pointer) for E and T
to keep track of the pointers returned by the function calls.
PRODUCTION SEMANTIC RULE
E → E1 + T E.nptr = mknode(“+”,E1.nptr ,T.nptr)
E → E1 - T E.nptr = mknode(“-”,E1.nptr ,T.nptr)
E→T E.nptr = T.nptr
T → (E) T.nptr = E.nptr
T → id T.nptr = mkleaf(id, id.lexval)
T → num T.nptr = mkleaf(num, num.val)
58
S-Attributed Definitions
1. Syntax-directed definitions are used to specify syntax-directed
translations.
2. To create a translator for an arbitrary syntax-directed definition can be
difficult.
3. We would like to evaluate the semantic rules during parsing (i.e. in a
single pass, we will parse and we will also evaluate semantic rules
during the parsing).
4. We will look at two sub-classes of the syntax-directed definitions:
– S-Attributed Definitions: only synthesized attributes used in the
syntax-directed definitions.
– All actions occur on the right hand side of the production.
– L-Attributed Definitions: in addition to synthesized attributes, we
may also use inherited attributes in a restricted fashion.
5. To implement S-Attributed Definitions and L-Attributed Definitions we
can evaluate semantic rules in a single pass during the parsing.
6. Implementations of S-attributed Definitions are a little bit easier than
implementations of L-Attributed Definitions
59
Bottom-Up Evaluation of S-Attributed
Definitions
• A translator for an S-attributed definition can often be
implemented with the help of an LR parser.
• From an S-attributed definition the parser generator can
construct a translator that evaluates attributes as it parses the
input.
• We put the values of the synthesized attributes of the grammar
symbols a stack that has extra fields to hold the values of
attributes.
– The stack is implemented by a pair of arrays val & state
– If the ith state symbol is A the val[i] will hold the value of the
attribute associated with the parse tree node corresponding to
this A.
60
Annotated
Parse Tree 𝑒𝑥𝑝𝑟. 𝑐𝑜𝑑𝑒 = "95 − 2 + "
"2"
𝑒𝑥𝑝𝑟. 𝑐𝑜𝑑𝑒 = "9" "−" 𝑡𝑒𝑟𝑚. 𝑐𝑜𝑑𝑒 = "5"
"9"
CS Swarnendu
335 Biswas
S-Attributed Definition
• An SDD that involves only synthesized attributes is called
S-attributed definition
• Each rule computes an attribute for the head nonterminal from
attributes taken from the body of the production
6
4
Draw the Example 3*5+4n
Tree L
E val=19
Print(19)
E val=15
T val=15
T val=4
T val=3
F val=4
F val=3 F val=5
n
digit
* + lexval =4
digit digit
lexval =3 lexval =5
L-Attributed Definitions
• When translation takes place during parsing, order of
evaluation is linked to the order in which the nodes of a
parse tree are created by parsing method.
• A natural order can be obtained by applying the procedure of visit
stands for left since attribute information flows from left to right).
L-Attributed Definitions
A syntax-directed definition is L-attributed if each inherited attribute
of Xj, where 1≤j≤n, on the right side of A → X1X2...Xn depends
only on
production
6
8
S.NO Synthesized Attributes Inherited Attributes
The production must have non-terminal as its The production must have non-terminal as a
2.
head. symbol in its body.
A synthesized attribute at node n is defined only A Inherited attribute at node n is defined only in
3. in terms of attribute values at the children of n terms of attribute values of n’s parent, n itself,
itself. and n’s siblings.
It can be evaluated during a single bottom-up It can be evaluated during a single top-down and
4.
traversal of parse tree. sideways traversal of parse tree.
Synthesized attributes can be contained by both Inherited attributes can’t be contained by both, It
5.
the terminals or non-terminals. is only contained by non-terminals.