






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!







The following is a grammar for simple arithmetic expressions that involve only natural numbers, addition, and multiplication.
ใdigitใ ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ใnumberใ ::= {ใdigitใ}+
ใexpressionใ ::= ใfactorใ | ใexpressionใ + ใexpressionใ
ใfactorใ ::= ใnumberใ | ใfactorใ * ใfactorใ
Solution. The tokens of the language are digits and the symbols + and (^) *.
/ |
/ |
| / |
(a) ambiguous (b) context-free (c) context-sensitive
(d) left-associative (e) right-associative
Solution. (a)
2 Evaluating Simple Arithmetic Expressions
For evaluation of expressions by an interpreter (or for code-generation by a compiler), parse trees are not the most convenient representation, because they still contain information about the original string representation that is unnecessary for computation. The following Oz program is a little expression evaluator that assumes its input is in the form of an abstract syntax tree constructed from Oz tuples.
fun {Eval X} case X of int(N) then N [] add(X Y) then {Eval X}+{Eval Y} [] mul(X Y) then {Eval X}*{Eval Y} end end
Solution. add /
/
add int /\ | / \ 3 int int | | 1 2
Solution. No. In fact, none of the recursive calls is a tail-call.
3 Some
Here is a definition of a function that, given a list Xs and a function P, returns true if {P X} returns true for some element X of Xs and otherwise it returns false.
else case Xs of X|Xr then
Solution.
Some = proc {$ Xs P B} case Xs of nil then B= false else case Xs of X|Xr then local Q in {P X Q} if Q then B= true else {Some Xr P B} end end else raise error( ยด missing else clause ยด ) end end end
4 An Abstract Machine Execution
The following sequence of abstract machine states is the beginning of an execution of an Oz (kernel language) statement, expressed in a format sim- ilar to one weโve used in several lab exercises. Continue the execution to termination. 10 pts
local Y in local X in X= local X in X= end Y=X end end
Env:{}
Store:{}
local X in X= local X in X= end Y=X end
Env:{Y โ y}
Store:{y}
X= local X in X= end Y=X
Env:{Y โ y, X โ x}
Store:{y, x}
X=
Env:{Y โ y, X โ x}
local X in X= end Y=X
Env:{Y โ y, X โ x}
Store:{y, x}
Solution.
Store:{y = 1, x = 1, xโฒ^ = 2}
Terminates.
What does {Arity r(b:1 a:2 3:7)} return? 2 pts
Solution. 3 a b (2 pts for this exact answer, 1 pt if these features are listed but in a different order)
List for each of the following Oz statements the free and bound variable identifiers. (Donโt forget: an identifier can be both free and bound in the same statement, because one occurrence of it is free and another occurrence is bound.) 6 pts
Solution.
7 Producing Power Functions
Consider the following variant of the Pow-function weโve seen in lectures and labs.
fun {PowFactory X} fun {PowAcc N A} if N==0 then A else {PowAcc N-1 X*A} end end fun {Pow N} {PowAcc N 1} end in Pow end
Solution.
local P3 = {PowFactory 3} in {P3 5} end
or simply
{{PowFactory 3} 5}
Solution. PowAcc itself and X.
Solution. PowAcc.
8 Semantics of exceptions
When an exception is raised in an Oz program by the (kernel language) semantic statement (raise ใxใ end, E)
semantic statements are popped off the semantic stack looking for a catch statement.
If 2 is a large statement (perhaps composed of dozens of smaller state- ments), itโs a problem that this translation duplicates it. Consider the following alternative translation that uses a boolean variable to keep track of whether an exception has occurred in order to avoid duplicating 2:
local ExceptionOccurred SavedException in ExceptionOccurred= true try ExceptionOccurred = false catch X then SavedException=X end if ExceptionOccurred then raise SavedException end end end
However, this statement does not have the same semantics as the first trans- lation. Why not? Make your answer precise by describing a case where the two statements would execute differently. 6 pts
Solution. Assume that neither 1 nor 2 raises an exception. With the first translation, both are executed and no exception is raised. But with the second translation, there are two assignments, with different values, to ExceptionOccurred. Since itโs a single assignment variable, the second assignment itself raises an exception, which will be caught by the catch statement and re-raised at the end.