CSCI565 - Compiler Design

Similar documents
Compiler Design. Fall Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Solutions Problem Set 2. Problem (a) Let M denote the DFA constructed by swapping the accept and non-accepting state in M.

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER MACHINES AND THEIR LANGUAGES ANSWERS

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

Minimal DFA. minimal DFA for L starting from any other

Finite State Automata and Determinisation

Nondeterministic Automata vs Deterministic Automata

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Thoery of Automata CS402

Compiler Design. Fall Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

CS 330 Formal Methods and Models

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

Regular expressions, Finite Automata, transition graphs are all the same!!

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

First Midterm Examination

Let's start with an example:

Worked out examples Finite Automata

Closure Properties of Regular Languages

First Midterm Examination

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Myhill-Nerode Theorem

Deterministic Finite Automata

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

State Complexity of Union and Intersection of Binary Suffix-Free Languages

Homework 3 Solutions

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

Tutorial 2 Euler Lagrange ( ) ( ) In one sentence: d dx

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

CS 314 Principles of Programming Languages

Chapter 2 Finite Automata

FABER Formal Languages, Automata and Models of Computation

Running an NFA & the subset algorithm (NFA->DFA) CS 350 Fall 2018 gilray.org/classes/fall2018/cs350/

CS 275 Automata and Formal Language Theory

CS 573 Automata Theory and Formal Languages

Fundamentals of Computer Science

Lecture 08: Feb. 08, 2019

ɛ-closure, Kleene s Theorem,

CMSC 330: Organization of Programming Languages

1.4 Nonregular Languages

CS375: Logic and Theory of Computing

CSC 311 Theory of Computation

Formal languages, automata, and theory of computation

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true.

Chapter 4 Regular Grammar and Regular Sets. (Solutions / Hints)

Formal Languages and Automata

Technische Universität München Winter term 2009/10 I7 Prof. J. Esparza / J. Křetínský / M. Luttenberger 11. Februar Solution

Designing finite automata II

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

Project 6: Minigoals Towards Simplifying and Rewriting Expressions

SWEN 224 Formal Foundations of Programming WITH ANSWERS

Review for the Midterm

CSE 355 Homework Two Solutions

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

Prefix-Free Regular-Expression Matching

Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Kleene-*

1 From NFA to regular expression

a b [^ab] ^a [^ab] [^ab]

CS 330 Formal Methods and Models Dana Richards, George Mason University, Spring 2016 Quiz Solutions

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

Automata Theory 101. Introduction. Outline. Introduction Finite Automata Regular Expressions ω-automata. Ralf Huuck.

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

2.4 Theoretical Foundations

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

Nondeterministic Finite Automata

Bottom-Up Parsing. Canonical Collection of LR(0) items. Part II

NON-DETERMINISTIC FSA

= state, a = reading and q j

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Regular languages refresher

Lecture 6 Regular Grammars

CSE : Exam 3-ANSWERS, Spring 2011 Time: 50 minutes

CISC 4090 Theory of Computation

CS241 Week 6 Tutorial Solutions

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

Formal Languages Simplifications of CFGs

For convenience, we rewrite m2 s m2 = m m m ; where m is repeted m times. Since xyz = m m m nd jxyj»m, we hve tht the string y is substring of the fir

CS 275 Automata and Formal Language Theory

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

NFA and regex. the Boolean algebra of languages. non-deterministic machines. regular expressions

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

MATH4455 Module 10 Evens, Odds and Ends

Lecture 09: Myhill-Nerode Theorem

Harvard University Computer Science 121 Midterm October 23, 2012

3 Regular expressions

CSE 401 Compilers. Today s Agenda

DFA Minimization and Applications

Context-Free Grammars and Languages

CS 311 Homework 3 due 16:30, Thursday, 14 th October 2010

p-adic Egyptian Fractions

Tutorial Automata and formal Languages

Languages & Automata

Java II Finite Automata I

Parse trees, ambiguity, and Chomsky normal form

I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines

80 CHAPTER 2. DFA S, NFA S, REGULAR LANGUAGES. 2.6 Finite State Automata With Output: Transducers

Finite-State Automata: Recap

1 Nondeterministic Finite Automata

Formal Language and Automata Theory (CS21004)

Transcription:

