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

Similar documents
Midterm I - Solution CS164, Spring 2014

Midterm I (Solutions) CS164, Spring 2002

CS 164 Programming Languages and Compilers Handout 8. Midterm I

UNIVERSITY OF CALIFORNIA

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

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

CSE 582 Autumn 2002 Exam 11/26/02

BSCS Fall Mid Term Examination December 2012

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

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

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS164: Midterm I. Fall 2003

CSE P 501 Exam 8/5/04

CSE 401 Midterm Exam 11/5/10

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

CS143 Midterm Fall 2008

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

ECE251 Midterm practice questions, Fall 2010

CSCE 531 Spring 2009 Final Exam

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

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

CSCI Compiler Design

CS606- compiler instruction Solved MCQS From Midterm Papers

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

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

2068 (I) Attempt all questions.

Compiler Design Aug 1996

CSCE 531, Spring 2015 Final Exam Answer Key

CS143 Midterm Sample Solution Fall 2010

Compiler Construction

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

CSE 582 Autumn 2002 Exam Sample Solution

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

Lecture Bottom-Up Parsing

COP 3402 Systems Software Syntax Analysis (Parser)

ECE 468/573 Midterm 1 September 30, 2015

CS164 Second Midterm Exam Fall 2014

LR Parsing - The Items

Compiler Construction Using

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

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

CSE302: Compiler Design

shift-reduce parsing

Compiler Construction

Question Bank. 10CS63:Compiler Design

CMSC 330, Fall 2018 Midterm 2

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

CS 536 Midterm Exam Spring 2013

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

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

Chapter 2 - Programming Language Syntax. September 20, 2017

CS143 Midterm Spring 2014

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

UNIT III & IV. Bottom up parsing

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

Syntax and Parsing COMS W4115. Prof. Stephen A. Edwards Fall 2003 Columbia University Department of Computer Science

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

Wednesday, September 9, 15. Parsers

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

ECE 468/573 Midterm 1 October 1, 2014

Question Points Score

LR Parsing Techniques

QUESTIONS RELATED TO UNIT I, II And III

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Syntax Analysis Part I

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications


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

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

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Lexical Analysis. Introduction

How do LL(1) Parsers Build Syntax Trees?

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

Wednesday, August 31, Parsers

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

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

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

CSE 5317 Midterm Examination 4 March Solutions

Conflicts in LR Parsing and More LR Parsing Types

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

CSE 401 Midterm Exam Sample Solution 11/4/11

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Monday, September 13, Parsers


Optimizing Finite Automata

Context-Free Grammars

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08008 INFORMATICS 2A: PROCESSING FORMAL AND NATURAL LANGUAGES

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

CSE3322 Programming Languages and Implementation

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

CSE P 501 Exam 11/17/05 Sample Solution

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

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

Context-free grammars

Lecture 8: Deterministic Bottom-Up Parsing

LR Parsing E T + E T 1 T

Transcription:

Midterm Examination Please read all instructions (including these) carefully. Please print your name at the bottom of each page on the exam. There are seven questions on the exam, each worth between 10 and 20 points. You have 1 hour and 20 minutes to work on the exam, so you should plan to spend approximately 12 minutes on each question. The exam is closed book, but you may refer to your two 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. 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: Sample Solutions SID or SS#: Problem Max points Points 1 15 2 15 3 20 4 10 5 20 6 10 7 10 TOTAL 100 Fall 94 page 1 of 8

1. Regular Expressions (15 points) Consider a language where real constants are dened as follows: A real constant contains a decimal point or E notation, or both. For instance, 0:01, 2:71821828, ~1:2E12, and 7E~5 are real constants. The symbol \~" denotes unary minus and may appear before the number or on the exponent. There is no unary \+" operation. There must be at least one digit to left of the decimal point, but there might be no digits to the right of the decimal point. The exponent following the \E" is a (possibly negative) integer. Write a regular expression for such real constants. Use the standard regular expression notation described by the following grammar: R! j char j R + R j R j RR j (R) You may dene names for regular expressions you want to use in more than one place (e.g., foo = R). digit = 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 posint = digit digit int = ( + ~) posint exp = E int frac = : digit real = (int frac (exp + )) + (int (frac + ) exp) Fall 94 page 2 of 8

2. Finite Automata (15 points) Consider a DFA with a start state s 0 and a transition function trans. For a state s and input character c, trans(s; c) = s 0 if there is a transition from state s to state s 0 on character c. If there is no transition from s on c then trans(s; c) = none. The following algorithm simulates the behavior of such a DFA, accepting if the input string is accepted by the DFA and rejecting otherwise. state s 0 while there's input left do: char next input character if trans(state,char) = none then stop and reject state trans(state,char) (end of loop) accept if state is a nal state, otherwise reject Now consider an NFA with a start state s 0 and a transition function trans. In this case, for a state s and input character c (we now allow c = ), trans(s; c) is the set of states s 0 for which there is a transition from s to s 0 on c. In the style of the algorithm above, give a (deterministic) algorithm that simulates the behavior of such an NFA. You may use the -closure operation described in class and in the text. Simulate subset construction. Use state-set instead of state. state-set -closure(s 0 ) while there's still input left do: char next input character if -closure( S s2state-set trans(s; char)) = ; then stop and reject state-set -closure( S s2state-set trans(s; char)) (end of loop) accept if there's a final state in state-set, otherwise reject. Fall 94 page 3 of 8

