CSE P 501 Exam 11/17/05 Sample Solution

Similar documents
CSE 582 Autumn 2002 Exam Sample Solution

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.

CSE 582 Autumn 2002 Exam 11/26/02

CSE 401 Midterm Exam Sample Solution 2/11/15

CSE 401 Midterm Exam Sample Solution 11/4/11

CSE P 501 Exam Sample Solution 12/1/11

CSE 413 Winter 2001 Final Exam Sample Solution

CSE P 501 Exam 12/1/11

CSE P 501 Exam 8/5/04

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

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

ECE251 Midterm practice questions, Fall 2010

LR Parsing LALR Parser Generators

CSE 413 Final Exam Spring 2011 Sample Solution. Strings of alternating 0 s and 1 s that begin and end with the same character, either 0 or 1.

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

LR Parsing LALR Parser Generators

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

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

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

SOFTWARE ARCHITECTURE 5. COMPILER

Bottom-Up Parsing. Lecture 11-12

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Semantic Analysis. Lecture 9. February 7, 2018

Appendix Set Notation and Concepts

Overview of Compiler. A. Introduction

CSE 401 Final Exam. December 16, 2010

CSE 401 Midterm Exam 11/5/10

CSE au Final Exam Sample Solution

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

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

Using an LALR(1) Parser Generator

Conflicts in LR Parsing and More LR Parsing Types

Lecture 7: Deterministic Bottom-Up Parsing

CSE 413 Final Exam. June 7, 2011

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

Lecture 8: Deterministic Bottom-Up Parsing

In One Slide. Outline. LR Parsing. Table Construction

Part 5 Program Analysis Principles and Techniques

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

CSCI312 Principles of Programming Languages

Downloaded from Page 1. LR Parsing

Question Points Score

Bottom-Up Parsing. Lecture 11-12

Context-free grammars

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

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

CSE 3302 Programming Languages Lecture 2: Syntax

Compiler Construction: Parsing

LR Parsing Techniques

CSE 413 Final Exam. December 13, 2012

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

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Syntax-Directed Translation

Computer Systems Organization V Fall 2009

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

CMSC 330: Organization of Programming Languages

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Chapter 3. Parsing #1

Monday, September 13, Parsers

3. Parsing. Oscar Nierstrasz

Sample Exam I PAC II ANSWERS

LALR Parsing. What Yacc and most compilers employ.

Wednesday, August 31, Parsers

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

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

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

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

CS 314 Principles of Programming Languages

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

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

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

Wednesday, September 9, 15. Parsers

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

An introduction introduction to functional functional programming programming using usin Haskell

Project 2 Interpreter for Snail. 2 The Snail Programming Language

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

A Simple Syntax-Directed Translator

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

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

Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc

UNIT III & IV. Bottom up parsing

LECTURE 3. Compiler Phases

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

The role of semantic analysis in a compiler

VIVA QUESTIONS WITH ANSWERS

CS 406: Syntax Directed Translation

More Bottom-Up Parsing

Outline CS4120/4121. Compilation in a Nutshell 1. Administration. Introduction to Compilers Andrew Myers. HW1 out later today due next Monday.

Lecture Bottom-Up Parsing

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Building Compilers with Phoenix

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

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

Introduction to Lexical Analysis

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

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

Transcription:

1. (8 points) Write a regular expression or set of regular expressions that generate the following sets of strings. You can use abbreviations (i.e., name = regular expression) if it helps to make your answer clearer. (a) (4 points) All strings of a s, b s, and c s that contain an even number of a s (if they contain any a s at all). (b c)* ( (b c)* a (b c)* a (b c)* )* (b) (4 points) All strings of a s, b s, c s, A s, B s, and C s that follow the Java capitalization convention for variable identifiers. This convention is that an identifier must begin with a lower-case letter (abc), followed by or more other letters, with the restriction that no two capital letters (ABC) may be next to each other. So a, aa, aaab, aacabcba, and abc are all examples of properly capitalized identifiers, while A, Aa, abc, and abba are examples of identifiers that are not properly capitalized. Here s one solution: (a b c)+ ( (A B C) (a b c)+ )* (A B C)? Note that there needs to be at least one leading lower case letter according to the definition, so expressions that can derive an empty string did not receive full credit. Also, it is possible for an identifier to end with a capital letter. 2. (8 points) For each of the following regular expressions, give a brief description in English of the set of strings generated by the expression. (Remember that * has higher precedence binds tighter than concatenation.) (Note: The question asked for a description of the sets of strings generated, not just a paraphrase or transliteration of the equations into English. Answers that did the later lost a point.) (a) (4 points) a*ba*ba*ba* All strings of a s and b s containing exactly 3 b s. (b) (4 points) (x*y*)* xx (x y)* All strings of x s and y s with at least one pair of adjacent x s. Page 1 of 8

