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

Similar documents
Midterm I (Solutions) CS164, Spring 2002

Midterm I - Solution CS164, Spring 2014

CS143 Midterm Sample Solution Fall 2010

CS143 Midterm Spring 2014

CS143 Midterm Fall 2008

CS164: Midterm I. Fall 2003

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

UNIVERSITY OF CALIFORNIA

CS 164 Programming Languages and Compilers Handout 8. Midterm I

ECE 468/573 Midterm 1 September 30, 2015

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

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

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

ECE 468/573 Midterm 1 October 1, 2014

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

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

shift-reduce parsing


CS Parsing 1

Bottom-Up Parsing LR Parsing

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

Lecture 7: Deterministic Bottom-Up Parsing

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

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

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

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

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

Lecture 8: Deterministic Bottom-Up Parsing

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

LR Parsing Techniques

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

Wednesday, September 9, 15. Parsers

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

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

CS 4120 Introduction to Compilers

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

HW 3: Bottom-Up Parsing Techniques

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

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

Chapter 3: Lexing and Parsing

Bottom-Up Parsing. Lecture 11-12

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Bottom-Up Parsing II. Lecture 8

More Bottom-Up Parsing

ECE468 Fall 2003, Final Exam

Monday, September 13, Parsers

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

CSE 401 Midterm Exam 11/5/10

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

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

CS606- compiler instruction Solved MCQS From Midterm Papers

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

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

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

In One Slide. Outline. LR Parsing. Table Construction

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

UNIT III & IV. Bottom up parsing

Bottom-Up Parsing. Lecture 11-12

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

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

Lexical Analysis. Chapter 2

CSE 401 Midterm Exam Sample Solution 2/11/15

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

Principles of Programming Languages

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


LR Parsing LALR Parser Generators

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

Compiler Construction 2016/2017 Syntax Analysis

Lexical and Syntax Analysis. Bottom-Up Parsing

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

VIVA QUESTIONS WITH ANSWERS

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

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

Implementation of Lexical Analysis

Lexical Analysis. Lecture 3-4

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

Wednesday, August 31, Parsers

Abstract Syntax Trees & Top-Down Parsing

Lecture Bottom-Up Parsing

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

CS164 Second Midterm Exam Fall 2014

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Lexical Analysis. Lecture 2-4

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

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

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

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

SLR parsers. LR(0) items

3. Parsing. Oscar Nierstrasz

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

LR Parsing LALR Parser Generators

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

Syntax Analysis Part I

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

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Syntax-Directed Translation

Conflicts in LR Parsing and More LR Parsing Types

Optimizing Finite Automata

Transcription:

Midterm I Solution Please read all instructions (including these) carefully. There are 5 questions on the exam, some with multiple parts. You have 1 hour and 20 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: Sam P. L. Solution SID or SS#: Problem Max points Points 1 15 2 25 3 20 4 25 5 15 TOTAL 100 Fall 2000 page 1 of 10

1. Regular Expressions and Finite Automata (15 points) Consider the design of a small language using only the letters z, o, and the slash character /. A comment in this language starts with /o and ends after the very next o/. Comments do not nest. Give a single regular expression that matches exactly one complete comment and nothing else. For full credit, use only the core regular expression notations A + B, AB, A, A +, ɛ, and abc. This is a classic regular expression puzzle, usually stated as describing C comments. We departed slightly from the usual /... / syntax to avoid confusion between * and. Regular expressions are not unique for a given language, so there are multiple correct answers. Here are just three of the many equally valid approaches: /o (o z + /) o + / /o / (o z / ) o + / /o (/ o z) / o + / The key feature of each of these solutions is the parenthesized expression in the middle. In each case, this parenthesized expression is designed to prevent an o from ever appearing immediately before a /. They all use a o z pattern to represent an optional sequence of o followed by a mandatory z. That is really the heart of the solution; the rest just takes care of letting slashes appear elsewhere in the body, and is not difficult to fill in after some careful thought. One other subtle point is worth noticing. Because o z always requires a z after each sequence of o in the comment body, we need some other way of dealing with a sequence of o that runs up to the very end of the comment. For this reason, all three solutions end with o + / rather than simply o/. If we had ended our patterns with o/, it would have been impossible to match a comment like /ooo/. Common errors: matching too long a string: the obvious /o (o + z + /) o/ pattern will incorrectly match /oo/zzzz/oo/ as one comment trying to prevent o/, but missing cases where we loop through some (... ) expression multiple times: for example, /o (/ + oz + oo) o + / not allowing a comment that has one or more / immediately after the opening /o not allowing a comment that has one or more o immediately before the closing o/ forbidding all o or all / from the body of a comment not allowing /o in the body of a comment not allowing comments of the form /oooo... oooo/ accepting /o/ as a comment Fall 2000 page 2 of 10

