Professional Documents
Culture Documents
Attribute Grammars
CSE 6341
Grammars
Regular Languages
*w
w1 w2 wn; denoted w1
n
String derivation
Production: x y
G = (N, T, S, P)
Empty string
* - set of all strings over (incl. )
+ - set of all non-empty strings over
S Ab and A a | Aa
CSE 6341
w,h,i,l,e,(,a,1,5,>,b,b,),d,o,
keyword[while],leftparen,id[a15],op[>],
id[bb],rightparen,keyword[do],
Context-Free Languages
Pattern matching
Each production: A w
A N, w (N T)*
CSE 6341
BNF Example
Derivation Tree
| <exp> := <exp>
| <id> ( <exprs> )
<term>
(
<expr>
One derivation:
<term>
<term>
z
Another derivation:
<expr> )
+
10
<expr>
+
CSE 6341
<expr>
(x+y)+z
CSE 6341
<term>
y
x
CSE 6341
11
Many more
CSE 6341
12
Ambiguous Grammars
Elimination of Ambiguity
13
CSE 6341
id(p34)
<formals>
. . . . . .
parse tree
)
<body>
. . . .
<procedure> [p34]
<formals>
AST
15
Generalization of context-free
grammars
17
16
Attribute Grammars
Implementation of a parser in a
compiler
<body>
CSE 6341
14
18
Example
BNF
Attributes
19
CSE 6341
Example: Alternative
BNF
Example
| a<A>2
<A>.Na := 1 + <A>2.Na
Attributes
CSE 6341
Conditions
<start> ::= <A><B><C>
Cond: <A>.Na = <B>.Nb = <C>.Nc
21
CSE 6341
Example: Alternative
<start> Cond:true
| a<A>2
<A>.N := 1 + <A>2.N
Conditions
<start> ::= <A><B><C>
Cond: <A>.N = <B>.N = <C>.N
CSE 6341
22
Parse Tree
<A> ::= a
<A>.N := 1
20
<A> Na:1 b
a
23
Nb:2<B>
Nb:1 <B>
b
CSE 6341
Nc:2<C>
c Nc:1<C>
c
24
Production X ::= Y1 Y2 Yn
25
CSE 6341
Context-free grammar
D
0
28
0
CSE 6341
CSE 6341
Add attributes
D
1
27
26
29
<B>.pos := 1
<B>.val := <D>.val
<D>.pow := 0
<B>1 ::= <D><B>2
<B>1.pos := <B>2.pos + 1
<B>1.val := <B>2.val + <D>.val
<D>.pow := <B>2.pos
<D> ::= 0
<D>.val := 0
<D> ::= 1
<D>.pow
<D>.val := 2
CSE 6341
30
B pos:4 val:10
B pos:3 val:2
pow:3 D
val:8
D pow:2
val:0
B pos:2 val:2
D pow:1
val:2
B pos:1 val:0
D pow:0
val:0
<I> ::= id
Attributes
0
CSE 6341
31
CSE 6341
Attribute Grammar
Attribute Grammar
environment;
33
CSE 6341
Example
I
x
env:nil val:2
in
1
env:nil
val:1
let
34
Another Example
32
let x = (x+1) in
E
( E + E )
env:x=1 I
val:1
CSE 6341
I
x
(x+2)
end
env:x=1
val:2
end
end
env:x=1
val:1
35
CSE 6341
36
L = { anbncn | n > 0 }
CSE 6341
Want left-associativity:
ambiguous
<A>1.x := <A>2.x
<A>1.x depends on <A>2.x
A1.x A2.x dependence edge
39
38
CSE 6341
40
<B>.pos := 1
<B>.val := <D>.val
<D>.pow := 0
<B>1 ::= <D><B>2
<B>1.pos := <B>2.pos + 1
<B>1.val := <B>2.val + <D>.val
<D>.pow := <B>2.pos
<D> ::= 0
<D>.val := 0
<D> ::= 1
<D>.pow
<D>.val := 2
<S> ::= a | b
<S>.len := 1
<S>1::= <S>2<S>3 <S>1.len := <S>2.len + <S>3.len
Cond: <S>2.len = 1
CSE 6341
37
L = { wcw | w {a,b}* }
41
CSE 6341
42
B1 pos val
D1 pow val
1
B2 pos val
D2 pow val
B3 pos val
D3 pow val
1
B4 pos val
B2.pos, D2.pow, D2.val, B2.val,
D4 pow val
0
CSE 6341
CSE 6341
Cycles
44
45
CSE 6341
46
Problem
| <block>
| ...
...
CSE 6341
47
48
Parse Tree
Data Structure
prog
block
decl
+
i
CSE 6341
Type Checking
CSE 6341
50
[
{(x",INT), ("i",INT)},
{("i",BOOL), ("j",INT)}
]
bottom of stack
CSE 6341
49
int i
51
CSE 6341
52
| <stmt> ; <stmtlist>2
<stmt>.alltbl := <stmtlist>1.alltbl
<stmtlist>2.alltbl := <stmtlist>1.alltbl
CSE 6341
53
CSE 6341
54
Declarations
Declarations
55
CSE 6341
Statements
Statements
the last
type on
the stack
Cond:
typeof(id.lexval,<assign>.alltbl) = INT
| id := <boolexp>
<boolexp>.alltbl := <assign>.alltbl
Cond:
typeof(id.lexval,<assign>.allbtl) = BOOL
57
Expressions
CSE 6341
58
Declarations
56
CSE 6341
60
10
Declarations
<declist>1 ::= <decl>
<declist>1.tbl := <decl>.tbl
<decl>.alltbl := <declist>1.alltbl
| <decl> ; <declist>2
<declist>1.tbl :=
<declist>.alltbl :=
<decl>.tbl <declist>2.tbl
push(<declist>.tbl, <block>.alltbl)
<decl>.alltbl := <declist>1.alltbl
<declist>2.alltbl := <declist>1.alltbl
Cond:
ids(<decl>.tbl) ids(<declist>2.tbl) =
61
CSE 6341
Declarations Again
Function Call
<actualslist> is
a list of <intexp>
and <boolexp>
65
63
62
CSE 6341
64
CSE 6341
66
11
Assembly Language
CSE 6341
67
CSE 6341
Assembly Language
Example:
<ae>1 ::= <ae>2 + <ae>3
<ae>1.code :=
<
<ae>2.code,
leaves its result
in the accumulator
"STO T1",
<ae>3.code,
"ADD T1"
>
69
70
Problems
temp
var
CSE 6341
CSE 6341
68
inherited temp
71
72
12
| <assign>
<c>.code := <assign>.code
<c>.labout := <c>.labin
73
CSE 6341
CSE 6341
75
<ae>.temp := 1
<assign>.code := concat(
<ae>.code,
("STO" id.lexval))
For recursive languages, we need to
access variables on the run-time call
stack (multiple instances of id.lexval)
CSE 6341
76
CSE 6341
74
77
CSE 6341
78
13
Source program
if x = 42 then
if a = b then
y := 1
else
y := 2
else
y := 3
CSE 6341
79
CSE 6341
Inner If Statement
# code for x = 42
BZ L1
# code for a = b
BZ L3
# code for y := 1
BR L4
L3: NOP
# code for y := 2
L4: NOP
BR L2
L1: NOP
# code for y := 3
L2: NOP
CSE 6341
80
81
82
Useful for expressing arbitrary cyclefree walks over context-free parse trees
CSE 6341
83
84
14