Syntax Directed Translation

14
Syntax Directed Translation

description

Syntax Directed Translation. Why do we need Syntax-directed?. To translate a programming language construct, a compiler may need to keep track of many quantities besides the code generated for the construct. Syntax-directed - PowerPoint PPT Presentation

Transcript of Syntax Directed Translation

Page 1: Syntax Directed Translation

Syntax Directed Translation

Page 2: Syntax Directed Translation

Why do we need Syntax-directed?• To translate a programming language construct,

a compiler may need to keep track of many quantities besides the code generated for the construct.

• Syntax-directed– We augment a context-free grammar by adding

semantic rules or actions to each production. – These rules define the values of attributes and how to

compute them.  – We call this augmented grammar an attribute grammar.

• Two types of attributes: – synthesized attribute which computes the value based on the

children – inherited attribute computed based on the parent and the

older siblings.– We can use two methods to translate the programming

language:• a formalism, syntax- directed definition• a more procedural notation called translation scheme.

Page 3: Syntax Directed Translation

Syntax-directed definition(1/2)

• Syntax-directed definition– A grammar -oriented compiling technique

– It is very helpful for organizing the compiler's front end and it translates infix expression into post form

• Postfix notation1) If E is a variable or constant, then the postfix notation

for E is E itself.

2) If E is an expression of the form E1 op E2 where op is any binary operator, then the postfix notation foe E is E1' E2' op.

3) If E is an expression of the form (E1), then the postfix notation for E1 is also the postfix notation for E.

Page 4: Syntax Directed Translation

Syntax-directed definition(2/2)

• Essentially a formal treatment of two simple concepts – Each node of a parse tree can have a set of associate

d values (attributes). – The creation and traversal of parse trees can be auto

mated • Example

if we have a production A --> X1 X2 ... Xn

we might have a rule A.a = f(X1.a,X1.b,X2.a,X2.b,...,Xn.a,Xn.b)

which describes how the attribute a of A is defined in terms of attributes of the children of A.

Page 5: Syntax Directed Translation

Translation scheme(1/2) • A translation is an iuput-output mapping • The output for each input x is specified in the followin

g way – construct a parse tree for x – compute the value using semantic rule for attribute associate

d with the X-production used at node n – two types of attributes

• synthesized attribute which computes the value based on the children

• inherited attribute computed based on the parent and the older siblings

• A translation scheme gives a procedural description of an action to be taken when a production is applied

Page 6: Syntax Directed Translation

Translation scheme(2/2)• The actions may be interspersed with the symbols on th

e right-hand side of the production – thus indicating an evaluation order – For example, in a translation scheme used to generate assemb

ly code while-stmt --> while { generate label L1,L2; emit("L1:" )

expression { emit("beqz L2"); } statement { emit("b L1); emit("L2:"); }

• Attributes may be evaluated during parsing or in a separate pass over the parse tree

• Different strategies are used, depending whether a top-down or bottom-up parser is used

Page 7: Syntax Directed Translation

How attributes are evaluated depends on whether they are synthsized or inherit

ed

• A synthesized attribute of a symbol is one which depends only on the attributes of its children (in the parse tree) – An inherited attribute is one which depends on attri

butes of the parent or siblings (ex. the data type of an identifier)

• In general, inherited attributes are more difficult to deal with, particularly for bottom-up parsers

Page 8: Syntax Directed Translation

How attributes are evaluated depends on whether they are synthesized or

inherited.  • A synthesized attribute of a symbol is one

which depends only on the attributes of its children (in the parse tree); – for example, the value of an expression. 

• An inherited attribute is one which depends on attributes of the parent or siblings; – for example the data type of an identifier. 

• In general, inherited attributes are more difficult to deal with, particularly for bottom-up parsers.

Page 9: Syntax Directed Translation

Synthesized Attributes The attribute value of the terminal at the left

hand side of a grammar rule depends on the values of the attributes on the right hand side.

Typical for LR (bottom up) parsing. Example: TT*F {$$.val=$1.val$3.val}.

T.val

T.val F.val

Page 10: Syntax Directed Translation

Running Example 1:Expressions

E E + T {$$.val:=$1.val+$3.val;} E T {$$.val:=$1.val;} T T * F {$$.val:=$1.val*$3.val;} T F {$$.val:=$1.val;} F ( E ) {$$.val:=$2.val;} F num {$1.val:=$1.val;}

Page 11: Syntax Directed Translation

Inherited attributes

The value of the attributes of one of the symbols to the right of the grammar rule depends on the attributes of the other symbols (left or right).

Typical for LL parsing (top down). D T {$2.type:=$1.type} L L id , {$3.type:=$1.type} L

D.type

,id

L.type

L.typeT.type L.type

Page 12: Syntax Directed Translation

Type definitions D T {$2.type:=$1.type} L T int {$$.type:=int;} T real {$$:=real;} L id , L {gen(id.name,$$.type); $3.type:=$$.type;} L id {gen(id.name,$$.type); }

T.type

int

Page 13: Syntax Directed Translation

• When the syntax-directed definition has only synthesized attribute, it is a S-attribute definitions. It can be computed by using bottom-up parser. While a syntax-directed definition is L-attribute if each inherited attribute of X_j, 1<= j <= n, on the right side of A-> X1X2¡ Xn, depends only on – 1) the attribute of the symbols X1,X2,¡ ,Xj-1 to the l

eft of Xj in the production and – 2) the inherited attributes of A.

Page 14: Syntax Directed Translation

• Every S-attributed definition is L-attributed, because the restriction 1) and 2) apply only to inherited attributes. It can be computed in depth-first order. All syntax-directed definition based on LL(1) grammars are L-attributed. 

• Following are some examples of S-attributed and L-attributed:1)     synthesized attribute on the parser stack:

2)     A non-L-attributed syntax-directed definition:

… …

X X.x

Y Y.y

Z Z.z

… …

Production Semantic rule

A -> LM L.i := l(A,I)

M.i := m(L.s)

A.s := f(M.s)

A -> QR R.i := r(A.i)

Q.i := q(R.s)

A.s := f(Q.s)