CS 164 Programming Languages and Compilers Handout 8. Midterm I

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

Midterm I - Solution CS164, Spring 2014

Midterm I (Solutions) CS164, Spring 2002

UNIVERSITY OF CALIFORNIA

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

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

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

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

Syntax Analysis. Chapter 4

UNIT III & IV. Bottom up parsing

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

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

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

CS143 Midterm Fall 2008

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

CS143 Midterm Sample Solution Fall 2010

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

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

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

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

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

LALR Parsing. What Yacc and most compilers employ.

Principles of Programming Languages

2068 (I) Attempt all questions.

CSE 401 Midterm Exam 11/5/10

CS164: Midterm I. Fall 2003

MODULE 14 SLR PARSER LR(0) ITEMS

shift-reduce parsing

Lecture 8: Deterministic Bottom-Up Parsing

Syntax Analysis Part I

Lecture 7: Deterministic Bottom-Up Parsing

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

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

Question Points Score

CS143 Midterm Spring 2014

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

CS164 Second Midterm Exam Fall 2014

Bottom-Up Parsing. Lecture 11-12

Compiler Construction Using

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

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

Syntax Analysis Check syntax and construct abstract syntax tree

Question Bank. 10CS63:Compiler Design

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

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

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

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

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

Compiler Construction

LR Parsing. The first L means the input string is processed from left to right.

Wednesday, August 31, Parsers

Compiler Design Aug 1996

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Lecture Notes on Bottom-Up LR Parsing

Concepts Introduced in Chapter 4

Downloaded from Page 1. LR Parsing

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

Bottom-Up Parsing II. Lecture 8

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

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

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

Conflicts in LR Parsing and More LR Parsing Types

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

Homework & Announcements

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

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

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

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

Bottom-Up Parsing. Lecture 11-12

LR Parsing Techniques

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

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

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

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

ECE 468/573 Midterm 1 September 30, 2015

Syntax-Directed Translation

HW 3: Bottom-Up Parsing Techniques

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Non-deterministic Finite Automata (NFA)

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

Compiler Construction

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

CS606- compiler instruction Solved MCQS From Midterm Papers

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

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

CSE 582 Autumn 2002 Exam 11/26/02

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

CSCE 314 Programming Languages

Principles of Programming Languages COMP251: Syntax and Grammars

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

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).

Context-free grammars

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

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 2016

Transcription:

Mterm I Please read all instructions (including these) carefully. There are six questions on the exam, each worth between 15 and 30 points. You have 3 hours 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 proved 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. olutions will be graded on correctness and clarity. There are no \tricky" problems on the exam 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. NAME: ample solution ID or #: Problem Max points Points 1 15 2 15 3 20 4 20 5 30 6 30 TOTAL 130 Fall 95 page 1 of 11

1. Regular Expressions (1) The following description of integer constants in C++ appears on page 480 of \The C++ Programming Language." An integer constant consisting of a sequence of digits is taken to be decimal (base ten) unless it begins with 0 (digit zero). A sequence of digits starting with 0 is taken to be an octal integer (base eight). The digits 8 and 9 are not octal digits. A sequence of digits preceded by 0x or 0X is taken to be a hexadecimal integer (base sixteen). The hexadecimal digits include a or A through f or F :::. The typeofaninteger depends on its :::sux. [An integer may have no sux.] :::If [the integer] is suxed by uor U, its type is :::. If it is suxed by l or L, its type is :::. [Either or both suxes may appear at most once each in either order.] Give a regular expression for C++ integer constants. Use only the operations +,, and concatenation in your expression (no special ex notation). You may also use parentheses and (which are not operations). For convenience, you may name a regular expression R by writing name = R. [spaces are not significant] octal = 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 nonzero = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 decimal = 0 + nonzero hex = decimal + a + A + b + B + c + C + d + D + e + E + f + F optu = u + U + epsilon optl = l + L + epsilon suffix = (optu optl)+(optl optu) number = (octal octal*) + (nozero decimal*) + ((0x + 0X) hex hex*) integer = number suffix [Note: We also allowed (0x hex*)] Fall 95 page 2 of 11

2. Finite Automata and Regular Expressions (1) For each of the following nite automata, give an equivalent regular expression. Clearly mark your answer to each part. As in the previous problem, use only the basic regular expression notation. (a) 0 1 RE: 0*1 (b) 0 1 RE: (0 + 1)* (c) 1 0 1 0 0 1 1 RE: ((10 (10)*) + (01 (01)*)) 10 Fall 95 page 3 of 11

3. Grammars (20 points) Give acontext-free grammar for each of the following languages. (a) Any string of x's followed by an equal number of y's. 7 points -> xy epsilon (b) Any string of x's of length n followed by a string of y's of length 2n + 1, for any n 0. 7 points -> xyy y (c) Any string of x's and y's with an equal number of x's and y's. The x's and y's may appear in any order. 6 points -> xy yx epsilon Fall 95 page 4 of 11

