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

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

Principles of Programming Languages

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

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

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

Midterm I - Solution CS164, Spring 2014

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

CS143 Midterm Sample Solution Fall 2010

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

Bottom-Up Parsing LR Parsing

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

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

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

shift-reduce parsing

CS143 Midterm Spring 2014

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

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

CS164: Midterm I. Fall 2003

Midterm I (Solutions) CS164, Spring 2002

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

LR Parsing LALR Parser Generators

Conflicts in LR Parsing and More LR Parsing Types

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

Wednesday, August 31, Parsers

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

UNIT III & IV. Bottom up parsing

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

LR Parsing LALR Parser Generators

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

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

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

CS143 Midterm Fall 2008

Wednesday, September 9, 15. Parsers

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

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

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

LR Parsers. Aditi Raste, CCOEW

CS 4120 Introduction to Compilers

QUESTIONS RELATED TO UNIT I, II And III

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Monday, September 13, Parsers

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

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

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

CS 164 Programming Languages and Compilers Handout 8. Midterm I

Lexical and Syntax Analysis. Bottom-Up Parsing

In One Slide. Outline. LR Parsing. Table Construction

MODULE 14 SLR PARSER LR(0) ITEMS

Context-free grammars

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Parsing Expression Grammars and Packrat Parsing. Aaron Moss

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

Compiler Construction: Parsing

Downloaded from Page 1. LR Parsing

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

LALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the

CSCI Compiler Design

Bottom-Up Parsing. Lecture 11-12

CS606- compiler instruction Solved MCQS From Midterm Papers

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

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

Bottom-Up Parsing. Lecture 11-12

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR Parse Tables by Wim Bohm and Michelle Strout. CS453 Shift-reduce Parsing 1

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

Syn S t yn a t x a Ana x lysi y s si 1

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

Lecture 8: Deterministic Bottom-Up Parsing

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Question Bank. 10CS63:Compiler Design

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

CS308 Compiler Principles Syntax Analyzer Li Jiang

CSCI Compiler Design

LR Parsing E T + E T 1 T

COMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )

Lecture 7: Deterministic Bottom-Up Parsing

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

LR Parsing - The Items

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur


LR Parsing Techniques

Compilers. Bottom-up Parsing. (original slides by Sam

UNIT-III BOTTOM-UP PARSING

Syntax-Directed Translation Part II

Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

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

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

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

Total 100

Syntax Analysis Part I

Compiler Design Aug 1996

Abstract Syntax Trees & Top-Down Parsing

COP4020 Spring 2011 Midterm Exam

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

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

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

Transcription:

EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write your final answers in the space provided on the exam. You may use the backs of the exam pages as scratch paper, or use additional pages (available at the front of the room). Each problem has a straightforward solution Solutions will be graded on correctness and clarity. Partial solutions will be given partial credit. NAME : Problem Max points Points 1 15 2 10 3 10 4 10 5 25 6 15 7 15 TOTAL 100

1. Consider the following augmented CFG for prefix expressions. E E E + E E - E E id (a) Compute the following closures of items. i) I a = closure({e E}) ii) I b = closure({e + EE}) iii) I c = closure({e +E E}) iv) I d = closure({e id }) 1

(b) Show the following entries of the SLR(1) parsing table M, where I b and I d indicate the states corresponding to the items sets computed in (a). i. M[I b, E] ii. M[I d, +] iii. M[I d,$] 2. Consider extending the LR(1) case to the general k lookahead symbols. Given an LR(k) grammar with N nonterminals and T terminals, how many columns would we have in a corresponding LR(k) action/goto table? State your assumptions and show your work for full credit. 2

3. Consider the following grammar G. E E + T T T id id ( ) id ( L ) L E ; L E (a) Give an LL(1) grammar that generates the same language as G. (b) Prove that your grammar is LL(1) by whatever method you like. (Hint: This does not require an exhaustive construction.) Don t just restate the definition of LL(1). 3

4. Here is an attribute grammar G for arithmetic expressions using multiplication, unary -, and unary +. S E { if (E.sign == POS) print( result is positive ); else print( result is negative ); } E unsigned_int + E - E E * E (a) Add semantic actions to compute an attribute E.sign for non-terminal E to record whether the arithmetic value of E is positive or negative. The attribute sign can have two values, either POS or NEG. To get you started, the first rule is provided. (b) Show the parse tree for input 2 * - 3 (where 2 and 3 are unsigned_ints ). Indicate at each node what the values of associated attributes are. (c) Is E.sign an inherited attribute or a synthesized attribute? 4

5. Consider the following grammar G: S Sa bl L ScL Sd a (a) Remove left recursion from G. The result is called G. (b) Left factor G. The result is G. 5

(c) Fill in the table below with the FIRST and FOLLOW sets for the non-terminals in grammar G (note that the number of rows may be more than you need): Non-terminal First Follow (d) Fill in the LL(1) parse table for this grammar. Non-terminal a b c d $ 6

(e) Using the parse table in (d), show a top-down parse of the string bbacbada. Stack (with top at left) Input Action 7

6. Consider the following simple context free grammars: G 1 : G 2 : S Aa S Aa A ε A ε A bab A Abb Note that the grammars generate the same language: strings consisting of even numbers of b s (including 0 of them), followed by an a. (a) Attempt to show a shift-reduce parse of the string bbbba for a parser for grammar G 1. Show the contents of the stack, the input, and the actions (i.e., shift, reduce, error, accept). You don t need to create a parse table; just use your knowledge of the grammar and how the parser works. Be sure to indicate any conflicts and explain why they are conflicts. Is G 1 LR(1)? Is G 1 LR(0)?. (b) Attempt to show a shift-reduce parse of the string bbbba for a parser for grammar G 2. Show the contents of the stack, the input, and the actions (i.e., shift, reduce, error, accept). You don t need to create a parse table; just use your knowledge of the grammar and how the parser works. Be sure to indicate any conflicts and explain why they are conflicts. Is G 2 LR(1)? Is G 2 LR(0)? 8

(c) Indicate whether G 1 and G 2 are LL(1) and explain your answer. You don t need to construct the parse tables, but may argue from other properties. (d) Of the language classes we have discussed, which is the smallest category into which L(G 1 ) fits? Justify your answer. 9

7. Consider the following GOTO graph of an LALR(1) parser. Notice the state numbers and refer to them in the answers to the following questions. State 1 S S, {$} S q w S, {$} S S z S, {$} S q, {$} S x, {$} S q State 2 S q w S, {$,z} S q, {$,z} q w State 3 State 6 S S, {$} S S z S, {$} z S S z S, {$,z} S q w S, {$,z} S S z S, {$,z} S q, {$,z} S x, {$,z} q x State 4 S x, {$,z} z x S State 5 S q w S, {$,z} S q w S, {$,z} S S z S, {$,z} S q, {$,z} S x, {$,z} S z State 7 S S z S, {$,z} S S z S, {$,z} State 8 S q w S, {$,z} S S z S, {$,z} (a) Which states of the parser have conflicts? What kind of conflicts are they, and under what circumstances do they arise? 10

(b) Describe the possible contents of the stack when the parser is in state 5. (c) Give a string that could remain in the input when the parser is in state 5 that would lead to a successful parse. 11