CS 411 Midterm Feb 2008

Similar documents
Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

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

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

ECE251 Midterm practice questions, Fall 2010

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

CSE 401 Midterm Exam Sample Solution 11/4/11

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

CPSC 121 Midterm 1 Friday October 14th, Signature: Section (circle one): 11:00 15:30 17:00

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

EXAMINATION INSTRUCTIONS

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

UNIVERSITY OF CALIFORNIA

UNIVERSITY REGULATIONS

CPSC 311, 2010W1 Midterm Exam #2

EXAMINATION INSTRUCTIONS

CPSC 121 Sample Final Examination December 2013

CS 415 Midterm Exam Spring SOLUTION

CPSC 320 Midterm 2. July 13, 2007

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 110: MIDTERM 1 Part B May 26, Important notes about this examination

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

UNIVERSITY REGULATIONS

EXAMINATION INSTRUCTIONS

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

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

Examination in Compilers, EDAN65

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

Parsing. COMP 520: Compiler Design (4 credits) Professor Laurie Hendren.

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

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

CPSC 121 Midterm 1 Friday February 5th, Signature: Section (circle one): Morning Afternoon

4. An interpreter is a program that

Compilers. Computer Science 431

Compilation 2010 SableCC

CS606- compiler instruction Solved MCQS From Midterm Papers

Semantic Analysis. Lecture 9. February 7, 2018

Lecture 8: Deterministic Bottom-Up Parsing

Compilation 2012 ocamllex and menhir

Midterm I - Solution CS164, Spring 2014

Introduction to Lexing and Parsing

Full Name: CS Account:

BSCS Fall Mid Term Examination December 2012

Computer Science 304

CS 415 Midterm Exam Spring 2002

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Building Compilers with Phoenix

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

CSE 401 Midterm Exam Sample Solution 2/11/15

Lexical Scanning COMP360

Midterm I (Solutions) CS164, Spring 2002

4. Lexical and Syntax Analysis

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

CS143 Midterm Sample Solution Fall 2010

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

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

4. Lexical and Syntax Analysis

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

COMPILER DESIGN. For COMPUTER SCIENCE

Lecture 7: Deterministic Bottom-Up Parsing

Building Compilers with Phoenix

Compiler Design Overview. Compiler Design 1

Lecture Notes on Bottom-Up LR Parsing

1. Consider the following program in a PCAT-like language.

Syntax Intro and Overview. Syntax

CSCI Compiler Design

The University of British Columbia Final Examination - December 02, 2014 Mathematics 308. Closed book examination. No calculators.

This examination has 11 pages. Check that you have a complete paper.

A simple syntax-directed

CS164: Midterm I. Fall 2003

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

The University of Oregon May 16, 2015 Oregon Invitational Mathematics Tournament:

Bottom-Up Parsing. Lecture 11-12

Programming Project II

shift-reduce parsing

Compiling Regular Expressions COMP360

G.PULLAIH COLLEGE OF ENGINEERING & TECHNOLOGY

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

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

CS131 Compilers: Programming Assignment 2 Due Tuesday, April 4, 2017 at 11:59pm

Syntax. 2.1 Terminology

EXAMINATION INSTRUCTIONS

CS143 Midterm Spring 2014

Downloaded from Page 1. LR Parsing

Parser Tools: lex and yacc-style Parsing

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

Stating the obvious, people and computers do not speak the same language.

CSE3322 Programming Languages and Implementation

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

Chapter 3: Lexing and Parsing

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Chapter 2 :: Programming Language Syntax

LECTURE NOTES ON COMPILER DESIGN P a g e 2

CE161-4-AU UNIVERSITY OF ESSEX. Undergraduate Examinations 2014 DIGITAL SYSTEMS ARCHITECTURE. Time allowed: TWO hours

The University of Oregon May 16, 2015 Oregon Invitational Mathematics Tournament:

Parser Tools: lex and yacc-style Parsing

CS143 Midterm Fall 2008

Transcription:

