Compilers Spring 2013 PRACTICE Midterm Exam

Similar documents
ECE 468/573 Midterm 1 September 28, 2012

Definition of Regular Expression

Midterm I Solutions CS164, Spring 2006

CS 321 Programming Languages and Compilers. Bottom Up Parsing

CSE 401 Midterm Exam 11/5/10 Sample Solution

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

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

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.

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

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

Theory of Computation CSE 105

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

CSCE 531, Spring 2017, Midterm Exam Answer Key

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

CS 430 Spring Mike Lam, Professor. Parsing

CMSC 331 First Midterm Exam

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

Fig.25: the Role of LEX

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

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

Dr. D.M. Akbar Hussain

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

Reducing a DFA to a Minimal DFA

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES

Lexical analysis, scanners. Construction of a scanner

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

ASTs, Regex, Parsing, and Pretty Printing

CMPSC 470: Compiler Construction

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

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

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

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

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

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

CS 241 Week 4 Tutorial Solutions

Lexical Analysis: Constructing a Scanner from Regular Expressions

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

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

Topic 2: Lexing and Flexing

Midterm 2 Sample solution

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

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

TO REGULAR EXPRESSIONS

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

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

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

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

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

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

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

MTH 146 Conics Supplement

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

Compiler Construction D7011E

COMP 423 lecture 11 Jan. 28, 2008

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

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

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

Lab 1 - Counter. Create a project. Add files to the project. Compile design files. Run simulation. Debug results

Naming 3D objects. 1 Name the 3D objects labelled in these models. Use the word bank to help you.

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

CS201 Discussion 10 DRAWTREE + TRIES

Context-Free Grammars

Answer Key Lesson 6: Workshop: Angles and Lines

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

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

Context-Free Grammars

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

12-B FRACTIONS AND DECIMALS

Scanner Termination. Multi Character Lookahead

PARALLEL AND DISTRIBUTED COMPUTING

8.2 Areas in the Plane

4-1 NAME DATE PERIOD. Study Guide. Parallel Lines and Planes P Q, O Q. Sample answers: A J, A F, and D E

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

Lecture T4: Pattern Matching

Outline. Motivation Background ARCH. Experiment Additional usages for Input-Depth. Regular Expression Matching DPI over Compressed HTTP

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

Agilent Mass Hunter Software

Chapter44. Polygons and solids. Contents: A Polygons B Triangles C Quadrilaterals D Solids E Constructing solids

Section 10.4 Hyperbolas

Eliminating left recursion grammar transformation. The transformed expression grammar

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

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

Recognition of Tokens

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

CS481: Bioinformatics Algorithms

MIPS I/O and Interrupt

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

Fall 2018 Midterm 2 November 15, 2018

Graphs with at most two trees in a forest building process

Lecture 7: Integration Techniques

COMMON FRACTIONS. or a / b = a b. , a is called the numerator, and b is called the denominator.

Principles of Programming Languages

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

Math 142, Exam 1 Information.

The Math Learning Center PO Box 12929, Salem, Oregon Math Learning Center

CS2204 DIGITAL LOGIC & STATE MACHINE DESIGN SPRING 2014

Typing with Weird Keyboards Notes

Announcements. CS 188: Artificial Intelligence Fall Recap: Search. Today. General Tree Search. Uniform Cost. Lecture 3: A* Search 9/4/2007

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

Assignment 4. Due 09/18/17

Transcription:

Compilers Spring 2013 PRACTICE Midterm Exm This is full length prctice midterm exm. If you wnt to tke it t exm pce, give yourself 7 minutes to tke the entire test. Just like the rel exm, ech question hs point vlue. There re 7 points in the exm, so tht you cn pce yourself to verge 1 point per minute (some prts will e fster, some slower). Questions: 1. SML [ points] 2. Regulr Lnguges [ points] 3. NFA to regulr expressions [10 points] 4. NFA to DFA [10 points]. Regulr expression to NFA [10 points] 6. Context Free Lnguges [ points] 7. LL Prsing [10 points] 8. LR Prsing [10 points] 9. Types [10 points] This is the solution set to the prctice exm. The solutions pper in lue oxes. 1

Question 1: SML [ pts] Prt A: Evlute the following SML expressions (1 point ech): 1. let vl = 4 vl = in if ( > ) then 1 else 2 end 2 2. let vl = 2 vl = let vl = 4 vl q = in + q end in + end 11 3. let fun f (0, ) = f (, ) = f (-1, +) in f (3, ) end 11 2

Prt B: Write down the type for ech of these SML functions (1 point ech): 1. fun f (x) = x ^ " " ^ x string string 2. fun f (x) = let fun g ([], ns) = ns g (::l,ns) = g(l, ::::ns) in g(x,[]) end list list 3

Question 2: Regulr Lnguges [ pts] 1. Write regulr expression for ll strings of s nd s which contins the sustring (2 point). ( ) ( ) 2. Write regulr expression for ll strings of xs nd ys where every y is immeditely followed y t lest 3 xs (2 points). (x (yxxx)) 4

