CS143 Midterm Sample Solution Fall 2010

Similar documents
CS143 Midterm Spring 2014

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

CS143 Midterm Fall 2008

Midterm I (Solutions) CS164, Spring 2002

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

CS164: Midterm I. Fall 2003

Midterm I - Solution CS164, Spring 2014

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

UNIVERSITY OF CALIFORNIA

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

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

More Bottom-Up Parsing

Bottom-Up Parsing LR Parsing

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

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

COP4020 Spring 2011 Midterm Exam

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

CS 164 Programming Languages and Compilers Handout 8. Midterm I

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

Types of parsing. CMSC 430 Lecture 4, Page 1

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

Lecture Bottom-Up Parsing

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

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

Abstract Syntax Trees & Top-Down Parsing

CSE3322 Programming Languages and Implementation

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS Parsing 1

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

LR Parsing LALR Parser Generators

Exercises II. Exercise: Lexical Analysis

Syntax-Directed Translation

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

LR Parsing LALR Parser Generators

CSE 401 Midterm Exam Sample Solution 2/11/15

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

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Compiler Construction

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

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

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.

Compilers. Predictive Parsing. Alex Aiken

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

Compiler Construction

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

HW 3: Bottom-Up Parsing Techniques

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

CS143 Final Fall 2009

Bottom-Up Parsing II. Lecture 8

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing


Monday, September 13, Parsers

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

2068 (I) Attempt all questions.

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

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

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

ECE 468/573 Midterm 1 September 30, 2015

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

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

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Syntax Analysis, III Comp 412

Parsing Part II (Top-down parsing, left-recursion removal)

Lecture 7: Deterministic Bottom-Up Parsing

3. Parsing. Oscar Nierstrasz

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

CSCI312 Principles of Programming Languages

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

In One Slide. Outline. LR Parsing. Table Construction

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

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

ECE 468/573 Midterm 1 October 1, 2014

CS 411 Midterm Feb 2008

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

BSCS Fall Mid Term Examination December 2012

CS606- compiler instruction Solved MCQS From Midterm Papers

CMSC 330, Fall 2018 Midterm 2

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Top down vs. bottom up parsing

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

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

CSCI Compiler Design

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

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

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Building Compilers with Phoenix

Bottom-Up Parsing. Lecture 11-12

QUESTIONS RELATED TO UNIT I, II And III

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Concepts Introduced in Chapter 4

LR Parsing Techniques

Wednesday, August 31, Parsers

Transcription:

CS143 Midterm Sample Solution Fall 2010 Please read all instructions (including these) carefully. There are 4 questions on the exam, all with multiple parts. You have 75 minutes to work on the exam. The exam is closed book, but you may refer to your four sheets of prepared 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. You may get as few as 0 points for a question if your solution is far more complicated than necessary. Partial solutions will be graded for partial credit. NAME: In accordance with both the letter and spirit of the Honor Code, I have neither given nor received assistance on this examination. SIGNATURE: Problem Max points Points 1 15 2 20 3 20 4 20 TOTAL 75 1

