Docsity

Syntax Directed Translation - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Syntax Directed Translation - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Chapter 5: Syntax Directed Translation

Overview

 Review Supporting Concepts

 (Extended) Backus Naur Form  Parse Tree and Schedule

 Explore Basic Concepts/Examples of Attribute Grammars  Synthesized and Inherited Attributes  Actions as Direct Effect of Parsing

 Examine more Complex Examples

 Attribute Grammars and Yacc – Jump to Slide Set

 Constructing Syntax Trees During Parsing Translation is Two-Pass:  First Pass: Construct Tree using Attribute Grammar  Second Pass: Evaluate Tree (Perform Translation)

 Concluding Remarks

Optional and Alternative Sections

E → id ( A ) → id A → integer → id

E → id [ ( A ) ] A → integer → id

Optional Part!

E → id [ ( A ) ] A → { integer | id }

E → id [ ( A ) ] A → integer → id

Simplifying for Alternatives

Kleene Closure

 Simplifies Grammar by Eliminating Epsilon Rules

E → id [ ( [ Args ] ) ] Args → E [ , Args ]*

E → id [ ( Args ) ] Args → E Rest → ε Rest → , E Rest → ε foo() foo(x) foo(x,y) foo(x,y,z) foo(w,x,y,z)

C-- EBNF Style

C-- EBNF Style

What is a Parse Tree?

 Two Options for a Parse Tree: A true physical parse tree that contains the program structure and associated relevant tokens A schedule of operations that must be performed

 Base example

y := 5; x := 10 + 3 * y

Physical Tree

 Positive

 Depicts the grammatical structure  Should be easy to create while parsing  Unambiguous  Easy to manipulate

 Negative

 Not “Operational”  Not closer to final product (code)  Compilation requires multiple passes

Schedule [a.k.a. Dependency Graph]

 Positive

 This is almost runnable code!  It give the sequence of step to follow  We bypassed the parse tree altogether (so this is lightweight)  Compilation doable in a single pass

 Negative

 Harder to manipulate  Can it always be created?  What is the connection with the grammar?

What is the Trade-Off?

 Physical Parse Tree

 Requires multiple pass for compilation  Very flexible  This is what we will use

 Schedule [Dependency Graph]

 Requires a single pass for compilation  Less flexible

 Bottom-line

 The construction of both rely on the same technique Attributed Grammars

Examples of Trees

a.b

a + b * c

a.b(x)

a.b(x)[y]

x = a + b

Tree for a Code Segment

while x<n { x = x + 1; b.foo(x); }

Action

 What is the nature of the action?

 Answer

 It depends on the production!

E → E + T

Here we know that On top of the stack we must have two operands So....

Action = a = pop(); b = pop(); c = new Addition(a,b); push(c);

What is Going On

 We synthesize the tree

 While parsing  In a bottom-up fashion

 What we need

 A stack to hold the synthesized “values”  Actions inserted in the grammar

 Issues to approach

 Where do we attach the actions in productions?  How do we attach the actions?  How can we automate the process?  It this always bottom-up?