CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. ·...

60
CS308 Compiler Principles Syntax-Directed Translation Li Jiang Department of Computer Science and Engineering Shanghai Jiao Tong University

Transcript of CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. ·...

Page 1: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

CS308 Compiler Principles

Syntax-Directed Translation

Li JiangDepartment of Computer Science and Engineering

Shanghai Jiao Tong University

Page 2: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Phases of Compilation

Lexical Analyzer

Syntax Analyzer

Semantic Analyzer

Intermediate Code

Generator

Code Optimizer

Code GeneratorSource

LanguageTarget

Language

Intermediate

Language

Analysis SynthesisSymbol

Table

Page 3: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

A Model of A Compiler Font End

• Lexical analyzer reads the source program character by character and

returns the tokens of the source program.

• Parser creates the tree-like syntactic structure of the given program.

• Intermediate-code generator translates the syntax tree into three-

address codes.

Page 4: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Syntax-Directed Translation

• Associate semantic meanings with the grammar.

– generate intermediate codes

– put information into the symbol table

– perform type checking

– issue error messages

– perform some other activities

– in fact, they may perform almost any activities.

Page 5: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Syntax-Directed Translation Cont’d

• Syntax-Directed Definitions:

– associate a production rule with a set of attributes and semantic rules

– give high-level specifications for translations

– hide many implementation details such as order of evaluation of semantic rules

• Translation Schemes:

– embed program fragments within production bodies

– indicate the order of evaluation of semantic actions associated with a production

Page 6: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Syntax-Directed Definition (SDD)

• A syntax-directed definition is an extension of

a context-free grammar:– Each grammar symbol is associated with a set of attributes.

– Each production is associated with a set of semantic rules.

• Attributes are divided into two kinds:– Synthesized attribute is defined only in terms of attribute

values at the node’s children and itself.

– Inherited attribute is defined in terms of attribute values

the node’s parent, itself, and siblings.

*

Imagine a parse tree!

Page 7: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

SDD Cont’d

• 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,

b is a synthesized attribute of A and c1,c2,…,cn are attributes of the grammar symbols in the production ( A→α ).

b is an inherited attribute of one of the grammar symbols in α, and c1,c2,…,cn are attributes of the grammar symbols in the production ( A→α ).

Page 8: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Attribute Grammar

• 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 not only evaluate the value of an

attribute, but also have some side effects

such as printing values.

• An attribute grammar is a syntax-directed

definition without side effects.

Page 9: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

SDD Example1Production Semantic Rules

L → E return print(E.val)E → E1 + T E.val = E1.val + T.valE → T E.val = T.valT → T1 * F T.val = T1.val * F.valT → F T.val = F.valF → ( E ) F.val = E.valF → digit F.val = digit.lexval

• Symbols E, T, and F are associated with a synthesized attribute val.

• The token digit has a synthesized attribute lexval(an integer value returned by the lexical analyzer).

Page 10: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

SDD Example2Production Semantic RulesE → E1 + T E.loc=newtemp(), E.code = E1.code || T.code || add E1.loc,T.loc,E.loc

E → T E.loc = T.loc, E.code=T.code

T → T1 * F T.loc=newtemp(), T.code = T1.code || F.code || mult T1.loc,F.loc,T.loc

T → F T.loc = F.loc, T.code=F.code

F → ( E ) F.loc = E.loc, F.code=E.code

F → id F.loc = id.name, F.code=“”

• Symbols E, T, and F are associated with synthesized attributes loc and code.

• The token id has a synthesized attribute name.

• || is the string concatenation operator.

Guess what happens

Page 11: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Annotated Parse Tree

A parse tree can be used to visualize the translation specified

by an SDD.

• A parse tree showing the values of attributes

at each node is called an annotated parse

tree.

• The process of computing the attributes

values at the nodes is called annotating (or

decorating) of the parse tree.

Page 12: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Annotated Parse Tree Example

Input: 5+3*4 L

E

E + T

T T * F