3. (5 points) The C family of languages includes prefix and binary + and operators, as well as the increment and decrement operators ++ and --, which can appear either before or after an expression. Recalling that a scanner uses a principle of longest match when it s reading the input and parsing it into tokens, what are the tokens in the following statement? a++=++thing+++other--; (Notes: Don t worry if this statement is not legal C after all, the scanner doesn t care, does it? Be sure that your tokens are easy to understand: PLUS, PLUSPLUS and ID(xyzzy) are easy to understand; FOO, CAT, and THING are not.) ID(a) PLUSPLUS EQUALS PLUSPLUS ID(thing) PLUSPLUS PLUS ID(other) MINUSMINUS SCOLN 4. (8 points) The follow regular expression is alleged to generate all strings of s and 1 s that contain exactly one pair of adjacent 1 s: ( 1)* 11 ( 1)* Draw a DFA that accepts strings generated by this regular expression. Be sure that your diagram distinguishes between final and non-final states (use a double circle for final states and a single circle for non-final states). You don t need to use a formal NFA to DFA construction, just draw a DFA that accepts this set of strings. 1 1 1 5. (3 points) The suggested strategy for checking types and other static semantics in MiniJava is to make at least two passes over the AST the first to collect global information about the types (classes) defined in the program, and later passes to process the information (including methods) in each class. Give one technical reason why we need multiple passes over the AST. Why can t we do all of the type checking in a single traversal of the AST? Class and method names can be used before they appear in the source program, so we either need two passes, or we need some way of backing up to finish processing earlier uses of items declared later in the program, which amounts to the same thing. Page 2 of 8

6. (8 points) Consider the following C (or Java, C++, etc.) program fragment. if (c[i]!= ) x = 2 / c[i]; else x = ; Draw an abstract syntax tree (AST) for this program fragment. The AST should reflect the semantics of the source program, for example, the abstract syntax for c[i] should be shown as an array subscripting operation, rather than breaking it down into a sequence of address arithmetic expressions, or, at the other extreme, just leaving it as a single node containing the label c[i]. if!= = = [ ] INT() ID(x) / ID(x) INT() ID(c) ID(i) INT(2) [ ] ID(c) ID(i) Page 3 of 8

7. (8 points) Consider the following grammar: exprs ::= exprs + expr exprs * expr expr expr ::= x (a) (4 points) Is this grammar ambiguous or unambiguous? If it s ambiguous, show that by giving two different parse trees for some string generated by the grammar. If it s not ambiguous, give an informal argument why it is not. This is not ambiguous. We could prove this formally, but informally, the only way to derive a sequence of operations is to expand the leftmost nonterminal exprs repeatedly and, since this can only derive a single expr or another exprs that is also left-recursive, we have no choice about which alternative for exprs to expand at any point. (b) (4 points) Does this grammar properly capture the normal precedence of arithmetic operators * and +? (i.e., * should have higher precedence than +) If so, give a brief argument as to why it does, if not, give an example that shows where it fails to capture the correct precedence relationship. No. Consider the string x + x * x. The only possible derivation for this is exprs -> exprs * expr -> (exprs + expr) * expr -> -> (x + x) * x Since all derivations in this grammar are left associative, with no distinction between + and *, the operator + binds more tightly than * in the expression x+x*x. Page 4 of 8

