Professional Documents
Culture Documents
FALL 2010
Context-sensitive Analysis
or
Semantic Elaboration
Comp 412
Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.
Students enrolled in Comp 412 at Rice University have explicit permission to make copies
of these materials for their personal use.
Faculty from other educational institutions may use these materials for nonprofit
educational purposes, provided this copyright notice is preserved.
Beyond Syntax
There is a level of correctness that is deeper than grammar
fie(a,b,c,d) { What is wrong with this program?
int a, b, c, d; (let me count the ways …)
… • number of args to fie()
}
• declared g[0], used g[17]
fee() {
int f[3],g[0], h, i, j, k; • “ab” is not an int
char *p; • wrong dimension on use of f
fie(h,i,“ab”,j, k); • undeclared variable q
k = f * i + j;
h = g[17];
• 10 is not a character string
printf(“<%s,%s>.\n”,p,q);
All of these are
p = 10;
} “deeper than syntax”
Example grammar
1 Number → Sign List
This grammar describes
2 Sign → +
signed binary numbers
3 | -
4 List → List Bit We would like to augment it
5 | Bit
with rules that compute the
decimal value of each valid
6 Bit → 0
input string
7 | 1
Bit 0
– Bit
1
1
Number
Syntax Tree
Sign List
– List Bit
1
List Bit
Bit 0
1 For “–101”
Number val: –5
Attributed Syntax Tree
pos: 0
Sign neg: true List
val: 5
pos: 1 pos: 0
– List Bit
val: 4 val: 1
pos: 2 pos: 1 1
List Bit
val: 4 val: 0
pos: 2
Bit 0
val: 4
1 For “–101”
Number val: –5
Inherited Attributes
pos: 0
Sign neg: true List
val: 5
pos: 1 pos: 0
– List Bit
val: 4 val: 1
pos: 2 pos: 1 1
List Bit
val: 4 val: 0
pos: 2
Bit 0
val: 4
1 For “–101”
Number val: –5
Synthesized attributes
pos: 0
Sign neg: true List
val: 5
pos: 1 pos: 0
– List Bit
val: 4 val: 1
pos: 2 pos: 1 1
List Bit
val: 4 val: 0
pos: 2
Bit 0
val: 4
1 For “–101”
Number val: –5
More Synthesized attributes
pos: 0
Sign neg: true List
val: 5
pos: 1 pos: 0
– List Bit
val: 4 val: 1
pos: 2 pos: 1 1
List Bit
val: 4 val: 0
pos: 2
Bit 0
val: 4
1 For “–101”
Number val: –5
If we show the computation ...
pos: 0
Sign neg: true List
val: 5
& then peel away the parse tree ...
pos: 1 pos: 0
– List Bit
val: 4 val: 1
pos: 2 pos: 1 1
List Bit
val: 4 val: 0
pos: 2
Bit 0
val: 4
1 For “–101”
1 For “–101”
Comp 412, Fall 2010 SNC comes from Kennedy & Warren, POPL 1976 21
A Circular Attribute Grammar
Remember, the circularity is in the attribution rules, not the underlying CFG
1 For “–101”
1 For “–101”
1 For “–101”
1 For “–101”
1 For “–101”
1 For “–101”