Question 3: NFA to Regexp [10 pts] Convert the following NFA to regulr expression : 2 c 1 d 3 g e 4 f The regulr expression is ( d) ( ( cg) (e f) ). We derive it in 4 steps shown elow (note you cn remove sttes in different orders nd come up with different, equivlent nswers which re still correct). Step 0: Put in new strt stte with n ɛ trnsition to the old strt. Step 1: Remove sttes 3 nd 4. Step 2: Remove stte 2. This results in loop on 1 nd new edge from 1 to. Step 3: Collpse the prllel 1 edges into n OR. Step 4: Remove stte 1. The nswer is written on the remining edge

Step 0 2 c 0 1 d 3 g e 4 f Step 1 2 0 1 d cg ef Workspce for question 3 6

Step 2 * d 0 1 * cg ef Step 3 * d 0 1 ( * cg) (ef) Step 4 0 ( * d) * (( * cg) (ef)) Workspce for question 3 7

Question 4: NFA to DFA [10 pts] Convert the following NFA to DFA: 1 2 3 4 {2,4} {1} {1,3} {3} {2,3,4} 8

Question : Regexp to NFA [10 pts] Drw n NFA for the following regulr expressions: 1. ( c) d (2 points) 1 2 3 d 4 c 2. (c) (2 points) 1 2 3 c 9

3. ( ( ) c (d e) ( f g) ) h (6 points) 0 1 2 3 h c 7 d 4 e f g 6 10

Question 6: Context Free Lnguges [ pts] Write context free grmmrs for the following lnguges (your grmmr does not hve to e LR(1), LL(1) etc): 1. The lnguge descried y the regulr expression (() (c d)) (2 points). S E S ɛ E A c A d A A ɛ Note tht this ws derived y mking A which is (). Then mking E which is the contents of the outer str. Then mking S e zero or more repetitions of E. 2. Expressions consisting of num, +, nd *. You should write your grmmr so tht * hs higher precedence thn + (3 points): E E + T T T T * num num 11

Question 7: LL Prsing [10 pts] Consider the following grmmr: S -> S S c Q q Q -> Q m Which non-terminls (if ny) cn derive empty? (1 point) Q cn derive empty. Wht re the FIRST sets of Q nd S? (1 point). The FIRST set of Q is {m}. The FIRST set of S is {m,q,c} Wht re the FOLLOW sets of Q nd S? (1 point) The FOLLOW set of Q is {q,m}. The FOLLOW set of S is {,}. This grmmr cn not e prsed y n LL(0) or LL(1) prser. Explin why not (2 points). The grmmr is left recursive (in oth the rules S S S nd Q Q m). LL prsers cnnot hndle left recursion. Rewrite the grmmr so tht it ccepts the sme lnguge, ut cn e prsed y n LL(1) prser ( points). S c Stil Q q Stil Stil S Stil Q m Q 12

Question 8: LR Prsing [10 pts] Consider the following grmmr : 0: S -> X 1: X -> X c 2: X -> X X 3: X -> 1. Wht is Closure({X X. X)? (2 points) X X. X X. X c X. X X X. 2. Wht is Goto({X. X c}, X)? (2 points) Closure{X X. c} which is just X X. c 3. Show the execution of the prser on the string c. The stte mchine for the prser is provided long with tle for you to fill in on the next pge (6 points). 13

Using the grmmr from the previous pge: 0: S -> X 1: X -> X c 2: X -> X X 3: X -> And the stte mchine for tht grmmr: Stte c $ S X 1 s2 s3 g g4 2 s2 s3 g6 3 r3 r3 r3 r3 4 s2 s3 r0 g7 Acpt 6 s3 s8 g7 7 r2 r2 r2 r2 8 r1 r1 r1 r1 Fill in the tle to the right. In one line, show the current sttus of the prser the position in the input, the stte the prser is in, nd the contents of the stck. In the next line, show the ction tht the prser tkes. Then show the new sttus in the following line. Repet this process until the prser ccepts the input. The first two re done for you. 14 Input Stte Stck. c 1 Shift to stte 2. c 2 1 Shift to stte 3. c 3 1 2 Reduce y rule 3. c 2 1 X 2 Goto 6. c 6 1 X 2 Shift to stte 3. c 3 1 X 2 6 Reduce y rule 3. c 6 1 X 2 X 6 Goto 7. c 7 1 X 2 X 6 Reduce y rule 2. c 2 1 X 2 Goto 6. c 6 1 X 2 Shift to stte 8 c. 8 1 X 2 c 6 Reduce y rule 1 c. 1 X 1 Goto 4 c. 4 X 1 Reduce y rule 0 c. 1 S 1 Goto c. S 1 Accept

Question 9: Types [10 pts] 1. Show the typing derivtion for the Tiger sttement x := f(r.) + 3. You my ssume tht your initil environment (Γ 0 ) hs the following mppings (in ddition to the se Tiger environment): Γ 0 (x) = int Γ 0 () = int Γ 0 (r) = Record(:string, :int) Γ 0 (f) = string int Γ 0 r : Record(:string,...) Γ 0 f : string int Γ 0 r. : string Γ 0 f(r.) : int Γ 0 3 : int Γ 0 x : int Γ 0 f(r.) + 3 : int Γ 0 x := f(r.) + 3 : unit 2. Fill in the correct premises for the su-typing rule for function types. T 1 S 1 S 2 T 2 S 1 S 2 T 1 T 2 1