Total 100

Similar documents
Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I Solutions

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

Topic 5: semantic analysis. 5.2 Attribute Grammars

Exploring the Commonality in Feature Modeling Notations

COMP 181. Prelude. Intermediate representations. Today. Types of IRs. High-level IR. Intermediate representations and code generation

ECE 573 Midterm 1 September 29, 2009

Algorithms for External Memory Lecture 6 Graph Algorithms - Weighted List Ranking

Reading Object Code. A Visible/Z Lesson

Reading Object Code. A Visible/Z Lesson

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

Algorithms, Mechanisms and Procedures for the Computer-aided Project Generation System

13.1 Numerical Evaluation of Integrals Over One Dimension

On - Line Path Delay Fault Testing of Omega MINs M. Bellos 1, E. Kalligeros 1, D. Nikolos 1,2 & H. T. Vergos 1,2

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

Boosted Random Forest

Compilation Lecture 11a. Register Allocation Noam Rinetzky. Text book: Modern compiler implementation in C Andrew A.

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

Midterm I (Solutions) CS164, Spring 2002

XML Data Streams. XML Stream Processing. XML Stream Processing. Yanlei Diao. University of Massachusetts Amherst

shift-reduce parsing

Parsing. Earley Parsing. Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 39

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

Outline: Software Design

CS606- compiler instruction Solved MCQS From Midterm Papers

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

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

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

Pipelined Multipliers for Reconfigurable Hardware

Parsing III. (Top-down parsing: recursive descent & LL(1) )

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

Gray Codes for Reflectable Languages

LR Parsing E T + E T 1 T

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

Compiler Construction

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

ZDT -A Debugging Program for the Z80

Runtime Support for OOLs Part II Comp 412

LR Parsing Techniques

CS143 Midterm Spring 2014

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

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

Dynamic Programming. Lecture #8 of Algorithms, Data structures and Complexity. Joost-Pieter Katoen Formal Methods and Tools Group

Syntax-Directed Translation Part II

OvidSP Quick Reference Card

Lecture Notes on Shift-Reduce Parsing

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

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

CleanUp: Improving Quadrilateral Finite Element Meshes

Interconnection Styles

Extracting Partition Statistics from Semistructured Data

3. Parsing. Oscar Nierstrasz

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

LR Parsers. Aditi Raste, CCOEW

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

Space- and Time-Efficient BDD Construction via Working Set Control

The Minimum Redundancy Maximum Relevance Approach to Building Sparse Support Vector Machines

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

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

Recursion examples: Problem 2. (More) Recursion and Lists. Tail recursion. Recursion examples: Problem 2. Recursion examples: Problem 3

Unsupervised Stereoscopic Video Object Segmentation Based on Active Contours and Retrainable Neural Networks

A DYNAMIC ACCESS CONTROL WITH BINARY KEY-PAIR

CS 314 Principles of Programming Languages. Lecture 3

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

Learning Convention Propagation in BeerAdvocate Reviews from a etwork Perspective. Abstract

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

WORKSHOP 20 CREATING PCL FUNCTIONS

Contents Contents...I List of Tables...VIII List of Figures...IX 1. Introduction Information Retrieval... 8

Compiler Construction

CS 314 Principles of Programming Languages

Run Time Environment. Implementing Object-Oriented Languages

Red-Black Trees 10/19/2009. Red-Black Trees. Example. Red-Black Properties. Black Height. Example

BSPLND, A B-Spline N-Dimensional Package for Scattered Data Interpolation

CS143 Midterm Fall 2008

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

Semantic Analysis with Attribute Grammars Part 1

Defining syntax using CFGs

CSCI312 Principles of Programming Languages

Automatic Physical Design Tuning: Workload as a Sequence Sanjay Agrawal Microsoft Research One Microsoft Way Redmond, WA, USA +1-(425)

CSE431 Translation of Computer Languages

Partial Character Decoding for Improved Regular Expression Matching in FPGAs

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

CSCE 531, Spring 2015 Final Exam Answer Key

COP4020 Spring 2011 Midterm Exam

A {k, n}-secret Sharing Scheme for Color Images

