CSCE 531, Spring 2017, Midterm Exam Answer Key

Similar documents
Definition of Regular Expression

Fig.25: the Role of LEX

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

Reducing a DFA to a Minimal DFA

Dr. D.M. Akbar Hussain

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

ASTs, Regex, Parsing, and Pretty Printing

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

Lexical Analysis: Constructing a Scanner from Regular Expressions

CMPSC 470: Compiler Construction

Compilers Spring 2013 PRACTICE Midterm Exam

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

TO REGULAR EXPRESSIONS

CSE 401 Midterm Exam 11/5/10 Sample Solution

Topic 2: Lexing and Flexing

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

CS 430 Spring Mike Lam, Professor. Parsing

ECE 468/573 Midterm 1 September 28, 2012

COMP 423 lecture 11 Jan. 28, 2008

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

Assignment 4. Due 09/18/17

Lexical analysis, scanners. Construction of a scanner

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

stack of states and grammar symbols Stack-Bottom marker C. Kessler, IDA, Linköpings universitet. 1. <list> -> <list>, <element> 2.

Compiler Construction D7011E

CS 321 Programming Languages and Compilers. Bottom Up Parsing

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22)

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

CMPT 379 Compilers. Lexical Analysis

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

CMSC 331 First Midterm Exam

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or

Scanner Termination. Multi Character Lookahead

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

Principles of Programming Languages

CIS 1068 Program Design and Abstraction Spring2015 Midterm Exam 1. Name SOLUTION

MTH 146 Conics Supplement

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

10.5 Graphing Quadratic Functions

Midterm I Solutions CS164, Spring 2006

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID:

Should be done. Do Soon. Structure of a Typical Compiler. Plan for Today. Lab hours and Office hours. Quiz 1 is due tonight, was posted Tuesday night

Lexical Analysis and Lexical Analyzer Generators

Context-Free Grammars

Algorithm Design (5) Text Search

CS 241 Week 4 Tutorial Solutions

Agenda & Reading. Class Exercise. COMPSCI 105 SS 2012 Principles of Computer Science. Arrays

Example: Source Code. Lexical Analysis. The Lexical Structure. Tokens. What do we really care here? A Sample Toy Program:

Ma/CS 6b Class 1: Graph Recap

From Dependencies to Evaluation Strategies

The Fundamental Theorem of Calculus

Theory of Computation CSE 105

Context-Free Grammars

2014 Haskell January Test Regular Expressions and Finite Automata

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications

COS 333: Advanced Programming Techniques

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

Suffix trees, suffix arrays, BWT

Distributed Systems Principles and Paradigms

Midterm 2 Sample solution

1.5 Extrema and the Mean Value Theorem

Compilation

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

Top-down vs Bottom-up. Bottom up parsing. Sentential form. Handles. Handles in expression example

Pointers and Arrays. More Pointer Examples. Pointers CS 217

Unit 5 Vocabulary. A function is a special relationship where each input has a single output.

box Boxes and Arrows 3 true 7.59 'X' An object is drawn as a box that contains its data members, for example:

CS201 Discussion 10 DRAWTREE + TRIES

Information Retrieval and Organisation

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example:

Alignment of Long Sequences. BMI/CS Spring 2012 Colin Dewey

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Suffix Tries. Slides adapted from the course by Ben Langmead

Lecture 7: Integration Techniques

What are suffix trees?

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins.

COS 333: Advanced Programming Techniques

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata

Slides for Data Mining by I. H. Witten and E. Frank

Recognition of Tokens

Ma/CS 6b Class 1: Graph Recap

Regular Expressions and Automata using Miranda

Transcription:

CCE 531, pring 2017, Midterm Exm Answer Key 1. (15 points) Using the method descried in the ook or in clss, convert the following regulr expression into n equivlent (nondeterministic) finite utomton: ( *c)* You my contrct -trnsitions provided the resulting utomton is equivlent. Answer: Any equivlent nswer etween the these two extremes is correct. c c There re other possile correct nswers. 2. (15 points) Using the sets-of-sttes pproch descried in clss, simulte the (nondeterministic) finite utomton given elow (in tulr form) on the input string w = cc. Tht is, for every prefix x of w (so x =,, c, c, c, cc, cc, cc in tht order), list ll sttes rechle from the strt stte y reding x. For ech prefix, list the sttes in incresing order without duplictes. Does the utomton ccept w? Why or why not? c 0 5 1, 2 1 3 2, 3 2 1 2 4 3 3, 6 0 4 7 4, 5 5 7 2, 4 6 6 0 6 7 3, 6 1

