












































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













































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
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
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)
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
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
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?
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
a.b
a + b * c
a.b(x)
a.b(x)[y]
x = a + b
while x<n { x = x + 1; b.foo(x); }
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);
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?