8. (2 points) (the inevitable LR parsing question) One of the first, and longest-lasting applications on the Unix operating system is text processing, particularly for documents containing mathematics. Unix includes a preprocessor eqn that translated equations written in a small language into appropriate typesetting codes. The eqn source language can be described by a context-free grammar, and lex and yacc can be used to implement the eqn translator. A fragment of that grammar is the syntax for expressions involving superscripts:. eqns' ::= eqns $ ($ is the end-of-file marker) 1. eqns ::= eq sup eqns 2. eqns ::= eq 3. eq ::= x For example, x sup x sup x describes the equation x^(x^x). (Note that, as in mathematics, multiple superscripts group to the right.) The symbols sup and x are terminals representing themselves (a) (8 points) Construct a LR() state machine for this grammar. Be sure to show the set of items in each state. eqns ::=. eqns $ eqns ::=. eq sup eqns eqns ::=. eq eq ::=. x eqns x 3 eqns ::= eqns. $ 4 eq ::= x. eq 1 eqns ::= eq. sup eqns eqns ::= eq. x sup eq 2 eqns ::= eq sup. eqns eqns ::=. eq sup eqns eqns ::=. eq eq ::=. x eqns 5 eqns ::= eq sup eqns. (continued next page) Page 5 of 8

8 (cont.) Grammar repeated below for convenience. eqns' ::= eqns $ ($ is the end-of-file marker) 1. eqns ::= eq sup eqns 2. eqns ::= eq 3. eq ::= x (b) (2 points) Is this grammar LR()? Why or why not? No. There is a shift/reduce conflict in state 1 (shift on sup vs. reduce eqns ::= eq.). (c) (4 points) Compute First, Follow, and Nullable for each of the non-terminals in the grammar. First Follow Nullable eqns' x no eqns x $ no eq x sup, $ no (d) (4 points) Write down the SLR(1) action and goto table for this grammar, using the information in the LR() diagram from part (a) and the First, Follow, and Nullable information from part (c) of the question. state x sup $ eqns eq s4 g3 g1 1 s2 r2 2 s4 g5 g1 3 acc 4 r3 r3 5 r1 (e) (2 points) Is this grammar SLR(1)? Why or why not? Yes. The 1-symbol lookahead information gets rid of the shift-reduce conflict in state 1, and there are no other shift-reduce or reduce-reduce conflicts. Page 6 of 8

9. (16 points) Consider the following integer valued C function: int fun(int a, int b, int c) { int ans; ans = max(a, c); ans = max(ans, b); if (ans < ) { ans = - ans; } return ans; } (a) (4 points) Draw a picture showing the layout of the stack frame after the function prologue has executed but before the first statement in the function body is executed. Be sure to show the locations and numeric offsets of all of the local variables and parameters, as well as any other information in this function s stack frame. where the esp and ebp registers point in the stack frame. +16 c +12 b +8 a +4 return addr old ebp <- ebp -4 ans <- esp (b) (12 points) On the next page, write an x86 assembly-language version of this function. Your answer doesn t need to strictly imitate the code shape discussed in class straightforward x86 code that reflects the source code is all that s needed, but You need to use the proper C language calling conventions. Your assembly language code must include all of the statements shown, i.e., your code must show all the stores and loads implied by the various statements, even though this might not be strictly needed to achieve the same effect. Your code should address variables and parameters using the proper base register and offset in the stack frame. Assume that max is an integer-valued external function that you can call without requiring any further declarations. You do not need to include things like.386,.model, or other assembly language directives. Page 7 of 8

9. (cont). Function repeated for reference. Write your x86 assembly language version below. You just need to translate this function don t worry about the code that calls it. int fun(int a, int b, int c) { int ans; ans = max(a, c); ans = max(ans, b); if (ans < ) { ans = - ans; } return ans; } This answer shows code using a single register as an accumulator. More compact code is possible, and as long as your answer included the addressing details (particularly the correct offsets from ebp for the different variables), it was fine to omit some redundant loads (a few of the most obvious are marked with *, below). fun: push ebp mov ebp,esp sub esp,4 ; prologue mov eax,[ebp+16] ; ans = max(a,c); push eax ; (could use push [ebp+16] instead) mov eax,[ebp+8] ; (etc.) push eax call max add esp,8 mov [ebp-4],eax mov eax,[ebp+12] ; ans = max(ans,b); push eax mov eax,[ebp-4] push eax call max add esp,8 mov [ebp-4],eax mov eax,[ebp-4] ; if (ans < ) * cmp eax, jnl skip mov eax,[ebp-4] ; ans = - ans; * neg eax mov [ebp-4],eax skip: mov eax,[ebp-4] ; return ans; * mov esp,ebp pop ebp ret Page 8 of 8