CS 411 Midterm Feb 2008 Exam number: P1: / 9 P4: / 10 P2: / 4 P5: / 9 Student Name: P3: / 7 P6: / 8 P7: / 8 SUBTOTALS: / 20 / 35 Student ID number: TOTAL: / 55 READ THIS CAREFULLY BEFORE PROCEEDING DO NOT TURN THE PAGE UNTIL WE TELL YOU TO DO SO CAUTION -- Candidates suspected of any of the following, or similar, dishonest practices shall be immediately dismissed from the examination and shall be liable to disciplinary action: a) Making use of any books, papers or memoranda. b) Speaking or communicating with other candidates. c) Purposely exposing written papers to the view of other candidates. The plea of accident or forgetfulness shall not be received. Instructions 1. This exam contains 7 problems. 2. Each candidate should be prepared to produce his/her student-id. 3. READ AND OBSERVE THE FOLLOWING RULES: - No candidate shall be permitted to ask questions of the invigilators except in case of supposed errors or ambiguities in exam questions. - No candidate shall be permitted to enter the examination room after the expiration of one half hour, or to leave during the first half hour of the examination. - No candidate shall be permitted to leave during the last half hour of the examination. Please remain in your seat until your paper has been collected. - Smoking is not permitted during examinations. 4. You are given 80 minutes to complete this exam. 5. Give concise answers. Long answers will be looked down upon with particular disregard - you will get points deducted for fluff. Put all your answers on the exam. Do any rough work on the back of the exam pages. All rough work must be handed in.

1. There are three parts to the specification of a programming language. Describe in a few sentences what each of them specifies. (9 pts) a. Syntax (3 pts) Specifies the textual structure of grammatically correct programs. Usually this constitutes of two parts. One part specifies "word structure" or "lexical" structure and is usually described in terms for regular expressions. A second part specifies how the "words" are arranged into higher-level "parse tree" structures. This is usually described using some form of context free grammar formalism such as BNF or EBNF. b. Contextual constraints (3 pts) Specifies additional constraints that well-formed programs (i.e. programs that are acceptable as input to the compiler and for which semantics is properly defined) must obey. These constrains, typically scope and typing rules, are specified separately from the syntax because they cannot be expressed with context free grammars. c. Semantics (3 pts) Specifies the meaning of well-formed programs, i.e. what behavior does a program generate when it is executed. 2. Put the following algorithms in order from least powerful to most powerful: LR(0), LR(1), SLR, LALR(1). (4 pts) LR(0), SLR, LALR(1), LR(1)

3. Sablecc parser generator algorithm (7 pts) a. Which of the above 4 algorithms mentioned in question 2 is the one used by sablecc? (2 pts) LALR b. Is this a bottom-up or a top-down algorithm? (2 pts) Bottom-up c. Focussing on high-level issues that matter to someone using a parser generator tool (rather than details of the algorithms), briefly dicuss what the potential advantages/disadvantages are of bottom-up versus a top-down parser generators. (3 pts) Bottom-up Top-down - Harder to debug + more expressive => grammar does not need to be modified (as much) and this results in more intuitive parse tree structure. + Easier to debug and more intuitive. + Can be implemented manually - less expressive => may require extensive changes to the grammar to make it parsable. This results in less intuitive parse tree structure.

4. For each of the following statements indicate whether they are true or false. Explain your answer briefly. (10 pts) a) EBNF is a more expressive notation than BNF, therefore some languages that can be defined using EBNF cannot be defined using BNF notation. False, EBNF is more expressive only in the sense that it is more convenient to use, but all EBNF specifications can be converted into equivalent BNF specifications. b) Every language that can be defined by a grammar (BNF) can also be defined by a regular expression. False, regular expression languages can only specify languages that do not have "self embedding". c) All of the following formalisms are equally powerfull in the sense that they can be used to define exactly the same class of languages: regular expressions, DFA, NDFA, NDFA-epsilon. True, they are all equivalent in that they can recognize the same class of languages, called "regular languages". d) The difference between statements and expressions in the language is that statements do not need to be type-checked but expressions do. False, although statements do not have a type, we still need to type check the expressions they contain, and verify whether those expressions are of the correct type given the context in which they occur. For example, the test expression of a while statement should be of type boolean. e) Although C is a statically scoped language, its stack frames do not need to contain a static link. True, C does not have nested procedures. Therefore, if C procedures would have a static link, each static link would just point to the global scope, so it would be redundant.

