0% found this document useful (0 votes)
21 views

Syntax Directed Translation

COMPILER DESIGN STUDY MATERIAL - Syntax directed translation

Uploaded by

deeplyfwind7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Syntax Directed Translation

COMPILER DESIGN STUDY MATERIAL - Syntax directed translation

Uploaded by

deeplyfwind7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Syntax directed translation

Outline

• Syntax Directed Translations

• Syntax Directed Definitions

• Implementing Syntax Directed Definitions


– S-Attributed Definitions
– L-Attributed Definitions

• Translation Schemes
Phases of Compiler
Semantic Analysis

• This phase 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.

• Representation formalism illustrates Syntax Directed Translations.


Introduction to Syntax Directed Translation

• 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.
Introduction to Syntax Directed Translation

• Evaluation of Semantic Rules may:


– Generate Code
– Insert information into the Symbol Table
– Perform Semantic Check
– Issue error messages

• 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 Scheme- 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 ).

• Uses CFG to specify syntactic structures of the input. It associates with


each grammar symbol a set of semantic rules for computation of values of
attributes.
Syntax Directed Definitions

• 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.- It can be evaluated using a
single bottom up traversal of the tree.
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

• Each production, A → α , is associated with a set of semantic rules:


b := f ( c1, c2, . . . , ck ) , where f is a function and either
1. b is a synthesized attribute of A , and c1, c2, . . . , ck are attributes of
the grammar symbols of the production, or
2. b is an inherited attribute of a grammar symbol in α , and c1, c2, . . . ,
ck
are attributes of grammar symbols in α or attributes of A .
• 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.

• digit has a synthesized attribute lexval assumed to be supplied by lexical analyzer


P RODUCTION S EMANTIC RULES
L → En print (E.val)
E → E1 + T E.val := E1 .val + T.val
E→T E.val := T.val
T → T1 ∗ F T.val := T1 .val ∗ F.val
T →F T.val := F.val
F → (E) F.val := E.val
F → digit F.val :=digit.lexval
S-Attributed Definitions
Definition. An S-Attributed Definition is a Syntax Directed Definition that
uses only synthesized attributes.
• Evaluation Order. Semantic rules in a S-Attributed Definition can
be evaluated by a bottom-up or Postorder traversal of the parse-tree.
• Example. The above arithmetic grammar is an example of an S-
Attributed Definition. The annotated parse-tree for the input 3*5+4n is:
L

E .val = 1 9 n

E .val = 1 5 + T.val = 4

T.val = 1 5 F.val = 4

T.val = 3 * F.val = 5 digit.lexval= 4

F.val = 3 digit.lexval= 5

digit.lexval= 3
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.
· 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”:
P RODUCTION S EMANTIC RULE
D → TL L.in := T.type
T →int T.type:=integer
T →real T.type :=real
L → L1 , id
L1 .in := L.in; addtype(id.entry, L.in)
L → id
addtype(id.entry, L.in)

• The non terminal T has a synthesized attribute, type, determined by the keyword in the
declaration.

• The production D → T L is associated with the semantic rule L .i n := T.t y pe which set
the inherited attribute L .i n .
• Note: The production L → L 1 , id distinguishes the two occurrences of L .
Inherited Attributes: An Example
• 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 sentence- real id1, id2, id3 is:
D
T.t y pe = r eal L .i n = r e a l

real L .i n = r e a l , id3

L .i n = r e a l , id2

id1
Inherited Attributes: An Example

• The annotated parse-tree for the input real id1, id2, id3 is:

T.t y pe = r eal L .i n = r e a l

real L .i n = r e a l , id3

L .i n = r e a l , id2

id1
• L .i n 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.
Evaluation Order

• The evaluation order of semantic rules depends from a Topological Sort


derived from the dependency graph.
• Topological Sort: Any ordering m 1, m 2, . . . , m k such that if m i →
mj
is a link in the dependency graph then m i < m j .
• Any topological sort of a dependency graph gives a valid order to
evaluate the semantic rules.
Bottom up Evaluation of S-Attributed Definitions

• Synthesized Attributes can be evaluated by a bottom-up parser as the input is being


analyzed avoiding the construction of a dependency graph.

• The parser keeps the values of the synthesized attributes in its stack.
• Whenever a reduction A → α is made, the attribute for A is computed from the
attributes of α which appear on the stack.