Downloaded from Page 1. LR Parsing

CS143 Midterm Sample Solution Fall 2010

CS 4120 Introduction to Compilers

A Novel Bit Level Time Series Representation with Implication of Similarity Search and Clustering

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

Types of parsing. CMSC 430 Lecture 4, Page 1

Adobe Certified Associate

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

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

CS164: Midterm I. Fall 2003

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class

Conflicts in LR Parsing and More LR Parsing Types

Lecture Notes of Möbuis Transformation in Hyperbolic Plane

LL parsing Nullable, FIRST, and FOLLOW

Constructing Transaction Serialization Order for Incremental. Data Warehouse Refresh. Ming-Ling Lo and Hui-I Hsiao. IBM T. J. Watson Research Center

UNIT III & IV. Bottom up parsing

Transcription:

CS331 SOLUTION Problem # Points 1 10 2 15 3 25 4 20 5 15 6 15 Total 100 1. ssume you are dealing with a ompiler for a Java-like language. For eah of the following errors, irle whih phase would normally issue the message: (L) lexial analysis, (P) parsing, (S) semanti analysis, (R) runtime, or (N) none. Misspelled built-in proedure name (e.g. Wirte for Write ) rray index out of bounds One-dimensional array aessed with two subsripts Identifier name that begins with an undersore Wrong number of arguments in funtion all S R S L S 2. For eah grammar below, answer yes (Y) or no (N) to eah of the three questions in the appropriate spae. You don t need to go through the entire parser onstrution to answer these. Is this grammar: LL(1)? LR(1)? Unambiguous? S asa asb N Y Y S Sa bs N N N S asa bs Y N Y S asa ɛ N N Y S Sa b N Y Y

3. Let G be the following CFG grammar: 1. S b 2. bd 3. d 4. ɛ a. Fill in the missing states (with items) in the partial LR(1) DF given below. 12 : [ d, b] 13 : [ bd, b] d d 2 : [S S, $] 9 : [ d, b] 4 : [ b d, b] S 0 : [S S, $] [S b, $] [ bd, b] [ d, b] [, b] 5 : [ bd, b] [ d, b] [ bd, d] [ d, d] [, d] b 6 : [ b d, b] [ bd, d] [ d, d] [, d] 1 : [S b, $] 7 : [ bd, d] [ d, d] [ bd, d] [ d, d] [, d] 10 : [ d, d] d b b 11 : [ d, d] 3 : [S b, $] 8 : [ b d, d] [ bd, d] [ d, d] [, d] 14 : [ b d, d] d 15 : [ bd, d] Page 2

b. Fill in the tion and Goto tables for state 0. (Note: eof is the end of input symbol.) CTION GOTO b d eof S 0 R 4 Shift 5 error error 2 1. How would an SLR(1) parser table differ from the table for this LR(1) parser? In general, an SLR parser would use follow sets when filling the ells of the tion table to determine the lookahead, rather than using the expliit lookahead attahed to the LR(1) items. Speifially, an SLR parser would use follow() determine the lookaheads for the produtions in state 5. Sine b is in follow() (f. prodution S b), the ation for State 5 on input b would inlude Shift b (due to the item [ bd, b]) but also Redue ɛ. Therefore, the SLR parser for this grammar would ontain a shift/redue onflit. The SLR parser would also have fewer states, sine without the differenes due to lookahead symbols, states 4 and 14, 5 and 7, 13 and 15, 9 and 10, 11 and 12, and 6 and 8 would be the same. d. How would an LLR(1) parser table differ from the table for this LR(1) parser? n LLR parser would ombine states 4 and 14, 5 and 7, 13 and 15, 9 and 10, 11 and 12, and 6 and 8 beause they have the same ores i.e., they differ in lookahead only. e. Give one prodution whih, if added to the grammar above, would introdue a shift/redue onflit. Explain your answer by indiating whih state would hange and how the onflit would be realized in the DF. There are many produtions that, if added to the grammar, would produe a shift/redue onflit. One is b, whih would add [ b, b] to the set of items for state 0, and a new state for GOTO(S 0, b) would be introdued. Thus, in state 0 there would be the option to shift b and go to the newly introdued state, or redue by ɛ on b. I.e., the entry in the parse table for state 0, input b would ontain both Shift and Redue by ɛ, reating the shift/redue onflit. Page 3

