G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 ·...

26
G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation Mohamed Zahran (aka Z) [email protected]

Transcript of G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 ·...

Page 1: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

G22.2130-001

Compiler Construction

Lecture 8: Syntax-Directed Translation

Mohamed Zahran (aka Z)

[email protected]

Page 2: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

A Step-Back

Page 3: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

A Step-Back

•Strings•Regular expressions•Tokens•Transition diagrams•Finite Automata

Chapter 3

Page 4: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

A Step-Back

Chapter 4

•Grammars•Derivations•Parse-trees•Top-down parsing (LL)•Bottom-up paring (LR, SLR,LALR)

Page 5: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

We Need Some Tools

• To help in semantic analysis

• To help in intermediate code generation

• Two such tools– Semantic rules (Syntax-Directed Definitions)

– Semantic actions (Syntax Directed Translations)

Page 6: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Syntax-Directed Definitions

• Context-free grammar

• With attributes and rules to calculate the attributes

Page 7: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Two Types of Attributes

Page 8: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Two Types of Attributes

Synthesized Attributes

Attribute of the node is defined in terms of:•Attribute values at children of the node•Attribute value at node itself

SDD involving onlysynthesized attributesis called S-attributed

Page 9: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Two Types of Attributes

Inherited Attributes

Attribute of the node is defined in terms of:•Attribute values at parent of the node•Attribute values at siblings•Attribute value at node itself

Page 10: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

3*5+4n

A parse tree showing the valuesof its attributes is calledannotated parse tree.

Page 11: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Example

Give the annotated parse tree of (3+4)*(5+6)n

Page 12: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

When Are Inherited Attributes Useful?

3*5

Page 13: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Example

Give annotated parse-trees for:int a, b, c

Page 14: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Evaluation Orders of SDDs

• Annotated parse tree shows attribute values

• Dependency graph helps us determine how those values are computed

Page 15: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Topological Order

Page 16: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

S-Attributed Definitions

• Every attribute is synthesized

• We can evaluate its attribute in any bottom-up order of the nodes of the parse tree

(e.g. postorder traversal -> LR parser).

Page 17: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

L-Attributed Definitions

• Dependency graph edges can only go from left to right– i.e. use attributes from above or from the

left

Page 18: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Example

Page 19: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Syntax-Directed Translations

• Context-free grammar• Can implement SDDs• Program fragments embedded within

production bodies– called semantic rules– Can appear anywhere within the production

body• Steps are usually as follows

– Build parse tree– perform actions as you traverse left-to-right,

depth-first (preorder)

Page 20: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Example

Page 21: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Implementing L-Attributed SDDs

• L-attributed definitions can be used in many translation applications

• Several methods of implementation– Build parse tree and annotate

– Build parse tree, add actions, execute in preorder

– Recursive descent

Page 22: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Recursive Descent

• Function A for each nonterminal A

• Arguments of A are inherited attributes of nonterminal A

• Return value of A is the collection of synthesized attributes of A

Page 23: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

ExampleFor that rule we want to generate labels:L1: CL2: S1

Page 24: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

ExampleFor that rule we want to generate labels:L1: CL2: S1

Page 25: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

ExampleFor that rule we want to generate labels:L1: CL2: S1

Page 26: G22.2130-001 Compiler Construction Lecture 8: Syntax-Directed Translation · 2010-11-03 · •Program fragments embedded within production bodies –called semantic rules –Can

Reading

• Skim: 5.3, 5.4.3, 5.4.4, 5.4.5, 5.5.3, and 5.5.4

• Read the rest