CS143 Midterm Fall 2008

Similar documents
Midterm I (Solutions) CS164, Spring 2002

CS143 Midterm Spring 2014

CS143 Midterm Sample Solution Fall 2010

CS164: Midterm I. Fall 2003

Midterm I - Solution CS164, Spring 2014

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

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

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

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

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

2068 (I) Attempt all questions.

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

BSCS Fall Mid Term Examination December 2012

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

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

CMSC 330: Organization of Programming Languages

CSE 401 Midterm Exam Sample Solution 2/11/15

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

JNTUWORLD. Code No: R

ECE251 Midterm practice questions, Fall 2010

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

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

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

UNIVERSITY OF CALIFORNIA

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

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

Lecture Bottom-Up Parsing

CMSC 330, Fall 2018 Midterm 2

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

Implementation of Lexical Analysis

Implementation of Lexical Analysis

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

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

Lexical Analysis. Chapter 2

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

CMSC 330: Organization of Programming Languages

Part 5 Program Analysis Principles and Techniques

Optimizing Finite Automata

CMSC 330: Organization of Programming Languages

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

Lexical Analysis. Lecture 2-4

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

CSE 401 Midterm Exam 11/5/10

CS 164 Programming Languages and Compilers Handout 8. Midterm I

Ambiguous Grammars and Compactification

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Introduction to Lexical Analysis

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

CS164 Second Midterm Exam Fall 2014

CMSC 330: Organization of Programming Languages. Context Free Grammars

Lexical Analysis. Lecture 3-4

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

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

Part I: Multiple Choice Questions (40 points)

CS 44 Exam #2 February 14, 2001

COP 3402 Systems Software Syntax Analysis (Parser)

1. [5 points each] True or False. If the question is currently open, write O or Open.

CMSC 330, Fall 2018 Midterm 2

CS 314 Principles of Programming Languages

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

LR Parsing Techniques

CMSC 330: Organization of Programming Languages. Context Free Grammars

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

Chapter 4. Lexical and Syntax Analysis

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

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

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

Lecture 7: Deterministic Bottom-Up Parsing

CSE 3302 Programming Languages Lecture 2: Syntax

Context-free grammars

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

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

CS143 Final Fall 2009

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

A simple syntax-directed

Lecture 8: Deterministic Bottom-Up Parsing

Question Bank. 10CS63:Compiler Design

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

CS606- compiler instruction Solved MCQS From Midterm Papers

CS 3L (Clancy) Solutions and grading standards for exam 1

CS415 Compilers. Lexical Analysis

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

Lecture Notes on Shift-Reduce Parsing

Interpreter. Scanner. Parser. Tree Walker. read. request token. send token. send AST I/O. Console

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

Bottom-Up Parsing. Lecture 11-12

Syntax Analysis Check syntax and construct abstract syntax tree

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

4. Lexical and Syntax Analysis

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

CMSC 330, Fall 2009, Practice Problem 3 Solutions

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

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

Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%

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

Transcription:

CS143 Midterm Fall 2008 Please read all instructions (including these) carefully. There are 4 questions on the exam, some with multiple parts. You have 75 minutes to work on the exam. The exam is closed book, but you may refer to your four 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. 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. Partial solutions will be graded for partial credit. NAME: In accordance with both the letter and spirit of the Honor Code, I have neither given nor received assistance on this examination. SIGNATURE: Problem Max points Points 1 20 2 30 3 20 4 30 TOTAL 1

1. Lexical Analysis (20 points) Consider the following flex rules: \n { printf("1") } A { printf("2") } a { printf("3") } d { printf("4") } [b-d]* { printf("5") } abc { printf("6") } [A-C]* { printf("7") } [a-c]* { printf("8") } [^a-c]+d[^a-c]+ { printf("9") } Here + means one or more and * means zero or more, as usual. What will be the output on the following input? Assume there is nothing to the right of the last visible character on each line except a newline character and that the following input is to be scanned all at once: ABCdabc Aabcd BCdbA bc abccdba The correct answer: 926921518531 using the tokenization: ABCdabc\n A abc d\nbcdb A \n bc \n abcc db a \n Also accepted: 912641921518531 using the tokenization: ABCdabc \n A abc d \n BCdb A \n bc \n abcc db a \n The most common mistakes were forgetting to use maximal munch or rule precedence. Note that [^a-c] and [^A-C] both match the newline character, which is why the second solution above is technically incorrect. However, newlines are treated specially in some contexts by lexing tools, and we decided not to to deduct points for this technical detail. The exception is that we did take off points if you were inconsistent --- if you included the 2

newline in [^a-c]/[^a-c] once but not both times. We also accepted any solution which did not put an extra newline at the very end of the input (i.e. did not include the final 1 ). 3

