Formal languages, automata, and theory of computation

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

Harvard University Computer Science 121 Midterm October 23, 2012

First Midterm Examination

First Midterm Examination

Convert the NFA into DFA

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

Nondeterminism and Nodeterministic Automata

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

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Theory of Computation Regular Languages

Designing finite automata II

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Minimal DFA. minimal DFA for L starting from any other

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

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

Chapter 2 Finite Automata

1 Nondeterministic Finite Automata

Context-Free Grammars and Languages

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Revision Sheet. (a) Give a regular expression for each of the following languages:

Homework 3 Solutions

Normal Forms for Context-free Grammars

FABER Formal Languages, Automata and Models of Computation

1.3 Regular Expressions

Lecture 08: Feb. 08, 2019

1.4 Nonregular Languages

State Minimization for DFAs

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

The University of Nottingham

Closure Properties of Regular Languages

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.

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

More on automata. Michael George. March 24 April 7, 2014

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

1 From NFA to regular expression

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

CMSC 330: Organization of Programming Languages

Regular Language. Nonregular Languages The Pumping Lemma. The pumping lemma. Regular Language. The pumping lemma. Infinitely long words 3/17/15

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

Formal Languages and Automata

CS 275 Automata and Formal Language Theory

3 Regular expressions

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2014

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

Lecture 09: Myhill-Nerode Theorem

Finite Automata-cont d

Worked out examples Finite Automata

Java II Finite Automata I

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

CSC 311 Theory of Computation

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Homework Solution - Set 5 Due: Friday 10/03/08

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

Some Theory of Computation Exercises Week 1

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

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2016

Exercises Chapter 1. Exercise 1.1. Let Σ be an alphabet. Prove wv = w + v for all strings w and v.

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

CS 275 Automata and Formal Language Theory

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

Homework 4. 0 ε 0. (00) ε 0 ε 0 (00) (11) CS 341: Foundations of Computer Science II Prof. Marvin Nakayama

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

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

Lexical Analysis Finite Automate

a,b a 1 a 2 a 3 a,b 1 a,b a,b 2 3 a,b a,b a 2 a,b CS Determinisitic Finite Automata 1

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

CS375: Logic and Theory of Computing

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

CSC 473 Automata, Grammars & Languages 11/9/10

Anatomy of a Deterministic Finite Automaton. Deterministic Finite Automata. A machine so simple that you can understand it in less than one minute

Fundamentals of Computer Science

Name Ima Sample ASU ID

Lexical Analysis Part III

Let's start with an example:

CISC 4090 Theory of Computation

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

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

a b b a pop push read unread

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

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

Parse trees, ambiguity, and Chomsky normal form

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.

Formal Language and Automata Theory (CS21004)

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

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

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

Thoery of Automata CS402

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

Tutorial Automata and formal Languages

Model Reduction of Finite State Machines by Contraction

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

NFAs continued, Closure Properties of Regular Languages

CS S-12 Turing Machine Modifications 1. When we added a stack to NFA to get a PDA, we increased computational power

SWEN 224 Formal Foundations of Programming WITH ANSWERS

Coalgebra, Lecture 15: Equations for Deterministic Automata

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

Transcription:

