Midterm I (Solutions) CS164, Spring 2002

Similar documents
Midterm I - Solution CS164, Spring 2014

CS164: Midterm I. Fall 2003

CS143 Midterm Spring 2014

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

CS143 Midterm Fall 2008

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

ECE 468/573 Midterm 1 October 1, 2014

ECE 468/573 Midterm 1 September 30, 2015

CS164 Second Midterm Exam Fall 2014

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

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

CS143 Midterm Sample Solution Fall 2010

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

CSE 401 Midterm Exam 11/5/10

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

CS 164 Programming Languages and Compilers Handout 8. Midterm I

CMSC 330, Fall 2009, Practice Problem 3 Solutions

Midterm II CS164, Spring 2006

LR Parsing - The Items

Lecture 7: Deterministic Bottom-Up Parsing

Bottom-Up Parsing. Lecture 11-12

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

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

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

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

Bottom-Up Parsing. Lecture 11-12

Lecture 8: Deterministic Bottom-Up Parsing

CS143 Handout 20 Summer 2012 July 18 th, 2012 Practice CS143 Midterm Exam. (signed)

CMSC 330 Practice Problem 4 Solutions

ECE468 Fall 2003, Final Exam

More Bottom-Up Parsing

Formal Languages and Compilers Lecture VI: Lexical Analysis

CS 44 Exam #2 February 14, 2001

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

CMSC 330, Fall 2018 Midterm 2

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

2068 (I) Attempt all questions.

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

CS 132 Compiler Construction, Fall 2012 Instructor: Jens Palsberg Multiple Choice Exam, Nov 6, 2012

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

6 NFA and Regular Expressions

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

Front End: Lexical Analysis. The Structure of a Compiler

In One Slide. Outline. LR Parsing. Table Construction

Conflicts in LR Parsing and More LR Parsing Types

Lexical Analysis. Implementation: Finite Automata

CSE 582 Autumn 2002 Exam 11/26/02

1 Parsing (25 pts, 5 each)

CMSC330 Spring 2014 Midterm 2 Solutions

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Lecture Bottom-Up Parsing

shift-reduce parsing

CSE 5317 Midterm Examination 4 March Solutions

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

Implementation of Lexical Analysis

Implementation of Lexical Analysis

Implementation of Lexical Analysis

Question Points Score

Computer Science Spring 98 Midterm Examination Professor L. Rowe

ECE251 Midterm practice questions, Fall 2010

CSE 582 Autumn 2002 Exam Sample Solution

CSCI Compiler Design

S Y N T A X A N A L Y S I S LR

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

Finite automata. We have looked at using Lex to build a scanner on the basis of regular expressions.

Part I: Multiple Choice Questions (40 points)

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

CS 536 Midterm Exam Spring 2013

SLR parsers. LR(0) items

Theory of Computations Spring 2016 Practice Final Exam Solutions

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

Midterm Exam. CS169 Fall November 17, 2009 LOGIN: NAME: SID: Problem Max Points Points TOTAL 100

JNTUWORLD. Code No: R

Lecture Notes on Bottom-Up LR Parsing

Introduction to Lexical Analysis

CMSC 330, Fall 2018 Midterm 2

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

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

CSCE 531 Spring 2009 Final Exam

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

Midterm Solutions COMS W4115 Programming Languages and Translators Wednesday, March 25, :10-5:25pm, 309 Havemeyer

Compiler Design Aug 1996

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

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

LR Parsing LALR Parser Generators

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

MIDTERM EXAMINATION - CS130 - Spring 2003

Lecture Notes on Bottom-Up LR Parsing

Theory Bridge Exam Example Questions Version of June 6, 2008

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

LR Parsing. Table Construction

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

COP4020 Spring 2011 Midterm Exam

Answer All Questions. All Questions Carry Equal Marks. Time: 20 Min. Marks: 10.

Transcription:

