



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!




Spring 09 CSCI 4430, A Milanova/BG Ryder 1
Spring 09 CSCI 4430, A Milanova/BG Ryder 2
Static (most modern languages) Dynamic (old Lisp)
Static Dynamic
3
program a, b, c: integer; procedure P c: integer; procedure S c, d: integer; procedure R โฆ end R; R(); end S; R(); S(); end P; procedure R a: integer; = a, b, c; end R; โฆ; P(); โฆ end program
a b c main.P
c main.R
a
Static Scoping: a bound to R.a, b to main.b, c to main.c
Dynamic Scoping: a bound to R.a, b to main.b, c to P.c
Spring 09 CSCI 4430, A Milanova/BG Ryder 4
Spring 09 CSCI 4430, A Milanova/BG Ryder 5
A (^) a
B (^) acf C (^) e
D (^) ag E (^) bd
Virtual function tables (VFTs) Capital characters denote classes, lowercase characters message selectors, and numbers method addresses
A (^0)
a
B (^3)
a 1
c 2
f
C (^0)
a
E (^0)
a 4
e 7
b d 8
D (^5)
a 1
c 2
f g 6
load [object_reg+#VFTOffset],table_reg load [table_reg+#selectorOffset],method_reg call method_reg
Extra instructions: cost extra!
4
e
6
Spring 09 CSCI 4430, A Milanova/BG Ryder 7
S-attributed grammars L-attributed grammars Attribute flow
Spring 09 CSCI 4430, A Milanova/BG Ryder 8
Spring 09 CSCI 4430, A Milanova/BG Ryder 9
E.g., determine static types of expressions, detect certain errors
E.g., could z=x/y cause division by zero, could expression a[i] index out of array bounds, etc.?
Spring 09 CSCI 4430, A Milanova/BG Ryder 10
Detect as many errors as possible Type inference and type checking
Detect errors by performing dynamic checks Tradeoff: dynamic checks slow program execution PLs differ greatly in the amount of run-time checks C++: none. Java: many (we will see examples).
Spring 09 CSCI 4430, A Milanova/BG Ryder 11
Spring 09 CSCI 4430, A Milanova/BG Ryder 12
Spring 09 CSCI 4430, A Milanova/BG Ryder 19
Intuitively, S.val holds the value of the expression represented by subtree rooted at symbol S. Fresh attributes are associated with every node in the parse tree
E.g., E โ E 1 +T E.val := E 1 .val+T.val
Spring 09 CSCI 4430, A Milanova/BG Ryder 20
S
num
num (^) 3
num (^) (^5) num 2
4 F 3
T 3
T 15 5 F 2
T 2
15
F 4
T 8
E 23
S โ E print( E.val) E โ E 1 +T E.val := E 1 .val+T.val E โ T E.val := T.val T โ T 1 F T.val := T 1 .valF.val T โ F T.val := F.val F โ num F.val := num. val
Spring 09 CSCI 4430, A Milanova/BG Ryder 21
Define semantic analyses Basis for translation into intermediate code
Spring 09 CSCI 4430, A Milanova/BG Ryder 22
E
E T
T
T F F
num:
T F
F
num:
num:
num:
Parse tree for 35+24 Abstract syntax tree: +
num: num:
num:
num:
How do we construct syntax trees for expressions?
Spring 09 CSCI 4430, A Milanova/BG Ryder 23
Production Semantic Rule E โ E 1 + T E.nptr := mknode(โ + โ, E 1 .nptr, T.nptr) E โ T E.nptr := T.nptr T โ T 1 ***** F T.nptr := mknode(โ*โ, T 1 .nptr, F.nptr) T โ F T.nptr := F.nptr F โ num F.nptr := mkleaf(โnumโ, num. val)
Function mknode(op,left,right) creates an operator node with label op, and two fields containing pointers left and right Function mkleaf(num,num.val) creates a leaf node with label num, and a field containing the value of the number
Attribute โnodepointerโ
Spring 09 CSCI 4430, A Milanova/BG Ryder 24
S
S โ E E โ E+T | T T โ T*F | F F โ num
num, 3
F
T F
num, 5
E
num, 2
F
T
num, 4
F
T *,^ ,
T *,^ ,
E +,^ ,
Spring 09 CSCI 4430, A Milanova/BG Ryder 25
Values must be computed โnormallyโ, i.e., 5 โ 3 โ 2 will be evaluated as (5-3)-2 and not as 5-(3-2)
E โ TEโ Eโ โ - TEโ | ฮต T โ FTโ Tโ โ / FTโ | ฮต F โ num
Spring 09 CSCI 4430, A Milanova/BG Ryder 26
E โ TEโ (1) Eโ.sub := T.val (2) E.val := Eโ.val Eโ โ - TEโ 1 (1) Eโ 1 .sub:= Eโ.sub - T.val (2) Eโ.val := Eโ 1 .val Eโ โ ฮต (1) Eโ.val := Eโ.sub
T โ FTโ (1) Tโ.sub := F.val (2) T.val := Tโ.val Tโ โ / FTโ 1 (1) Tโ 1 .sub:= Tโ.sub / F.val (2) Tโ.val := Tโ 1 .val Tโ โ ฮต (1) Tโ.val := Tโ.sub
F โ num (1) F.val := num. val (synthesized)
E โ TEโ Eโ โ - TEโ | ฮต T โ FTโ Tโ โ / FTโ | ฮต F โ num
Spring 09 CSCI 4430, A Milanova/BG Ryder 27
Eโ
T Eโ 1
E.g., 25 - 34 - 3 - 6 Eโ holds subtotal 13 (for 25 - 34, computed so far) T holds value 3 (i.e., the value of next in the sum) Eโ 1 gets subtotal 10 (for 25 - 3*4 - 3 computed as 13 - 3) Passed down the tree of Eโ 1 to next Eโ on chain Eventually, we hit Eโ โ ฮต and value gets subtotal 4 Value 4 is passed back up
โฆ
โฆ (^) โฆ
3
13
10
Attribute Eโ 1 .sub: computed based on parent Eโ and sibling T ( Eโ.sub - T.val)
4
4
Eโ
Spring 09 CSCI 4430, A Milanova/BG Ryder 28
E.g., T in E โ TEโ : value of T is passed right to Eโ E.g., T in Eโ โ - TEโ 1 : value of T is passed right to Eโ 1 as right operand when computing the subtotal of Eโ 1
Spring 09 CSCI 4430, A Milanova/BG Ryder 29
E.g., in Eโ โโโโ - TEโ 1 the subtotal of Eโ 1 is computed by subtracting the value of T from the subtotal of Eโ; the subtotal of Eโ 1 is carried down the subtree of Eโ 1 E.g., in Tโ โโโโ / FTโ 1 the same happens
Spring 09 CSCI 4430, A Milanova/BG Ryder 30
In order to compute value โnormallyโ we needed sub (inherited attributes).