0% found this document useful (0 votes)
30 views30 pages

Compiler Design (KCS-502) Unit-3 Syntax Directed Translation

The document discusses Syntax Directed Translation (SDT) in compiler design, focusing on the relationship between syntactic structure and semantic analysis. It explains Syntax Directed Definitions (SDDs), their implementation through dependency graphs, and the distinction between synthesized and inherited attributes. The document also covers evaluation methods for semantic rules and introduces S-attributed and L-attributed definitions, highlighting their applications in programming languages.

Uploaded by

sneha vasishth
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)
30 views30 pages

Compiler Design (KCS-502) Unit-3 Syntax Directed Translation

The document discusses Syntax Directed Translation (SDT) in compiler design, focusing on the relationship between syntactic structure and semantic analysis. It explains Syntax Directed Definitions (SDDs), their implementation through dependency graphs, and the distinction between synthesized and inherited attributes. The document also covers evaluation methods for semantic rules and introduces S-attributed and L-attributed definitions, highlighting their applications in programming languages.

Uploaded by

sneha vasishth
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/ 30

Compiler Design

(KCS-502)
Unit-3
Syntax Directed Translation
Contents
• Syntax Directed Translations
• Syntax Directed Definitions
• Implementing Syntax Directed Definitions
– Dependency Graphs
– S-Attributed Definitions
– L-Attributed Definitions
• Translation Schemes
Semantic Analysis
• Semantic Analysis computes additional information related to the meaning of the program once
the syntactic structure is known.

• In typed languages as C, semantic analysis involves adding information to the symbol table and
performing type checking.

• The information to be computed is beyond the capabilities of standard parsing techniques,


therefore it is not regarded as syntax.

• As for Lexical and Syntax analysis, also for Semantic Analysis we need both a Representation
Formalism and an Implementation Mechanism.

• As representation formalism this lecture illustrates what are called Syntax Directed Translations.
Syntax Directed Translation: Intro

• The Principle of Syntax Directed Translation states that the meaning of an input
sentence is related to its syntactic structure, i.e., to its Parse-Tree.

• By Syntax Directed Translations we indicate those formalisms for specifying


translations for programming language constructs guided by context-free grammars.

– We associate Attributes to the grammar symbols representing the language


constructs.

– Values for attributes are computed by Semantic Rules associated with grammar
productions.
Cont.
• Evaluation of Semantic Rules may:

– Generate Code;

– Insert information into the Symbol Table;

– Perform Semantic Check;

– Issue error messages;

– etc.

• There are two notations for attaching semantic rules:

1. Syntax Directed Definitions. High-level specification hiding many implementation details (also called
Attribute Grammars). 2. Translation Schemes. More implementation oriented: Indicate the order in which
semantic rules are to be evaluated.
Syntax Directed Definitions
• Syntax Directed Definitions are a generalization of context-free grammars in which:
1. Grammar symbols have an associated set of Attributes;
2. Productions are associated with Semantic Rules for computing the values of attributes.
• Such formalism generates Annotated Parse-Trees where each node of the tree is a record with a field for
each attribute (e.g., X.a indicates the attribute a of the grammar symbol X).
• The value of an attribute of a grammar symbol at a given parse-tree node is defined by a semantic rule
associated with the production used at that node.
•We distinguish between two kinds of attributes:
1. Synthesized Attributes. They are computed from the values of the attributes of the children nodes.
2. Inherited Attributes. They are computed from the values of the attributes of both the siblings and the parent
nodes
Form of Syntax Directed Definitions

Note. Terminal symbols are assumed to have synthesized attributes supplied by the lexical analyzer.
• Procedure calls (e.g. print in the next slide) define values of Dummy synthesized attributes of the non terminal on the left-
hand side of the production.
Syntax Directed Definitions: An Example
• Example. Let us consider the Grammar for arithmetic expressions. The Syntax Directed
Definition associates to each non terminal a synthesized attribute called val.
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

