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

Chapter 4 Syntax Directed Translation

Uploaded by

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

Chapter 4 Syntax Directed Translation

Uploaded by

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

7/12/2021

Parse Tree Vs. (Abstract)Syntax Tree


A parse tree corresponding to a derivation is a labeled tree in which
Chapter 4: Syntax Directed Translation the interior nodes are labeled by none terminals, the leaf nodes are
labeled by terminals, and the children of each internal node
Grammar Rules + Semantics Rules = SDT
represents the replacement of the associated non terminals in one step
Syntax-Directed Definitions of the derivations. To give a simple example, the derivation :

Construction of Syntax Trees Exp  exp op exp

number op exp

number + exp

number + number, corresponding parse tree is:

Abstract Syntax Tree(Syntax Tree): A parse tree is a useful We have augmented the parse tree to show the actual numeric values
representation of the structure of a given input(string) of tokens in of each of the number tokens (this is an attribute of the token that is
that the tokens appear as the leaves of the parse tree(from left to computed either by the scanner or the parser).
right) and the internal nodes of the parse tree represent the steps in a
The principal of Syntax-Directed Translation states that the meaning
derivation (in some order). However a parse tree contains much more
or semantics of the string 3+4 should be directly related to its
information than is absolutely necessary for a compiler to produce
syntactical structure as represented by the parse tree above.
executable code.
In this case the principle of syntax Directed translation means that
To see this, consider the parse tree for
the parse tree should imply that the values 3 and 4
the expression 3+4 according to our
are to be added. (34-3)*42.
simple expression grammar.

1
7/12/2021

Syntax-Directed Definitions Example: E  E+T | T


•Background: Parser uses a CFG(Context-free-Grammar) to validate the input T  T*F | F
string and produce output for next phase of the compiler. F  int
•Output could be either a parse tree or abstract syntax tree(AST). Now to interleave This is a grammar to syntactically validate an expression having additions and
semantic analysis with syntax analysis phase of the compiler, we use Syntax multiplications in it.

Directed Translation. Now, to carry out semantic analysis we will augment SDT rules to this grammar, in
order to pass some information up the parse tree and check for semantic errors, if any.
Definition: Syntax Directed Translation is augmented/additional/ rule to the
In this example we will focus on evaluation of the given expression, as we don’t have
grammar that facilitate semantic analysis.
any semantic assertions to check in this very basic example.
SDT involves passing information bottom-up and/or top-down the parse tree in
E -> E+T { E.val = E.val + T.val } PR#1
form of attributes attached to the nodes.
E -> T { E.val = T.val } PR#2
•Syntax directed translation rules use:
T -> T*F { T.val = T.val * F.val } PR#3
1) lexical values of nodes, T -> F { T.val = F.val } PR#4
2) constants & F -> int { F.val = int.lexval } PR#5 0-9
3) attributes associated to the non-terminals in their definitions.

•For understanding translation rules further, we take the first SDT •Let’s take a string to see how semantic analysis happens
augmented to [ E  E+T ] production rule. The translation rule in
S = 2+3*4. Parse tree corresponding to S would be:
consideration has val as attribute for both the non-terminals – E & T.
Right hand side of the translation rule corresponds to attribute values
of right side nodes of the production rule and vice-versa.
Generalizing, SDTs are augmented rules to a CFG that associate:

1) set of attributes to every node of the grammar and

2) set of translation rules to every production rule using attributes,


constants and lexical values.

2
7/12/2021

•To evaluate translation rules, we can employ one depth first search
traversal on the parse tree.

•This is possible only because SDT rules don’t impose any specific
order on evaluation until children attributes are computed before
parents for a grammar having all synthesized attributes.

•Otherwise, we would have to figure out the best suited plan to


traverse through the parse tree and evaluate all the attributes in one or
more traversals.

•For better understanding, we will move bottom up in left to right


fashion for computing translation rules of our example.

•Above diagram shows how semantic analysis could happen. •Synthesized Attributes: are such attributes that depend only on the
•The flow of information happens bottom-up and all the children attribute values of children nodes.
attributes are computed before parents, as discussed above. •Thus [ E  E+T { E.val = E.val + T.val } ] has a synthesized
•Right hand side nodes are sometimes annotated with subscript 1 to attribute val corresponding to node E.

distinguish between children and parent. Additional Information • If all the semantic attributes in an augmented grammar are
synthesized, one depth first search traversal in any order is sufficient
for semantic analysis phase.
•Inherited Attributes: are such attributes that depend on parent
and/or siblings attributes. Thus [ Ep -> E+T { Ep.val = E.val + T.val,
T.val = Ep.val } ], where E & Ep are same production symbols
annotated to differentiate between parent and child, has an inherited
attribute val corresponding to node T.

You might also like