CSE 401 Midterm Exam 11/5/10 Sample Solution

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

CSE 401 Midterm Exam 11/5/10

Dr. D.M. Akbar Hussain

Midterm I Solutions CS164, Spring 2006

Compilers Spring 2013 PRACTICE Midterm Exam

ECE 468/573 Midterm 1 September 28, 2012

Assignment 4. Due 09/18/17

Definition of Regular Expression

CMPSC 470: Compiler Construction

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

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

CMSC 331 First Midterm Exam

CSCE 531, Spring 2017, Midterm Exam Answer Key

Fig.25: the Role of LEX

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

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

CS 321 Programming Languages and Compilers. Bottom Up Parsing

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

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

CS 430 Spring Mike Lam, Professor. Parsing

ASTs, Regex, Parsing, and Pretty Printing

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.

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

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

Theory of Computation CSE 105

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

Topic 2: Lexing and Flexing

Reducing a DFA to a Minimal DFA

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

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

Lexical Analysis: Constructing a Scanner from Regular Expressions

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

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

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

Stack. A list whose end points are pointed by top and bottom

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

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

TO REGULAR EXPRESSIONS

Lexical analysis, scanners. Construction of a scanner

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

PYTHON PROGRAMMING. The History of Python. Features of Python. This Course

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

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

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

Functor (1A) Young Won Lim 10/5/17

Functor (1A) Young Won Lim 8/2/17

Scanner Termination. Multi Character Lookahead

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

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

Lecture T4: Pattern Matching

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

2014 Haskell January Test Regular Expressions and Finite Automata

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

Spring 2018 Midterm Exam 1 March 1, You may not use any books, notes, or electronic devices during this exam.

Compilation

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

Midterm 2 Sample solution

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

Fall 2018 Midterm 1 October 11, ˆ You may not ask questions about the exam except for language clarifications.

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

CSc 453 Compilers and Systems Software. 6 : Top-Down Parsing I

Eliminating left recursion grammar transformation. The transformed expression grammar

Principles of Programming Languages

LEX5: Regexps to NFA. Lexical Analysis. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

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

Context-Free Grammars

Data Flow on a Queue Machine. Bruno R. Preiss. Copyright (c) 1987 by Bruno R. Preiss, P.Eng. All rights reserved.

Compiler Construction D7011E

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

Scanning Theory and Practice

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

CS201 Discussion 10 DRAWTREE + TRIES

Context-Free Grammars

Context-Free Grammars

File Manager Quick Reference Guide. June Prepared for the Mayo Clinic Enterprise Kahua Deployment

CS 236 Language and Computation. Alphabet. Definition. I.2.1. Formal Languages (10.1)

Lexical Analysis and Lexical Analyzer Generators

CPSC 213. Polymorphism. Introduction to Computer Systems. Readings for Next Two Lectures. Back to Procedure Calls

12-B FRACTIONS AND DECIMALS

5 Regular 4-Sided Composition

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

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

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

EECS 281: Homework #4 Due: Thursday, October 7, 2004

Engineer To Engineer Note

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

HW Stereotactic Targeting

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

1. SEQUENCES INVOLVING EXPONENTIAL GROWTH (GEOMETRIC SEQUENCES)

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

Questions About Numbers. Number Systems and Arithmetic. Introduction to Binary Numbers. Negative Numbers?

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

MA1008. Calculus and Linear Algebra for Engineers. Course Notes for Section B. Stephen Wills. Department of Mathematics. University College Cork

Subtracting Fractions

MATH 25 CLASS 5 NOTES, SEP

What do all those bits mean now? Number Systems and Arithmetic. Introduction to Binary Numbers. Questions About Numbers

The Fundamental Theorem of Calculus

Virtual Machine (Part I)

From Dependencies to Evaluation Strategies

Fall 2017 Midterm Exam 1 October 19, You may not use any books, notes, or electronic devices during this exam.

Transcription:

Question 1. egulr expressions (20 points) In the Ad Progrmming lnguge n integer constnt contins one or more digits, but it my lso contin embedded underscores. Any underscores must be preceded nd followed by t lest one digit, which implies tht number my not begin or end with n underscore, nd numbers my not contin two or more djcent underscores. Underscores hve no significnce other thn redbility. Exmples: 0, 17, 017, 0_01_7, 1_048_576. Integers my lso be written in bses other thn 10 by preceding the number with the bse nd # chrcter. For exmple, ll of these constnts represent the deciml number 15231: 15231, 10#15231, 16#3B7F, 16#03B_7F, 2#00111011_0111111, nd 8#35577. Other bses besides powers of 2 re lso llowed, for exmple, 2#00101, 7#5, nd 5#10 ll represent the deciml number 5. The bse of number (preceding the # sign) must be number tht is t lest 2 nd t most 16, nd is written in deciml. The digits in number consist of the deciml digits 0 through 9, nd the letters A, B, C, D, E, F, written only in upper cse. The lnguge specifies tht the digits must be less thn the bse if one is given, but for this problem we ll ignore tht requirement nd llow ny string of digits, including A through F, to pper following bse if one is present. If no bse is present, the digits my only be 0 through 9. () (10 points) Give regulr expression for Ad integer constnts s described bove. You my only use bsic regulr expression opertors (conctention rs, choice r s, Kleene str r*) nd the dditionl opertors r+ nd r?. You my lso specify chrcter sets using the nottion [bw-z], nd sets excluding specified chrcters [^eiou]. Finlly, you cn nme prts of the regulr expression, like vowel=[eiou]. decint = [0-9](_?[0-9])* bsedint = (1[0-6] [2-9])#[0-9A-F](_?[0-9A-F])* integer = decint bsedint (continued on next pge) Pge 1 of 8