Mälrdlen University TEN1 DVA337 2015 School of Innovtion, Design nd Engineering Forml lnguges, utomt, nd theory of computtion Thursdy, Novemer 5, 14:10-18:30 Techer: Dniel Hedin, phone 021-107052 The exm hs totl of 40 points nd consists of 3 pges. No ids re llowed. Answers must e given in English nd should e clerly justified. 1. Regulr lnguges (14 p) ) Floting point numers re uilt y n optionl sign followed y digits seprted y deciml point nd end with n optionl exponent prt. Exmples of floting point numers re 1.35e+10 +2.54-5.2 245.3E-2 Write regulr expression tht recognizes floting point numers. (2 p) Solution: (- +)?[0-9]+.[0-9]+( (e E)(+ -)[0-9]+ )? ) Convert the following NFA to n equivlent miniml DFA. (6 p) q1 q2,, q3 Solution: We first convert the NFA to DFA using the suset construction lgorithm. For ook keeping we stte the trnsition function δ q 1 {q 2 } {q 1 } q 2 {q 2,q 3 } {q 1,q 3 } q 3 {q 1 } {q 1,q 3 } Strting in stte {q 1 } (the set of λ-rechle sttes from the strting stte of the NFA), we cn systemticlly construct the following DFA, where Q 1 = {q 1 }, Q 2 = {q 2 }, Q 3 = {q 2,q 3 }, Q 4 = {q 1,q 2,q 3 }, Q 5 = {q 1,q 3 }, Q 6 = {q 1,q 2 } with Q 2, Q 3, Q 4 nd Q 5 re finl sttes, since they contin the t lest one finl stte from the NFA. 1

Q1 Q2 Q3 Q4 Q5 Q6 To minimize this DFA we pply stte prtitioning strting with two sets of sttes A = {Q 1,Q 5 } nd B = {Q 2,Q 3,Q 4,Q 6 } corresponding to the set of non-finl nd the set of finl sttes respectively nd see tht the trnsitions of the originl DFA do not prtition these sets further. A Q 1 Q 5 B B A A B Q 2 Q 3 Q 4 Q 6 B B B B A A A A Bsed on this we get the following mininl DFA. A B c) Stte the pumping lemm for regulr lnguges nd use it to prove tht L = { n n n 0} is not regulr. (4 p) Solution: The pumping lemm for regulr lnguges sttes tht for ll regulr lnguges L, there exists positive numer m such tht for ll strings w L with w m it holds tht the exists decomposition of w = xyz suject to xy m nd y 1 for which it holds tht xy i z L for ny i 0. To prove tht L = { n n n 0} is not regulr we proceed with proof y contrdiction. Assume tht L is regulr, i.e., tht the pumping lemm holds for L nd show how to construct string w in terms of m chosen so tht we cn show tht for ny decomposition w = xyz suject to xy m nd y 1 there is t lest one i 0 for which xy i z L. This shows tht the pumping lemm does not hold for L nd we hve reched contrdiction. In this prticulr prolem we cn chose w = m m. Due to xy m we hve tht x = j, y = k, for k 1, nd z = l m for j + k + l = m. According to the pumping lemm if L is regulr then for every i 0 it should hold tht xy i z L. But for ny i 1 we cn show tht xy i z L. Consider for instnce i = 0 then xy i z = xy = j+l m, ut 2

