ECE 468/573 Midterm 1 October 1, 2014

Similar documents
ECE 468/573 Midterm 1 September 30, 2015

ECE 573 Midterm 1 September 29, 2009

Midterm I (Solutions) CS164, Spring 2002

CSE302: Compiler Design

Monday, September 13, Parsers

ECE 468/573 Midterm 1 September 28, 2012

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

Bottom-Up Parsing LR Parsing

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

2068 (I) Attempt all questions.

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

Midterm I - Solution CS164, Spring 2014

Wednesday, August 31, Parsers

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

Wednesday, September 9, 15. Parsers

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

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

Downloaded from Page 1. LR Parsing

CS 4120 Introduction to Compilers

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

CSE 401 Midterm Exam Sample Solution 2/11/15

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

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

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

ECE468 Fall 2003, Final Exam

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

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

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

Principles of Programming Languages

Conflicts in LR Parsing and More LR Parsing Types

LR Parsing - The Items

In One Slide. Outline. LR Parsing. Table Construction

BSCS Fall Mid Term Examination December 2012

Compilers. Predictive Parsing. Alex Aiken

Lecture Bottom-Up Parsing

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

Top down vs. bottom up parsing

LECTURE NOTES ON COMPILER DESIGN P a g e 2

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

CMSC 330, Fall 2018 Midterm 2


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

LR Parsing E T + E T 1 T

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

SLR parsers. LR(0) items

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

Bottom-Up Parsing. Lecture 11-12

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

More Bottom-Up Parsing

CS502: Compilers & Programming Systems

shift-reduce parsing

Final Term Papers 2013

Table-Driven Parsing

LR Parsing LALR Parser Generators

MODEL ANSWERS COMP36512, May 2016

Bottom-Up Parsing. Lecture 11-12

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

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

CS606- compiler instruction Solved MCQS From Midterm Papers

LR Parsing LALR Parser Generators

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

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

Compiler Design Aug 1996

CMSC330 Spring 2018 Midterm 2 9:30am/ 11:00am/ 3:30pm

Configuration Sets for CSX- Lite. Parser Action Table

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

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

COP4020 Spring 2011 Midterm Exam

CS164: Midterm I. Fall 2003

ECE251 Midterm practice questions, Fall 2010

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

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

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

Formal Languages and Compilers Lecture VI: Lexical Analysis

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

Concepts Introduced in Chapter 4

LR Parsing. Table Construction

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Compiling Regular Expressions COMP360

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

CS143 Handout 14 Summer 2011 July 6 th, LALR Parsing

LR Parsing Techniques

LALR Parsing. What Yacc and most compilers employ.

Translator Design CRN Test 3 Revision 1 CMSC 4173 Spring 2012

Lexical and Syntax Analysis. Bottom-Up Parsing

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

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

UNIVERSITY OF CALIFORNIA

CSE 582 Autumn 2002 Exam Sample Solution

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

Lecture 7: Deterministic Bottom-Up Parsing

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

1 Parsing (25 pts, 5 each)

UNIT III & IV. Bottom up parsing

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

CMSC 330, Fall 2018 Midterm 2

Transcription:

ECE 468/573 Midterm 1 October 1, 2014 Name: Purdue email: Please sign the following: I affirm that the answers given on this test are mine and mine alone. I did not receive help from any person or material (other than those explicitly allowed). X Note: ECE 468 students do not have to complete Part 6. Part Points Score 1 10 2 18 3 14 4 24 5 34 6 15 Total 115

Part 1: Short answers (10 points) 1) In recent years, many companies have switched from GCC-based compiler toolchains to LLVM-based compiler toolchains even though LLVM re-used GCC s front ends. Given that, what reason might a company have had for switching to LLVM? (5 points) LLVM may have a better optimizing back end even if the same front-end is used, LLVM can do a better job of producing optimized code Some people thought that LLVM was a true virtual machine with a JIT compiler; I gave credit for reasonable answers along those lines. 2) One nice thing about maintaining a common ISA is that when a new processor is released, you don t have to recompile any code, or change the compiler. Nevertheless, when Intel releases new x86 processors, companies update their compilers anyway. Why might they want to do that? (5 points) Even if the ISA is the same, the implementation of that ISA might be different some instructions may be more efficient so it can be important to change the backend to account for these differences. Also, newer x86 processors may support instruction set extensions (SIMD, x86-64) and compiler backends need to be changed to generate code targeting those extensions.