4. Consider the following grammar: E B &B ɛ B true B false where {E,, B} is the set of nonterminal symbols, E is the start symbol, {&, true, false} is the set of terminal symbols, and ɛ denotes the empty string. a. Compute the following sets: first (B) first () follow () follow (B) true, false &, ɛ eof &, eof b. Construt the LL(1) parse table for this grammar. & true false eof E E B E B &B ɛ B B true B false Page 4

. Trae the stak and input ontents for a parse of the string true & false & true (you may not need all the rows). Stak E eof B eof true eof eof &B eof B eof false eof eof &B eof B eof true eof eof eof Remaining input true & false & true eof true & false & true eof true & false & true eof & false & true eof & false & true eof false & true eof false & true eof & true eof & true eof true eof true eof eof eof aept Page 5

5. Using the grammar and parse table below, show the steps of an LR(1) parser on input string a b b by filling in the table on the next page (you may not need all the rows). eof is the end of input signal, and a indiates ept. Produtions in the table are referened by number. 0: S 1: a 2: 3: b CTION GOTO State a b eof S 1 s2 s3 5 4 2 s2 s3 6 3 r3 r3 r3 r3 4 s2 s3 r0 7 5 a 6 s3 s8 7 7 r2 r2 r2 r2 8 r1 r1 r1 r1 Stak Input tion [1 $] abb$ Shift 2 [1 $][2 a] bb$ Shift 3 [1 $][2 a][3 b] b$ Redue 3: b [1 $][2 a][6 ] $ Shift 3 [1 $][2 a][6 ][3 b] $ Redue 3: b [1 $][2 a][6 ][7 ] $ Redue 2: [1 $][2 a][6 ] $ Shift 8 [1 $][2 a][6 ][8 ] $ Redue 1: a [1 $][4 ] $ Redue 0: S [1 $][5 S] $ CCEPT Page 6

6. Given the grammar: S E E E + E (E) id num E = E where S and E are non-terminals and all the other grammar symbols are terminals. ssume that the above grammar represents expressions suh as a = (b = ) a = (b = + 1) a. Extend the above grammar with attributes suh that S has a boolean synthesized attribute whih is true iff the left hand side of any assignments derived from S are simple ids. So for all of the examples above, the attribute should be true, but for (a + b) = or (a = b) = the attribute should be false. Note that a statement like (a) = 1 should also yield true. The approah is straightforward. We add boolean synthesized attribute Simple to both S and E, whih keeps trak of whether or not the subtree below a given E node has a single id (or a parenthesized id) as a left hand side of an equal sign. We also add a boolean synthesized attribute V alid to E, whih indiates whether the subtree below a given E node an serve as a valid left-hand side. This results in the following grammar: Prodution S E E E + E E (E 1 ) E id E num E E 1 = E 2 Semanti Rule S.V alid = E.V alid; E.Simple = false; E.V alid = E 1.V alid && E 2.V alid; E.Simple = E 1.Simple; E.V alid = E 1.V alid; E.V alid = E.Simple = true; E.V alid = true; E.Simple = false; E.Simple = false; E.V alid = E 1.V alid && E 2.V alid && E 1.Simple; Page 7

b. List the attributes in your extended grammar and indiate whether eah is synthesized or inherited. Explain your answer. Simple is a synthesized attribute that is used to determine if the left-hand side value is a simple id. It is initialized at nodes of the tree labeled with E that resolve to id (given the value true) and num (given the value false). From there, values of the attribute are passed to parent nodes based on omputations involving the values of the attribute for the hildren. V alid is a synthesized attribute that keeps trak of whether or not the left-hand side of the urrent expression (derived from the E in the given prodution) is valid, i.e., is a simple id.. Give an attributed parse tree for the soure string a = (b = ) and deorate the tree with the appropriate attributes and values. S E Simple=false E Simple=true = E Simple=false id a ( E Simple=false ) E Simple=true = E Simple=true id id b Page 8