Docsity

Midterm Exam 1 with Solution - Programming Languages | CSCI 4430, Exams of Programming Languages

Toggle sidebar
bg1
Name: SOLUTION KEY
CSCI-4430 Programming Languages
Midterm Exam
Part I
1 A Grammar for Simple Arithmetic Expressions
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๎˜‚
1. What are the tokens of this language? 2pts
Solution. The tokens of the language are digits and the symbols +
and *.
2. With this grammar, the expression 1+2+3 has two parse trees:
<exp>
/|\
/|\
<exp> + <exp>
/|\ |
/|\ |
<exp> + <exp> <fac>
|||
<fac> <fac> <num>
|||
<num> <num> <dig>
|||
<dig> <dig> 3
||
12
<exp>
/|\
/|\
<exp> + <exp>
| /|\
|/|\
<fac> <exp> + <exp>
|||
<num> <fac> <fac>
|||
<dig> <num> <num>
|||
1 <dig> <dig>
||
23
The existence of two parse trees for the same string shows that the
grammar is (circle one) 2pts
(a) ambiguous
(b) context-free
(c) context-sensitive
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Midterm Exam 1 with Solution - Programming Languages | CSCI 4430 and more Exams Programming Languages in PDF only on Docsity!

Name: SOLUTION KEY

CSCI-4430 Programming Languages

Midterm Exam

Part I

1 A Grammar for Simple Arithmetic Expressions

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ใ€‰

  1. What are the tokens of this language? 2 pts

Solution. The tokens of the language are digits and the symbols + and (^) *.

  1. With this grammar, the expression 1+2+3 has two parse trees:

/|
/ |
+ /|\ | / | \ | + | | | | | | | | | 3 | | 1 2

/|
/ |
+ | /|
| / |
+ | | | | | | | | | 1 | | 2 3 The existence of two parse trees for the same string shows that the grammar is (circle one) 2 pts

(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

  1. For the first parse tree shown in part 2 of the preceding question, 4 pts draw the corresponding abstract syntax tree.

Solution. add /
/
add int /\ | / \ 3 int int | | 1 2

  1. Is the Eval function tail-recursive? Explain. 2 pts

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.

Midterm Exam, Part II

5 Arity

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)

6 Free and Bound Identifiers

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

  1. {P X Y} local X in {X P Y} end Free: Bound:
  2. local X in local Y in {X Y Z} end end Free: Bound:
  3. case X of f(Y) then {P Y} else {Q Y} end Free: Bound:

Solution.

  1. Free: P, X, Y; bound: X.
  2. Free: Z; bound: X, Y.
  3. Free: P, X, Y, Q; bound: Y.

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

  1. Write an expression using PowFactory to compute 3 to the 5-th power. 4 pts

Solution.

local P3 = {PowFactory 3} in {P3 5} end

or simply

{{PowFactory 3} 5}

  1. Give the external references for PowAcc 2 pts

Solution. PowAcc itself and X.

  1. Give the external references for Pow. 2 pts

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.