0% found this document useful (0 votes)
14 views28 pages

Compi Desi CHP 04

Chapter 4 discusses Syntax Directed Translation and Semantic Analysis in compiler design, emphasizing the importance of semantics in interpreting the constructs of a programming language. It covers topics such as Syntax Directed Definitions, attribute grammars, and the distinction between synthesized and inherited attributes. The chapter also explains how semantic errors are identified and the role of annotated parse trees and dependency graphs in semantic analysis.

Uploaded by

ephremd406
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)
14 views28 pages

Compi Desi CHP 04

Chapter 4 discusses Syntax Directed Translation and Semantic Analysis in compiler design, emphasizing the importance of semantics in interpreting the constructs of a programming language. It covers topics such as Syntax Directed Definitions, attribute grammars, and the distinction between synthesized and inherited attributes. The chapter also explains how semantic errors are identified and the role of annotated parse trees and dependency graphs in semantic analysis.

Uploaded by

ephremd406
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/ 28

Course Name:

Compiler Design

1
CHAPTER 4

Syntax Directed Translation/Semantic


Analysis

2
Chapter Topics

 Syntax Directed Definitions (SDD)


 Evaluation Order for SDD
 Construction of Syntax Trees

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.

CFG + semantic rules = Syntax Directed Definitions

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.

Each attribute has well-defined domain of


values, such as integer, float, character,
string, and expressions.

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.

Attribute grammar (when viewed as a parse-


tree) can pass values or information among
the nodes of a tree.

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

Based on the way the attributes get their


values, they can be broadly divided into two
categories :

I. Synthesized attributes and

II. Inherited attributes.

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

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

15
Annotated Parse Tree
A parse tree showing the values of attributes at
each node is called an annotated parse tree.

 The process of computing the attributes values


at the nodes is called annotating (or decorating)
of the parse tree.

 Of course, the order of these computations


depends on the dependency graph induced by the
semantic rules.
16
PARSE TREE V SYNTAX 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

determining an evaluation order for the


attribute instances in a given parse tree.
 While an annotated parse tree shows the

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.

 The dependencies among the nodes can


be depicted by a directed graph

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

If an SDT uses only synthesized attributes, it


is called as S-attributed SDT.
These attributes are evaluated using S-
attributed SDTs that have their semantic
actions written after the production (right
hand side).

23
Cont’

As depicted below, attributes in S-attributed


SDTs are evaluated in bottom-up parsing,
As the values of the parent nodes depend
upon the values of the child nodes.

24
L-attributed SDT

This form of SDT uses both synthesized and


inherited attributes with restriction of not
taking values from right siblings.
In L-attributed SDTs, a non-terminal can get
values from its parent, child, and sibling
nodes.
 As in the following production:
 S → ABC
 S can take values from A, B, and C (synthesized).
 A can take values from S only.
 B can take values from S and A.
 C can get values from S, A, and B.
 No non-terminal can get values from the sibling to its right.
25
Cont’d
Attributes in L-attributed SDTs are evaluated
by depth-first and left-to-right parsing
manner.

We may conclude that if a definition is S-


attributed, then it is also L-attributed as L-
attributed definition encloses S-attributed
definitions. 26
ANNOTATED PARSE TREE -- EXAMPLE n

27
End of Chapter
4
28

You might also like