This exam consists of 10 multiple-choice questions (Part I) and 3 open questions
... 1) What is the language of the grammar with the following production rules?
Preparatory Course – Grammars and Parsing Friday, November 11, 2005 (9:00-12:00)
This exam consists of 10 multiple-choice questions (Part I) and 3 open questions (Part II). The maximum number of points for each question is given (100 points in total): observe that the points are distributed unevenly over the three open questions. It is not allowed to consult the lecture notes or other material during the test. Good luck!
Part I: Multiple Choice Questions 1)
(40 points)
What is the language of the grammar with the following production rules? S → ASb | c A → a a) {an cbn | n ∈ IN} b) {xcb | x ∈ {a}∗ } c) {acy | y ∈ {b}∗ } d) All of the answers above are incorrect
2)
A grammar has the following productions: S
→ aSSb | a | bSa
Which of the following sentences are in the language that is generated by this grammar? a) aaaaabb b) aabbaabb c) bbbaabbaa d) All of the answers above are correct
3)
The following grammar generates a small part of the statements in Java: S
→ S;S | if Bool then S else S | Identifier := IN |
You may assume that an Identifier is represented by a string. What is the most precise statement about this grammar? a) This grammar is unambiguous b) This grammar is left recursive c) This grammar is LL(1) d) All three statements are correct 1
4)
My grade administration consists of a number of lines below each other, where each line contains a student number, the name of the student, and a number of grades separated by spaces. You may assume that the types StudentNr , Name, and Grade, as well as the parsers pStudentNr :: Parser Char StudentNr pName :: Parser Char Name pGrade :: Parser Char Grade are predefined. How can I define a function pAdmin :: Parser Char [(StudentNr , Name, [Grade ])] that parses my grade administration? a) pAdmin = (λu v w → (u, v , w )) (listOf (pStudentNr spaces pName spaces listOf pGrade spaces ) (token "\n") ) b) pAdmin = listOf ((λu v w → (u, v , w )) pStudentNr spaces pName spaces pGrade) (token "\n") c) pAdmin = listOf ((λu v w → (u, v , w )) pStudentNr spaces pName spaces listOf pGrade spaces ) (token "\n") d) None of the answers above is correct
5)
A family tree (Dutch: stamboom) consists of: • two persons with a number of children family trees (possibly zero), or • a single person,
where a person is a value of type Person. We want to write a program to parse and manipulate family trees. Which abstract syntax should we choose? a) data FamilyTree = Pair Person Person [FamilyTree ] | Person b) data FamilyTree = Pair Person FamilyTree | Person c) data FamilyTree = Pair FamilyTree FamilyTree | Person FamilyTree d) data FamilyTree = Pair Person Person [FamilyTree ] | Person Person
2
6)
Consider the following definition: p = (λx y z → (x , y)) succeed 1 natural
What is the most general type of parser p? a) Parser Char (a → (Int, Int)) b) Parser Char (Int → (Int, Int)) c) Parser Char (Int → Int → Int) d) Parser Char (Int, Int)
7)
Take a look at the following grammar: S A B C
→ → → →
AaC | Bd BC bB | C accS |
For which non-terminals N is symbol a part of the collection follow N ? Give the best answer. a) {A} b) {A, C} c) {A, B, C} d) {A, B, C, S}
8)
Which of the following grammars is a (right) regular grammar with the same language as the regular expression a∗ +b∗ +ab? a) S A B
→ AB → aA | → bB |
S A B
→ ab | A | B → Aa | → Bb |
S A B
→ A|B → aA | b → Bb | a
S A B
→ ab | A | B → aA | → bB |
b)
c)
d)
3
9)
Look at the following non-deterministic finite-state automaton (NFA), with A as the start state, and D as the only accepting state.
A
b
b
B a
a C
a
D
Which deterministic finite-state automaton (DFA) with d as its state transition function accepts the same language? a) Start state d A d B d C
A, b a a
accepting states C and D. = B = C = D
b) Start state d A d B d C
A, b a a
accepting state C. = B = C = C
c) Start state d A d B d B d C
A, b a b a
accepting state D. = B = D = C = D
d) All the answers above are correct
10)
We use the following data type for representing expressions: data Expr = If Expr Expr Expr | Apply Expr Expr | ConInt Int
Which algebra should we use for these expressions? a) type ExprAlg a b = (b → b → b → a, b → b → a, Int → a) b) type ExprAlg a = (If → a → a → a → a, Apply → a → a → a, ConInt → Int → a) c) type ExprAlg a = (a → a → a → a, a → a → a, Int → a) d) type ExprAlg a = (a → a → a, a → a, Int)
4
Part II: Open Questions 11)
Pumping lemma
(60 points)
(10 points)
Recall the following theorem for proving that a language L is not regular: for all there exist for all there exist
n ∈ IN x, y, z u, v, w i ∈ IN
: : xyz ∈ L and | y |> n : : y = uvw and | v |> 0 : : xuv i wz 6∈ L
Prove that the following language is not regular: L = {an b2n c3n | n > 0}
12)
Parser combinators
(20 points)
In this assignment (and the next one) we study pictures that are composed of (images of) letters. The language of pictures is described by the following grammar: P → P + P | P − P | (P ) | Letter Letter → a | . . . | z | A | . . . | Z In this language, P1 +P2 means putting picture P1 besides P2 (horizontal arrangement), and P1 −P2 denotes putting P1 above P2 (vertical arrangement). Parentheses can be used to disambiguate. a) The presented grammar is ambiguous. Prove this by writing down two different parse trees for the sentence "(A-B)+C-D". b) To remove the ambiguity, we decide that • + has a higher priority than − ("A+B-C" is interpreted as "(A+B)-C"), and • both operators are right associative. Transform the grammar and incorporate these choices. c) We define the following data type for describing the abstract syntax of pictures: data Picture = Besides Picture Picture | Above Picture Picture | Letter Char Write a parser for pictures: a successful parsing should construct a value of type Picture. Give the type of your parser, and use the chain expression combinators. (Not using a chain will result in a small penalty.)
5
13)
Folds and algebras
(30 points)
We extend the data type for pictures (from the previous assignment) with two alternatives: the new data type supports rotation and scaling of pictures. data Picture = Besides Picture Picture | Above Picture Picture | Rotate Picture | Scale Int Picture | Letter Char
-----
horizontal arrangement vertical arrangement 90 degrees, clockwise scale a picture
With these “combinators”, we can construct beautiful pictures such as:
If two pictures with different heights are put besides each other, then the height of the composed picture is the maximum height of the two sub-pictures. Pictures with different widths that are arranged vertically are treated likewise. Although not relevant for this assignment, we choose to align sub-pictures top-left if heights and widths do not agree. The picture drawn above can be defined in the following way (although there are different ways to obtain the same result). example = Besides (Letter ’A’) (Rotate (Above (Scale 2 (Above (Rotate (Letter ’E’)) (Letter ’D’))) (Besides (Letter ’B’) (Letter ’C’)) ) ) a) Define the type PictureAlgebra: such an algebra can be used to specify semantic computations on pictures. b) Give the definition and the type of foldPicture. c) Write a function letters :: Picture → Int that returns the number of letters appearing in a picture. If a letter has multiple appearances in a picture, you may count each occurrence separately. You have to use foldPicture. d) We introduce the type synonym Size to represent the size of an image. type Size = (Int, Int)
-- (width, height)
Assume that the size of a letter is (2, 3). Write a function size :: Picture → Size that computes the size of a picture. You have to use foldPicture.
6