3. Grammars (20 points) Consider the following grammar. The nonterminals are E, T, and L. The terminals are +,id,(,), and ;. The start symbol is E. E! E + T j T T! id j id() j id(l) L! E; L j E Give an LL(1) grammar that generates the same language as this grammar. As part of your work please show that your grammar is LL(1). (a) Eliminate left recursion: E! TE 0 E 0! +TE 0 j T! id j id() j id(l) L! E; L j E (b) Left factor: E! TE 0 E 0! +TE 0 j T! id T 0 T 0! j (T 00 T 00! ) j L) L! EL 0 L 0! ; L j (c) Check that it's LL(1). For this part you just needed to give enough information to show that there would be no conflicts in the parsing table. The following is sufficient: E 0! +TE 0 j First(+TE 0 ) = f+g Follow(E 0 ) = f$; ); ; g T 0! j (T 00 First((T 00 ) = f(g Follow(T 0 ) = f+; $; ); ; g T 00! ) j L) First()) = f)g First(L)) = fidg L 0! ; L j First(; L) = f; g Follow(L 0 ) = f)g Fall 94 page 4 of 8

4. Parsing (10 points) In both parts of this question, we are looking for clarity and brevity as well as the right idea. Suppose you are writing a parser for a programming language that includes the following syntax for looping constructs: Loop! do stmt while expr j j do stmt until expr do stmt forever (a) (5 points) A predictive parser (i.e., a top-down parser with no backtracking) can't use this grammar. Give a brief (a couple of sentences) explanation of this fact. When trying to expand a production for non-terminal ``loop'', the parser cannot decide which of the three productions to expand using only the next few input tokens. (b) (5 points) Give a brief explanation of why a bottom-up parser does not have diculty with this grammar. When a bottom-up parser must decide which of the three productions to choose (reduce), the ``while'', ``until'', or ``forever'' has already been read and shifted onto the stack. Fall 94 page 5 of 8

5. Parsing (20 points) Consider the following grammar. The nonterminals are S 0 and S. The terminals are op and x. The start symbol is S 0. S 0! S S! S op S j x (a) (15 points) Draw the DFA built from sets of LR(0) items for this grammar. Show the contents of each state. (Note: Don't augment the grammar with a new start symbol.) S' ->. S S ->. S op S S ->. x S x --------------- ------------ V V ------------ S' -> S. ** S -> x. S -> S. op S ------------ ^ op x --------------- --------------- V S -> S op. S S ->. S op S S ->. x ^ op S V S -> S op S. * S -> S. op S (b) (3 points) Is this grammar SLR(1)? Briey explain why or why not. No. The grammar is ambiguous. For example, there are two parses of the string ``x op x op x''. An alternative justification is that there is a shift/reduce conflict in state *. Note that there is no shift/reduce conflict in the state **. (c) (2 points) Is this grammar LR(1)? No. The grammar is ambiguous. Briefly explain why or why not. Fall 94 page 6 of 8

6. Bison and Abstract Syntax Trees (10 points) Consider the following constructors for a tree language: Expression app(expression, Expression); Expression lambda(expression, Expression); Expression id(); Now consider the following Bison grammar: %token ID LAMBDA %type <Expression> Expr %% Expr : ID { $$ = id(); } '(' LAMBDA ID '.' Expr ')' { $$ = lambda( id(), $5); } '(' Expr Expr ')' { $$ = app($2, $3); } Draw the abstract syntax tree that would be produced when parsing the sequence of tokens below. Label all the nodes of your AST with the appropriate constructor. ( ( LAMBDA ID. (ID ID) ) ( LAMBDA ID. ID ) ) app ----- lambda lambda -------------- ------------- id app id id --------- id id Fall 94 page 7 of 8

7. Type Checking (10 points) Suppose we want to design a type checker for Scheme programs. In Scheme, functions can be passed as arguments to and returned as results from functions. Recall the type checking rule for function application given in class: A ` f : t 1! t 2 A ` x : t 1 A ` f(x) : t 2 Self-application occurs when a function is called with itself as a parameter. This is, if f is a function taking a function as a parameter, the f(f) is an instance of self-application. Briey explain why type checking of self-application must always fail using the type checking rule above. From the rule, in a self-application f(f) we know that: f : t1 -> t2 f : t1 But t1 is a proper subexpression of t1 -> t2, so t1 <> t1 -> t2. Since f cannot have two disinct distinct types, type checking fails for self application. An alternative (but equivalent) answer is: If f : t1 -> t2 and f : t1 then t1 = t1 -> t2 since f has one type. But then t1 = t1 -> t2 = (t1 -> t2) -> t2 = ((t1 -> t2) -> t2) -> t2 =... -> t2 -> t2 -> t2 which is infinite. self-application. Since types are finite size, type checking must fail for Fall 94 page 8 of 8