CSCI565 - Compiler Deign Spring 6 Due Dte: Fe. 5, 6 t : PM in Cl Prolem [ point]: Regulr Expreion nd Finite Automt Develop regulr expreion (RE) tht detet the longet tring over the lphet {-} with the following propertie:. The tring egin with n hrter nd end with hrter;. After the firt, the tring n inlude n lterntion of two uequene, nmely equene tht egin with hrter followed y ero or more hrter ending with n e hrter nd uequene tht egin with hrter ut doe not hve ny hrter until terminting t hrter.. If t ny point in the uequene there i x hrter, tht peifi equene i onidered terminted, i.e., the x t the e hrter in the firt type of equene nd the t hrter in the eond type of equene. 4. The two uequene nnot e neted ut n e repeted in ny lternting order. A n exmple the tring efghte i to e epted well the tring xfft Quetion: ) [5 point] Develop regulr expreion tht pture the truture of the eptle tring deried ove. Ue hort-hnd to pture uet of hrter in the lphet o tht your deritpion nd the orreponding FA re kept hort. ) [ point] Uing the regulr expreion define in etion ) devie the orreponding Non- Determiniti Finite Automton (NFA) uing the Thompon ontrution deried in l. ) [ point] Convert the NFA in etion ) to DFA uing the uet ontrution. Show the mpping etween the tte in the NFA nd the reulting DFA. d) [5 point] Minimie the DFA derived in etion ) (or how it i lredy miniml) uing the itertive refinement lgorithm deried in l. Solution: ) Among the mny poile olution for regulr expreion the one elow eem to e the mot intuitive. Here we ue the hort-hnd notstx for ll hrter in the lphet exept nd t i.e., notstx = {-r, u, v, y, }. RE =. [ (. ( * ). (x e) ) (. ( notstx * ). (x t) ) ] *. ) The NFA elow reflet the truture of the RE preented ove where we hve lo ompreed ome ε-trnition for revity, in prtiulr the one tht reflet Kleene loure nd lterntion. of 7

5 7 9 5 6 trt 4 /{tx} 6 8 4 ) The uet ontrution of thi NFA i hown elow where gin we hve merged trnition on hrter with nlogou ehvior. trt,,,4 5, 6,8,,,,4,,,5,6,,,4,,4, 5,6 /tx 8, /tx d) The minimition of thi DFA uing the itertive refinement lgorithm i ummried elow. We egin y imple prtitionetween the epting tte nd the non-epting tte. Thi immeditely low u to iolte the epting tte. The eond prtition ued the hrter the dirimintig token howing tht tte,,,4,,,5,6 nd,,,4,,4,5,6 my e equivlent. The lt prtition digrm reflet the ft tht ll tte re dijoint. of 7

of 7 P P P4 P5 P6 P7 trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6 P P4 P5 P6 P7 trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6 P8 P trt 5 /tx /tx 8 4 6 P P P4 trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6 P P trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6 P trt,,,4 5,,,,4,,,5,6 6,8, /tx /tx 8,,,,4,,4,5,6

Prolem [ point]: Trnltion from DFA to Regulr Expreion Given the DFA elow over the lphet {,} determine the following:. [5 point] Ue the dynmi-progrmming Kleene lgorithm to derive the regulr expreion tht denote the lnguge epted y it. Mke ure your lelling of the DFA tte i orret nd tht the DFA i ompletely peified, i.e., eh tte h trnition on ll the lphet hrter.. [5 point] Derie uintely wht re the word epted y thi DFA?, trt Solution:.. [5 point] Ue the dynmi-progrmming Kleene lgorithm to derive the regulr expreion tht denote the lnguge epted y it. Mke ure your lelling of the DFA tte i orret nd tht the DFA i ompletely peified, i.e., eh tte h trnition on ll the lphet hrter. Expreion for k = R = (ε) R = () R = () R R = ( ε) R R R = () R = ( ε) Expreion for k = R = R (R )*R R = (ε)(ε) * (ε) (ε) = (ε) R = R (R )*R R = (ε)(ε) * () () = () R = R (R )*R R = (ε)(ε) * () () = () R = R (R )*R R = R R = R (R )*R R = R = ( ε) R = R (R )*R R = R R = R (R )*R R = R 4 of 7