CS 164 Programming Languages and Compilers Handout 9 We did not ask for a finite automaton that recognizes the same language. Some of you did find it useful, though, to create an automaton first and then work backward to a regular expression. Here is the smallest possible deterministic finite automaton that matches the language described in this question: start /, z o / o o z / The key feature of the above DFA is the pair of states with self edges. The one on the left consumes any sequence of (/ + z). The one on the right consumes any sequence of o. Once a sequence of o has begun, though, the only way to get back to the (/ + z) consuming node is to read a z. If instead we read a /, we transition into the accepting state. The other important point is that the accepting state is a dead end; there are no transitions elsewhere. This ensures that we will not pick up anything else after the very first o/. Fall 2000 page 3 of 10

2. Semantic Actions (25 points) Consider the following grammars and associated semantic actions. In the actions, the operations And, Or, and Not are constructors for an abstract syntax tree data type. For each grammar, answer three things. Say whether each attribute of a non-terminal is inherited or synthesized and why. Show the value of the attributes of G after parsing (A (A B)). (a) G F G.p = F.p F F 1 F 2 F.p = And(F 1.p, F 2.p) F F 1 F 2 F.p = Or(F 1.p, F 2.p) F F 1 F.p = Neg(F 1.p) F F 1 F 2 F.p = Or(Not(F 1.p), F 2.p) F (F 1 ) F.p = F 1.p F id F.p = id.lexeme Both attributes G.p and F.p are synthesized. In every production, G.p and F.p are functions of attributes of their child nodes. The can be computed in a bottom-up parse. A simplified parse tree for the expression (A (A B)) is shown here: G F F F F A F F A B Using the parse tree, the attribute values can be calculated from the bottom up by applying the semantic actions in the grammar. The value of G.p is Not(And(A,Or(Not(A),B))). Fall 2000 page 4 of 10

(b) Remember: Say whether each attribute of a non-terminal is inherited or synthesized and why. Show the value of the attributes of G after parsing (A (A B)). G F G.q = F.q F.b = true F F 1 F 2 F.q = F.b? And(F 1.q, F 2.q) : Or(F 1.q, F 2.q) F 1.b = F.b F 2.b = F.b F F 1 F 2 F.q = F.b? Or(F 1.q, F 2.q) : And(F 1.q, F 2.q) F 1.b = F.b F 2.b = F.b F F 1 F.q = F 1.q F 1.b = F.b F F 1 F 2 F.q = F.b? Or(F 1.q, F 2.q) : And(F 1.q, F 2.q) F 1.b = F.b F 2.b = F.b F (F 1 ) F.q = F 1.q F 1.b = F.b F id F.q = F.b? id.lexeme : N eg(id.lexeme) The attributes G.q and F.q can be computed bottom-up. However, F.b is inherited, and must be computed top-down. Even though G.q andf.q can be computed bottom-up, they depend on inherited attributes, and are themselves inherited. To compute G.q, the value of F.b must be propagated down the tree. Then, F.q can be computed at each node based on the values of F.b. The value of G.q is Or(Not(A),And(A,Not(B))). Fall 2000 page 5 of 10

3. First and Follow Sets (20 points) Give a grammar with the following First and Follow sets. Your grammar should have exactly two productions per non-terminal and no epsilon productions. The non-terminals are X, Y, Z and the terminals are a, b, c, d, e, f. First(X) = {b, d, f} Follow(X) = {$} First(Y ) = {b, d} Follow(Y ) = {c, e} First(Z) = {c, e} Follow(Z) = {a} Follow(d) = {c, e} Follow(a) = {$} Follow(e) = {a} Follow(b) = {b, d} Follow(f) = {$} Follow(c) = {c, e} There are many ways to solve this problem. In our solution, we began by using the First sets to generate the obvious productions that have the correct first (and only) terminal: X -> b d f Y -> b d Z-> c e We are only allowed two productions per non-terminal, so we need to eliminate an X production. Noticing that b and d are in the first of Y, we can try X -> Y f Y -> b d Z -> c e At this point we have the First sets correct for the non-terminals; now we try to modify the productions to have the correct Follow sets as well. Starting arbitrarily with the terminal c, we notice that both c and e are in Follow(C) and in First(Z), so we can add Z after c in some production. There is only one choice: X -> Y f Y -> b d Z -> cz e Similar reasoning about Follow(b) leads to: X -> Y f Y -> by d Z -> cz e Fall 2000 page 6 of 10

