Midterm I - Solution CS164, Spring 2014

Similar documents
Midterm I (Solutions) CS164, Spring 2002

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

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

CS164 Second Midterm Exam Fall 2014

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

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

CS164: Midterm I. Fall 2003

CS143 Midterm Fall 2008

CS 164 Programming Languages and Compilers Handout 8. Midterm I

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

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS143 Midterm Spring 2014

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

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

CSE 401 Midterm Exam 11/5/10

Bottom-Up Parsing. Lecture 11-12

CSE 401/M501 18au Midterm Exam 11/2/18. Name ID #

Bottom-Up Parsing. Lecture 11-12

Lecture 7: Deterministic Bottom-Up Parsing

ECE 468/573 Midterm 1 October 1, 2014

CS143 Midterm Sample Solution Fall 2010

Conflicts in LR Parsing and More LR Parsing Types

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

Lecture 8: Deterministic Bottom-Up Parsing

ECE 468/573 Midterm 1 September 30, 2015

Midterm II CS164, Spring 2006

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

Lexical Analysis. Implementation: Finite Automata

BSCS Fall Mid Term Examination December 2012

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

UNIVERSITY OF CALIFORNIA

Chapter 3 Parsing. time, arrow, banana, flies, like, a, an, the, fruit

CMSC 330, Fall 2018 Midterm 2

CSE 582 Autumn 2002 Exam 11/26/02

Lexical Analysis. Chapter 2

1. Consider the following program in a PCAT-like language.

CSCI Compiler Design

Introduction to Lexical Analysis

Lexical Analysis. Lecture 2-4

Theory Bridge Exam Example Questions Version of June 6, 2008

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

Lexical Analysis. Lecture 3-4

CS154 Midterm Examination. May 4, 2010, 2:15-3:30PM

Homework 1 Due Tuesday, January 30, 2018 at 8pm

In One Slide. Outline. LR Parsing. Table Construction

Question Points Score

CMSC 330, Fall 2018 Midterm 2

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

CSE344 Midterm Exam Winter 2017

CS164 Spring 1997 Midterm 1

Principles of Programming Languages

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

CS164 First Midterm Exam Fall 2014

More Bottom-Up Parsing

2068 (I) Attempt all questions.

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

Theory of Computations Spring 2016 Practice Final Exam Solutions

MIDTERM EXAMINATION - CS130 - Spring 2003

UNIT III & IV. Bottom up parsing

Compiler Design Aug 1996

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

Non-deterministic Finite Automata (NFA)

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

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

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

CSE 401 Midterm Exam Sample Solution 2/11/15

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

Languages and Compilers

LR Parsing LALR Parser Generators

Question Bank. 10CS63:Compiler Design

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

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

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

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

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

LR Parsing. Table Construction

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

CSE 582 Autumn 2002 Exam Sample Solution

Compiler Construction

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

Lecture 4: Syntax Specification

Compiler Construction

Spring 2018 PhD Qualifying Exam in Languages

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

Implementation of Lexical Analysis

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

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Lexical Analysis. Finite Automata

CS606- compiler instruction Solved MCQS From Midterm Papers

CMSC330 Spring 2017 Midterm 2

Lexical Analysis. Lecture 3. January 10, 2018

Lexical Analysis. Finite Automata

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Transcription:

164sp14 Midterm 1 - Solution Midterm I - Solution CS164, Spring 2014 March 3, 2014 Please read all instructions (including these) carefully. This is a closed-book exam. You are allowed a one-page handwritten cheat sheet. Write your name, login, and SID. There are TODO pages in this exam and 3 questions, each with multiple parts. If you get stuck on a question move on and come back to it later. You have 1 hour and 15 minutes to work on the exam. 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. Do not use any additional scratch paper. Solutions will be graded on correctness and clarity. Each problem has a relatively simple and straightforward solution. Partial solutions will be graded for partial credit. No electronic devices are allowed, including cell phones used merely as watches. Silence your cell phones and place them in your bag. LOGIN: NAME: SID: Problem Max points Points 1 28 2 24 3 48 Sub Total 100 1 Regular Expressions and Finite Automata Consider a small language using only the letters z, o, and the slash character /. A comment in this language starts with /o and ends after the very next o/. (a) Give a regular expression that matches exactly one complete comment and nothing else. Assume comments do not nest. For full credit, use only the core notations: ε, ab, AB, A B, and A * [8 points] /o((o*z) /)*o*o/ Consider the language over the alphabet = {a,b} containing strings in which number of 'a's is a multiple of 3

or number of 'b's is a multiple of 2. (b) Write a regular expression for this language. [4 points] (a*ba*ba*)* (b*ab*ab*ab*)* a* b* (c) Write an NFA for this language. [4 points] (d) Write a DFA with at most 6 states for this language [4 points] (3) Can we construct a regular expression for a language over the alphabet = {a,b} whose strings have equal number of occurrences of a and b? Explain. [4 points] No. Corresponding state machine requires infinite number of states. Yes/No (1) Reason (3)

2 LL Parsing Consider the following grammar with terminals *,!, n, (, and ). E F H H * E F F! G G n (E) (a) The grammar is not LL(1). Explain in one sentence why [2 points] F -> F! is left recursive (b) Fix the grammar to make it LL(1) by filling in the blanks below [4 points] E F H H * E G n (E) F G K K! K! K K (c) Compute the first and the follow set of the fixed grammar [8 points] First Follow E (, n $, ) F (, n $, ),* G (, n $, ),*,! H *, $, ) K!, $, ),* (d) Compute LL(1) parsing table [10 points] *! ( ) $ n E E FH E FH F F GK F GK G G (E) G n H H *E K K!K 3 LR Parsing and Ambiguity Consider the following grammar with terminals - (the negation operator) and int. S E E E - E - - E int (a) Draw all the parse trees for the string int - - - int - int [6 points]

(b) Is this grammar ambiguous? Why or why not? [2 point] The grammar is ambiguous because it admits multiple parse trees for string int - - - int - int

(reduction labels) state 3: reduce E-> --E on $/- state 4: reduce E->E-E on $/- state 6: accept S->E on $ state 7: reduce E->Int on $/- (c) Complete the above partial LR(1) DFA for the grammar. [16 points] Fill in items of all states by performing closure operation. (6) Fill in missing transition labels on all edges (4) Write the necessary reduce by on labels on states (2) Add missing transition edges (Hint: State 2 and State 5) (4) (d) For each state with a conflict, list the state, the lookahead token, and the type of conflict (i.e. shift-reduce conflict, or reduce-reduce conflict). [4 points] 3: Lookahead token: -, shift-reduce conflict 4: Lookahead token: -, shift-reduce conflict Suppose we want the string - - int - int - int to have only the following parse tree (call this property P).

(e) Describe in English the precedence and associativity rules necessary to ensure property P. [4 points] 1. - is right-associative 2. -- has higher precedence than - (f) Explain, for each conflict in the LR(1) parsing DFA for this grammar, how it should be resolved to ensure property P. [4 points] State 3: reduce instead of shift on - State 4: shift instead of reduce on - (g) Rewrite the grammar to an equivalent unambiguous grammar to ensure property P. Two grammars are equivalent when they accept the same language. [8 points] S E E T - E T T - - T int (h) The Cool grammar has an ambiguity introduced by let-expression. Give an example illustrating the ambiguity associated with let-expression. [4 point] let in 1 + 2

can be parsed as: (let in 1) + 2 or let in (1 + 2) Published by Google Drive Report Abuse Updated automatically every 5 minutes