Sample problems for CS 421 Midterm 1. Suppose the following code:

Transcription

Sample problems for CS 421 Midterm 1. Suppose the following code:
Sample problems for CS 421 Midterm
1. Suppose the following code:
let x = 0.5;;
let mult_half y = x *. y;;
For each independent following question, either give the result, or give the reason why an
error occurs.
(a) mult_half 2.4;;
1.2
(b) mult_half 2;;
Type mismatch
(c) let x = 1;; mult_half 2;;
Type mismatch
2. Write an Ocaml function palindrome : string list -> unit that first prints the strings
in the list from left to right, followed by printing them right to left, recursing over the list
only once.
A sample execution is as follows:
# palindrome [ "a" ; "b"; "c" ];;
abccba- : unit = ()
You may use print_string : string -> unit to print a string.
(a) Use recursion
let rec palindrome l = match l with
[] -> ()
| s::ss -> (print_string s; palindrome ss; print_string s);;
(b) Use List.fold_right
let rec palindrome l = List.fold_right
(fun s -> fun print_middle -> (fun () ->
(print_string s; print_middle (); print_string s)))
l
(fun () -> ())
();;
1
Sample problems for CS 421 Midterm
(c) Use List.fold_left
let palindrome l =
List.fold_left
(fun print_middle -> fun s ->
(print_string s; fun () -> (print_middle (print_string s))))
(fun () -> ())
l
();;
3. Using the typing rules provided in class, give a type derivation for the following judgement.
{x:bool, y:bool} |(let f = fun x -> (x > 7)
&& y in if f 2 then x else y) : bool
T1 = { x:bool, y:bool }
T2 = { x:bool, y:bool, f : int → bool }
T3 = { x:int, y:bool } (or { x:int, x: bool, y:bool })
Let C stand for the Constants rule, V stand for the Variables rule, R stand for the Relations
rule, CN stand for the Connectives rule, ITE stand for the ‘if then else’ rule, F stand for the
Fun rule, APP stand for the Application rule, and L stand for the Let rule.
------------V -----------C
T3 |- x : int T3 |- 7: int
-------------------R -------------V --------------------V -------------C
T3 |- (x > 7) : bool T3 |- y : bool T2 |- f : int -> bool T2 |- 2 : int
-----------------------------------CN
-------------APP
-------------V -------------V
T3 |- (x > 7) && y : bool
T2 |- f 2 : bool
T2 |- x : bool T2 |- y : bool
------------------------------------------F
----------------------------------------------ITE
T1 |- (fun x -> (x > 7) && y): int -> bool
T2 |- (if f 2 then x else y ) : bool
-----------------------------------------------------------------------------------------L
{x:bool, y:bool } |- (let f = fun x -> (x > 7) && y in if f 2 then x else y ) : bool
2
Sample problems for CS 421 Midterm
4. Using the rules provided in class, give a type inference and constraint set for the following
expression for
let rec f = fun x -> f (x - 1) in (f 3) > 0
(The rules will be provided for you on the exam, if this kind of question is asked.)
Let V stand for the variable rule, C stand for the constants rule, PO stand for the primitive
operations rule, AR stand for the arithmetic relations rule, LC for logical connectives, ITE
for the if then else rule, F stand for the function rule, APP for the application rule, L for the
let, and LR for the let-rec rule.
--------------------V --------------------C
{x:C, f:B} |- x : int {x:C, f:B} |- 1 : int
-----------------------V
-----------------------------PO
--------------------V ------------C
{x:C, f:B} |- f : E -> D
{x:C, f:B} |- (x - 1) : E
{f:B} |- f : F -> int {f:B} |- 3: F
--------------------------------------APP
---------------------------APP
-----------------C
{x:C, f:B} |- (f (x - 1)) : D
{f:B} |- f 3: int
{f:B} |- 0 : int
--------------------------------F
-------------------------------AR
{f:B} |- (fun x -> f (x - 1)) : B
{f:B} |- (f 3 > 0 ) : A
---------------------------------------------------------------------------------------LR
{ } |- (let rec f = fun x -> f (x - 1) in f 3 > 0 ) : A
Constraints: { B = C -> D, B = E-> D, C = int, E = int, A = bool, B = F -> int, F = int}
3
Sample problems for CS 421 Midterm
5. Give a most general substitution that satisfies the following set of constraints. Capital letters
denote variables, and lower case letters denote constructors.
(
!
!)
f (A, C), B , p s(A), s(B) , p C, s(B)
(
Orient →
!
Eliminate →
!)
!)
p s(A), s(f (A, C)) , p C, s(f (A, C))
(
Decompose →
!
s(A), C
(
Orient →
!)
s(f (A, C)), s(f (A, C))
!
C, s(A)
s(f (A, C)), s(f (A, C))
!)
s(f (A, s(A))), s(f (A, s(A)))
()
n
n
o
n
o
o
with B ` f (A, C)
n
o
with C ` s(A); B ` f (A, s(A))
o
with C ` s(A); B ` f (A, s(A))
4
n
with B ` f (A, C)
with B ` f (A, C)
!)
(
Delete →
B, f (A, C) , p s(A), s(B) , p C, s(B)
(
Eliminate →
Sample problems for CS 421 Midterm
6. Give a regular expression and a DFA (in ocamllex notation) for the following token:
An identifier in OCaml is either a lower-case letter followed by any number of letters or
digits, or a non-empty sequence of the symbols +, -, *, or / (with no spaces) surrounded by
parentheses.
You do not need to label any of the nodes, but write the start node on the left.
Regex:
([’a’-’z’][’a’-’z’ ’A’-’Z’ ’0’-’9’]*) | ’(’ [’+’ ’-’ ’*’ ’/’]+ ’)’
5
Sample problems for CS 421 Midterm
a − z, A − Z, 0 − 9
a−z
(
+, −, ∗, /
)
+, −, ∗, /
1
6
Sample problems for CS 421 Midterm
7. Write an ocamllex code to divide the input into tokens. Each token is of the following type:
type token = PLUS | TIMES | IDENT of string
where an IDENT is a sequence of one more lower-case letters.
let letter = [a-z]
let letters = letter+
rule main = parse
| letters as s { IDENT s }
| + { PLUS }
| * { TIMES }
7
Sample problems for CS 421 Midterm
8. Consider the following grammar:
1
2
3
4
5
6
:
:
:
:
:
:
S ⇒ E eol
E ⇒E∗N
E⇒N
N ⇒ fN
N ⇒x
N ⇒y
The following are the LR parse table for the above grammar:
State
1
2
3
4
5
6
7
8
9
10
11
*
f
S5
ACTION
x
y eol
S3 S4
R5
R6
R5
R6
S5
R5
R6
S3
R5
R6
S4
R3
R4
S5
R1
R2
R3
R4
S3
R1
R2
R3
R4
S4
R1
R2
S9
R3
R4
R1
R2
R5
R6
$
GOTO
S E N
2 6 7
acc
R5
R6
8
S10
R3
R4
R3
R4
11
R1
R2
R1
R2
Describe how f x ∗ y eol would be parsed using the above table.
State
Stack
1
5
3
1
1: f : 5
1: f : 5: x: 3
8
7
6
9
4
12
7
11
2
1
1
1
1
1
1
1
1
1
:
:
:
:
:
:
:
:
:
f : 5: N : 8
N : 7
E: 6
E: 7: ∗: 9
E: 7: ∗: 9: y: 4
E : 7 : ∗ : 9 : N : 11
E: 6
E : 6 : eol : 10
S: 2
Input
f x ∗ y eol
f x ∗ y eol
x ∗ y eol
∗y eol
∗y eol
∗y eol
∗y eol
y eol
eol
eol
eol
8
Action
init the stack and state
shift f , go to state 5
shift x, go to state 3
reduce by 5: N ⇒ x; i.e., remove 3 and x
from the stack, temporarily putting us in state
5, push N and 8 (because GOTO(5, N ) = 8)
onto the stack, go to state 8
reduce by 4: N ⇒ f N , go to state 7
reduce by 3: E ⇒ N , go to state 6
shift ∗, go to state 9
shift y, go to state 4
reduce by 6: N ⇒ y, go to state 11
reduce by 2: E ⇒ E ∗ N , go to state 6
shift eol, go to state 10
reduce by 1: S ⇒ E eol, go to state 2
Accept
Sample problems for CS 421 Midterm
9. A grammar for arithmetic expressions should have these properties, if possible:
a. It should be unambiguous
b. It should be LL(1)
c. It should enforce left-associativity of + and *
d. It should enforce precedence of * over +
None of the following grammars satisfies all of these criteria. For each grammar, list all the
properties that it fails to satisfy.
(a)
E → id | E + id | E × id | ( E )
Fails (b), (d)
(b)
E → id | id + E | id × E | ( E )
Fails (b), (c), (d)
(c)
E → T+E|T
T → P×T|P
P → id | ( E )
Fails (b), (c)
(d)
E → id F | ( E )
F → |+E|×E
Fails (c), (d)
(e)
E → E+T|T
T → T×P|P
P → id | ( E )
Fails (b)
(f)
E → T E’
E’ → | + E
T → P T’
T’ → | × E
P → id | ( E )
Fails (c)
9
Sample problems for CS 421 Midterm
10. Write a recursive descent parser for the following grammar:
E → T E’
E’ → ε | + E
T → P T’
T’ → ε | * T
P → id
Suppose the tokens are given by this type:
type token = ID of string | PLUS | STAR
type token = ID of string | PLUS | STAR
type e =
| TE’ of (t * e’)
and e’ =
| Plus of e
| EmptyE’
and t =
| PT’ of (p * t’)
and t’ =
| Star of t
| EmptyT’
and p =
| Id of string
let rec parseE tokens =
begin match parseT tokens with
| (t, tokens_after_t) ->
begin match parseE’ tokens_after_t with
| (e’, tokens_after_e’) -> (TE’(t, e’), tokens_after_e’)
end
end
and parseE’ tokens =
begin match tokens with
| PLUS::tokens’ ->
begin match (parseE tokens’) with
| (e, tokens_after_e) -> (Plus(e), tokens_after_e)
end
| _ -> (EmptyE’, tokens)
10
Sample problems for CS 421 Midterm
end
and parseT tokens =
begin match (parseP tokens) with
| (p, tokens_after_p) ->
begin match (parseT’ tokens_after_p) with
| (t’, tokens_after_t’) -> (PT’(p, t’), tokens_after_t’)
end
end
and parseT’ tokens =
begin match tokens with
| STAR::tokens’ ->
begin match (parseT tokens’) with
| (t, tokens_after_t) -> (Star(t), tokens_after_t)
end
| _ -> (EmptyT’, tokens)
end
and parseP tokens =
begin match tokens with
| (ID id)::tokens’ -> (Id(id), tokens’)
| _ -> failwith "parsing error";
end
11