1. First and Follow Sets (15 points) Consider the following grammar with 2 missing productions: S as... (1) A... (2) ɛ X cs ɛ Y ds ɛ Z es Fortunately we have the First and Follow sets for this grammar: First Follow S {a, b, c, d, e {$ F ollow(x) F ollow(y ) F ollow(z) A {c, d, e, ɛ {b X {c, ɛ F irst(y )/ɛ F irst(z) Y {d, ɛ F irst(z) Z {e F ollow(a) a {a F irst(s) b {b F ollow(s) c {c F irst(s) d {d F irst(s) e {e F irst(s) Reconstruct the grammar by filling in the missing productions. Note that you can only define two productions. (And recall that X A B is two productions). (1) Ab (2) XYZ Since First(A) has c,d,e, (2) cannot start with a terminal, so we have to use X, Y, and Z to generate c,d,e. Since Z does not go to ɛ and Follow(A) Follow(Z), we can conclude that Z has to go last. Since First(Y) Follow(X) and First(Z) Follow(Y), we can conclude that the order has to be XYZ. First(S) has a,b,c,d,e, so (1) should be able to generate b,c,d,e in the first position. Since we have no rule that produces b, it needs to have b, but the b cannot be the first position, since that wouldn t allow another character to be in the first position. Follow(S) Follow(b), so b would be the last character in this production. To generate c,d,e in the first position we have to use A, since it also has to be able to go to ɛ. Follow(A) = b, so we know there are no other (non-)terminals in between the A and b. (Something like Abbbb would also work, though we didn t directly specify b Follow(b)) 2

2. Semantic Actions (20 points) Consider the following grammar for generating binary fractions. F 0.B B 0B 1B 0 1 Fill in the missing semantic actions below to calculate the decimal value of an input string. Recall that the numeric value of a binary fraction 0.b 1 b 2... b n is calculated as Σ n i=1 b i 2 i. Each non-terminal has a synthesized attribute val that is used to store its value. The final value should be returned in F.val. F 0.B { F.val = B.val B 0 0B 1 { B 0 1B 1 { B 0 { B 1 { F 0.B {F.val = B.val B 0 0B 1 {B 0.val = B 1.val/2 B 0 1B 1 {B 0.val = B 1.val/2 + 1/2 3

B 0 {B.val = 0 B 1 {B.val = 1/2 4

Consider a grammar for binary strings or their complements: N B B B B0 B1 0 1 The value of a (non-negated) string is just the decimal value of the binary number the string represents; the value of a negated string is the decimal value of the string with 1 s replaced by 0 s and 0 s replaced by 1 s. For example, the value of 010 is 2 and 010 is 5. Fill in the semantic actions below to calculate the decimal value of an input string. Each non-terminal has a synthesized attribute val that is used to store its value, with the final value in N.val. Your solution must use at least one inherited attribute. N B { N B { B 0 B 1 0 { B 0 B 1 1 { B 0 { B 1 { 5

N B {N.val = B.val; B.neg = false N B {N.val = B.val; B.neg = true B 0 B 1 0 {B 0.val = 2 B 1 + (B 0.neg)?1 : 0; B 1.neg = B 0.neg B 0 B 1 1 {B 0.val = 2 B 1 + (B 0.neg)?0 : 1; B 1.neg = B 0.neg B 0 {B.val = (B.neg)?1 : 0 B 1 {B.val = (B.neg)?0 : 1 6

3. Parsing Algorithms (20 points) Give a grammar that is SLR(1) but cannot be recognized by a recursive descent parser. Your grammar must have the following properties: It should contain no more than two productions (again, remember X A B is two productions). The language generated by the grammar should contain at least two distinct strings. The grammar should be as simple as possible, given the preceding two constraints. A very simple grammar that has these properties is S Sa b. This grammar is SLR(1), but cannot be recognized by a recursive descent parser because it is left recursive. Prove that your grammar is SLR(1). The NFA of viable prefixes for this grammar is given by: Here, there is a potential shift/reduce conflict in State 1 on input a, however this conflict can be resolved by SLR(1) rules. Since a is not in Follow(start), the conflict is resolved in favor of a shift. Is your grammar also LR(0)? Why or why not? This grammar is not LR(0) because there is a shift/reduce conflict in state 1 between S S.a (shift) and start S. (reduction). 7

4. Lexical Analysis and Parsing (20 points) After completing many tedious CS143 problem sets involving context free grammars, you have decided that you want to automate common tasks on grammars. To do this, you first need to write a lexer and parser for parsing valid context free grammars. You decide that the input grammars must obey the following specification: Every non-terminal starts with an uppercase, followed by any alphanumeric characters. Every terminal starts with a lowercase, followed by any alphanumeric characters. A valid grammar may contain any number (including 0) productions, separated by newlines. The right-hand-side of productions cannot be empty. For example, the following is a valid input grammar: Nt1 -> term1 Nt2 term2 Nt3.. Nt2 -> term3..... Write a flex specification that recognizes the following tokens: term, the token representing terminals non term, representing non-terminals arrow, representing > sep, representing end, representing \n (newline) You do not need to provide actions to record the lexemes; simply indicate after your flex rule in { which token is matched. 8

[a-z][a-z,a-z,0-9]* { term [A-Z][a-z,A-Z,0-9]* { non_term -> { arrow { sep \n { end Assuming you have tokenized the input using your flex specification, give a grammar that recognizes CFG grammars. G > ɛ P G P > non term arrow R end R > R sep U U U > non term U term U non term term Is it possible for an input to have no lexer or parser error and still be malformed, in the sense that it is not a valid grammar? Justify your answer. Yes, consider the grammar: S -> A Here, A is undefined since no production contains A on its LHS. This is not a context free property and can therefore never be detected by the lexer or parser. 9