Since $ is not in the follow of Y, there must be something that comes after Y in the X -> Y production. Since First(Z) is in the Follow(Y) we try: X -> YZ f Y -> by d Z -> cz e At this point we still haven t used a. Follow(Z) = {a}. So we get Now Follow(a) = {$} and X -> YZa f Y -> by d Z -> cz e At this point it is routine to check that all the requirements are satisfied. Fall 2000 page 7 of 10

4. LR Parsing and Error Recovery(25 points) Consider the following grammar. S S a b error a (a) Show the DFA for recognizing viable prefixes of this grammar. Use LR(0) items, and treat error as a terminal. States: I0 = { S ->.Sa, S ->.b, S ->.error a } I1 = { S -> S.a } I2 = { S -> b. } I3 = { S -> error. a } I4 = { S -> error a. } I5 = { S -> Sa.} Transitions: I0 -S-> I1 I0 -b-> I2 I0 -error-> I3 I1 -a-> I5 I3 -a-> I4 Fall 2000 page 8 of 10

(b) Tools such as bison use error productions in the following way. When a parsing error is encountered (i.e., the machine cannot shift, reduce, or accept): bacadfa First, pop and discard elements of the stack one at a time until a stack configuration is reached where the error terminal of an error production can be shifted on to the stack. Second, discard tokens of the input one at a time until one is found that can be shifted on to the stack. Third, resume normal execution of the parser. Show the sequence of stack configurations of an SLR(1) parser for the grammar above on the following input. Be sure to show all shift, reduce, and discard actions (for both stack and input). bacadfa$ shift b b acadfa$ reduce S -> b S acadfa$ shift a Sa cadfa$ error; discard stack (*) S cadfa$ discard stack cadfa$ shift error on to stack (written "e") e cadfa$ discard input e adfa$ shift a ea dfa$ error; discard stack (**) e dfa$ discard stack dfa$ shift error on to stack (written "e" again) e dfa$ discard input e fa$ discard input e a$ shift a ea $ reduce S -> error a S $ - accept - Notes: There was no need to build a parsing table for this problem. Either the DFA alone or deducing the moves directly from the grammar were enough. A very common mistake was reducing by S -> Sa on line (*) in the configuration Sa c. Reducing here is not possible because c is not in the Follow(S). The same mistake can occur on line (**). Fall 2000 page 9 of 10

5. Miscellaneous Parsing (15 points) (a) Give an unambiguous grammar that is not LR(1). There are an infinite number of unambiguous non-lr(1) grammars. simpler ones: Here is one of the S A x y B x z A a B a The grammar is unambiguous; it contains exactly two strings and each string has a unique derivation: S A x y a x y S B x z a x z However, the grammar is not LR(1). Consider an input that begins with a x. After shifting the a, the parser must reduce to either A or B. However, based solely on the lookahead character of x, we cannot decide which of A or B is correct, leading to a reduce/reduce conflict. Parsing this grammar correctly would require SLR(2), LR(2), or LALR(2). With two characters of lookahead, we would know to reduce a to A on lookahead x y, but to reduce a to B on lookahead x z. (b) How many strings are in the language of the grammar S as? The language of a grammar is the set of strings derivable from the grammar s start symbol. A derivation results from applying some finite sequence of productions from the grammar; a string is a finite sequence of terminals selected from a given alphabet Σ. The grammar given above can never have a finite derivation that yields a finite string. There is no string of terminals ω such that S + ω. Therefore, there are zero strings in the language of this grammar. (c) Which of LL(1), SLR(1), and LR(1) can parse strings in the following grammar, and why? E A B A a c B b c The grammar is ambiguous; there are two possible derivations of the string c. Neither LL(1) nor SLR(1) nor LR(1) is able to parse any ambiguous grammar. While it is possible to work through complete LL(1), SLR(1), and LR(1) constructions to show exactly where they fail, that is not necessary. As soon as you show that a grammar is ambiguous you immediately know that none of LL(1), SLR(1), or LR(1) can possibly work. Fall 2000 page 10 of 10