Question 1. (cont.) (b) (10 points) Drw DFA (Deterministic Finite Automt) tht recognizes Ad integer constnts s described in the problem nd generted by the regulr expression in your nswer to prt (). 0-9 0-9 _ 0 7-9 _ 0-9 _ 0-9A-F 1 0-6 # _ 0-9A-F 2-9 0-9A-F Pge 2 of 8

Question 2. Ambiguity (14 points) The syntx used to specify regulr expressions cn itself be defined by context-free grmmr. Here is one possible grmmr for regulr expressions with the opertors conctention, choice ( ), Kleene str ( * ), nd prenthesized subexpressions over the lphbet {, b }. ::= ::= ::= * ::= ( ) ::= ::= b (conctention) (the here is the literl regulr expression choice opertor) (Kleene str) Show tht this grmmr for specifying the syntx of regulr expressions is mbiguous. There re huge number of possibilities. Here re two prse trees for : Pge 3 of 8

Question 3. (30 points) The you re-probbly-not-surprised-to-see-it L-prsing question. Here is tiny grmmr. 0. S ::= S $ 1. S ::= x S S 2. S ::= y () (12 points) Drw the L(0) stte mchine for this grmmr. 1 S ::=. S $ S ::=. x S S S ::=. y S y S ::= S. $ S ::= y. 3 2 x x 4 S ::= x. S S S ::=. x S S S ::=. y y x S 5 S ::= x S. S S ::=. x S S S ::=. y S y 6 S ::= y. (continued on next pge) Pge 4 of 8

Question 3. (cont.) Grmmr repeted from previous pge for reference. 0. S ::= S $ 1. S ::= x S S 2. S ::= y (b) (12 points) Construct the L(0) prse tble for this grmmr bsed on the stte mchine in your nswer to prt (). x y $ S 1 s4 s3 g2 2 cc 3 r2 r2 r2 4 s4 s3 g5 5 s4 s3 g6 6 r1 r1 r1 (b) (3 points) Is this grmmr L(0)? Why or why not? Yes. No shift-reduce or reduce-reduce conflicts in the tble. (c) (3 points) Is this grmmr SL? Why or why not? Yes. All L(0) grmmrs re lso SL. Pge 5 of 8

Question 4. First/Follow/Nullble (20 points) Consider the following grmmr: S ::= A C B A ::= B C B A B ::= b C C ::= C b C c ε Complete the following tble to give the FIST nd FOLLOW sets nd NULLABLE ttribute for ech of the non-terminls in the grmmr. Symbol NULLABLE FIST FOLLOW S F { b } $ A F { b } { b, c } B F { b } {, b, c } C T { b, c } {, b, c } Pge 6 of 8

Question 5. Prsing tools (16 points) For this problem we would like to trnslte rithmetic expressions from ordinry infix nottion to postfix using CUP. In postfix nottion binry expression like *b is written s b*, with the opernds ppering in the sme order s they did originlly followed by the opertor. Opertors ct on the two expressions or subexpressions immeditely to their left nd opertors re executed s soon s they re reched scnning left to right. Postfix expressions re unmbiguous nd do not require prentheses or opertor precedence rules. Here re severl exmples: Infix Postfix Evlution order -b+c b-c+ -b is computed, then c is dded to the result -(b+c) bc+-, b, nd c re scnned, then b+c is computed first, then tht result is the right opernd of the subtrction from +b*c bc*+ multiply b*c, then dd tht result to (+b)*c b+c* dd +b first, then multiply by c *b+c b*c+ multiply *b first, then dd c (*b)+c b*c+ sme *b+c*d b*cd*+ multiply *b, multiply c*d, then dd the two products On the next pge, complete the CUP specifiction so the resulting prser will ccept infix rithmetic expressions involving identifiers; the opertors +, -, nd *; nd prenthesized subexpressions, nd print the corresponding postfix expression. (Note: you re not building tree s you did for the prser prt of the project. Just print things t pproprite plces.) You should ssume tht the semntic ctions cn cll function print(s) tht will print the contents of string s to the output immeditely following ny previous output. The semntic ction for identifier is supplied s n exmple. (We re tking some liberties with types here nd re ssuming tht when n identifier token is printed the corresponding identifier string ppers in the output. Tht is not true for the opertors PLUS, TIMES, etc. You need to print pproprite literl strings in the right plces.) You will lso need to dd precedence declrtions to hndle the mbiguities in the given grmmr. Write your nswer on the (you cn detch this pge if you like) next pge => Pge 7 of 8

Question 5 (cont.) Complete the CUP specifiction below to red infix expressions nd print the corresponding postfix. (Hint: the nswers my be quite short nd/or simple don t be lrmed if tht hppens.) /* Terminls (tokens returned by the scnner) */ terminl PLUS, MINUS, TIMES, LPAEN, PAEN, IDENTIFIE; /* Nonterminls */ nonterminl void Expr; /* Precedence declrtions dd nything pproprite here */ precedence left PLUS, MINUS; precedence left TIMES; /* Productions */ Expr ::= IDENTIFIE:id {: print(id) :} Expr:exp1 PLUS Expr:exp2 {: print( + ); :} Expr:exp1 MINUS Expr:exp2 {: print( - ); :} Expr:exp1 TIMES Expr:exp2 {: print( * ); :} LPAEN Expr:exp PAEN {: /* nothing needed */ :} ; Pge 8 of 8