• Thus, a translator for an S-Attributed Definition can be simply implemented by


extending the stack of an LR-Parser.
Extending a Parser Stack
• Extra fields are added to the stack to hold the values of synthesized attributes.
• In the simple case of just one attribute per grammar symbol the stack has two
fields: state and val
• The current top of the stack is indicated by the pointer top.
• Synthesized attributes are computed just before each reduction:
– Before the reduction A → X Y Z is made, the attribute for A is
computed: A .a := f ( val [t op], val [t op − 1 ], val [t op − 2 ]) .

State Val
… …
X X.x
Y Y.y
top Z Z.z
…. …..
Extending a Parser Stack: An Example
• Example. Consider the S-attributed definitions for the arithmetic
expressions. To evaluate attributes the parser executes the following code
P RODUCTION C ODE
L → En print(val[top − 1])
E → E1 + T val[ntop] := val[top] + val[top − 2]
E→T
T → T1 ∗ F val[ntop] := val[top] ∗ val[top − 2]
T →F
F → (E) val[ntop] := val[top − 1]
F → digit

• The variable n t op is set to the new top of the stack. After a reduction is
done top is set to ntop: When a reduction A → α is done with |α | =
r , then n t op = t op − r + 1 .
• During a shift action both the token and its value are pushed into the stack.
Extending a Parser Stack: An Example
• The following Figure shows the moves made by the parser on input
3*5+4n.
– Stack states are replaced by their corresponding grammar symbol;
– Instead of the token digit the actual value is shown.
L-Attributed Definitions

• L-Attributed Definitions contain both synthesized and inherited


attributes but do not need to build a dependency graph to evaluate them.

• Definition. A syntax directed definition is L-Attributed if each inherited


attribute of X j in a production A → X 1 . . . X j . . . X n , depends only
on:
1. The attributes of the symbols to the left (this is what L in L-Attributed
stands for) of X j , i.e., X 1X 2 . . . X j − 1, and
2. The inherited attributes of A .
• Theorem. Inherited attributes in L-Attributed Definitions can be
computed by a PreOrder traversal of the parse-tree.
Evaluating L-Attributed Definitions
• L-Attributed Definitions are a class of syntax directed definitions whose
attributes can always be evaluated by single traversal of the parse-tree.
• The following procedure evaluate L-Attributed Definitions by mixing
PostOrder (synthesized) and PreOrder (inherited) traversal.
Algorithm: L-Eval(n: Node)
Input: Node of an annotated parse-tree.
Output: Attribute evaluation.
Begin
For each child m of n , from left-to-right Do
Begin
Evaluate inherited attributes of m ; L-
Eval(m)
End;
Evaluate synthesized attributes of n
End.
Translation Schemes

• Translation Schemes are more implementation oriented than syntax


directed definitions since they indicate the order in which semantic
rules and attributes are to be evaluated.

• Definition. A Translation Scheme is a context-free grammar in which


1. Attributes are associated with grammar symbols;
2. Semantic Actions are enclosed between braces {} and are
inserted within the right-hand side of productions.

• Yacc uses Translation Schemes.


Translation Schemes

• Translation Schemes deal with both synthesized and inherited attributes.


• Semantic Actions are treated as terminal symbols: Annotated parse-trees
contain semantic actions as children of the node standing for the
correspond- ing production.

• Translation Schemes are useful to evaluate L-Attributed definitions at


parsing time (even if they are a general mechanism).
– An L-Attributed Syntax-Directed Definition can be turned into
a Translation Scheme.
Design of Translation Schemes

• When designing a Translation Scheme we must be sure that an attribute


value is available when a semantic action is executed.

• When the semantic action involves only synthesized attributes:The


action can be put at the end of the production.
– Example. The following Production and Semantic Rule:
T → T1 ∗F T.val := T1.val ∗F.val
yield the translation scheme:
T → T1 ∗F { T.val := T1.val ∗F.val }
Design of Translation Schemes

• Rules for Implementing L-Attributed SDD’s. If we have an L-Attibuted


Syntax-Directed Definition we must enforce the following restrictions:
1. An inherited attribute for a symbol in the right-hand side of a production
must be computed in an action before the symbol;
2. A synthesized attribute for the non terminal on the left-hand side can only be
computed when all the attributes it references have been computed: The
action is usually put at the end of the production.

You might also like