Compiler Design (KCS-502) Unit-3 Syntax Directed Translation
Compiler Design (KCS-502) Unit-3 Syntax Directed Translation
(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.
• 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.
– Values for attributes are computed by Semantic Rules associated with grammar
productions.
Cont.
• Evaluation of Semantic Rules may:
– Generate Code;
– etc.
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
• 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…
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