Vous êtes sur la page 1sur 3

Principles of Programming Languages

University of Sussex, Informatics


Bernhard Reus

Exercise Class 4

1. Arrays and Java


What about design issues of arrays in Java? Answer the following
questions:

(a) What kind of arrays does Java implement regarding binding times
of subscription range and memory?
(b) What options for array initialisation does Java offer?
(c) Can we use multi-dimensional arrays in Java?
(d) Can one use subscript ranges other than 0 . . . n for Java arrays?
Can one use other ordinal types like enumeration types for sub-
scription?

2. Records

(a) Explain how records can be simulated (encoded) in Java and how
field selection is done in this encoding. Can you also simulate
nested records in Java?
(b) Try to summarize the difference between proper record types and
their encoding in Java.

3. Variant Records

(a) Write Java code that matches as closely as possible the following
variable declaration using a variant record type (given in a Pascal
like syntax) from the lecture:
var x: record
name: String;
member: Boolean;
case member of
true: (num: Integer);
false: (with: Person);
end;
assuming that Person is some other type that is also available in
Java.

1
(b) Compare variant records with their encoding in Java (see above
example). Also take type safety into consideration.
(c) Can you do union types in Java?

4. Implementing arrays

Here is a (subset) of the abstract syntax we have introduced so far


in lectures, including typed variable declarations in (non procedural)
blocks:
x ∈ var n ∈ intval
u ∈ boolval

e ∈ exp ::= v | e (+| − |∗) e | − e | n


v ∈ varexp ::= x | v[e]
b ∈ bexp ::= v | u | b ∧ b | b ∨ b | ¬b
c ∈ com ::= v = e | if b then c else c | c;c | while b do c
| block [d] in c end
t ∈ type ::= int | bool | array[0, e] of t
d ∈ decl ::= x : t | x : t, d

Note that underlined symbols emphasize that these symbols are part
of the abstract syntax and not BNF meta-symbols. Also note that
intentionally the left end of array subscription is supposed to be 0 as
in Java. This is just to simplify implementation in a Java interpreter.
On the course webpage you find a complete interpreter of a language
discussed in Question 4 on Sheet 3, which is very similar to the one
above but has untyped variable declarations and no variable expres-
sions for array subscription (just variables). Try to change and extend
the code where necessary to obtain an interpreter for the language
above. The lifetime of arrays in this language should be stack-dynamic.
Proceed by answering the questions below (using proper Java code ide-
ally or at least sketching the code you would have to write).

(a) Why was it not necessary to include “array of t” in the abstract


syntax of types?
(b) Implement the abstract syntax for variable expressions varexp and
for types type in Java.
(c) Change the code for the abstract block syntax (Block) to incor-
porate types in declarations and make the initalisation implicit

2
(choosing 0 for integers and false for booleans). Update its inter-
pretation function for blocks accordingly. Explain the problem
created by nested arrays.
(d) Update the evaluation function for expressions so that variable
expressions (including subscriptions) can be handled. It turns
out that the typing of the evaluation function for arithmetic ex-
pressions to return Integer is now problematic. Why is that?
Give the evaluation method for Subscription expressions and
the changed interpretation for the Assign statement.
(e) Extra homework for diligent students (optional): Finish off the
interpreter. Merge the BExp into the Exp abstract syntax into
one hierarchy and update the evaluation function according to
the above.

Vous aimerez peut-être aussi