Answer: prefix sttes 0, 1, 2, 4 2, 3, 4, 5 c 0, 1, 2, 3, 4, 6, 7 (ll ut 5) c 1, 2, 3, 4, 5, 6, 7 (ll ut 0) c 0, 1, 2, 3, 4, 5, 6 (ll ut 7) cc 0, 1, 2, 3, 4, 6, 7 (ll ut 5) cc 1, 2, 3, 4, 5, 6, 7 (ll ut 0) cc 0, 1, 2, 3, 4, 5, 6 (ll ut 7) The input is rejected, ecuse the finl set of sttes does not contin n ccepting stte (i.e., stte 7). 3. (10 points) Give single flex-suitle regulr expression tht mtches ll Pscl-style comments. Tht is, your regex should mtch ll strings tht strt with (*, end with *), nd do not hve ny occurrence of *) in etween s sustring. For exmple, mtch not mtch -------------+------------------ (* hi *) (* hi *) (* ho *) (*********) (*) (*(*(* **) (* (* nest *) *) (*)))*(*) (* * Note tht opening nd closing delimiters cnnot overlp. Your nswer my include ny nmed suexpressions in curly rces, provided you define them fully s you would in the premle. You re NOT llowed to use the flex / opertor. Be s concise s possile. [Keep in mind tht the flex scnner will look for the longest mtch possile.] Answer: There re numer of correct nswers. To void confusion, I will define some nmed suexpressions, then list some equivlent ptterns. str "*" rpren ")" nonstr [^*] neither [^)*] open "(*" close "*)" %% {open}{nonstr}*{str}(({neither}{nonstr}*)?{str})*{rpren} {open}({nonstr} {str}+{neither})*{str}+{rpren} {open}{nonstr}*({str}({neither}{nonstr}*)?)*{close} {open}({nonstr} {str}+{neither})*{str}*{close} 2

There re other correct nswers. 4. (15 points totl) Here is grmmr with strt symol : () () (10 points) Using this grmmr, show complete prse tree yielding the string (((()))()()). () (5 points) Is this grmmr miguous? Explin. Answer: Here is prse tree tht does not use the second -production, tht is, it does not use. There re other prse trees for the sme string tht do use this production. ( ) ( ) ( ) ( ) ( ) ( ) The grmmr is miguous, ecuse nother string, (), is yielded y two different prse trees (equivlently, two different leftmost derivtions): () () () () () () () () 3

5. (25 points totl) Recll one of our stndrd, simplified grmmrs for rithmetic expressions, given in ycc/ison form (expr is the strt symol): expr : term expr + term expr - term term : fctor term * fctor term / fctor fctor : CONT VAR ( expr ) () (15 points) Alter to the grmmr so tht expressions cn involve nmed function clls. A nmed function cll hs the form f( ), where f is nme (VAR) nd is commseprted list of zero or more expressions (the rguments). Ech rgument cn e n ritrry expression. For exmple, the following re grmmticlly correct function cll expressions: foo() foo(r) r(t,ft) t(t+foo(6),t-foo*r)... Your ltered grmmr should still e unmiguous. () (10 points) Add semntic ctions to the originl, unltered grmmr ove so tht the root of the prse tree contins s its ttriute the sum of ll constnt vlues ppering in the expression. You cn ssume tht ll constnt vlues re integers. Also ssume tht the lexicl scnner sets the ttriute of ech CONT token to its vlue. If there re no constnts ppering in the expression, then the root ttriute should e 0. For exmple, if the input is 3*(42+z)+6, then the root should hve ttriute vlue 51 if the input is x-y+z, then the root should hve ttriute 0. Mke your ctions s simple s possile, nd do not use ny lirry routines. Any omitted ctions re ssumed to e the defult ction {$$ = $1}. 4

Answer: () We dd four productions to the grmmr ove, strting with fctor (the expr nd term productions re unchnged): fctor : CONT VAR ( expr ) VAR ( ) VAR ( rgs ) rgs : expr rgs, expr () Here is correct nswer: expr : term expr + term { $$ = $1 + $3 } expr - term { $$ = $1 + $3 } term : fctor term * fctor { $$ = $1 + $3 } term / fctor { $$ = $1 + $3 } fctor : CONT VAR { $$ = 0 } ( expr ) { $$ = $2 } Any missing ctions re defult ctions ($$ = $1). The first four ctions re identicl. 5