CSCE 531, Spring 2015 Final Exam Answer Key

Similar documents
CSCE 531 Spring 2009 Final Exam

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

Fall 2016 CSE Qualifying Exam CSCE 531, Compilers

Intermediate Code Generation

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

VIVA QUESTIONS WITH ANSWERS

The PCAT Programming Language Reference Manual

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

Compiler Construction Assignment 3 Spring 2018

A Simple Syntax-Directed Translator

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Building a Parser Part III

Context-free grammars

CMSC 330: Organization of Programming Languages

COP5621 Exam 4 - Spring 2005

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

Syntax-Directed Translation

CSCI Compiler Design

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

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

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

CSCI Compiler Design

The SPL Programming Language Reference Manual

CSE P 501 Exam Sample Solution 12/1/11

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

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

Compiler Construction: Parsing

Question Bank. 10CS63:Compiler Design

More Assigned Reading and Exercises on Syntax (for Exam 2)

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

COP4020 Spring 2011 Midterm Exam

Appendix Set Notation and Concepts

UNIVERSITY OF CALIFORNIA

Error Handling: SYNTAX

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Chapter 3: Lexing and Parsing

Qualifying Exam in Programming Languages and Compilers

CSE 582 Autumn 2002 Exam 11/26/02

A simple syntax-directed

Compilation a primer

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

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

Principles of Programming Languages COMP251: Syntax and Grammars

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Page No 1 (Please look at the next page )

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

ICOM 4036 Spring 2004

2068 (I) Attempt all questions.

Project 2 Interpreter for Snail. 2 The Snail Programming Language

CS5363 Final Review. cs5363 1

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

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Lexical and Syntax Analysis. Bottom-Up Parsing

CSE P 501 Exam 12/1/11

CSE 582 Autumn 2002 Exam Sample Solution

CS143 Midterm Spring 2014

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

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

CS 415 Midterm Exam Fall 2003

CS 314 Principles of Programming Languages

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Chapter 3. Describing Syntax and Semantics

Syntax-Directed Translation Part II

Syntax Analysis Check syntax and construct abstract syntax tree

Bottom-Up Parsing. Lecture 11-12

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

Language Reference Manual simplicity

Semantic actions for declarations and expressions

CSCE 314 Programming Languages

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

Appendix A The DL Language

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CS 403: Scanning and Parsing

CS606- compiler instruction Solved MCQS From Midterm Papers

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

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

Semantic actions for declarations and expressions. Monday, September 28, 15

CS 415 Midterm Exam Spring SOLUTION

Semantic actions for declarations and expressions


COP5621 Exam 3 - Spring 2005

SECOND PUBLIC EXAMINATION. Compilers

LR Parsing Techniques

LR Parsing LALR Parser Generators

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

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

CSCI565 Compiler Design

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

Spring 2010 CSE Qualifying Exam Core Subjects. March 6, 2010

Conflicts in LR Parsing and More LR Parsing Types

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

COMPILERS BASIC COMPILER FUNCTIONS

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

Code Generation. CS 540 George Mason University

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Transcription:

CSCE 531, Spring 2015 Final Exam Answer Key 1. (40 points total) Consider the following grammar with start symbol S : S S S asb S T T T a T cs T ɛ (a) (10 points) Find FIRST(S), FIRST(T ), FOLLOW(S), and FOLLOW(T ). You need not justify your answer. (b) (15 points) Using the method described in class or the text, start to construct the set {I 0, I 1, I 2,... of states for a canonical LR(1) parser for this grammar, defining the transition function at the same time. Compute the start state I 0 first, then only the possible transitions from I 0. This should produce four additional states I 1, I 2, I 3, I 4, and you do not compute any further transitions. You should display states in condensed form, that is, if there are multiple lookaheads attached to the same LR(0) item, then list the item once with the lookaheads separated by slashes, for example, T.T a, $/a (Omit the brackets for all items.) Note that in class I denoted the transition function as trans. The textbook denotes the same function as goto. You may use the letter t to denote this function. (c) (10 points) Give as much of the action table for this parser as you can, based on your partial construction above. That is, define action[i, a] for each state I i and terminal a (including $) that you can establish from your construction. Omit error entries. (d) (5 points) Describe any conflicts you find, if any, giving the conflicting actions. (a) FIRST(S) = FIRST(T ) = {a, c, ɛ, FOLLOW(S) = FOLLOW(T ) = {a, b, $. 1

(b) I 0 = start state : I 2 = t(i 0, a) : I 4 = t(i 0, c) S.S, $ S a.sb, $ T c.s, $/a S.aSb, $ S.aSb, b S.aSb, $/a S.T, $ S.T, b S.T, $/a T.T a, $/a T.T a, b/a T.T a, $/a T.cS, $/a T.cS, b/a T.cS, $/a T., $/a T., b/a T., $/a I 1 = t(i 0, S) : I 3 = t(i 0, T ) S S., $ S T., $ T T.a, $/a (c) We omit any action items that mention states we have not already constructed. a b c $ 0 shift 2, reduce T ɛ shift 4 reduce T ɛ 1 accept 2 reduce T ɛ reduce T ɛ 3 reduce S T 4 reduce T ɛ shift 4 reduce T ɛ (d) Our table in (c) above indicates a shift/reduce conflict in action[0, a] and no other conflicts. However (and this is optional), we know that there will be shift/reduce conflicts in action[2, a] and action[4, a]. 2. (20 points) Consider a bottom-up grammar in BNF for control flow constructs in a typical Pascal-like programming language start ::= stmt stmt ::= if e then stmt 1 stmt ::= if e then stmt 1 else stmt 2 stmt ::= repeat stmt list until e stmt ::= begin stmt list end stmt ::= break stmt ::= other stmt list ::= /* empty */ stmt list ::= stmt list 1 stmt A break statement is meant to unconditionally jump to the end of the closest surrounding repeat-loop, if there is one. (Note that a break statement might jump out of an arbitrary number of nested if and begin end statements.) Add semantic rules to compute the following inherited attribute: stmt.break is a string that is the destination label to which to jump if a break statement is encountered within stmt. If the statement is not inside any repeat statement, then use the special value Nil for this 2

attribute. You may assume a function newsymbol() is available that returns a fresh label every time it is called. Also add semantic actions that emit an unconditional jump to the proper destination whenever a break statement is encountered (if there is no proper destination, issue an error message), and emit the corresponding break destination as a label (i.e., followed by a colon) after each repeat statement. You may assume a function emit(...) is available that emits whatever string is passed to it. You may define any additional attributes that you find helpful, but you must pass all data around on the semantic stack, i.e., you may not declare or use any external (global) variables or data structures. Also, your syntax-directed translation scheme must be L-attributed, and no intermediate actions are allowed. start ::= stmt stmt.break := Nil stmt ::=if e then stmt 1 stmt 1.break := stmt.break stmt ::=if e then stmt 1 else stmt 2 stmt 1.break := stmt.break stmt 2.break := stmt.break stmt ::=repeat stmt list until e stmt list.break := newsymbol() emit( stmt list.break) emit( : ) stmt ::=begin stmt list end stmt list.break := stmt.break stmt ::=break if stmt.break <> Nil then emit( goto ) emit( stmt.break) else semantic error() stmt ::=other stmt list ::= /* empty */ stmt list ::= stmt list 1 stmt stmt list 1.break := stmt list.break stmt.break := stmt list.break 3. (20 points) Consider the following bison grammar for a C-like programming language fragment (with start symbol assignment): 3

assignment : expr = expr expr : expr + unary unary unary : * unary & unary factor factor : CONST VAR ( expr ) Note: The assignment operator = requires an L-value for the left-hand expr, and an R-value for the right-hand expr. (The assignment itself returns no value.) The addition operator + requires two R-values and returns an R-value. The pointer indirection operator * requires an R-value and returns an L-value. The address-of operator & requires an L-value and returns an R-value. Constants (CONST) are R-values, and variables (VAR) are L-values. Parentheses have no effect on the kind of value (R- versus L-). Add bison-appropriate semantic actions that do three things: Compute, as synthesized attributes of expr, unary, and factor, whether or not the (sub)expression is an R-value or an L-value. Use the integer value 0 to mean R-value and 1 to mean L-value. Compute these using the usual $-references. Issue instructions (for a human being) to dereference an operand whenever it is an L- value but an R-value is expected for the operation. For example, when the left operand of the + operator is an L-value, then include the action msg("deref left operand") in that production (and similarly for the right operand). Whenever an R-value is present where an L-value is expected, include the action error("l-val expected") Do not use any intermediate actions, only reduce-actions. You do not need to provide the human with any other information, e.g., which production generates a particular message. You also need not worry about (data) types of expressions. 4

assignment : expr = expr expr : expr + unary { if (!$1) error("l-val expected") if ($3) msg("deref right-hand side") { if ($1) msg("deref left operand") if ($3) msg("deref right operand") $$ = 0 unary /* default action: $$ = $1 */ unary : * unary { if ($2) msg("deref operand") $$ = 1 & unary { if (!$2) error("l-val expected") $$ = 0 factor /* default action */ factor : CONST { $$ = 0 VAR { $$ = 1 ( expr ) { $$ = $2 4. (15 points) Consider the following Pascal declaration: var a : array[3..10] of array[2..9] of array[4..8] of Real Assuming that a Pascal Real is eight bytes and that the base address of a is 1500, find the base address of the Real variable a[9][6][5]. [Note: the original question mistakenly suggested that a[9][6][5] is an integer variable. It is a Real variable.] Here are the sizes of the relevant types: type size Real 8 array[4..8] of Real (8 4 + 1) 8 = 40 array[2..9] of array[4..8] of Real (9 2 + 1) 40 = 320 Thus the base address of a[9][6][5] is 1500 + (9 3)320 + (6 2)40 + (5 4)8 = 1500 + 2088 = 3588. 5. (25 points total) Consider the following three-address code (line numbers added): 5

1 L1: i := a 2 t1 := i + 5 3 L2: if t1 <= 10 then goto L4 4 j := i + 1 5 if j >= 0 then goto L2 6 a := a + j 7 L3: if j <= 10 then goto L5 8 L4: i := j + 1 9 t1 := 2 * j 10 goto L3 11 L5: a := t1-1 12 if a > 0 then goto L1 13 Assume that control enters at line 1 and that there are no other entry points. (a) (5 points) Describe the basic blocks B 1, B 2,... by giving an inclusive range of line numbers for each block. (b) (10 points) Draw the flow diagram as a directed graph, labeling the nodes B 1, B 2,.... Give dangling arrows both for the entry point and for the exit point of the code as a whole. (c) (5 points) Using the strict definition of a loop as defined in class and in the text, list any sets of vertices that constitute loops. Label any inner loop(s) as such. (d) (5 points) Is the variable j alive between lines 2 and 3? Explain. (a) Block Line(s) B 1 1 2 B 2 3 B 3 4 5 B 4 6 B 5 7 B 6 8 10 B 7 11 12 B 1 B 2 B 3 B 4 B 5 B 6 B 7 (b) (c) There are two loops: {B 1, B 2, B 3, B 4, B 5, B 6, B 7. {B 2, B 3 (inner loop). (d) Yes, j is alive. Line 3 could go to line 8, where j is used but not set in between. 6