Compiler Lecture 5
Compiler Lecture 5
Objectives:
Understand the Semantics of the language.
Understand the evaluation process of input by the compiler.
Outcomes:
Students should be able to understand the annotated parse tree .
Students will analyze how to construct the input from infix to postfix
by the compiler.
Syntax Directed Translation
Here, expr is the sum of the two subexpressions expr and term. (The subscript
in expr is used only to distinguish the instance of expr in the production body
from the head of the production). We can translate expr by exploiting its
structure, as in the following pseudo-code:
translate expr;
translate term;
handle +;
Syntax Directed Translation
Important Note: Such formalism generates Annotated Parse Tree where each
node of the tree is a record with a field for each attribute( e.g expr.t indicates
the attribute t of the grammar symbol expr)
Syntax Directed Definitions
Here Semantic Rule is applied for one production of a context free grammar.
Example:
expr.t =95-2+
expr.t =9 term.t =5
term.t =9
9 - 5 + 2
It starts at the root and recursively visits the children of each node in left-
to-right order.
The semantic rules at a given node are evaluated once all descendants of
that node have been visited.
A parse tree showing all the attribute values at each node is called
annotated parse tree.
Syntax Directed Definitions
Attribute Grammars: Each grammar symbol has an associated set of
attributes. An attribute can represent anything we choose:
I. Synthesized Attributes.
II. Inherited Attributes.
Syntax Directed Definitions
Synthesized Attributes: An attribute is synthesized if the attribute value of
parent is determined from attribute values of children in the parse tree.
Example:
expr.t =95-2+
expr.t =9 term.t =5
term.t =9
9 - 5 + 2
Example:
Syntax Directed Translation Schemes
- {print(‘-’)} 2 {print(‘2’)}
expr term
5 {print(‘5’)}
term
9 {print(‘9’)}
Class Exercises