Part I: Multiple Choice Questions (40 points)

Similar documents
INFOB3TC Solutions for Exam 1

Midterm I (Solutions) CS164, Spring 2002

CT32 COMPUTER NETWORKS DEC 2015

CS143 Midterm Fall 2008

2068 (I) Attempt all questions.

CSE 582 Autumn 2002 Exam 11/26/02

Compiler Construction

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Ambiguous Grammars and Compactification

CMSC 330: Organization of Programming Languages. Context Free Grammars

Midterm I - Solution CS164, Spring 2014

CMSC 330: Organization of Programming Languages. Context Free Grammars

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

JNTUWORLD. Code No: R

CMSC 330: Organization of Programming Languages

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CSE 401 Midterm Exam Sample Solution 2/11/15

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

BSCS Fall Mid Term Examination December 2012

Question Bank. 10CS63:Compiler Design

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

INFOB3TC Solutions for the Exam

CMSC 330: Organization of Programming Languages

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

CMSC 330: Organization of Programming Languages

CS164: Midterm I. Fall 2003

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Syntax Analysis Check syntax and construct abstract syntax tree

Multiple Choice Questions

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Principles of Programming Languages COMP251: Syntax and Grammars

COP 3402 Systems Software Syntax Analysis (Parser)

Introduction to Parsing. Lecture 5

ECE251 Midterm practice questions, Fall 2010

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Lecture 2 Finite Automata

Formal Languages and Compilers Lecture VI: Lexical Analysis

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CS 314 Principles of Programming Languages

Lexical Analysis. Introduction

CSCE 531 Spring 2009 Final Exam

CMSC 330 Practice Problem 4 Solutions

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Context-Free Grammars

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

CMSC 330, Fall 2009, Practice Problem 3 Solutions

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Compilers and computer architecture From strings to ASTs (2): context free grammars

ECS 120 Lesson 7 Regular Expressions, Pt. 1

UVa ID: NAME (print): CS 4501 LDI Midterm 1

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

INFOB3TC Solutions for Exam 2

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

CSE 401 Midterm Exam 11/5/10

CSCI312 Principles of Programming Languages!

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

1. [5 points each] True or False. If the question is currently open, write O or Open.

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

CSCE 314 Programming Languages. Functional Parsers

CS415 Compilers. Lexical Analysis

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

CMSC330 Spring 2017 Midterm 2

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Optimizing Finite Automata

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

Compiler Construction

UNIT -2 LEXICAL ANALYSIS

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

Finite State Automata are Limited. Let us use (context-free) grammars!

Lexing, Parsing. Laure Gonnord sept Master 1, ENS de Lyon

CS164 Second Midterm Exam Fall 2014

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

Class Information ANNOUCEMENTS

DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING UNIT -1-INTRODUCTION TO COMPILERS 2 MARK QUESTIONS

Models of Computation II: Grammars and Pushdown Automata

CS 314 Principles of Programming Languages. Lecture 3

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur

Lecture Bottom-Up Parsing

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

CMSC330 Spring 2018 Midterm 2 9:30am/ 11:00am/ 3:30pm

Announcements! P1 part 1 due next Tuesday P1 part 2 due next Friday

Transcription:

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