4. Top-Down Parsing (20 points) Conser the following grammar for a subset of English sentences. The nonterminals are NP VP AP. The terminals are the, noun, adective, verb. The start symbol is.! NP VP NP VP NP NP! the AP noun AP! AP adective VP! verb Give an LL(1) grammar that generates the same language as this grammar. how the parsing table for the grammar you produce. a) Left factor! NP VP NP opt NP opt! NP NP! the AP noun AP! AP adective VP! verb b) Eliminate left recursion! NP VP NP opt NP opt! NP NP! the AP noun AP! adective AP VP! verb c) Construct table the noun verb adective $! NP VP NP opt NP opt NP opt! NP NP opt! NP NP! the AP noun AP AP! AP! adective AP VP VP! verb Fall 95 page 5 of 11

5. LALR Parsing and Conicts (30 points) Conser the following DFA of LR(1) items for a LALR(1) parser. The states are numbered refer to these numbers where appropriate in the following questions. [ ->., $ ] [ ->. :=, $ ] [ ->. +, $ ] [ ->., $ ] [ ->. string, $ ] [ ->., $ ] [ ->. +, $ ] 6 + 3 [ -> +., $/+ ] [ ->. :=, $/+ ] [ ->. +, $/+ ] [ ->., $/+ ] [ ->. string, $/+ ] 1 string [ ->. :=, $/+ ] [ ->., $/+ ] [ -> string., $/+ ] string + 4 string 2 [ -> :=., $/+ ] [ ->. +, $/+ ] := [ -> :=., $/+ ] [ ->. :=, $/+ ] [ ->. +, $/+ ] [ ->., $/+ ] [ ->. string, $/+ ] 8 5 7 + [ -> +., $/+ ] [ ->. +, $/+ ] Fall 95 page 6 of 11

(a) Give the grammar used in constructing the DFA. -> string + := (b) how that the grammar is ambiguous. The string + + has two parse trees: + + + + (c) Which of the following strings are viable prexes? For each string that is a viable prex, give a sux that produces a right sentential form. i. 2 points Yes: suffix: $ ii. + + 1 point No. iii. + := 2 points Yes: suffix: $ (d) Which states of the DFA have conicts and of what kind (shift-reduce or reduce-reduce)? Be sure to state clearly the cause of the conict in each case. tate 7 has a shift-reduce conflict on input +. tate 8 has a shift-reduce conflict on input +. Fall 95 page 7 of 11

(e) Which conicts should be resolved in which way to give the := operator higher precedence than +? Reduce should be chosen in state 8. (f) Which conicts should be resolved in which way to cause the + operator to associate to the right? hift should be chosen in state 7. Fall 95 page 8 of 11

6. Error Recovery (30 points) This problem uses the same DFA as the previous problem. You will need to refer to that DFA again to answer the parts of this question. Assume that any shift/reduce conicts are resolved in favor of reducing. (a) Conser the string := string string how the sequence of congurations of the LALR(1) parsing algorithm as it parses this string according to the DFA of the previous problem. Indicate exactly where the error is detected. 10 points (# := string string) ( # := string string) ( := # string string) ( := string # string) Error: cannot reduce. (b) Conser a new action panic N t, wheren is a non-terminal and t is a terminal. The meaning of the action is to push N on the stack and discard all input tokens up to, but not including, the next t. If there is no next t, then N is pushed on the stack and all remaining input is discarded. Assume we want the parser to take action panic + whenever it is in conguration +#w and the rst token of w is erroneous. Which entries in the action table should have panic +? (You do not need to give the entire action table to answer the question ust indicate which entries have the new action.) how the sequence of congurations of the parser on input 10 points + := ++ action(6, :=) = panic + action(6, +) = panic + action(6, $) = panic + (# + := + + ) ( # + := + + ) ( # + := + + ) ( + # := + + ) ( + # + + ) ( # + + ) ( + # + ) ( + # + ) ( # + ) ( + # ) ( + #) Fall 95 page 9 of 11

( + #) ( #) (' #) Fall 95 page 10 of 11

(c) Conser a new action insert t where t is a terminal. The meaning of the action is to insert t before the next input token (i.e., t becomes the next input). Give insert actions that implement each of the following local recovery strategies. Be sure to say which entries in the action table have the insert actions. i. If the input contains an followed by astring, insert an := between them. action(2, string) = insert := ii. If the input contains two consecutive strings, insert a + between them. action(4, string) = insert + iii. If the input contains a string followed by an, insert a + between them. action(4, ) = insert + Fall 95 page 11 of 11