Chapter 4 Syntax Directed Translation
Chapter 4 Syntax Directed Translation
number op exp
number + exp
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
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:
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.
•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.