Lect 11
Lect 11
DT L L.in=T.type
Tint T.type=integer
| real T.type=real
LL1, id L1.in=L.in; id.type=L.in
| id id.type=L.in
D In practice, we don’t
type=real need to do all this
T L with variable decla-
type=real in=real rations! We can use a
symbol table (for
L id more see lecture 11).
real in=real
, type=real There, all we need to
do is to add a new
What kind of id entry into a symbol
attribute is “type”? type=real table. E.g.:
What Apr
about “in”?
26, 2023 COMP36512 Lecture 11 addtype(id.type, L.in)
9
Another Example
NumberSign List List.pos=0;
(signed binary numbers)
If Sign.neg then Number.val=-List.val
else Number.val=List.val
Sign + Sign.neg=false
| – Sign.neg=true
ListList1 Bit List1.pos=List.pos+1; Bit.pos=List.pos;
List.val= List1.val+Bit.val
| Bit Bit.pos=List.pos; List.val=Bit.val
Bit 0 Bit.val=0
Num
| 1 Bit.val=2Bit.pos
val:-5
Sign List •Number and Sign have
pos:0
neg:T val:5 one attribute each. List and Bit
List Bit have two attributes each.
pos:1 pos:0
val:4 val:1 •Val and neg are synthesised
List Bit attributes; pos is an inherited
- pos:2 pos:1
val:4 val:0 attribute.
•What about an evaluation
Bit 1 order? Knuth suggested a data-
pos:2
val:4 0 flow model of evaluation with
independent attributes first.
Apr 26, 2023 1 COMP36512 Lecture 11 Implies a dependency graph!
10
Attribute dependence graph
If an attribute at one node depends on an attribute of another node then the
former must be evaluated before the latter.
Attribute Dependence Graph:
• Nodes represent attributes; edges represent the flow of values.
• Graph is specific to the parse tree (can be built alongside and its size is
related to the size of the parse tree).
• Evaluation order:
– Parse tree methods: use a topological sort (any ordering of the nodes such that
edges go only from the earlier nodes to the later nodes: sort the graph, find
independent values, then walk along graph edges): cyclic graph fails!
– Rule-based methods: the order is statically predetermined.
– Oblivious methods: use a convenient approach independent of semantic rules.
• Problem: how to deal with cycles.
• Another issue: complex dependencies.