R = R (R )*R R = R = () R = R (R )*R R = R = ( ε) Expreion for k = R = R (R )*R R = (). ( ε) *. Ø (ε) = (ε) R = R (R )*R R = (). ( ε) *. ( ε) () = ( + ) R = R (R )*R R = (). ( ε) *. Ø () = () R = R (R )*R R = ( ε) ( ε)* Ø Ø R = R (R )*R R = ( ε) ( ε)* ( ε) ( ε) = ( * ) R = R (R )*R R = ( ε) ( ε)* Ø Ø R = R (R )*R R = (). ( ε)*. Ø Ø R = R (R )*R R = (). ( ε)*. ( ε) () = (). ()* R = R (R )*R R = (). ( ε)*. Ø ( ε) = ( ε) Expreion for k = R = R (R )*R R = (). ()*. Ø (ε) R = R (R )*R R = (). ()*. (). ()* ( + ) R = R (R )*R R = (). ()*. ( ε) () = (). ()* R = R (R )*R R. ()*. Ø Ø R = R (R )*R R. ()*. (). ()* ( * ) = ( * ) R = R (R )*R R. ()*. ( ε) Ø R = R (R )*R R = ( ε). ()*. Ø Ø R = R (R )*R R = ( ε). ()*. (). ()* (). ()* = ()*. (). ()* R = R (R )*R R = ( ε). ()*. ( ε) ( ε) = ()* The lnguge reognied y thi DFA n e expreed y the regulr expreion R. L = R = (). ( ε)*. ( ε) = (). ( ε)* =.*. [5 point] Derie uintely wht re the word epted y thi DFA? Thi utomton reognie ll inrie tring tht denote integer tht re non-negtive power of two nd uh n e deried y the ompt regulr expreion: *. 5 of 7

Prolem [5 point]: Preditive Top-Down Pring Conider the CFG grmmr G = (N={S, A, B}, T={ }, P, S) where the et of prodution P i given elow: S A A B B A ε B ε Quetion: ) [5 point] Cn thi grmmr e ued preented for pring uing preditive (ktrkfree) lgorithm? Why or why not? ) [ point] Devie n lterntive (ut equivlent) grmmr for the me lnguge tht h the LL() property. ) [5 point] Compute the FIRST nd FOLLOW et for eh prodution RHS nd the nonterminl ymol repetively. Ue thee to how tht the grmmr h in ft the LL() property. d) [ point] Derive the LL() pring tle deried in the leture nd how tht in ft the grmmr i prele uing the LL() pring lgorithm. e) [ point] Show the equene of pring tep nd the orreponding pre tree for thi lgorithm nd the two input w = nd w =. Solution: ) [5 point] Cn thi grmmr e ued preented for pring uing preditive (ktrk-free) lgorithm? Why or why not? While t firt ight thi grmmr doe pper not to uffer from the iue of left-reurion nd ommon prefixe in ft it i not LL(). Thi i eue the prodution for either the non-terminl A nd B llow for the derivtion of the empty tring nd the hrter nd elow to the FOLLOW et of the repetive non-terminl. ) [ point] Devie n lterntive (ut equivlent) grmmr for the me lnguge tht h the LL() property. A wy to void thi iue, nd hene derive n equivlent LL() grmmr will e to eliminte the ε- trnition hown in the revied grmmr elow. Notie tht thi led to n inree in the numer of prodution in the grmmr ll omintion of trnition for the two ourring A nd B nonterminl in the prodution of S need to e explored. S A A A A S B B B B A B Now we hve to del with the iue if left-ftorition in oth et of prodution for S. S A S B A A A A ε A ε B B B B ε B ε 6 of 7

Notie now tht the FOLLOW of ny non-terminl tht derive the empty tring doe not onflit with it FIRST et nd o the grmmr i LL(). ) [5 point] Compute the FIRST nd FOLLOW et for eh prodution RHS nd the non-terminl ymol repetively. Ue thee to how tht the grmmr h in ft the LL() property. FOLLOW(S) = {} FOLLOW(A ) = {} FOLLOW(A ) = {} FOLLOW(A ) = {} FOLLOW(B ) = {} FOLLOW(B ) = {} FOLLOW(B ) = {} FIRST(A ) = {}, FIRST(B ) = {} FIRST + (A ) FIRST + (B ) = FOLLOW(S) = {} FIRST( A ) = {} FIRST( A ) = {}, FIRST(ε) = {ε} eue A derive the empty tring, FIRST + (A A ) = {,} FIRST() = {}, FIRST(ε) = {ε} eue A derive the empty tring, FIRST + (A ) = {,} Similrly, for B prodution we get: FIRST( B ) = {} FIRST( B ) = {}, FIRST(ε) = {ε} eue B derive the empty tring, FIRST + (B B ) = {,} FIRST() = {}, FIRST(ε) = {ε} eue B derive the empty tring, FIRST + (B ) = {,} d) [ point] Derive the LL() pring tle deried in the leture nd how tht in ft the grmmr i prele uing the LL() pring lgorithm. S A S A A A S B A A B B B A A A B B B B B A ε A ε B ε B ε e) [ point] Show the equene of pring tep nd the orreponding pre tree for thi lgorithm nd the two input w = nd w =. Stk Input Prod. S A A A A A S A A A A A A A S A A Stk Input Prod. S B B B S B B B B B S B 7 of 7