Compi Desi CHP 04
Compi Desi CHP 04
Compiler Design
1
CHAPTER 4
2
Chapter Topics
3
Introduction
We have learnt how a parser constructs parse trees in the
syntax analysis phase.
The plain parse-tree constructed in that phase is
generally of no use for a compiler, as it does not carry
any information of how to evaluate the tree.
The productions of context-free grammar, which makes
the rules of the language, do not accommodate how to
interpret them.
4
Cont’d
For example
E→E+T
The above CFG production has no semantic rule associated
with it, and it cannot help in making any sense of the
production.
5
Semantics
Semantics of a language provide meaning to its
constructs, like tokens and syntax structure.
Semantics help interpret symbols, their types, and their
relations with each other.
Semantic analysis judges whether the syntax structure
constructed in the source program derives any meaning
or not.
6
Cont’d
For example
int a = “value”;
Should not issue an error in lexical and syntax analysis phase,
as it is lexically and structurally correct.
But it should generate a semantic error as the type of the
assignment differs.
These rules are set by the grammar of the language and
evaluated in semantic analysis.
The following tasks should be performed in semantic
analysis:
Scope resolution
Type checking
Array-bound checking
7
Semantic Errors
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.
8
Attribute Grammar
Attribute grammar is a special form of
context-free grammar where some additional
information (attributes) are appended to one
or more of its non-terminals in order to
provide context-sensitive information.
9
Cont’d
Attribute grammar is a medium to provide
semantics to the context-free grammar and it
can help specify the syntax and semantics of
a programming language.
10
Cont’d
For example
Ec→ E + T { E.value = E.value + T.value }
The right part of the CFG contains the semantic
rules that specify how the grammar should be
interpreted.
Here, the values of non-terminals E and T are
added together and the result is copied to the
non-terminal E.
Semantic attributes may be assigned to their
values from their domain at the time of parsing
and evaluated at the time of assignment or
conditions.
11
Syntax Directed Definitions example
12
Cont’d
13
I. Synthesized attributes
These attributes get values from the attribute
values of their child 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 ABC are synthesized to S.
As in our previous example (E → E + T), the parent
node E gets its value from its child node.
NOTE
Synthesized attributes never take values from
their parent nodes or any sibling nodes
14
II. Inherited attributes
In contrast to synthesized attributes, inherited
attributes can take values from parent and/or
siblings.
As in the following production.
S → ABC
15
Annotated Parse Tree
A parse tree showing the values of attributes at
each node is called an annotated parse tree.
17
EVALUATION ORDERS FOR SDD'S
Semantic rules set up dependencies
between attributes which can be
represented by a dependency graph.
"Dependency graphs" are a useful tool for
values of attributes
A dependency graph helps us
determine how those values can be
computed.
18
DEPENDENCE GRAPH
Ifan attribute b depends on an attribute c,
then the semantic rule for b must be
evaluated after the semantic rule for c.
19
EXAMPLE
20
EXAMPLE
21
Cont’d
Semantic analysis uses Syntax Directed
Translations to perform the above tasks.
Semantic analyzer receives AST (Abstract Syntax
Tree) from its previous stage (syntax analysis).
Semantic analyzer attaches attribute information
with AST, which are called Attributed AST.
Attributes are two tuple value,
<attribute name, attribute value>
For example:
int value = 5;
<type, “integer”>
<presentvalue, “5”>
Note: For every production, we attach a semantic
rule.
22
S-attributed SDT
23
Cont’
24
L-attributed SDT
27
End of Chapter
4
28