Part 2: Regular expressions, finite automata and scanners (18 points) 1) Consider the following NFA. Fill in the transition table below with its corresponding DFA using the subset construction. You may not use all of the rows (16 points): 2 c 4 a 6 1 a a b a b b c 3 c 5 c 7 State Final? a b c 1 No 2, 3 X X 2, 3 No X 3 4, 5 3 No X X 5 4, 5 No 3, 6 4 7 5 No X 4 7 3, 6 Yes X X 5 4 No 3, 6 4 7 7 Yes X X X 2 pts/row 2) List which states (if any) should be merged when you reduce the DFA you just derived (2 points): {4, 5} and 4 Note that 3 and {3, 6} can t be merged because one is final and the other isn t.

Part 3: Grammars (14 points) Let G be the grammar: S! A$ A! (AB) A! B! (A) B! x Using this grammar, answer the following questions. 1) What are the terminals and non-terminals of this grammar? (6 points) Terminals: (, ), x, $ Non-terminals: S, A, B 1 point per mistake (note that λ is not a terminal!) 2) Draw the parse tree for the string ((x)x)$ (8 points) S A B $ ( A B ) x λ x

Part 4: LL parsers (24 points) Answer the questions in this part using the same grammar from Part 3. 1) Give the First sets for each non-terminal in the grammar (6 points) First(S) : {(, $} [3 points] First(A) : {(, λ} [2 points] First(B) : {(, x} [1 point] 2) Give the Follow sets for each non-terminal in the grammar (6 points) Follow(S) : { } [1 point] Follow(A) : {x, (, $, )} [4 points] Follow(B) : { ) } [1 point 3) Give the Predict sets for each production in the grammar (5 points) Predict(1) : {(, $} Predict(2) : { ( } Predict(3) : { x, (, $, ) } Predict(4) : { ( } Predict(5) : { x } 4) Fill in the following parse table (6 points) ( ) x $ S 1 1 A 2, 3 3 3 3 B 4 5 5) Is the grammar LL(1)? Why or why not? (1 points) No, there is a predict conflict when predicting an A with a lookahead of (

Part 5: LR(0) Parsers (34 points) Use the following grammar for the next questions: S! A$ A! (AB) A! () B! (A) B! () 1) Fill in the missing information for the for the following CFSM (15 points) State 0 S A$ A (AB) A () State 3 A () State 6 B () State 9 B (A ) ) State 11 B (A) A State 1 $ ( ( ) ) A S A $ State 4 A ( AB) A ( ) A (AB) A () ( State 7 B ( A) B ( ) A (AB) A () A ( State 2 S A$ State 5 A (A B) B (A) B () B State 8 A (AB ) ) State 10 A (AB) 2) List the shift and reduce states in the above CFSM (6 points) Shift: 0, 1, 4, 5, 7, 8, 9 Reduce: 2 (accept), 3, 6, 10, 11 3) Is this an LR(0) grammar? Why or why not? (1 point) Yes. No S/R or R/R conflicts

4) For the following sub-questions, use the CFSM you built in the previous question. Each question will provide the state of the parser in mid-parse, giving the state stack (most recent state on the right) and the next token. For each question, give the action the parser will take next, using the format Shift X for shift actions (where X is the state being shifted to) and Reduce R, goto X for reduce actions (where R is the rule being reduced, and X is the state the parser winds up in after finishing the reduction). Also provide the new state stack. (3 points each) a) State stack: 0 4 5 7. Next token: ( Shift 4 0 4 5 7 4 b) State stack: 0 4 4 5 8 10. Next token: ( Reduce 2 goto 5 0 4 5 c) State stack: 0 4 4 5. Next token: ) Parse error d) State stack: 0 4 5 7 4. Next token: ) Shift 3 0 4 5 7 4 3

Part 6 (573 only): LR(1) Parsers (15 points): 1) For the following grammar, show what the first state of the LR(1) machine would be (10 points): S! AB$ A! xy A! Bz B! Cz B! C! w S AB$, { } A xy, {w, $} A Bz, {w, $} B Cz, {z} B λ, {z} C w, {z} 2) Give the action table entries for the first state (i.e., for each token that could be coming up, say whether the parser would shift or reduce). For reduce actions, say what rule would be reduced. (5 points) Lookahead Action x Shift y Error z Reduce 5 w Shift $ Error