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

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

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Shift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.

CA Compiler Construction

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

Parsing -3. A View During TD Parsing

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

Top-Down Parsing and Intro to Bottom-Up Parsing

Compiler Design Spring 2017

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

CSE 355 Test 2, Fall 2016

Context-Free Grammar

Syntax Analysis Part I

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Syntax Analysis Part I

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

Lecture 11 Sections 4.5, 4.7. Wed, Feb 18, 2009

THEORY OF COMPILATION

Why augment the grammar?

n Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)

1. For the following sub-problems, consider the following context-free grammar: S A$ (1) A xbc (2) A CB (3) B yb (4) C x (6)

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7

Context free languages

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

CSE302: Compiler Design

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Syntax Directed Transla1on

Syntax Analysis (Part 2)

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Notes for Comp 497 (Comp 454) Week 10 4/5/05

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos

Syntactic Analysis. Top-Down Parsing

Creating a Recursive Descent Parse Table

Parsing VI LR(1) Parsers

Compiler Construction Lent Term 2015 Lectures (of 16)

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

Compiler Construction Lent Term 2015 Lectures (of 16)

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

1. For the following sub-problems, consider the following context-free grammar: S AB$ (1) A xax (2) A B (3) B yby (5) B A (6)

Compiler Construction Lectures 13 16

Conflict Removal. Less Than, Equals ( <= ) Conflict

Type 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic.

Translator Design Lecture 16 Constructing SLR Parsing Tables

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions

Announcements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75

Notes for Comp 497 (454) Week 10

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

CS415 Compilers Syntax Analysis Top-down Parsing

Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008

1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4)

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

CPS 220 Theory of Computation

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. source code. IR IR target.

Computer Science 160 Translation of Programming Languages

Computing if a token can follow

Bottom-Up Syntax Analysis

Compiler Principles, PS4

CS375 Midterm Exam Solution Set (Fall 2017)

Fundamentele Informatica II

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Computational Models - Lecture 4

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412

CS20a: summary (Oct 24, 2002)

Theory Bridge Exam Example Questions

CS415 Compilers Syntax Analysis Bottom-up Parsing

Non-context-Free Languages. CS215, Lecture 5 c

CS 406: Bottom-Up Parsing

Pushdown Automata: Introduction (2)

Bottom-Up Syntax Analysis

CS 314 Principles of Programming Languages

Section 1 (closed-book) Total points 30

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

10. The GNFA method is used to show that

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form

Syntax Analysis Part III

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

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

MA/CSSE 474 Theory of Computation

Einführung in die Computerlinguistik

Problem Session 5 (CFGs) Talk about the building blocks of CFGs: S 0S 1S ε - everything. S 0S0 1S1 A - waw R. S 0S0 0S1 1S0 1S1 A - xay, where x = y.

Homework. Context Free Languages. Announcements. Before We Start. Languages. Plan for today. Final Exam Dates have been announced.

Name: Student ID: Instructions:

Bottom-Up Syntax Analysis

Context-Free Grammars: Normal Forms

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Syntax Analysis, VI Examples from LR Parsing. Comp 412

CS153: Compilers Lecture 5: LL Parsing

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

Context Free Grammars

Chapter 4: Bottom-up Analysis 106 / 338

Pushdown Automata. Reading: Chapter 6

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}) { E E E + E E E - E E E id } ii) I b = closure({e + EE}) { E + EE E + E E E - E E E id } iii) I c = closure({e +E E}) { E + E E E + E E E - E E E id } iv) I d = closure({e id }) { E id } (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] [goto] I c ii. M[I d, +] Reduce E id iii. M[I d,$] Reduce E id 1

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. With one symbol lookahead, we have one column for each terminal and one for each non-terminal, so N + T columns. For k lookahead, we still have one column for each non-terminal and each individual terminal, but a column must be added for each possible combination of k symbols. So when k = 2, we have T + T(T-1) + N columns. For larger values of k, 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. First, eliminate left recursion: E TE E + TE ε T id id ( ) id ( L ) L E ; L E Then eliminate common prefixes by left factoring: E TE E + TE ε T id T T ( T ε T ) L ) L E L L ; L ε (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). Remember a grammar G is LL(1) iff whenever A u v are two distinct productions of G, the following conditions hold: For no terminal a do both u and v derive strings beginning with a. At most one of u and v can derive the empty string (ε), and If v =>* ε then u does not derive any string beginning with an element in FOLLOW(A). By constructing these FIRST and FOLLOW sets, we can prove the three requirements are satisfied: E + T E e FIRST ( +TE ) = { + } FOLLOW( E ) = { $, ), ; } T ε ( T FIRST ( T ) = { ( } FOLLOW( T ) = { +, $, ; } T ) L) FIRST ( ) ) = { ) } FIRST ( L) ) = { id } L ; L ε FIRST ( ;L ) = { ; } FOLLOW( L ) = { ) } 3

E, T, T, and L are the only nonterminals with two productions. 1. Only T has 2 rules deriving non-null strings, and their FIRST sets are distinct. 2. At most of one rule of E, T and L derives ε, since the other always includes a terminal. 3. E, T and L are nullable, but the non-null rule of each RHS doesn t derive any string beginning with a symbol in the FOLLOW set of the LHS. 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. E unsigned_int { E.sign = POS ; } + E 1 { E.sign = E 1.sign ; } - E 1 { if (E 1.sign == POS) E.sign = NEG ; else E.sign = POS ; } E 1 * E 2 { if (E 1.sign == E 2.sign) E.sign = POS ; else E.sign = NEG ; } (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. E.sign = NEG E E.sign = POS E * E E.sign = NEG 2 - E E.sign = POS 3 4

(c) Is E.sign an inherited attribute or a synthesized attribute? Synthesized, as it is propagated from the leaf nodes upwards through the tree. 5

5. Consider the following grammar G: S Sa bl L ScL Sd a (a) Remove left recursion from G. The result is called G. S bls S as ε L ScL Sd a (b) Left factor G. The result is G. S bls S as ε L SL a L cl d (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 S b c, d, $ S a, ε c, d, $ L a, b a L c, d a (d) (e) (f) Fill in the LL(1) parse table for this grammar. 6

Non-terminal a b c d $ S S bls S S as S ε S ε S ε L L a L SL L L cl L d (e) Using the parse table in (d), show a top-down parse of the string bbacbada$. Stack (with top at left) Input Action S$ bbacbada$ S bls bls $ bbacbada$ match b LS $ bacbada$ L SL SL S $ bacbada$ S bls bls L S $ bacbada$ match b LS L S $ acbada$ L a as L S $ acbada$ match a S L S $ cbada$ S ε L S $ cbada$ L cl cls $ cbada$ match c LS $ bada$ L SL SL S $ bada$ S bls bls L S $ bada$ match b LS L S $ ada$ L a as L S $ ada$ match a S L S $ da$ S ε L S $ da$ L d ds $ da$ match d S $ a$ S as as $ a$ match a S $ $ S ε $ $ ACCEPT 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)? Stack (with top at right) Input Action $ bbbba$ shift b $b bbba$ shift b $bb bba$ CONFLICT: reduce A ε or shift b? There is always a conflict because (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 8

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)? 9

(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. 10

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? 11

(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. 12