Professional Documents
Culture Documents
3
Over View..
Analysis is organized around the "syntax" of the language to be
compiled.
4
Over View...
Context Free Grammar is used to specify the syntax of the
language.
It has components
5
Over View...
Given a CF grammar determining the set of all strings generated by
the grammar is known as derivation.
6
Over View...
Derivation for 9 – 5 + 2
7
Over View...
Parsing is the problem of taking a string of terminals and figuring
out how to derive it from the start symbol of the grammar.
8
Over View...
Parse tree of the string 9-5+2 using grammar G
list
list digit
list digit
digit
The sequence of
9 - 5 + 2 leafs is called the
yield of the parse tree
9
Over View…
Ambiguity: Two Parse Trees for 9 – 5 + 2
string string
9 - 5 + 2 9 - 5 + 2
10
11
Contents
Attributes
Translation Schemes
Postfix Notation
Synthesized Attributes
Tree Traversals
Translation Schemes
Preorder and Postorder Traversals
12
Syntax-Directed Translation
Syntax-directed translation is done by attaching rules or program
fragments to productions in a grammar.
13
ROUGH
Syntax Directed Translation are augmented rules to the grammar
that facilitate semantic analysis. SDT involves passing information
bottom-up and/or top-down the parse tree in form of attributes
attached to the nodes. Syntax directed translation rules use
1) lexical values of nodes,
2) constants &
3) attributes associated to the non-terminals in their definitions.
14
Syntax-Directed Translation..
Syntax directed translation introduces two new concepts
1-Attributes and 2-Translation schemes.
15
ROUGH
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.
17
Syntax-Directed Translation...
A translation scheme is a notation for attaching program fragments
to the productions of a grammar.
18
ROUGH
The Syntax directed translation scheme is a context -free
grammar.
The syntax directed translation scheme is used to evaluate the
order of semantic rules.
In translation scheme, the semantic rules are embedded within
the right side of the productions.
The position at which an action is to be executed is shown by
enclosed between braces. It is written within the right side of the
production.
19
Syntax direct translation is implemented by constructing a parse
tree and performing the actions in a left to right depth first
order.
20
Postfix Notation
The postfix notation for an expression E can be defined inductively
as follows:
21
Postfix Notation..
Ex (9 - 5) + 2
22
Postfix Notation...
Another Ex 9 – (5 + 2)
23
ROUGH
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. Based on the way the attributes get
their values, they can be broadly divided into two categories :
synthesized attributes and inherited attributes.
24
Synthesized Attributes
A syntax-directed definition associates
25
ROUGH
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.
27
Synthesized Attributes..
Suppose a node N in a parse tree is labeled by the grammar
symbol X then X.a is used to denote the value of attribute a of X
at that node.
28
Synthesized Attributes...
An attribute is said to be synthesized if its value at a parse-tree
node N is determined from attribute values at the children of N
and at N itself.
29
ROUGH
AN ANNOTATED PARSE TREE is a parse tree showing the values of
the attributes at each node. The process of computing the
attribute values at the nodes is called annotating or decorating
the parse tree
30
ROUGH
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 example (E → E + T), the parent node E gets its
value from its child node. Synthesized attributes never
take values from their parent nodes or any sibling
nodes.
31
Synthesized Attributes...
Each non terminal has a string-valued attribute t that represents
the postfix notation for the expression generated by that non
terminal in a parse tree.
String concatenation operator ||
A traversal of a tree starts at the root and visits each node of the tree in
some order.
33
Tree Traversals
A depth-first traversal of a tree
34
Translation Schemes
A syntax-directed translation scheme is a notation for specifying
a translation by attaching program fragments to productions in
a grammar.
Action to be executed
A common method of syntax-directed translation is translating a string
into a sequence of actions by attaching one such action to each rule
of a grammar. Thus, parsing a string of the grammar produces a
sequence of rule applications. SDT provides a simple way to
attach semantics to any such syntax.
35
Translation Schemes..
36
Translation Schemes...
37
Translation Schemes...
38
Translation Schemes...
39
Preorder and Postorder Traversals
If the action is done just before we leave a node for the last time,
then we say it is a postorder traversal of the tree.
40
Thank You