we hve tht j +l m, since k 1, nd we cn drw the conclusion tht j+l m L. Hence, L is not regulr. d) Consider the prolem of nested C-like comments. A comment is well-formed if the numer of strting symols /* mtches the numer of ending symols */ nd in ny given prefix the numer of strting symols is greter thn or equl to the numer of ending symols. To illustrte consider the following exmples of well-formed nested comments /* Well-formed comment */ /* Also /* well-formed nested comment */!! */ nd the following exmples of mlformed nested comments /* Not */ well-formed comment */ /* Also not /* well-formed comment */ Give convincing rgument tht it is not possile to crete DFA tht ccepts precisely the well-formed nested comments. (2 p) Solution: Recll tht regulr lnguges re closed under intersection, i.e., tht if L 1 nd L 2 re regulr lnguges then so is L 1 L 2. Thus, if it cn e shown tht L 1 L 2 is not regulr nd tht, e.g, L 2 is regulr then we cn drw the conclusion tht L 1 is not regulr. Let L 1 denote the lnguge of well formed nested C-like comments nd let L 2 = L((/*) (*/) ), i.e., the regulr lnguge ny numer of /* followed y ny numer of /*. Now consider the intersection of L 1 nd L 2. It is cler tht L 3 = L 1 L 2 = {(/*) n (*/) n n 1}, ut we hve lredy proved (in 1.c) tht L = { n n n 0} is not regulr for ny nd. This implies tht L 3 is not regulr, which together with the fct tht L 2 is regulr implies tht L 1 is not regulr. 2. Context-free lnguges (14 p) ) Explin wht it mens for grmmr to e miguous. (2 p) Solution: A grmmr is miguous if there exists string with more thn one left-most derivtion (or, equivlently, more thn one rightmost derivtion). Alterntively, grmmr is miguous if there exist string with more thn one prse tree. 3

) Consider the following smll grmmr for n expression lnguge E ::= E + E E E NUM where NUM represents numers. Show tht this grmmr is miguous. (2 p) Solution: Numer the production rules 1, 2 nd 3 in order from the left. Consider the string 1 + 2 3 with the following two left-most derivtions 1) E 1 E + E 3 1 + E 2 1 + E E 3 1 + 2 E 3 1 + 2 3 2) E 2 E E 1 E + E E 3 1 + E E 3 1 + 2 E 3 1 + 2 3 corresponding to 1 + (2 3) nd (1 + 2) 3 respectively. c) Rewrite the ove grmmr to e unmiguous nd show y exmple tht your grmmr follows the stndrd precedence rules for ddition nd multipliction. (2 p) Solution: We need to encode tht multipliction inds hrder thn ddition. This cn e done in the following wy E ::= E + T T T ::= T NUM NUM Now, numering the production rules E 1, E 2, T 1 nd T 2 from left to right the string 1 + 2 3 hs unique left-most derivtion E E 1 E + T E 2 T + T T 2 1 + T T 1 1 + T 3 T 2 1 + 2 3 d) Consider the following grmmr of well-formed nested comments S ::= λ / A / A ::= λ A SA where,, nd / re terminls. Crete deterministic pushdown utomton tht recognizes the ove grmmr, nd show the sequence of instntneous description corresponding to running the utomton on 1) /*/**/*/ nd 2) /**/ (6 p) Solution: Note tht the methods for utomticlly trnslting context-free grmmrs to push-down utomt generte non-deterministic push-down utomt. By studying the lnguge generted y S, however, it is reltively esy to crete DPDA. The key ide is to use the stck to count the numer of strting symols /* nd to mke sure they mtch up with the numer of ending symols */. Between these symols ny numer of s cn occur. A key thing to note is tht once the outermost strting symol hs een mtched nothing else cn e mtched. Below it is ssumed tht the stck is initilly empty nd use $ s the distinguished symol to signl the end of the stck. 4

q1 /, λ $ q2 *, λ X, λ λ q3 /, λ λ *, λ X *, λ λ q4 q5 /, X X /, X λ *, X X q6 λ, $ λ q7, X X 1) For /*/**/*/ we get the following derivtion (q 1,/*/**/*/,$) (q 2,*/**/*/,$) (q 3,/**/*/,X$) (q 3,/**/*/,X$) (q 4,**/*/,X$) (q 3,*/*/,XX$) (q 5,/*/,XX$) (q 6,*/,X$) (q 5,/,X$) (q 6,λ,$) (q 7,λ,$) ccept 2) For /**/ we get the following derivtion. (q 1,/**/,$) (q 2,**/,$) (q 3,*/,X$) (q 5,/,X$) (q 6,,$) (q 7,,$) reject - input string not empty e) Do deterministic pushdown utomt hve the sme computtionl power s nondeterministic pushdown utomt? Give convincing rgument for or ginst. (2 p) Solution: Nondeterministic pushdown utomt hve re strictly more powerful thn deterministic pushdown utomt. As n exmple, consider the lnguge of even length plindromes {ww R w Σ } for Σ = {,}. In order for deterministic pushdown utomton to check if given string w is plindrome it would hve to know when it hs reched the middle of the string in order to strt mtching. However, unlike the lnguge { n n n 0}, where the trnsition from the lst to the first signifies the middle there is no wy for deterministic pushdown utomton to know when it hs reched the middle of the plindrome. A nondeterministic pushdown utomton, however, is le to do this, since the non-determinism llows it to serch ll possile splits. 5

