Chapter 4 Syntax Directed Translation (SDT)
Chapter 4 Syntax Directed Translation (SDT)
Chapter 4 Syntax Directed Translation (SDT)
Instructor: Fikru T. (MSc.) Context Fee Grammar (CFG) + Semantic rules = Syntax Directed Definitions
Cont'd ...
Role of Semantic Analyzer
• Semantic analysis performs the following tasks:
• It uses syntax tree and symbol table to check whether the given program is
1. Scope resolution
semantically consistent with language definition.
2. Type checking
• It gathers type information and stores it in either syntax tree or symbol table.
3. Array-bound checking
• This type information is subsequently used by compiler during intermediate-code
• Semantic analyzer has to recognize some of the semantic errors such as:
generation
Type mismatch
• The semantic analyzer produces an annotated syntax tree as an output.
Undeclared variable
Reserved identifier misuse
input Lexical token parse Semantic annotated Rest of target
Parser
string Analyzer tree Analyzer parse tree Compiler Code Multiple declaration of variable in a scope
Page 1
Syntax Directed Translation (SDT) Cont'd ...
• So we can say that:
• Syntax-directed translation (SDT) refers to a method of compiler implementation
Grammar + semantic rule = SDT (syntax directed translation)
where the source language translation is completely driven by the parser,
• In syntax directed translation, every non-terminal can get one or more than one
i.e., based on the syntax of the language.
attribute or sometimes 0 attribute depending on the type of the attribute.
• In syntax directed translation, along with the grammar we associate some informal
• The value of these attributes is evaluated by the semantic rules associated with the
notations and these notations are called as semantic rules.
production rule.
• Almost all modern compilers are syntax-directed.
• In the semantic rule, attribute is VAL and an attribute may hold anything like a string, a
• SDT can be a separate phase of a compiler or we can augment our conventional
number, a memory location and a complex record.
grammar with information to control the semantic analysis and translation.
• In Syntax directed translation, whenever a construct encounters in the programming
• Such grammars are called attribute grammars.
language then it is translated according to the semantic rules define in that particular
programming language.
5 6
Figure: Syntax Directed Translation
• Attribute grammar (when viewed as a parse-tree) can pass values or information among the 1. Synthesized attributes
nodes of a tree. 2. Inherited attributes
7 8
Page 2
Synthesized Attributes Inherited Attributes
• In contrast to synthesized attributes, inherited attributes can take values from parent
• These attributes get values from the attribute values of their child nodes.
and/or siblings.
• To illustrate, assume the following production:
• As in the following production, S → ABC
S → ABC
A can get values from S, B and C.
• If S is taking values from its child nodes (A,B,C), then it is said to be a synthesized B can take values from S,A, and C.
attribute, as the values of ABC are synthesized to S. C can take values from S,A, and B.
• As in our previous example (E → E + T), the parent node E gets its value from its • Expansion : When a non-terminal is expanded to terminals as per a grammatical rule.
child node. • Reduction : When a terminal is reduced to its corresponding non-terminal according to
grammar rules.
• Synthesized attributes never take values from their parent nodes or any sibling
• Syntax trees are parsed top-down and left to right.
nodes.
9
• Whenever reduction occurs, we apply its corresponding semantic rules (actions). 10
Page 3
S-attributed SDT L-attributed SDT
• If an SDT uses only synthesized attributes, it is called as S-attributed SDT. • This form of SDT uses both synthesized and inherited attributes with restriction
• These attributes are evaluated using S-attributed SDTs that have their semantic of not taking values from right siblings.
actions written after the production (right hand side). • In L-attributed SDTs, a non-terminal can get values from its parent, child, and
sibling nodes.
• As in the following production S → ABC
S can take values from A, B, and C (synthesized).
A can take values from S only.
• As depicted above, attributes in S-attributed SDTs are evaluated in bottom-up B can take values from S and A.
parsing, as the values of the parent nodes depend upon the values of the child C can get values from S,A, and B.
nodes. 13 No non-terminal can get values from the sibling to its right. 14
Cont'd ...
Evaluation Order of SDT
• Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing
• SDT are augmented rules to the grammar that facilitate semantic analysis.
manner.
• 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.
• Example: E → E+T | T
Page 4
Cont'd ... Cont'd ...
• This is a grammar to syntactically validate an expression having additions and multiplications in • Let’s take a string to see how semantic analysis happens: S = 2+3*4. •
it.
• Parse tree corresponding to S would be:
• 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.
• To evaluate translation rules, we can employ one depth first search
• In this example we will focus on evaluation of the given expression, as we don’t have any
traversal on the parse tree.
semantic assertions to check in this very basic example.
• This is possible only because SDT rules don’t impose any specific order on evaluation
E.val → E.val + T.val | T.val
until children attributes are computed before parents for a grammar having all
T .val → T.val * F.val | F.val
F.val → INTLIT.lexval synthesized attributes.
• Generalizing, SDT are augmented rules to a CFG that associate • Otherwise, we would have to figure out the best suited plan to traverse through the
1) set of attributes to every node of the grammar and parse tree and evaluate all the attributes in one or more traversals.
2) set of translation rules to every production rule using attributes, constants and lexical • For better understanding, we will move bottom up in left to right fashion for computing
values. 17
translation rules of our example in the next slide. 18
Cont'd ...
E.val=E.val+T.val =2+12=14 Construction of Syntax Trees
E
• Syntax Directed Definitions are useful for constructing of syntax trees.
E.val=T.val=2 + T.val=T.val*F.val=12
E T • A syntax tree is a condensed form of parse tree.
• Syntax tree are useful for representing expressions and statements.
T.val=F.val=3 T * F F.val=4
T.val=F.val=2 T
E
+
F.val=3 F 4
F
F.val=2 E + T
* 4
2 3
T * digit digit
• Above diagram shows how semantic analysis could happen. 2 3
• The flow of information happens bottom-up and all the children attributes are computed digit 3 4
Syntax Tree
before parents, as discussed above.
2 Parse Tree
• Right hand side nodes are sometimes annotated with subscript 1 to distinguish between
2*3 +4
children and parent. 19 20
Page 5
Cont'd ... Cont'd ...
• In constructing the Syntax Tree, we follow the convention:
• Each node of the tree can be represented as a record consisting of at least two • SDT to build Syntax Tree
fields to store operators and operands.
1. Operators: one field for operator, remaining fields ptrs to operands E → E +T {E.nptr = mknode('+' , E .nptr,T.nptr); } mknode(op, left, right)
2. Identifier: one filed with label id and another ptr to symbol table T → T * F {E.nptr = mknode(‘*' ,T .nptr, F.nptr); } mkleaf(id, entry)
3. Number: one field with label num and another to keep the value of the F → id {F.nptr = mknode(id , id .place); }
number
mkleaf(num, val)
21 22
Cont'd ...
Application of Syntax Directed Translation
• Example: a + b * c +
• SDT is used for Executing Arithmetic Expression.
E. nptr= 500 a *
• In the conversion from infix to postfix expression.
+ b c • In the conversion from infix to prefix expression.
E.nptr=100 T.nptr=400
Fig. Syntax Tree
• It is also used for Binary to decimal conversion.
* F.nptr=300
F.nptr=200 • In counting number of Reduction.
T.nptr=100
Page 6