Docsity

Haskell Exam: BNF Equivalence, Function Definitions, and Derivation Trees, Exams of Programming Languages

Toggle sidebar
bg1
22C:111
page 1 of 2
Exam I
Open book/notes
1. [20 points]
In each part of this problem, there is an extended BNF expression (terminal
character set = {a, b})
and
a syntax diagram. Determine whether or not the pair is
equivalent, and either explain the equivalence, or if they are not equivalent, give
an example of a string that is described by one but not the other.
2. [25 points]
Write the Haskell definition of a function 'delSpaces' that accepts one argument
that is a string (i.e., [char]), and returns a value that is a copy of the argument
string with all spaces deleted. For example, delSpaces "now is the time " =
"nowisthetime".
Your solution will not receive full credit without an explanation of its operation and
justification that it behaves as required.
(a) (a* b)*
a
b
(b) (abab)* aba
a
b
a
b
pf2

Partial preview of the text

Download Haskell Exam: BNF Equivalence, Function Definitions, and Derivation Trees and more Exams Programming Languages in PDF only on Docsity!

22C:

page 1 of 2 Exam I Open book/notes

1. [20 points] In each part of this problem, there is an extended BNF expression (terminal character set = {a, b}) and a syntax diagram. Determine whether or not the pair is equivalent, and either explain the equivalence, or if they are not equivalent, give an example of a string that is described by one but not the other. 2. [25 points] Write the Haskell definition of a function 'delSpaces' that accepts one argument that is a string (i.e., [char]), and returns a value that is a copy of the argument string with all spaces deleted. For example, delSpaces "now is the time " = "nowisthetime". Your solution will not receive full credit without an explanation of its operation and justification that it behaves as required. (a) (a* b)* a (^) b (b) (abab)* aba (^) a b a b

22C:

page 2 of 2

3. [25 points] For the Haskell expression below, show its derivation tree (from ): 2*x + x^3 > 0 The necessary portion of the BNF is included below. Bold | denotes the BNF alternative operator, bold ( ) denotes BNF grouping not expression parenthesis, and bold [ ] denotes the BNF “optional” operator (zero or one) -- in ordinary font, these are each Haskell characters. Syntax categories lexp and rexp are for left and right associative operators, respectively. The productions for identifiers and various types of literal constants are omitted — for this problem, assume that these syntax categories derive their familiar results in a single step. exp Æ exp^0

expi^ Æ expi+1^ [ qop(n,i)^ expi+1 ] | lexpi^ | rexpi^ (0≤ i ≤9)

lexpi^ Æ ( lexpi^ | expi+1 ) qop(l,i)^ expi+1^ (0≤ i ≤9)

rexpi^ Æ expi+1^ qop(r,i)^ ( rexpi^ | expi+1 ) (0≤ i ≤9)

qop(n,4)^ Æ < | > (non-assoc, prec 4)

qop(l,6)^ Æ + | – (left assoc, prec 6)

qop(l,7)^ Æ * | / (left assoc, prec 7)

qop(r,8)^ Æ ^ | ** (right assoc, prec 8)

exp^10 Æ fexp fexp Æ [ fexp ] aexp

aexp Æ qvar | literal

qvar Æ identifier literal Æ numeral | charconst | stringconst | boolconst (partial) Haskell EBNF

4. [30 points] Provide a Haskell definition of a polymorphic function 'replace' that takes three arguments — values x and y of polymorphic type 'a' and a list xs of type [a] — and returns the list xs with each occurrence of x replaced by y, and other elements unchanged. For instance, replace 's' 'l' "bass" yields "ball". Your solution will not receive full credit without an explanation of its operation and justification that it behaves as required.