0% found this document useful (0 votes)
92 views15 pages

CD-Ch04 SDT

The document discusses syntax directed translations which associate semantic rules with context free grammar productions to provide meaning to language constructs during compilation. It covers syntax directed definitions and attributed grammars which define synthesized and inherited attributes. Synthesized attributes obtain values from child nodes while inherited attributes come from parent or sibling nodes. The document provides examples of syntax directed definitions for arithmetic expressions to calculate values and pass them up the parse tree.

Uploaded by

HASEN SEID
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views15 pages

CD-Ch04 SDT

The document discusses syntax directed translations which associate semantic rules with context free grammar productions to provide meaning to language constructs during compilation. It covers syntax directed definitions and attributed grammars which define synthesized and inherited attributes. Synthesized attributes obtain values from child nodes while inherited attributes come from parent or sibling nodes. The document provides examples of syntax directed definitions for arithmetic expressions to calculate values and pass them up the parse tree.

Uploaded by

HASEN SEID
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Compiler Design (CoSc3102)

Chapter Four
Syntax Directed Translation
Outline
4.1. Syntax directed definitions
4.2. Types of Attributes
4.3. Dependency graph and evaluation order
4.4. S-attributed definitions
4.5. L-attributed definitions

1
10/31/202 WCU-CS Compiled by TM.
Syntax Directed Translations
 In syntax analysis phase, we have learnt how a parser constructs parse trees.
 But the plain parse-tree constructed in that phase is generally not used for a
compiler as it is,
 Because, it does not carry any information about of how to evaluate the tree.
 Means that, the productions of CFG makes the rules of the language, but do
not accommodate how to interpret them.
 For example: E → E + T

 This CFG production has no any semantic rule associated with it, and it
cannot help in making any sense of the production.
 Semantic information can be represented by associating CFG used in SA
with semantic rules
 Then the result is a syntax-directed translation,

 i.e., SDT=Context free Grammar(production rule) + Semantic Rules


 Grammar symbols are associated with attributes to associate
information with the programming language constructs.

10/31/202 WCU-CS Compiled by TM. 2


Syntax directed Translations cont’d..
 What is Semantics?
 Semantics of a language provide meaning to its constructs, like tokens and
syntax structure.
 Semantics help to interpret symbols, their types and relations with each other

 Semantic analysis judges whether the syntax structure constructed in the


source program derives any meaning or not.
For example: int a = “value”;
 This is not an error issue in LA and SA phase, as it is lexically and
structurally correct,
 But it should generate a semantic error as the type of the assignment differs.
 Semantic Errors
 Some of the semantics errors that the semantic analyzer is expected to
recognize:
 Type mismatch
 Undeclared variable
 Reserved identifier misuse.
 Multiple declaration of variable in a scope.
 Accessing an out of scope variable.

10/31/202 WCU-CS Compiled by TM. 3


Syntax directed Translations cont’d..
 Attributes for expressions can be a:
 type of value: int, float, double, char, string,...
 type of construct: variable, constant, operations, ...
 Values of these attributes are evaluated by the semantic rules associated with
the production rules, which is set by grammar of the language
 Evaluation of these semantic rules:
 may generate intermediate codes
 may put information into the symbol table
 may perform type checking
 may issue error messages
 may perform some other activities
 The following tasks should be performed in semantic analysis:
 The source program will be checked for semantic errors
 Scope resolution
 Type checking: type information will be collected for the code generation
 Array-bound checking

10/31/202 WCU-CS Compiled by TM. 4


SDD and SDT
 When we associate semantic rules with productions, we use two
notations/approaches
1. syntax-directed definition (SDD)
 associates a semantic actions with each grammar production rule,
 the rule states how attributes are calculated, but not say when they
will be evaluated
 give high-level specifications for translations.
 hide many implementation details such as order of evaluation of
semantic actions.
2. Translation scheme/syntax-directed translation(SDT)
 uses semantic actions embedded anywhere in the bodies of
productions.
 actions may perform arbitrary computations, such as appending
output; they are carried out in left-to-right order during parsing
 indicate the order of evaluation of semantic actions associated with a
productions.
10/31/202 WCU-CS Compiled by TM. 5
Syntax directed definitions cont’d….
 In SDT, every non-terminals can get zero or more attributes depend on the
type of attribute.
 Example: newval := oldval + 12 here, the type of the identifier
newval must match with type of the expression (oldval+12)
 In Semantic rule, attributes (such as data type, string, number, memory
location, etc.) can be represented by VAL.
 A SDD specifies the values of attributes by associating semantic rules with
the grammar productions.
 Example:
 Production Semantic Rule
EE+T E.val=E.val + T.val
ET E.val=T.val
TE*T T.val=E.val * T.val
TF T.val=F.val
FNUM F.val=num.lexval (this attribute return by LA)
 We may also alternatively insert the semantic actions inside the grammar.
10/31/202  Example: E  E+T {print ‘+’} 6
4.2. Types of Attribute
 Attribute Grammar
 It is a special form of CFG, where some additional information (attributes)
are appended to one or more of its non-terminals
 This provide context-sensitive information.
 Attribute grammar is a medium to provide semantics to the context-free
grammar.
 Attribute grammar (when viewed as a parse-tree) can pass values or