F F digit

digit digit

Page 13: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Annotated Parse Tree Example

Input: 5+3*4 L

E.val=17

E.val=5 + T.val=12

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

F.val=5 F.val=3 digit.lexval=4

digit.lexval=5 digit.lexval=3

What about these rules: E.code = E1.code || T.code || add E1.loc,T.loc,E.loc

Page 14: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Dependency Graph• While an annotated parse tree shows the values of attributes

• Semantic rules set up dependencies among

attributes.

• Dependency graph determines the

evaluation order of the semantic rules.

– An edge from one attribute to another indicates

that the value of the former one is needed to

compute the later one.

Page 15: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Dependency Graph Example

Input: 5+3*4 L

E.val=17

E.val=5 T.val=12

T.val=5 T.val=3 F.val=4

F.val=5 F.val=3 digit.lexval=4

digit.lexval=5 digit.lexval=3

Page 16: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Inherited Attributes Example

Production Semantic Rules

D → T L L.in = T.type

T → int T.type = integer

T → real T.type = real

L → L1 id L1.in = L.in, addtype(id.entry,L.in)

L → id addtype(id.entry,L.in)

• Symbol T is associated with a synthesized attribute type.

• Symbol L is associated with an inherited attribute in.

Input: real p q

Page 17: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

A Dependency Graph with Inherited Attributes

Input: real p q

D T.type=real L.in=real

T L L1.in=real, addtype(q,real)

real L id addtype(p,real) id.entry=q

id id.entry=p

parse tree dependency graph

Page 18: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

S & L-Attributed Definitions

• We will look at two sub-classes of the

syntax-directed definitions:– S-Attributed Definitions: only synthesized

attributes are used in the syntax-directed

definitions.

– L-Attributed Definitions: both synthesized

and inherited attributes are used in a

restricted fashion.

• dependency-graph edges can go from left to right,

but not from right to left Why?

Page 19: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

S-Attributed Definitions

• S-Attributed Definitions: only synthesized

attributes are used in the syntax-directed

definitions

– each rule computes an attribute for the non-

terminal at the head of a production from

attributes taken from the body of the production

– the attributes can be evaluated by performing a

post-order traversal of the parse tree

– can be implemented naturally with an parser

– can also be implemented with an LL parser

LRHow?

Page 20: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Bottom-Up Evaluation of S-Attributed

Definitions

• Put the values of the synthesized attributes of the

grammar symbols into a parallel stack

• Evaluate the values of the attributes during reductions

Example: A XYZ A.a=f(X.x,Y.y,Z.z)

(all attributes are synthesized)

stack parallel-stack

top

f()

top

Z

Y

X

.

A

.

A.a

.

Z.z

Y.y

X.x

.

Page 21: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

SDD Example Recall

Production Semantic Rules

L → E return print(E.val)

E → E1 + T E.val = E1.val + T.val

E → T E.val = T.val

T → T1 * F T.val = T1.val * F.val

T → F T.val = F.val

F → ( E ) F.val = E.val

F → digit F.val = digit.lexval

• Symbols E, T, and F are associated with a synthesized attribute val.

• The token digit has a synthesized attribute lexval (an integer value returned by the lexical analyzer).

Page 22: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Canonical LR(0) Collection for The Grammar

L’→.LL →.ErE →.E+TE →.TT →.T*FT →.FF →.(E)F →.d

I0:I1:

I2:

I4:

I3:

I5:

I6:

L

E

T

F

