Syntax Directed Translation
Syntax Directed Translation
1
Introduction
Attaching attributes to the variables of the context Free Grammar and defining semantic
action (meaning) of each production of grammar is called Syntax Directed Translation.
Every non-terminal can get one or more than one attribute or sometimes 0 attribute
depending on the type of the attribute. The value of these attributes is evaluated by the
semantic rules associated with the production rule.
In the semantic rule, attribute is VAL and an attribute may hold anything like a string, a
number, a memory location and a complex record.
2
Introduction
E → E + T E.val := E.val + T.val, E.val is one of the attributes of E.
Values of these attributes are evaluated by the semantic rules associated with the
production rules. So we can say that
Syntax-directed definitions
Translation schemes
3
Introduction
Syntax directed definitions
Give high-level specifications for translations
Hide many implementation details such as order of evaluation of semantic
actions.
We associate a production rule with a set of sematic actions, and we do not say
when they will be evaluated.
Translation schemes
Indicate the order of evaluation of semantic actions associated with a production
rule.
In other words, translation schemes give a little bit information about
implementation details.
4
Introduction
5
Syntax Directed Definition
A SDD is a context free grammar with attributes and rules.
Attributes are associated with grammar symbols and rules with productions.
Synthesized attributes
A synthesized attribute at node N is computed from the values of attributes at the
children in that node of the parse tree.
A Synthesized attribute is an attribute of the non-terminal on the left-hand side of a
production. For eg. let’s say A -> BC is a production of a grammar, and A’s attribute
is dependent on B’s attributes or C’s attributes then it will be synthesized attribute.
6
Syntax Directed Definition
Inherited attributes
An inherited attribute at node N is defined only in terms of attribute values at N’s parent, N
itself and N’s siblings.
A syntax directed definition specifies the values of attributes by associating semantic rules
with the grammar productions.
E->E1+T E.code=E1.code||T.code||’+’
7
Syntax Directed Definition
In a syntax-directed definition, each production A is associated with
a set of semantic rules of the form:
L-attributed SDT:
If an SDT uses both synthesized attributes and inherited attributes with a restriction
that inherited attribute can inherit values from left siblings only, it is called as L-
attributed SDT.
2. The token digit has a synthesized attribute lexval(it is assumed that it is evaluated by the
lexical analyzer).
3. Terminals are assumed to have synthesized attributes only. Values for attributes of terminals
are usually supplied by the lexical analyzer.
4. The start symbol does not have any inherited attribute unless otherwise stated.
11
L-Attributed definitions
A SDD is L-Attributed if the edges in dependency graph goes from Left to
Right but not from Right to Left. More precisely, each attribute must be either
Synthesized
12
Example of L-attributed
definition
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. No non-terminal can get values from the sibling to its right.
13
Annotated parse tree -- Example
Input: 5+3*4
14
Evaluation orders for SDD’s
A dependency graph is used to determine the order of computation of
attributes
Dependency graph
For each parse tree node, the parse tree has a node for each attribute
associated with that node
If a semantic rule defines the value of synthesized attribute A.b in terms of the
value of X.c then the dependency graph has an edge from X.c to A.b
If a semantic rule defines the value of inherited attribute B.c in terms of the
value of X.a then the dependency graph has an edge from X.c to B.c
15
Dependency Graph
16
Ordering the evaluation of
attributes
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
Input string Parse Tree Dependency graph evaluation order for semantic
rules.
17
Evaluating Semantic rules
Parse Tree methods
At compile time evaluation order obtained from the topological sort of dependency graph.
Fails if dependency graph has a cycle
Oblivious Methods
Evaluation order is chosen without considering the semantic rules.
Restricts the class of syntax directed definitions that can be implemented.
If translation takes place during parsing order of evaluation is forced by parsing method.
18
Syntax directed translation
schemes
A translation scheme is a context free grammar in which
Attributes are associated with grammar symbols;
Semantic actions are enclosed between braces{} and are inserted within the right-hand
side of production.
Any SDT can be implemented by first building a parse tree and then
performing the actions in a left-to-right depth first order
20
Postfix translation schemes
Simplest SDDs are those that we can parse the grammar
bottom-up and the SDD is s-attributed
21
Example of postfix SDT
1) L -> E n {print(E.val);}
2) E -> E1 + T {E.val=E1.val+T.val;}
3) E -> T {E.val = T.val;}
4) T -> T1 * F {T.val=T1.val*F.val;}
5) T -> F {T.val=F.val;}
6) F -> (E) {F.val=E.val;}
7) F -> digit {F.val=digit.lexval;}
22
Type Checking
Type checking is the process of verifying and enforcing constraints of types in
values.
Check that the source program follows both the syntactic and semantic
conventions of the source language.
It checks the type of objects and reports a type error in the case of a violation,
and incorrect types are corrected.
The type checker is a module of a compiler and its task is type checking.
23
Type Checking
Conversion from one type to another type is known as implicit
if it is to be done automatically by the compiler. also called
Coercion.
It checks the type variables at compile time, which means the type of the
Name-related checks
25
Type Checking
Dynamic type Checking
Dynamic Type Checking is defined as the type checking
being done at run time.
In Dynamic Type Checking, types are associated with values, not
variables.
Dynamic typing is more flexible.
26
Position of type checker
Notes:
The idea is to associate each language construct with an expression describing its
type and so called type expression.
Note:
A type checker implements a type system
The type systems are specified in a syntax-directed manner
Different type systems may be used by different compilers or
processors of the same language
29
Type Checker
30
Type Checker
31
Type Checker
32
Type Checker
33
Type Checker
34
Type Checker
35