Midterm I (Solutions) CS164, Spring 2002 February 28, 2002 Please read all instructions (including these) carefully. There are 9 pages in this exam and 5 questions, each with multiple parts. Some questions span multiple pages. All questions have some easy parts and some hard parts. If you get stuck on a question move on and come back to it later. You have 1 hour and 20 minutes to work on the exam. The exam is closed book, but you may refer to your two pages of handwritten notes. Please write your answers in the space provided on the exam, and clearly mark your solutions. You may use the backs of the exam pages as scratch paper. Please do not use any additional scratch paper. Solutions will be graded on correctness and clarity. Each problem has a relatively simple and straightforward solution. We might deduct points if your solution is far more complicated than necessary. Partial solutions will be graded for partial credit. NAME: SID or SS#: Circle the time of your section: 9:00 10:00 11:00 12:00 1:00 2:00 3:00 4:00 Problem Max points Points 1 20 2 20 3 15 4 30 5 15 TOTAL 100 1

1 Regular Expressions and Finite Automata (20 points) a) Draw a deterministic finite automaton (DFA) that recognizes the language over the alphabet {0, 1} consisting of all those strings that contain an odd number of 1 s. b) Write a regular expression for this language. Solution: (0 10 )(10 10 ) c) Draw a deterministic finite automaton (DFA) for the language of all strings over the alphabet {0, 1} that do not contain the substring 110. Solution: (state D is a garbage state) 2

d) Consider the following NFA over the alphabet {a}. Convert this NFA to a DFA. For each DFA state write the set of the NFA states that it corresponds to. Solution: 3

2 Context-Free Grammars (20 points) The following grammar is ambiguous: E id E + E E E E. E E E a) Show two parse trees for the string id + id. id. b) The ambiguity is removed by adding the following predecence declarations. Recall the convention that the first declaration assigns the lowest precedence: %right + - %left * %left. Considering these declarations, show the parse tree for the string: id + id. id. id * id + id 4

d) Write an unambiguous grammar for the same language, resolving ambiguity as specified by the precedence declarations shown above. Solution: E T T + E T E T T F F F F.id id 5

3 LL Parsing (15 points) a) Consider the following grammar over the alphabet {a, b, d}: S A B D A a B S B B b D D d ɛ Fill in the table below with the First and Follow sets for the non-terminals in this grammar: S First Follow a, b, d b, d, $ A a, b, d b, d, $ B b, d, ɛ a, b, d, $ D d, ɛ a, b, d, $ 6

4 LR Parsing (30 points) The following figure shows the LR(1) DFA for a grammar. The DFA is missing several elements that you ll have to fill-in: the LR(1) items that describe each state, the labels on the transitions and the lookahead terminals for the reduce labels. 7

a) Write the grammar that corresponds to the above DFA. Solution: just copy the productions listed in the reduce labels. b) Fill in the labels on the transitions. You can do this either by actually filling in the sets of LR(1) items or by examining closely the reduce labels. c) For each of the following configurations of the LR(1) parser, say what action is taken by the parser (one of shift or reduce with production... or error ) and write the next configuration (except in the case of an error). Configuration Action Next configuration T +a + a$ shift T + a + a$ T + a$ error T + T + a a$ reduce T a T + T + T a$ T a $ reduce T T a T $ T + E +a$ error d) Fill in the large boxes (representing states) with the LR(1) items. e) Fill in the lookahead terminals in the small boxes next to each reduce label. 8

5 LL Parsing (15 points) Consider the following grammar in which S is the start non-terminal and in which the productions for the non-terminals A, B and C are not shown. There are no other productions for S. S A B A C A... B... C... Consider now the LL(1) parsing table for this grammar. What conditions must be satisfied by the First and Follow sets of the non-terminals, such that the row corresponding to S in this table does not contain multiply-defined entries? Write your answer in the table below considering six separate cases. For example, in the top-left entry consider the case when ɛ First(B) and ɛ First(C) and First(A) = {ɛ}, and write what additional conditions must be met, or write never if no additional conditions would help. First(A) = {ɛ} ɛ First(B) ɛ First(B) ɛ First(B) ɛ First(C) ɛ First(C) ɛ First(C) First(B) First(C) = First(B) First(C) = never (since both AB and AC end up in the column for $) First(B) Follow(S) = First(A) {ɛ} never (since both AB and AC end up in the column for anything in First(A) {ɛ}) never never 9