Answers to January 2002 CS2121 Exam Q2
(Parts in italics are just my working out loud, that I wouldn't
expect to see in real exam answers.)
a)
float d; d = a + b + 2;
->
(float) (id,d) (;) (id,d) (=) (id,a) (+) (id,b) (+) (number,2) (;)
e.g. float is syntactically a keyword,
";" is punctuation and "=" and "+" are different operators,
whereas a, b and d are all syntactically identifiers - the semantic
information says which identifier.
b)
I would expect something better drawn than this, but otherwise
similar:
function----------
/ | \ \
void f param declaration
/ \ | | \
int a | float d
|
statement
| \
| =
| / \
| d +
| / \
| + 2
| / \
| a b
|
statement
\
=
/ \
c *
/ \
d 3
c)
i)
float -> built-in type
f -----> function void params
|
v
a -----> param int local 1 ------> var float global 1
b -----> var float global 2
c -----> var float global 3
d -----> var int local 1
int ---> built-in type
As exit f, lose information about d and (current) a,
resetting a back to previous definition,
but don't lose parameter information for f e.g.:
float -> built-in type
f -----> function void params
|
v
param int local 1
a -----> var float global 1
b -----> var float global 2
c -----> var float global 3
d
int ---> built-in type
ii)
should be links from each identifier to property entry in dictionary,
giving kind (var/param etc.) and type (int=I/float=F etc.),
which is then propagated around the tree and used for automatic
conversions, type checking etc.
function----------
/ | \ \
void f param declaration
/ \ | | \
int a | float d
|
statement
| \
| =(F)
| / \
| d(F) +(F)
| / \
| +(F) 2.0(F)
| / \
| I->F \
| | \
| a(I) b(F)
|
statement
\
=(F)
/ \
c(F) *(F)
/ \
d(F) 3.0(F)
I would also expect some explanation like e.g.
handout 8
d)
i)
Bookwork from
handout 9
and maybe also from
handout 10
ii)
Bookwork from
handout 10