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 (40 points 1 What is the language of the grammar with the following production rules? S ASb c A a a {a n cb n 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 AaC Bd A BC B bb C 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 b c d S AB A aa ɛ B bb ɛ S ab A B A Aa ɛ B Bb ɛ S A B A aa b B Bb a S ab A B A aa ɛ B bb ɛ 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 A, accepting states C and D. d A b = B d B a = C d C a = D b Start state A, accepting state C. d A b = B d B a = C d C a = C c Start state A, accepting state D. d A b = B d B a = D d B b = C d C a = 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 (60 points 11 Pumping lemma (10 points Recall the following theorem for proving that a language L is not regular: for all n IN : there exist x, y, z : xyz L and y n : for all u, v, w : y = uvw and v > 0 : there exist i IN : xuv i wz L Prove that the following language is not regular: L = {a n b 2n c 3n 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, P 1 +P 2 means putting picture P 1 besides P 2 (horizontal arrangement, and P 1 P 2 denotes putting P 1 above P 2 (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 -- horizontal arrangement Above Picture Picture -- vertical arrangement Rotate Picture -- 90 degrees, clockwise Scale Int Picture -- scale a picture Letter Char 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