Chapter 4 Syntax Directed Translation (SDT)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Semantic Analysis

• Semantic Analysis is the third phase of Compiler.


• For tokens and syntax structure, meaning is provided by a language known as Semantics.
Compiler Design • The symbols, their type and their relation is interpreted by semantics.
• Type checking is an important part of semantic analysis where compiler makes sure that
each operator has matching operands.
Chapter 4: Syntax Directed Translation(SDT)
• Semantic analysis facilitates in judging whether the syntax structure in the source
program derives any meaning.

Instructor: Fikru T. (MSc.) Context Fee Grammar (CFG) + Semantic rules = Syntax Directed Definitions

Email: [email protected] • For instance: int a = "value";


• The above statement will not issue any error in the lexical and syntax analysis phase, but a
semantic error should be generated as assignment types differ.
1 2

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

Figure: Role of Semantic Analyzer


 Accessing an out of scope variable
 Actual and formal parameter mismatch
3 4

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

Cont'd ... Cont'd ...


E → E +T { E . value = E . value + T . value }
• The right part of the CFG contains the semantic rules that specify how the grammar
should be interpreted.
Attribute Grammar
• Here, the values of non-terminals E and T are added together and the result is copied
• is a special form of context-free grammar where some additional information (attributes) are
to the non-terminal E.
appended to one or more of its non-terminals in order to provide context-sensitive information.
• Semantic attributes may be assigned to their values from their domain at the time of
• Each attribute has well-defined domain of values, such as integer, float, character, string, and
parsing and evaluated at the time of assignment or conditions.
expressions.
• Based on the way the attributes get their values, they can be broadly divided into two
• Attribute grammar is a medium to provide semantics to the context-free grammar and it can
help specify the syntax and semantics of a programming language. categories:

• 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

Cont'd ... Cont'd ...


• Semantic analysis uses Syntax Directed Translations to perform the above tasks. • We also suppose that the terminal digit has a synthesized attribute lexval, which is an
• Semantic analyzer receives AST (Abstract Syntax Tree) from its previous stage integer value returned by the lexical analyzer.
(syntax analysis). • An SDD that involves only synthesized attributes is called S-attributed.
• Semantic analyzer attaches attribute information with AST, which are called Attributed • In an S-attributed SDD, each rule computes an attribute for the nonterminal at the
AST. head of a production from attributes taken from the body of the production.
• Attributes are two tuple value, <attribute name, attribute value> • Attribute Grammar: An SDD without side effects is sometimes called an
• For example: attribute grammar.
int value = 5; • The rules in an attribute grammar define the value of an attribute purely in terms of
<type, "integer"> the values of other attributes and constants.
<presentvalue, "5">
• For every production, we attach a semantic rule.
11 12

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

• We may conclude that if a definition is S-attributed, then it is also L-attributed as L- T →T*F | F


attributed definition encloses S-attributed definitions. F→ INTLIT
15 16

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)

mknode(op, left, right). E →T {E.nptr = T.nptr; } mkunode(op, child)

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)

mkleaf(id, id.entry) T→ F {T.nptr = F.nptr; } mkleaf(num, val)

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

id id c • In creating a Syntax tree.


F.nptr=100
b
• SDT is used to generate intermediate code.
id • In storing information into symbol table.
a
• SDT is commonly used for type checking.
Fig. Annotated Parse Tree
Fig. Data Structure of the above syntax Tree 23 24 End

Page 6

You might also like