3. Restriction-free lnguges nd theory of computtion (12 p) ) Consider the Turing mchine descried y M = ({q 1,q 2,q 2,q 3,q 4,q 5,q 6,q 7 },{,},{,, },δ,q 1,,{q 7 }) with δ defined s follows δ(q 1,) = (q 2,,R) δ(q 1,) = (q 3,,R) δ(q 1, ) = (q 7,,L) δ(q 4,) = (q 6,,L) δ(q 2,) = (q 2,,R) δ(q 2,) = (q 2,,R) δ(q 2, ) = (q 4,,L) δ(q 5,) = (q 6,,L) δ(q 3,) = (q 3,,R) δ(q 3,) = (q 3,,R) δ(q 3, ) = (q 5,,L) δ(q 6,) = (q 6,,L) δ(q 6,) = (q 6,,L) δ(q 6, ) = (q 1,,R) 1. Drw the trnsition grph of M. (2 p) 2. Wht lnguge does M ccept? (2 p) 3. Select string w {,} 4 tht is ccepted y M nd show the execution of M on w s sequence of instntneous descriptions. (2 p) Solution: 1. The stte mchine corresponding to M is the following.,,r;,,r q2,,l,,r,,r;,,r q4,,l,,l;,,l q1,,r q3,,l q5,,l q6,,l,,r q7 2. M recognizes the lnguge of even length plindromes, {ww R w Σ }. To see this consider how M opertes. First, M ssume tht M is strted with some input w, i.e., corresponding to the instntneous description q 1 w. First, the empty string is ccepted, since if the first chrcter is the lnk symol, M ccepts. Otherwise, the first symol is either n, or. In oth cses, M overwrites it with the nd serches for the lst non-lnk symol if it exists. If it does not exist or if it does not mtch the overwritten symol the mchine hlts. Otherwise, M overwrites the mtching symol nd serches for the first non-lnk symol. If it exists, the process repet until no more symols remin. At this point M ccepts. Phrsed more distinctly, M shves off mtching symols in oth ends until only the empty string remins, which is ccepted. 6

3. For w = we hve the following derivtion q 1 q 2 q 2 q 2 q 2 q 4 q 6 q 6 q 6 q 1 q 3 q 3 q 5 q 6 q 1 q 7, hlt nd ccept ) Explin the concept of reduction proof in the context of theory of computtion, i.e., proving undecidility y reducing one prolem to nother. (2 p) Solution: A proof y reduction is proof where we reduce known undecidle prolem A to nother prolem B. The reduction is performed showing how to use decision procedure for B s decision procedure for A. This mens tht the existence of decision procedures for B implies the existence of decision procedure for A. However, since we know tht there cnnot exist ny decision procedures for prolem A we drw the conclusion tht there cnnot exist ny decision procedures for B. c) Recll the stte-entry prolem, i.e., given Turing mchine M = (Q,Σ,Γ,δ,q 0,,F) nd ny q Q, nd w Σ +, decide whether or not the stte q is entered y M when it is pplied to w. Prove tht the stte-entry prolem is undecidle. (4 p) Solution: Proof y reducing the hlting prolem into the stte-entry prolem. We do this y creting Turing mchine ˆM tht hlts in stte ˆq if nd only if M hlts. Recll tht Turing mchine hlts in stte q i for input when δ(q i,) is undefined (we demnd tht finl sttes re hlting for ll input). Thus, we cn crete ˆδ y mpping ny undefined trnsitions in to ˆq. ˆδ(q i,) = { δ(q i,) if δ(q i,) defined ( ˆq,, R) otherwise Now, the stte-entry prolem for ˆM, ˆq, nd w is equivlent to the hlting prolem for M nd w. 7