• Semantic rules in a S-attributed definition can be evaluated by a bottom-up or postorder


traversal of the parse tree

• An S-attributed SDD can be implemented naturally in conjunction with an LR parser


Contd…
• Example. The above arithmetic grammar is an example of an S-Attribute d Definition. The
annotated parse-tree for the input 3*5+4n is:
Annotated Parse Tree
• A parse tree showing the value(s) of its attribute(s) is called an annotated parse tree .

• Consider a grammar for signed binary numbers

• Associate attributes with grammar symbols

• Build attribute grammar that annotates 𝑛𝑢𝑚𝑏𝑒𝑟 with the value it represents
Contd…
• Example Attribute Grammar
Contd…
• Parse tree Annotated parse tree
Inherited Attributes
• Inherited Attributes are useful for expressing the dependence
of a construct on the context in which it appears.
• It is always possible to rewrite a syntax directed definition to use
only synthesized attributes, but it is often more natural to use both
synthesized and inherited attributes.
• Evaluation Order. Inherited attributes cannot be evaluated by
a simple PreOrder traversal of the parse-tree:
– Unlike synthesized attributes, the order in which the inherited
attributes of the children are computed is important!!! Indeed:
∗ Inherited attributes of the children can depend from both left and right
siblings!
Inherited Attributes: An Example
• Example. Let us consider the syntax directed definition with
both inherited and synthesized attributes for the grammar for
“type declarations”:
Contd…
• Synthesized attributes can be evaluated by a PostOrder
traversal.
• Inherited attributes that do not depend from right children
can be evaluated by a classical PreOrder traversal.
• The annotated parse-tree for the input real id1, id2, id3 is:
Contd…

• L.in is then inherited top-down the tree by the other L-nodes.


• At each L-node the procedure addtype inserts into the symbol
table the type of the identifier.
Dependency Graphs
• Implementing a Syntax Directed Definition consists primarily in
finding an order for the evaluation of attributes
– Each attribute value must be available when a computation is performed
• Dependency Graphs are the most general technique used to
evaluate syntax directed definitions with both synthesized and
inherited attributes.
• A Dependency Graph shows the interdependencies among the
attributes of the various nodes of a parse-tree.
– There is a node for each attribute;
– If attribute b depends on an attribute c there is a link from the node for c
to the node for b ( b ← c).
• Dependency Rule: If an attribute b depends from an attribute c,
then we need to fire the semantic rule for c first and then the
semantic rule for b.
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
Construction of Dependency graph
Evaluation Order
• The evaluation order of semantic rules depends from a
Topological Sort derived from the dependency graph.
• Topological Sort: Any ordering m1, m2, . . . , mk such that if mi
→ mj is a link in the dependency graph then mi < mj .
• Any topological sort of a dependency graph gives a valid
order to evaluate the semantic rules.
Dependency Graphs: An
Example
• Example. Build the dependency graph for the parse-tree of
following grammar:
Contd..

Parse Tree and Annotated Parse Tree and Dependency graph for 3∗5
Dependency Graphs: An
Example
• Example. Build the dependency graph for the parse-tree of
float id1, id2, id3.
Dependency Graphs: An
Example
• Example. Build the dependency graph for the parse-tree of
real id1, id2, id3.
Implementing Attribute
Evaluation: General Remarks
• Attributes can be evaluated by building a dependency graph
at compile-time and then finding a topological sort.
• Disavantages
1. This method fails if the dependency graph has a cycle: We
need a test for non-circularity;
2. This method is time consuming due to the construction of
the dependency graph.
• Alternative Approach. Design the syntax directed definition in
such a way that attributes can be evaluated with a fixed order
avoiding to build the dependency graph (method followed by
many compilers)( Rule-based methods (compiler-construction
time)).
L-Attributed Definitions
Are these SDDs S- or L-attributed?
S-Attributed and L-Attributed
Definitions
The End

30

You might also like