2. Grammars (30 points) Consider the following language: strings that can be split into k 0 substrings, where each substring is i 1 a s followed by i b s; different substrings may use different values of i. For example, this language includes the strings ɛ, ab, aabbab, and abaaabbbab. Give an LL(1) grammar for this language; prove your grammar is LL(1). The solution to this problem requires the grammar and an LL(1) parse table for it showing no conflicts. The parse table is needed to prove the grammar is LL(1); a grammar that is unambiguous, is not left recursive and has been left factored might still contain conflicts in its LL(1) parsing table. One solution is S -> atbs e T -> atb e with the table a b $ S atbs e T atb e An alternate solution is S -> TS e T -> au U -> b Tb with the table a b $ S TS e T au U Tb b There were two common mistakes in this problem. First, many people had a grammar that recognized the wrong language, such as S -> asbs e. These grammars can recognize strings not in the target language, such as aababb. To recognize the right language, at least two nonterminals are needed. Second, many people used a grammar that was ambiguous, and thus not LL(1), such as S -> TS e, T -> atb e. This grammar has an infinite number of 4

possible derivations for the string e. Give an ambiguous grammar for this language. Prove your grammar is ambiguous. The solution to this problem requires the grammar and two different parse trees or their derivations for the same string. S -> TS e T -> atb e Two derivations for different parse trees on string e: S -> e S -> TS -> S -> e The only common mistake here was giving a grammar that recognized the wrong language, such as S -> SaSbS e. Some people used the right language in part a) and the wrong language here, or vice versa. At least two nonterminals are needed whether the grammar is ambiguous or not. 5

3. Grammars and Semantic Actions (20 points) Consider the following grammar and associated semantic actions. S -> ABCD { x = 11 * x + 1; } D -> d { x = 7 * x + 1; } C -> cc { x = 5 * x + 1; } B -> Bb { x = 3 * x + 1; } B -> b { x = x + 1; } A -> gba { x = 2 * x + 1; } The variable x is global i.e., all semantic actions update the same x. Assume x is initialized before parsing to 0. What is the final value of x in a bottom-up parse of the following input string: gbbabbccd For full credit, show how you derived your answer. The rightmost derivation is shown in the right column, the production (reduction) used is shown in the middle column, and the semantic action is shown in the right column. Since a bottom-up parser traces a rightmost derivation in reverse, the actions are performed in order from the bottom to the top of the column. S -> S -> ABCD 11 * 1093 + 1 = 12024 <- answer ABCD -> D -> d 7 * 156 + 1 = 1093 ABCd -> C -> cc 5 * 31 + 1 = 156 ABccd -> B -> Bb 3 * 10 + 1 = 31 ABbccd -> B -> b 9 + 1 = 10 Abbccd -> A -> gba 2 * 4 + 1 = 9 gbabbccd -> B -> Bb 3 * 1 + 1 = 4 gbbabbccd -> B -> b 0 + 1 = 1 gbbabbccd The most common mistakes were: tracing a leftmost derivation and having a rightmost derivation but evaluating the semantic actions in the wrong order. While it was unnecessary to construct the parsing automaton to answer the question, some people did so and we did not penalize that. Similarly, quite a few people made arithmetic errors, which we did not penalize if the order of the semantic actions was clear to us. 6

4. Short Answer (30 points) For each of the following questions, give a short, concise answer. We are definitely looking for simplicity and clarity in addition to correctness. Give a regular expression and a CFG that accept the same infinite language of your choice. There are many possible answers, but a simple one is: regular expression: a* context-free grammar: S -> Sa epsilon A common mistake was writing a grammar that had no strings in the language at all because it lacked a base case where a non-terminal was replaced by only terminals (e.g., the grammar S -> Sa). For any n, show how to construct a CFG that has a single string x in its language but at least n derivations for x. There are many possible answers. S -> A1... An A1 -> x... An -> x A straightforward one is Another common class of answers gave grammars with infinitely many derivations: S -> Ax A -> A epsilon Give a regular expression that, for any n 0, has 2 n strings of length n in its language. (0+1)* An answer such as (ab)* is incorrect --- the language has only even length strings. Give two implementations for deciding whether an NFA accepts an input string. There are at least three possible implementations: (1) Convert the NFA to an equivalent DFA and run the DFA on the input. (2) Simulate the execution of the NFA, making arbitrary choices for non-deterministic choices and backtracking to undo choices that do not lead to acceptance of the input string. (3) Simulate the execution of the NFA, keeping track at each step of the set 7

of states the NFA could be in. True or false: The smallest DFA that accepts a given language may be exponentially larger than the smallest NFA that accepts the same language. Why? True. The subset of states construction can result in exponentially many more states in the DFA than in the NFA. (Note this argument is just that, an argument and not a proof.) 8