Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

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.

 In Syntax directed translation, whenever a construct encounters in the programming


language then it is translated according to the semantic rules define in that particular
programming language.

2
Introduction
 E → E + T E.val := E.val + T.val, E.val is one of the attributes of E.

 We associate information with the programing language constructs by attaching


attributes to grammar symbols.

 Values of these attributes are evaluated by the semantic rules associated with the
production rules. So we can say that

Grammar + semantic rule = SDT (syntax directed translation)

 When we associate semantic rules with productions, we use two notations:

 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

 Conceptually with both the syntax directed


translation and translation scheme we
 Parse the input token stream
 Build the parse tree
 Traverse the tree to evaluate the semantic rules at the parse
tree nodes.
Input string  Parse Tree  Dependency graph  evaluation order for semantic rules.
Conceptual view of syntax directed translation.

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.

 Attributes may be of many kinds: numbers, types, table references, strings,


etc.

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.

 An attribute of a nonterminal on the right-hand side of a production is called an inherited


attribute. For example, let’s say A -> BC is a production of a grammar and B’s attribute is
dependent on A’s attributes or C’s attributes then it will be inherited attribute.

 A syntax directed definition specifies the values of attributes by associating semantic rules
with the grammar productions.

Production semantic rules

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:

 b= f(c1,c2,…,cn) where f is a function and b can be on of the


followings.

 b is a synthesized attribute of A and c1,c2,…,cn are attributes of the


grammar symbols in the production (A). OR

 b is an inherited attribute one of the grammar symbols in (on the


right side of the production), and c1,c2,…,cn are attributes of the
grammar symbols in the production(A).
8
Attribute grammar
 So, a semantic rule b=f(c1,c2,…cn) indicates that the attribute b
depends on attributes c1,c2,…cn.

 In a syntax-directed definition, a semantic rule may just


evaluate a value of attribute or it may have some side effects
such as printing values.

 An attribute grammar is a syntax-directed definition in which


the functions in the semantic rules can not have side effects
(they can only evaluate values of attributes).
9
S-attribute & L-attribute
S-attributed SDT

 If an SDT uses only synthesized attributes, it is called as S-attributed SDT.

 S-attributed SDTs are evaluated in bottom-up parsing, as the values of the


parent nodes depend upon the values of the child nodes.

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.

 Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing


manner.
10
Example of S-attributed SDD

Production Semantic Rules


1) L -> E n L.val = 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
1. Symbol E,T and F are associated with a synthesized attribute val.

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

 Inherited, But if there is a production A->X1X2…Xn and there is an inherited


attribute Xi.a computed by a rule associated with this production, then the rule
may only use:
 Inherited attributes associated with the head A
 Either inherited or synthesized attributes associated with the occurrences of symbols X1,X2,…,Xi-1
located to the left of Xi
 Inherited or synthesized attributes associated with this occurrence of Xi itself, but in such a way that
there is no cycle in the graph

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

 Such an ordering is called a Topological Sort of a Graph

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

 Rule Based Methods


 Semantic rules analyzed by hand or specialized tools at compiler construction time
 Order of evaluation of attributes associated with a production is pre-determined at compiler
construction time

 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.

 Translation Schemes deal with both synthesized and inherited attributes.

 Semantic Actions are treated as terminal symbols: Annotated parse-trees contain


semantic actions as children of the node standing for the corresponding
production.

 Translation Schemes are useful to evaluate L-Attributed definitions at parsing

time (even if they are a general mechanism).


19
Syntax directed translation
schemes
 An SDT is a Context Free grammar with program fragments embedded within
production bodies

 Those program fragments are called semantic actions

 They can appear at any position within production body

 Any SDT can be implemented by first building a parse tree and then
performing the actions in a left-to-right depth first order

 Typically SDT’s are implemented during parsing without building a parse


tree.

20
Postfix translation schemes
 Simplest SDDs are those that we can parse the grammar
bottom-up and the SDD is s-attributed

 For such cases we can construct SDT where each action is


placed at the end of the production and is executed along
with the reduction of the body to the head of that production

 SDT’s with all actions at the right ends of the production


bodies are called postfix SDT’s.

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 should also check the type rules of the 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.

 Conversion is said to be Explicit if the programmer writes


something to do the Conversion.

 There are two kinds of type checking:


 Static Type Checking.

 Dynamic Type Checking.


24
Type Checking
 Static Checking

 Is defined as type checking performed at compile time.

 It checks the type variables at compile time, which means the type of the

variable is known at the compile time.

Examples of static check


 Type checks (incompatible operand)

 Flow-of-control checks (break statement)

 Uniqueness checks (uniquely declared identifier)

 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:

 A type checker verifies that the type of a construct


matches that expected by its context.

 Type information gathered by a type checker may be


needed when code is generated.
27
Type Systems
Type Expressions

 The idea is to associate each language construct with an expression describing its
type and so called type expression.

 Denotes type of language constructer.

 Denote the type of a language construct


 A basic type is a type expression(like int, real, boolean, char).
 A type name is A type constructor applied to type expression . Constructors include:

Arrays, products, records, pointers, functions


 Type expressions may contain variables whose values are type expressions.

Notes: A convenient way to represent a type expression is to use a graph


28
Type Systems
A type system

 is a collection of rules for assigning type expressions to


the various parts of a program.

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

You might also like