5. Tombstone Diagrams (9 pts) You are given a standard SDK (downloaded from Sun). The SDK provides a bytecode compiler called javac in the form of a number of.class files containing bytecode. It also provides a implementation, in the form of a single java.exe binary file that can be run directly on your -based windows box. You are also provided with the source code of a compiler called myjavac. This compiler takes a program (i.e. a number of.java source files) and compiles it directly to an.exe file. Myjavac is itself implemented in the language. a. Draw tombstone diagram components for all of the given language processors (3 pts) java.exe: javac: -> processor: myjavac (source code): -> b. Draw a series of tombstone diagrams showing how you can bootstrap the myjavac compiler, using the Sun compiler and. This should end with a diagram showing myjavac running natively on the machine, compiling itself into native code. (6 pts) 1) Compile myjavac into with javac 2) Compile myjavac into using compiled myjavac from step 1 3) We now have a version of myjavac running natively on, capable of compiling itself 1) 2) -> -> -> -> -> -> 3) -> -> ->

6. Below is a grammar for a simple expression language. (8pts) S ::= E "$" E ::= E "+" T T T ::= T "*" F F F ::= "i" "i" "(" E ")" "1" "(" E ")" a. what is the starting state of the LR0 DFA (list the LR0 items in that state) (3pts) S ::= E "$" E ::= E "+" T E ::= T T ::= T "*" F T ::= F F ::= "i" F ::= "i" "(" E ")" F ::= "1" F ::= "(" E ")" b. what state will the automaton be in after shifting an i token from the starting state? (3pts) F ::= "i" F ::= "i" "(" E ")" c. Is this language LR 0? Explain your answer. (2pts) No, there is a shift-reduce conflict: F ::= "i" is a "reduce" item. F ::= "i" "(" E ")" is a "shift" item.

7. Below is a sablecc lexer and parser specification for a language called "simply typed lambda calculus". This language should be familiar to you from 311. It differs from the lambda calculus that you know (e.g. from 311 and Scheme) in that it is statically typed (all variable declarations are explicitly associated with a type). (8 pts) Helpers letter = ['a'..'z'] ['A'..'Z'] ; digit = ['0'..'9'] ; cr = 13; lf = 10; tab = 9; Tokens lpar = '(' ; col = ':' ; rpar = ')' ; arrow = '->'; lambda = 'lambda'; int = 'int' ; ident = letter (letter digit)* ; int_lit = digit digit* ; whitespace = (' ' cr lf tab)* ; Ignored Tokens whitespace ; Productions program = exp ; exp = {lambda} lpar lambda ident col type rpar exp {var_ref} ident {apply} lpar [rator]:exp [rand]:exp rpar {int} int_lit ; type = {function} [arg]:primary_type arrow [return]:type {int} int {primary} primary_type ; primary_type = {int} int {parens} lpar type rpar ; Note: This is the "nicest" solution, not the simplest solution. When we run sablecc on this file the following error is reported: shift/reduce conflict in state [stack: TLpar TLambda TIdent TColon PType TArrow PType *] on TArrow in { [ PType = PType * TArrow PType ] (shift), [ PType = PType TArrow PType * ] followed by TArrow (reduce) } a. Explain what this error means and why it arises (4pts) This means the parser cannot chose whether to shift or reduce when it sees an arrow token after a PType. This arises because the grammar is ambigous about how to parse something like int -> int -> int: should the parsetree be like (int->int)->int or int->(int->int)? b. Fix this problem by marking-up the sablecc file above (e.g. cross-out what you want to change, and write your changes to the right, or below the existing definitions) (4pts)