(

d

I7:

I10:

I9:

I8:

r

E

T

F

(

d

*

+

6

3

5

4

* 9

I12:

I11:

I13:

TF

F

(

(

d

d

)

+

6

6

5

5

8

4

L’→L.L →E.rE →E.+T

E →T.T →T.*F

T →F.F → (.E)E →.E+TE →.TT →.T*FT →.FF →.(E)F →.d

F →d.

L →Er.E →E+.TT →.T*FT →.FF →.(E)F →.d

T →T*.FF →.(E)F →.d

F →(E.)E →E.+T

E →E+T.T →T.*F

T →T*F.

F →(E).

Page 23: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Bottom-Up Evaluation Example• At each shift of digit, we also push digit.lexval into val-stack.

stack val-stack input action semantic rule

0 5+3*4r s6 d.lexval(5) into val-stack

0d6 5 +3*4r F→digit F.val=d.lexval

0F4 5 +3*4r T→F T.val=F.val

0T3 5 +3*4r E→T E.val=T.val

0E2 5 +3*4r s8 push empty slot into val-stack

0E2+8 5- 3*4r s6 d.lexval(3) into val-stack

0E2+8d6 5-3 *4r F→digit F.val=d.lexval

0E2+8F4 5-3 *4r T→F T.val=F.val

0E2+8T11 5-3 *4r s9 push empty slot into val-stack

0E2+8T11*9 5-3- 4r s6 d.lexval(4) into val-stack

0E2+8T11*9d6 5-3-4 r F→digit F.val=d.lexval

0E2+8T11*9F12 5-3-4 r T→T*F T.val=T1.val*F.val

0E2+8T11 5-12 r E→E+T E.val=E1.val+T.val

0E2 17 r s7 push empty slot into val-stack

0E2r7 17- $ L→Er print(17), pop empty slot from val-stack

0L1 17 $ acc

Page 24: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Bottom-Up Eval. of S-Attributed Definitions

Production Semantic RulesL → E return print(val[top-1])E → E1 + T val[ntop] = val[top-2] + val[top]E → TT → T1 * F val[ntop] = val[top-2] * val[top]T → FF → ( E ) val[ntop] = val[top-1]F → digit push digit.lexval

• At each shift of digit, we also push digit.lexvalinto val-stack.

• At all other shifts, we do not put anything into val-stack because other terminals do not have attribute (but we increment the stack pointer for val-stack).

Page 25: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Top-Down Eval. of S-Attributed Definitions

Productions Semantic Rules

A → B print(B.n0), print(B.n1)

B → 0 B1 B.n0=B1.n0+1, B.n1=B1.n1

B → 1 B1 B.n0=B1.n0, B.n1=B1.n1+1

B → B.n0=0, B.n1=0

B has two synthesized attributes (n0 and n1).

Guess what is the semantic meaning?

Page 26: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Top-Down Eval. of S-Attributed Definitions

• In a recursive predictive parser, each non-terminal corresponds to a procedure.

procedure A() {

call B(); A → B

}

procedure B() {

if (currtoken=0) { consume 0; call B(); } B → 0 B

else if (currtoken=1) { consume 1; call B(); } B → 1 B

else if (currtoken=$) {} // $ is end-marker B →

else error(“unexpected token”);

}

Page 27: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Top-Down Eval. of S-Attributed Definitions

procedure A() {

int n0,n1; Synthesized attributes of non-terminal B

call B(&n0,&n1); are the output parameters of procedure B.

print(n0); print(n1);

} All the semantic rules can be evaluated

procedure B(int *n0, int *n1) { at the end of parsing of production rules

if (currtoken=0)

{ int a,b; consume 0; call B(&a,&b); *n0=a+1; *n1=b; }

else if (currtoken=1)

{ int a,b; consume 1; call B(&a,&b); *n0=a; *n1=b+1; }

else if (currtoken=$) {*n0=0; *n1=0; } // $ is end-marker

else error(“unexpected token”);

}

Page 28: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

L-Attributed Definitions

• L-Attributed Definitions: both synthesized

and inherited attributes are used in a

restricted fashion.

– can always be evaluated by a depth first

traversal of the parse tree

– can also be evaluated during the parsing

Page 29: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

L-Attributed Definitions

• A syntax-directed definition is L-attributed if each inherited attribute of Xj, where 1jn, on the right side of A → X1X2...Xn depends only on:1. the inherited attribute of A

2. the attributes of the symbols X1,...,Xj-1 to the left of Xj in the production

3. attributes associated with Xj itself, under the condition that there is no cycle in the dependency graph involving the attributes of Xj

• Every S-attributed definition is L-attributed, the restrictions only apply to the inherited attributes (not to synthesized attributes).

Page 30: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

A L-Attributed SDD

Productions Semantic RulesT → F T’ T’.inh = F.val

T’ → * F T’1 T’1.inh=T’.inh * F.val

Page 31: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

A Definition that is NOT L-Attributed

Productions Semantic RulesA → L M L.in=l(A.i), M.in=m(L.s), A.s=f(M.s)

A → Q R R.in=r(A.in), Q.in=q(R.s), A.s=f(Q.s)

• This syntax-directed definition is not L-

attributed because the semantic rule

Q.in=q(R.s) violates the restrictions of L-

attributed definitions.

Think: How can we evaluate Q.in ? See next Page.

Page 32: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Syntax-Directed Translation Schemes (SDT)

• A syntax-directed translation scheme is

a context-free grammar in which: – attributes are associated with the grammar symbols

– semantic actions enclosed between braces {} are

inserted within the body of productions.

• Example: A → { ... } X { ... } Y { ... }

Semantic Actions

Page 33: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

SDT Cont’d

• In translation schemes, we use semantic action instead of semantic rule used in syntax-directed definitions.

• Restrictions in designing a translation scheme:

– The position of the semantic action on the right side indicates when that semantic action will be evaluated.

– These restrictions (motivated by L-attributed definitions) ensure that a semantic action does not refer to an attribute that has not yet computed.

A new term: not exists in SDD

Page 34: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

A SDT Example

• A simple translation scheme that converts infix expressions to the corresponding postfix expressions.

E → T R

R → + T { print(“+”) } R1

R →

T → id { print(id.name) }

a+b+c ab+c+

infix expression postfix expression

When to evaluate X{}Y: - Bottom-up:?

- Top-down:?

Explain later

Page 35: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

A SDT Example Cont’d

E

T R

id {print(“a”)} + T {print(“+”)} R

id {print(“b”)} + T {print(“+”)} R

id {print(“c”)}

A depth first traversal of the parse tree will produce the postfix representation of the infix expression.

Page 36: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

SDT for S-Attributed Definition

• For each associated semantic rule in a S-

attributed SDD, append a semantic action

to the end of the production body.

Production Semantic Rule

E → E1 + T E.val = E1.val + T.val

E → E1 + T { E.val = E1.val + T.val }

Page 37: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

SDT for L-Attributed Definition

• Conversion rules:1. An inherited attribute of a symbol on the right side of a

production must be computed in a semantic action before that symbol.

2. A semantic action must not refer to a synthesized attribute of a symbol to the right of that semantic action.

3. A synthesized attribute for the non-terminal on the left can only be computed after all attributes it references have been computed (this semantic action is placed at the end of the production body).

• Any L-attributed definition can always be converted to a corresponding translation scheme satisfying these three rules.

Page 38: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

A SDT with Inherited Attributes

D → T id { addtype(id.entry,T.type), L.in = T.type } L

T → int { T.type = integer }

T → real { T.type = real }

L → id { addtype(id.entry,L.in), L1.in = L.in } L1

L →

• This is a translation scheme for an L-attributed

definitions.

Page 39: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

When to evaluate the sematic action?

• For production B → X {a} Y

– If the parsing is bottom-up, then we perform

action a as soon as this occurrence of X

appears on the top of the parsing stack.

– If the parsing is top-down, we perform a just

before we attempt to expand this occurrence

of Y (if Y is a nonterminal) or check for Y on

the input (if Y is a terminal).

Page 40: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Implementing SDT

• Using Recursive-Descent Parsing

– Decide the production used to expand A

– Match each terminal appears on the input

– Preserve, in local variables, the values of all

attributes needed to compute inherited and

synthesized attributes

– Call functions corresponding to nonterminals

in the body, and provide them with the proper

arguments

Page 41: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Recursive-Descent Parsing of SDT

procedure D() {int T.type,L.in,id.entry;call T(&T.type); consume(id,&id.entry);addtype(id.entry,T.type); Lin=T.type;call L(L.in); a synthesized attribute (an output parameter)

}

procedure T(int *T.type) {if (currtoken is int) { consume(int); *T.type=TYPEINT; }else if (currtoken is real) { consume(real); *T.type=TYPEREAL; }else { error(“unexpected type”); }

} an inherited attribute (an input parameter)

procedure L(int L.in) {if (currtoken is id) { int L1.in,id.entry; consume(id,&id.entry);

addtype(id.entry,L.in); L1.in=L.in; call L(L1.in); }else if (currtoken is endmarker) { }else { error(“unexpected token”); }

}

Page 42: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Eliminating Left Recursion from SDT

• A translation scheme with a left recursive grammar.

E → E1 + T { E.val = E1.val + T.val }E → E1 - T { E.val = E1.val - T.val }E → T { E.val = T.val }T → T1 * F { T.val = T1.val * F.val }T → F { T.val = F.val }F → ( E ) { F.val = E.val }F → digit { F.val = digit.lexval }

• When we eliminate the left recursion from the grammar (to get a suitable grammar for the top-down parsing) we also have to change semantic actions

Page 43: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Eliminating Left Recursion

A → A1 Y { A.a = g(A1.a,Y.y) } a left recursive grammar with

A → X { A.a=f(X.x) } synthesized attributes (a,y,x).

eliminate left recursion

inherited attribute of the new non-terminal

synthesized attribute of the new non-terminal

A → X { R.in=f(X.x) } R { A.a=R.syn }

R → Y { R1.in=g(R.in,Y.y) } R1 { R.syn = R1.syn }

R → { R.syn = R.in }

Page 44: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Eliminating Left Recursion Cont’d

A parse tree of left recursive grammar

A Y A.a=g(f(X.x),Y.y)

parse tree of non-left-recursive grammar

X X.x=f(X.x) A

X R.in=f(X.x) R A.a=g(f(X.x,Y.y)

Y R1.in=g(f(X.x),Y.y) R1 R.syn=g(f(X.x),Y.y)

R1.syn=R1.in

Page 45: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Eliminating Left Recursion from SDT

• A translation scheme with a left recursive grammar.

E → E1 + T { E.val = E1.val + T.val }E → E1 - T { E.val = E1.val - T.val }E → T { E.val = T.val }T → T1 * F { T.val = T1.val * F.val }T → F { T.val = F.val }F → ( E ) { F.val = E.val }F → digit { F.val = digit.lexval }

• When we eliminate the left recursion from the grammar (to get a suitable grammar for the top-down parsing) we also have to change semantic actions

Page 46: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Eliminating Left Recursion Example

inherited attribute synthesized attribute

E → T { A.in=T.val } A { E.val=A.syn }

A → + T { A1.in=A.in+T.val } A1 { A.syn = A1.syn }

A → - T { A1.in=A.in-T.val } A1 { A.syn = A1.syn }

A → { A.syn = A.in }

T → F { B.in=F.val } B { T.val=B.syn }

B → * F { B1.in=B.in*F.val } B1 { B.syn = B1.syn}

B → { B.syn = B.in }

F → ( E ) { F.val = E.val }

F → digit { F.val = digit.lexval }

Page 47: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Intermediate Code Generation with SDT

E → T { A.in=T.loc } A { E.loc=A.loc }

A → + T { A1.in=newtemp(); emit(add,A.in,T.loc,A1.in) }

A1 { A.loc = A1.loc}

A → { A.loc = A.in }

T → F { B.in=F.loc } B { T.loc=B.loc }

B → * F { B1.in=newtemp(); emit(mult,B.in,F.loc,B1.in) }

B1 { B.loc = B1.loc}

B → { B.loc = B.in }

F → ( E ) { F.loc = E.loc }

F → id { F.loc = id.name }

Page 48: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Intermediate Code Generation with Predictive Parsing

procedure E(char **E.loc) {

char *A.in, *T.loc, *A.loc;

call T(&T.loc); A.in=T.loc;

call A(A.in,&A.loc); *E.loc=A.loc;

}

procedure A(char *A.in, char **A.loc) {

if (currtok is +) {

char *A1.in, *T.loc, *A1.loc;

consume(+); call T(&T.loc); A1.in=newtemp();

emit(“add”,A.in,T.loc,A1.in);

call A(A1.in,&A1.loc); *A.loc=A1.loc;

}

else { *A.loc = A.in }

}

E → T { A.in=T.loc } A { E.loc=A.loc }

A → + T { A1.in=newtemp(); emit(add,A.in,T.loc,A1.in) }

A1 { A.loc = A1.loc}

A → { A.loc = A.in }

Page 49: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Intermediate Code Generation with Predictive Parsing

procedure T(char **T.loc) {

char *B.in, *F.loc, *B.loc;

call F(&F.loc); B.in=F.loc;

call B(B.in,&B.loc); *T.loc=B.loc;

}

procedure B(char *B.in, char **B.loc) {

if (currtok is *) {

char *B1.in, *F.loc, *B1.loc;

consume(+); call F(&F.loc); B1.in=newtemp(); emit(“mult”,B.in,F.loc,B1.in);

call B(B1.in,&B1.loc); B.loc=B1.loc;

}

else { *B.loc = B.in }

}

procedure F(char **F.loc) {

if (currtok is “(“) { char *E.loc; consume(“(“); call E(&E.loc); consume(“)”);

*F.loc=E.loc }

else { char *id.name; consume(id,&id.name); *F.loc=id.name }

}

Page 50: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Bottom-Up Evaluation of L-Attributed SDD

• In bottom-up evaluation, the semantic actions are evaluated during the reductions.

• During the bottom-up evaluation of S-attributed definitions, we have a parallel stack to hold synthesized attributes.

• Problem: Where do we hold inherited attributes?

• Solution: – Convert the grammar to guarantee the followings:

• All embedding semantic actions in the translation scheme is moved to the end of the production rules.

• All inherited attributes is copied into the synthesized attributes.

Page 51: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Moving Embedding Semantic Actions

• Translation rules:

1. Remove an embedding semantic action Si,

and put a new non-terminal Mi in the place of

the semantic action.

2. Put that semantic action Si to the end of a

new production rule Mi for the new non-

terminal Mi.

• The semantic action Si will be evaluated

when this new production rule is reduced.

• The evaluation order of the semantic rules

are not changed by this transformation.

Page 52: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Removing Embedding Semantic Actions

Example1

A {S1} X1 {S2} X2 ... {Sn} Xn

remove embedding semantic actions

A M1 X1 M2 X2 ... Mn Xn

M1 {S1}

M2 {S2}

Mn {Sn}

Page 53: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Removing Embedding Semantic Actions

Example2E → T RR → + T { print(“+”) } R1

R → T → id { print(id.name) }

remove embedding semantic actions

E → T RR → + T M R1

R → T → id { print(id.name) }M → { print(“+”) }

Page 54: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Copying Inherited Attributes

• For every production rule A X1 X2 ... Xn ,

– Introduce new marker non-terminals M1,M2,...,Mn

– Replace the production rule with A M1 X1 M2

X2 ... Mn Xn

– Synthesized attributes of Xi will not be changed.

– Inherited attributes of Xi will be copied into the

synthesized attribute of Mi by the new semantic

action added to the end of the new production

rule Mi.

– The inherited attribute of Xi can be found in the

synthesized attribute of Mi (which is immediately

available in the stack).

Page 55: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Copying Inherited Attributes Cont’d

A {B.i=f1(...)} B {C.i=f2(...)} C {A.s= f3(...)}

A {M1.i=f1(...)} M1 {B.i=M1.s} B {M2.i=f2(...)} M2 {C.i=M2.s} C {A.s= f3(...)}

M1 {M1.s=M1.i}

M2 {M2.s=M2.i}

Page 56: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Translation with Inherited Attributes

S {A.i=1} A {S.s=k(A.i,A.s)}

A {B.i=f(A.i)} B {C.i=g(A.i,B.i,B.s)} C {A.s= h(A.i,B.i,B.s,C.i,C.s)}

B b {B.s=m(B.i,b.s)}

C c {C.s=n(C.i,c.s)}

S {M1.i=1} M1 {A.i=M1.s} A {S.s=k(M1.s,A.s)}

A {M2.i=f(A.i)} M2 {B.i=M2.s} B

{M3.i=g(A.i,M2.s,B.s)} M3 {C.i=M3.s} C {A.s= h(A.i, M2.s,B.s, M3.s,C.s)}

B b {B.s=m(B.i,b.s)}

C c {C.s=n(C.i,c.s)}

M1 {M1.s=M1.i}

M2 {M2.s=M2.i}

M3 {M3.s=M3.i}

Solution?

Page 57: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Actual Translation Scheme

S {M1.i=1} M1 {A.i=M1.s} A {S.s=k(M1.s,A.s)}

A {M2.i=f(A.i)} M2 {B.i=M2.s} B {M3.i=g(A.i,M2.s,B.s)} M3 {C.i=M3.s} C {A.s= h(A.i, M2.s,B.s,

M3.s,C.s)}

B b {B.s=m(B.i,b.s)}

C c {C.s=n(C.i,c.s)}

M1 {M1.s= M1.i}

M2 {M2.s=M2.i}

M3 {M3.s=M3.i}

S M1 A { s[ntop]=k(s[top-1],s[top]) }

M1 { s[ntop]=1 }

A M2 B M3 C { s[ntop]=h(s[top-4],s[top-3],s[top-2],s[top-1],s[top]) }

M2 { s[ntop]=f(s[top]) }

M3 { s[ntop]=g(s[top-2],s[top-1],s[top])}

B b { s[ntop]=m(s[top-1],s[top]) }

C c { s[ntop]=n(s[top-1],s[top]) }

Page 58: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Evaluation of AttributesS

S.s=k(1,h(..))

A.i=1

A

A.s=h(1,f(1),m(..),g(..),n(..))

B.i=f(1) C.i=g(1,f(1),m(..))

B C

B.s=m(f(1),b.s) C.s=n(g(..),c.s)

b c

Page 59: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Evaluation of Attributes Cont’d

stack input l-attribute stackbc$

M1 bc$ 1

M1 M2 bc$ 1 f(1)

M1 M2 b c$ 1 f(1) b.s

M1 M2 B c$ 1 f(1) m(f(1),b.s)

M1 M2 B M3 c$ 1 f(1) m(f(1),b.s) g(1,f(1),m(f(1),b.s))

M1 M2 B M3 c $ 1 f(1) m(f(1),b.s) g(1,f(1),m(f(1),b.s)) c.s

M1 M2 B M3 C $ 1 f(1) m(f(1),b.s) g(1,f(1),m(f(1),b.s)) n(g(..),c.s)

M1 A $ 1 h(f(1),m(..),g(..),n(..))

S $ k(1,h(..))

Page 60: CS416 Compiler Design - SJTUjiangli/teaching/CS308/CS308-slides... · 2019. 4. 1. · Syntax-Directed Translation Cont’d • Syntax-Directed Definitions: –associate a production

Compiler Principles

Test Yourself

• Textbook page 337, Exercise 5.4.3

Solution:

B 1 { R.in = 1 } R { B.val = R.syn}

R 0 { R1.in = 2 * R.in } R1 { R.syn = R1.syn }

R 1 { R1.in = 2 * R.in + 1 } R1 { R.syn = R1.syn }

R { R.syn = R.in }