


















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!



















CMSC430 Spring 2007 (^1)
There is a level of correctness that is deeper than grammar
fie(a,b,c,d) int a, b, c, d; { … }
fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i,“ab”,j, k); k = f * i + j; h = g[17]; printf(“<%s,%s>.\n”, p,q); p = 10; }
What is wrong with this program? (let me count the ways …)
CMSC430 Spring 2007 (^2)
There is a level of correctness that is deeper than grammar
To generate code, we need to understand its meaning!
fie(a,b,c,d) int a, b, c, d; { … }
fee() { int f[3],g[0], h, i, j, k; char *p; fie(h,i,“ab”,j, k); k = f * i + j; h = g[17]; printf(“<%s,%s>.\n”, p,q); p = 10; }
What is wrong with this program? (let me count the ways …)
All of these are “deeper than syntax”
CMSC430 Spring 2007 (^3)
To generate code, the compiler needs to answer many questions
CMSC430 Spring 2007 (^4)
These questions are part of context-sensitive analysis
How can we answer these questions?
Context-sensitive grammars? Syntax directed translation using attribute grammars? (attributed grammars?)
Symbol tables
CMSC430 Spring 2007 (^7)
What is an attribute grammar?
This grammar describes signed binary numbers We would like to augment it with rules that compute the decimal value of each valid input string
CMSC430 Spring 2007 (^8)
We will use these two throughout the lecture
Number → Sign List → – List → – Bit → – 1
Number
List
Bit
1
Sign
For “–1” Number → Sign List → Sign List Bit → Sign List 1 → Sign List Bit 1 → Sign List 1 1 → Sign Bit 0 1 → Sign 1 0 1 → – 101
Number
Sign List
1
List
Bit
0
List
Bit
1
For “–101”
CMSC430 Spring 2007 (^9)
Add rules to compute the decimal value of a signed binary number
Productions Attribution Rules Number → Sign List List.pos ← 0 Number.val^ ← If Sign.neg then – List.val else List.val Sign (^) → + (^) Sign.neg ← false | – Sign.neg ← true List 0 → List 1 Bit List 1 .pos ← List 0 .pos + 1 Bit.pos ← List 0 .pos List 0 .val ← List 1 .val + Bit.val | Bit (^) Bit.pos ← List.pos List.val ← Bit.val Bit (^) → 0 Bit.val ← 0 | (^1) Bit.val ← 2 Bit.pos
Symbol Attributes Number val Sign neg List pos, val Bit pos, val
CMSC430 Spring 2007 (^10)
Number
List
Bit
1
Sign
neg ← true
Bit.pos ← 0 Bit.val ← 2 Bit.pos ≡ 1
List.pos ← 0 List.val ← Bit.val ≡ 1
For “–1” (^) Number.val ← – List.val ≡ –1 One possible evaluation order: 1 List.pos 2 Sign.neg 3 Bit.pos 4 Bit.val 5 List.val 6 Number.val Other orders are possible
Knuth suggested a data-flow model for evaluation
Evaluation order must be consistent with the attribute dependence graph
CMSC430 Spring 2007 (^13)
Attribute grammars can specify context-sensitive actions
Synthesized Attributes
Good match to LR parsing
Inherited Attributes
CMSC430 Spring 2007 (^14)
Dynamic, dependence-based methods
Rule-based methods (treewalk)
Oblivious methods (passes, dataflow)
Imagine a diminutive person who knows how to evaluate attributed trees ...
CMSC430 Spring 2007 (^15)
Number
Sign List
List Bit
List Bit
Bit
1
0
1 For “–101”
CMSC430 Spring 2007 (^16)
Number
Sign List
List Bit
List Bit
Bit
1
0
1
pos: val:
pos: val:
pos: val:
pos: val:
pos: val:
pos: 0 val:
val:
neg:
For “–101”
CMSC430 Spring 2007 (^19)
Number
Sign List
List Bit
List Bit
Bit
1
0
1
pos: 1 val: 0
pos: 0 val: 1
pos: 2 val: 4
pos: 2 val: 4
pos: 1 val: 4
pos: 0 val: 5
val: –
neg: true
For “–101”
Synthesized attributes
CMSC430 Spring 2007 (^20)
Number
Sign List
List Bit
List Bit
Bit
1
0
1
pos: 1 val: 0
pos: 0 val: 1
pos: 2 val: 4
pos: 2 val: 4
pos: 1 val: 4
pos: 0 val: 5
val: –
neg: true
For “–101”
& then peel away the parse tree ...
If we show the computation ...
CMSC430 Spring 2007 (^21)
1
0
1
pos: 1 val: 0
pos: 0 val: 1
pos: 2 val: 4
pos: 2 val: 4
pos: 1 val: 4
pos: 0 val: 5
val: –
neg: true
For “–101”
All that is left is the attribute dependence graph. This succinctly represents the flow of values in the problem instance. The dynamic methods sort this graph to find independent values, then work along graph edges. The rule-based methods try to discover “good” orders by analyzing the rules. The oblivious methods ignore the structure of this graph.
The dependence graph must be acyclic
CMSC430 Spring 2007 (^22)
We can only evaluate acyclic instances
Many evaluation methods discover circularity dynamically
⇒ Bad property for a compiler to have
CMSC430 Spring 2007 (^25)
Adding attribution rules
Block 0 → Block 1 Assign Block 0 .cost ← Block 1 .cost + Assign.cost ⏐ Assign^ Block 0 .cost ← Assign.cost Assign (^) → Ident = Expr ; (^) Assign.cost ← COST (store) + Expr.cost Expr 0 → Expr 1 + Term Expr 0 .cost ← Expr 1 .cost + COST (add) + Term.cost ⏐ Expr 1 – Term Expr 0 .cost ← Expr 1 .cost + COST (add) + Term.cost ⏐ Term Expr 0 .cost ← Term.cost Term (^0) → Term 1 * Factor (^) Term 0 .cost ← Term 1 .cost + COST (mult ) + Factor.cost ⏐ Term^1 / Factor^ Term^0 .cost^ ← Term^1 .cost +^ COST^ (div) + Factor.cost ⏐ Factor Term 0 .cost ← Factor.cost Factor (^) → ( Expr ) (^) Factor.cost ← Expr.cost ⏐ Number^ Factor.cost ← COST (loadi) ⏐ Identifier^ Factor.cost^ ←^ COST^ (load)
All the attributes are synthesized!
CMSC430 Spring 2007 (^26)
Properties of the example grammar
Good fit to bottom-up, shift/reduce parser
What about an improvement?
CMSC430 Spring 2007 (^27)
Adding load tracking
Factor →^ ( Expr ) Factor. cost ← Expr.co st ; Expr.B efore ← Factor. Before ; Factor. After ← Expr.Af ter ⏐ Number Factor. cost ← COST (loadi) ; Factor. After ← Factor. Before ⏐ (^) Identifi er If (Identifi er.name ∉ Factor. Before) then Factor. cost ← COST (load); Factor. After ← Factor. Before ∪ Identifi er.name else Factor. cost ← 0 Factor. After ← Factor. Before
This looks more complex!
CMSC430 Spring 2007 (^28)
A sample production
These copy rules multiply rapidly
Each creates an instance of the set
Lots of work, lots of space, lots of rules to write
Expr 0 →^ Expr 1 + T erm (^) Expr 0 .cost ← Expr 1 .cost + COST (add)
CMSC430 Spring 2007 (^31)
Tracking loads
Serialized evaluation into execution order
Finite register set
Why is one change hard and the other easy?
CMSC430 Spring 2007 (^32)
The Problems
Need copies of attributes Can use pointers, for even more cognitive overhead
Must build the parse tree Must search tree for answers
CMSC430 Spring 2007 (^33)
If you gave this problem to a chief programmer
Field in table for loaded/not loaded state
Clean, efficient implementation Good techniques for implementing the table (hashing) When its done, information is in the table! (no navigation) Cures most of the problems
Do we care?
CMSC430 Spring 2007 (^34)
Includes ability to do tasteless & bad things
To make this work
Typically, one attribute passed through parser + arbitrary code (structures, globals, statics, …) Yacc introduced $$, $1, $2, … $n , left to right A ::= B C {$$ = concat($1,$2)}
Fits nicely into LR(1) parsing algorithm
CMSC430 Spring 2007 (^37)
analysis
Advantages
Disadvantages
Most parser generators support a yacc-like notation
CMSC430 Spring 2007 (^38)
Enter declaration information as processed At end of declaration syntax, do some post processing Use table to check errors as parsing progresses → Note: this assumes table is global
Define before use → lookup on reference Dimension, type, ... → check as encountered Type conformability of expression → bottom-up walk Procedure interfaces are harder → Build a representation for parameter list & types → Create list of sites to check → Either check offline, or handle the cases for arbitrary orderings
CMSC430 Spring 2007 (^39)
Relationship between practice and attribute grammars
Similarities
Differences
CMSC430 Spring 2007 (^40)
Left to right only Bottom up only
Declarations before uses Context information cannot be passed down → How do you know what rule you are called from within? → Example: cannot pass bit position from right down
Could you use globals? → In this case we could get the position from the left, which is not much help (and it requires initialization)
List → List Bit : $$ = 2*$1 + $