information among the nodes of a tree.
 Example: 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.
 Here, the values of E and T are added together and the result is copied to
E.
 Based on the way the attributes get their values, they can be broadly divided
into two categories:
i. synthesized attributes and
ii. inherited attributes.
10/31/202 7
Types of Attribute cont’d
i. synthesized, meaning that its value is obtained
 from attributes of child nodes in the parse tree,

 or from other attributes of the same node in the parse tree

 Example: S → ABC

 If S is taking values from its child nodes (A,B,C),

 then it is said to be a synthesized attribute, as the values of ABC are

synthesized to S.
ii. inherited, meaning that its value is obtained
 from the parent node in the parse tree,

 or from sibling nodes in the parse tree

 useful when there is mismatch between grammar for parsing, and

“abstract syntax” for translation


 Example S → ABC

 A can get values from S, B and C. B can take values from S, A, and C.

Likewise, C can take values from S, A, and B

10/31/202 8
Types of Attribute cont’d
 Expansion: When a non-terminal is expanded to terminals as per a
grammatical rule

 Reduction: When a terminal is reduced to its corresponding non-terminal


according to grammar rules.
 Syntax trees are parsed top-down and left to right.

 Whenever reduction occurs, we apply its corresponding semantic rules


(actions).
 Semantic analysis uses Syntax Directed Translations to perform the above
tasks.
 Semantic analyzer receives AST (Abstract Syntax Tree) from its previous
stage (syntax analysis).
 Semantic analyzer attaches attribute information with AST, which are called
Attributed AST.
 Attributes are two tuple value, <attribute name, attribute value>
 For example: int value = 5;<type, “integer”><presentvalue, “5”>

 For every production, we attach a semantic rule. 9


Types of Attribute cont’d…..
 Example: synthesized attributed SDD for simple calculator’s Arithmetic
Expression; “9+3*5\n”;
Production Semantic Rule
L  E \n L.val = E.val
E  E1 + T E.val = E1.val + T.val L.val=24
E T E.val = T.val
T  T1 * F T.val = T1.val * F.val
T F T.val = F.val E.val=24 \n
F (E) F.val = E.val
F  digit F.val = digit.lexval
+
E.val=9 T.val=15

T.val=3 * F.val=5
T.val=9

 Symbols E, T, and F are This is an annotated parse tree


associated with a synthesized F.val=9 Note that no complete parse tree need
attribute val. actually be constructed.
 The token digit has a synthesized
attribute lexval (it is assumed that digit.lexval=9
it is evaluated by the lexical
analyzer)
10
4.3. Dependency graph and evaluation order
 Dependencies of Attributes
 In semantic rule: b := f(c1, c2, ..., ck) we say b depends on c1, c2, ..., ck
 The semantic rule for b must be evaluated after the semantic
rules for c1, c2, ..., ck
 This dependencies of attributes can be represented by a directed graph
called dependency graph
 Dependency Graphs and Evaluation order of SDD’s
 It indicate the flow of information among the instances of attributes in a
given parse tree.
 An edge from one attribute instance to another
 means that the value of the first is needed to compute the second.
 Example: if dependency graph has an edge from M to N then M must be
evaluated before the attribute of N
 Thus the only allowable orders of evaluation are those sequence of nodes
N1,N2,…,Nk such that if there is an edge from Ni to Nj then i<j
 Such an ordering is called a topological sort of a graph, which produces a
possible sequential order of evaluation
10/31/202 11
Dependency graph & evaluation order cont’d…
 Evaluation order for SDD includes how the SDD(Syntax Directed
Definition) is evaluated with the help of attributes, dependency graphs,
semantic rules, and S and L attributed definitions.
 A dependency graph is used to determine the order of evaluation of
attributes with the help of edges.
 An edge from, the first node to the second node attribute gives the
information that evaluation of first node attribute is required for the
evaluation of the second node attribute.
 Edges represent the semantic rules of the corresponding production.
 Values of synthesized attributes may be calculated in any order
 For a S-attributed SDD, any bottom-up order be sufficient

 Whereas for inherited attributes, order of evaluation is important


 it is possible to write rules for which no order of evaluation is
possible
 In a L-attributed SDD, dependencies always go left-to-right, so no
circularity is possible.

10/31/202 12
4.4. S-attributed definitions
 S-attributed SDD
 If an SDT uses only synthesized attributes, it is called as S-attributed
SDT.
 These attributes are evaluated using S-attributed SDTs that have their
semantic actions written after the production (right hand side).

 As depicted above, attributes in S-attributed SDTs are evaluated in


bottom-up parsing,
 as the values of the parent nodes depend upon the values of the child
nodes.
10/31/202 13
4.5. L-attributed definitions
 L-attributed SDD
 This form of SDT uses both synthesized and inherited attributes with
restriction of not taking values from right siblings.
 In L-attributed SDTs, a non-terminal can get values from its parent, child,
and sibling nodes.
 See the following production
S → ABC S can take values from A, B, and C (synthesized).
 A can take values from S only.
 B can take values from S and A.
 C can get values from S, A, and B.
 Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right
parsing manner.

10/31/202 14
Next
Chapter Five
Type Checking

Outline
5.1. Type systems
5.2. Specifications of a type checker
5.3. Equivalence of types
5.4. Type conversion

15
10/31/202 WCU-CS Compiled by TM.

You might also like