Docsity

Sample Questions for Midterm 2 (CS 421 Spring 2007) - Prof. Elsa Gunter, Exams of Computer Science

Toggle sidebar
bg1
Sample Questions for Midterm 2 (CS 421 Spring 2007)
On the actual midterm, you will have plenty of space to put your answers.
Some of these questions may be reused for the exam.
1. Write the definition of an OCaml variant type reg_exp to express abstract syntax
trees for regular expressions over a base character set of booleans. Thus, a
boolean is a reg_exp, epsilon is a reg_exp, the concatenation of two reg_exp’s is
a reg_exp, the “choice” of two reg_exp’s is a reg_exp, and the Kleene star of a
reg_exp is a reg_exp.
2. Given the following OCAML datatype:
type int_seq = Null | Snoc of (int_seq * int)
write a tail-recursive function in OCAML all_pos : int_seq -> bool that returns true if
every integer in the input int_seq to which all_pos is applied is strictly greater than 0
and false otherwise. Thus all_pos (Snoc(Snoc(Snoc(Null,3),5),7)) should returns true,
but
all_pos (Snoc(Null,~1)) and all_pos (Snoc(Snoc(Null, 3),0)) should both return
false.
3. Using the rules provided in class, derive a valid type judgment for
let rec fact = fun n -> if n = 0 then 1 else let r = fact (n - 1) in n * r in fact;;
(The rules will be provided for you on the exam, if this kind of question is asked.)
4. G!i!v!e! !a! (most general) !u!n!i!f!i!e!r! !f!o!r! !t!h!e! !f!o!l!l!o!w!i!n!g! !u!n!i!f!i!c!a!t!i!o!n! !i!n!s!t!a!n!c!e!.! !Capital !l!e!t!t!e!r!s!
!d!e!n!o!t!e! !v!a!r!i!a!b!l!e!s! !o!f! !u!n!i!f!i!c!a!t!i!o!n!.! !S!h!o!w! !y!o!u!r! !w!o!r!k! !b!y! !l!i!s!t!i!n!g! !t!h!e! !o!p!e!r!a!t!i!o!n!
!p!e!r!f!o!r!m!e!d! !i!n! !e!a!c!h! !s!t!e!p! !o!f! !t!h!e! !u!n!i!f!i!c!a!t!i!o!n! !a!n!d! !t!h!e! !r!e!s!u!l!t! !o!f! !t!h!a!t! !s!t!e!p!.! !!!
{X = f(g(x),W), h(y) = Y, f(Z,x) = f(Y,W)}
5. For each of the regular expressions below (over the alphabet {a,b,c}), draw a
nondeterministic finite state automaton that accepts exactly the same set of strings
as the given regular expression.
a. a* ∨ b* ∨ c*
b. ((aba ∨ bab) c (aa ∨ bb))*
c. (a*b*)*(c ∨ ε) (b*a*)*
6. Consider the following grammar:
<S> ::= <A> | <A> <S>
<A> ::= <Id> | ( <B>
<B> ::= <Id> ] | <Id><B> | ( <B>
<Id> ::= 0 | 1
For each of the following strings, give a parse tree for the following expression as
an <S>, if one exists, or write “No parse” otherwise:
a. ( 0 1 ( 1 ] ( ( 1 0 ] 1
b. 0 ( 1 0 ( 1 ]
c. ( 0 ( 1 0 1] 0 ]
pf2

Partial preview of the text

Download Sample Questions for Midterm 2 (CS 421 Spring 2007) - Prof. Elsa Gunter and more Exams Computer Science in PDF only on Docsity!

Sample Questions for Midterm 2 (CS 421 Spring 2007)

On the actual midterm, you will have plenty of space to put your answers. Some of these questions may be reused for the exam.

  1. Write the definition of an OCaml variant type reg_exp to express abstract syntax trees for regular expressions over a base character set of booleans. Thus, a boolean is a reg_exp, epsilon is a reg_exp , the concatenation of two reg_exp ’s is a reg_exp, the “choice” of two reg_exp ’s is a reg_exp , and the Kleene star of a reg_exp is a reg_exp.
  2. Given the following OCAML datatype: type int_seq = Null | Snoc of (int_seq * int) write a tail-recursive function in OCAML all_pos : int_seq -> bool that returns true if every integer in the input int_seq to which all_pos is applied is strictly greater than 0 and false otherwise. Thus all_pos (Snoc(Snoc(Snoc(Null,3),5),7)) should returns true, but all_pos (Snoc(Null,~1)) and all_pos (Snoc(Snoc(Null, 3),0)) should both return false.
  3. Using the rules provided in class, derive a valid type judgment for let rec fact = fun n -> if n = 0 then 1 else let r = fact (n - 1) in n * r in fact;; (The rules will be provided for you on the exam, if this kind of question is asked.)
  4. G!i!v!e! !a! (most general) !u!n!i!f!i!e!r! !f!o!r! !t!h!e! !f!o!l!l!o!w!i!n!g! !u!n!i!f!i!c!a!t!i!o!n! !i!n!s!t!a!n!c!e!.! !Capital !l!e!tt!!e!r!s! ! d!e!n!o!t!e! !v!a!r!i!a!b!l!e!s! !o!f! !u!n!i!f! i!c!a!t!i!o!n!.! !S!h!o!w! !y!o!u!r! !w!o!r!k! !b!y! !l!i!s!t!i!n!g! !t!h!e! !o!p!e!r!a!t!i!o!n! ! p!e!r!f!o!r!m!e!d! !i!n! !e!a!c!h! !s!t!e!p! !o!f! !t!h!e! !u!n!i!f!i!c!a!t!i!o!n! !a!n!d! !t!h!e! !r!e!s!u!l!t! !o!f! !t!h!a!t! !s!t!e!p!.! !!! {X = f(g(x),W), h(y) = Y, f(Z,x) = f(Y,W)}
  5. For each of the regular expressions below (over the alphabet {a,b,c}), draw a nondeterministic finite state automaton that accepts exactly the same set of strings as the given regular expression. a. a* ∨ b* ∨ c* b. ((aba ∨ bab) c (aa ∨ bb))* c. (ab)(c ∨ ε ) (ba)
  6. Consider the following grammar: ::= | ::= | ( ::= ] | | ( ::= 0 | 1 For each of the following strings, give a parse tree for the following expression as an , if one exists, or write “No parse” otherwise: a. ( 0 1 ( 1 ] ( ( 1 0 ] 1 b. 0 ( 1 0 ( 1 ] c. ( 0 ( 1 0 1] 0 ]

  1. Consider the following ambiguous grammar (Capitals are nonterminals, lowercase are terminals): S → A a B | B a A A → b | c B → a | b Give an example of a string for which this grammar has two different parse trees, and give its parse trees.
  2. Write an unambiguous grammar generating the set of all strings over the alphabet {0, 1, +, - } , where + and – are infixed operators which both associate to the left and such that + binds more tightly than -.