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

Similar documents
AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

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

Java II Finite Automata I

Non-deterministic Finite Automata

Non-deterministic Finite Automata

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

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

CISC 4090 Theory of Computation

Theory of Computation Regular Languages

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

CMSC 330: Organization of Programming Languages

CHAPTER 1 Regular Languages. Contents

Worked out examples Finite Automata

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

1 Nondeterministic Finite Automata

Let's start with an example:

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

NFAs continued, Closure Properties of Regular Languages

Finite-State Automata: Recap

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

Deterministic Finite Automata

Some Theory of Computation Exercises Week 1

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

Designing finite automata II

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.

Lexical Analysis Finite Automate

Chapter 2 Finite Automata

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

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

CSCI 340: Computational Models. Transition Graphs. Department of Computer Science

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

Lexical Analysis Part III

Automata and Languages

CHAPTER 1 Regular Languages. Contents. definitions, examples, designing, regular operations. Non-deterministic Finite Automata (NFA)

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

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

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

Formal Language and Automata Theory (CS21004)

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

Nondeterminism and Nodeterministic Automata

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

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

Lecture 08: Feb. 08, 2019

Languages & Automata

Formal languages, automata, and theory of computation

Finite Automata-cont d

Minimal DFA. minimal DFA for L starting from any other

Fundamentals of Computer Science

Formal Languages and Automata

NFAs continued, Closure Properties of Regular Languages

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

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

Convert the NFA into DFA

CS375: Logic and Theory of Computing

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

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.

ɛ-closure, Kleene s Theorem,

In-depth introduction to main models, concepts of theory of computation:

CMSC 330: Organization of Programming Languages. DFAs, and NFAs, and Regexps (Oh my!)

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

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA

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

Lecture 09: Myhill-Nerode Theorem

Non-Deterministic Finite Automata

Today s Topics Automata and Languages

Automata and 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.

Homework 3 Solutions

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

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

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

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

Harvard University Computer Science 121 Midterm October 23, 2012

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

GNFA GNFA GNFA GNFA GNFA

3 Regular expressions

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

input tape head moves current state

Lecture 6 Regular Grammars

Thoery of Automata CS402

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

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

Deterministic Finite-State Automata

Non Deterministic Automata. Formal Languages and Automata - Yonsei CS 1

Section: Other Models of Turing Machines. Definition: Two automata are equivalent if they accept the same language.

1.3 Regular Expressions

First Midterm Examination

First Midterm Examination

Coalgebra, Lecture 15: Equations for Deterministic Automata

FABER Formal Languages, Automata and Models of Computation

Name Ima Sample ASU ID

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

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers. Mehryar Mohri Courant Institute and Google Research

CSC 311 Theory of Computation

State Minimization for DFAs

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

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.

Transcription:

Regulr Expressions (RE) Regulr Expressions (RE) Empty set F A RE denotes the empty set Opertion Nottion Lnguge UNIX Empty string A RE denotes the set {} Alterntion R +r L(r ) L(r ) r r Symol Alterntion Conctention Kleene-* M + N M N M* A RE denotes the set {} If M is RE for the set M nd N is RE for the set N, then M+N is RE for the set M U N If M is RE for the set M nd N is RE for the set N, then M.N is RE for the setm. N If M is RE for the set M, then M* is RE for the setm* Conctention Kleene-* Kleene-+ Exponentition r r r * r + r n L(r ) L(r ) L(r )* L(r ) + L(r ) n (r )(r ) (r ) (r )+ (r ){n} Regulr Expressions (RE) Regulr Expressions (RE) Exmple For the lphet S={, } + is RE denote the set {} U {} * is RE denote the set {}*={,,, }.* is RE denote the set {}.{,,, }={,,, } Notes For RE r, r i = r.r.r i-times Opertions precedence: * >. > + So we cn omit mny prentheses, for exmple: the RE (((*))+) cn e written s *+ We me revite rr* to r + The corresponding set (lnguge) denoted y RE r will e expressed s L(r) Definition Nondeterministic Finite Automt A nondeterministic finite utomton M is defined y 5-tuple M=(Q,S,d,q,F), with Q: finite set of sttes S: finite input lphet d: trnsition function d:q S P(Q) q Q: strt stte F Q: set of finl sttes Definition Nondeterministic Finite Automt A string w is ccepted y n NFA M if nd only if there exists pth strting t q which is leled y w nd ends in finl stte. The lnguge ccepted y n NFA M is the set of ll strings which re ccepted y M nd is denoted y L (M ). L(M)={w: d(q,w) F F}

Definition Nondeterministic Finite Automt A nondeterministic finite utomton hs trnsition rules like: q : : q Nondeterministic trnsition Nondeterministic Finite Automt Nondeterminism ~ Prllelism For ny string w, the nondeterministic utomton cn e in suset Q of severl possile sttes. If the finl set contins finl stte, then the utomton ccepts the string. The utomton processes the input in prllel fshion; its computtionl pth is no longer line, ut more like tree. Nondeterministic Finite Automt We cn write the NFA in two wys. Stte digrph. Tle q δ q q q q {q} {q,q} {q} q Exmple Nondeterministic Finite Automt Write n NFA for the lnguge, over S={,}, ending in q Σ = {, } F = { q} Q = { q, q, q} q ( ) q Check the input Nondeterministic Finite Automt Nondeterministic Finite Automt Quiz Check the input ( ) Exmple Write n NFA for the lnguge, over S={,}, L=( U )* ( U )* q q q q q q Input: q is finl stte hence the input is ccepted

Nondeterministic Finite Automt Nondeterministic Finite Automt Exmple Exmple 4 Write n NFA for the lnguge, over S={,}, L=( U )* ( U ) ( U )* q q q strt Wht lnguge is ccepted y this NFA q q Answer: (+)* Nondeterministic Finite Automt Exmple 5 For exmple, consider the following NFA which reds the input.,, Accepted! Theorem: For every lnguge L tht is ccepted y nondeterministic finite utomton, there is (deterministic) finite utomton tht ccepts L s well. FA nd NFA re equivlent computtionl models. Proof ide: When keeping trck of nondeterministic computtion of n NFA N we use mny fingers to point t the suset Q of sttes of N tht cn e reched on given input string. We cn simulte this computtion with deterministic utomton M with stte spce P(Q). Proof Let L e the lnguge recognized y the NFA N = (Q,S,d,q,F). Define the DFA M = (Q,S,d,q,F ) y. Q = P(Q). d (R,) = { q Q q d(r,) for n r R }. q = {q } 4. F = {R Q R contins finl stte of N} It is esy to see tht the previously descried deterministic finite utomton M ccepts the sme lnguge s N. Exmple Convert the NFA: q, q into DFA Given NFA Constructed DFA Q={q, q } Q =P(Q)={F, {q }, {q }, {q, q }} q q = {q } F={q } F ={{q }, {q, q }} For d see the next slide

Exmple Convert the NFA: q, q into DFA Given NFA Constructed DFA d d(q,)={q,q } d ({q },)={q,q } F F F d(q,)={q } d ({q },)={q } {q } {q,q } {q } d(q,)=f d ({q },)=F {q } F {q,q } d(q,)={q,q } d ({q },)={q,q } {q,q } {q,q } {q,q } {q } {q } d ({q,q },)=d(q,) U d(q,) ={q,q }, d ({q,q },)=d(q,) U d(q,) ={q,q } {q,q } F, Exmple Strt with the NFA: Q: Wht s the ccepted lnguge Q: How mny sttes does the suset construction crete in this cse Exmple A: L = {x {,}* rd it of x from right is } Exmple Strt with {}: A: 6 = 4 sttes. Tht s lot of sttes. Would e nice if only hd to construct useful sttes, I.e. those tht cn e reched from strt stte. Exmple Exmple Brnch out. Notice tht δ(,) = {,}. Brnch out. Notice tht δ ({,},) = {,,}. 4

Exmple Exmple NFA = DFA Brnch out. Note tht δ ({,,},) = {,,,4} Exmple Exmple Exmple Exmple 5

Exmple Exercise Summrizing: Convert the following NFA into n equivlent DFA q q q Therefore, we sved 5% effort y not constructing ll possile sttes unthinkingly. Nondeterministic Finite Automt with empty moves (-NFA) Nondeterministic Finite Automt with empty moves (-NFA) Definition A nondeterministic finite utomton with empty moves (-NFA) M is defined y 5-tuple M=(Q,S,d,q,F), with Q: finite set of sttes S: finite input lphet d: trnsition function d:q (S U {}) P(Q) q Q: strt stte F Q: set of finl sttes Definition A string w is ccepted y -NFA M if nd only if there exists pth strting t q which is leled y w nd ends in finl stte. The lnguge ccepted y -NFA M is the set of ll strings which re ccepted y M nd is denoted y L (M ). L(M)={w: d(q,w) F F} Notes Nondeterministic Finite Automt with empty moves (-NFA) δ : Q ( Σ { }) P( Q) A -trnsition cuses the mchine to chnge its stte non-deterministiclly, without consuming ny input. q Notes Nondeterministic Finite Automt with empty moves (-NFA) A -NFA hs trnsition rules/possiilities like: q Empty string trnsition And q : : q Nondeterministic trnsition 6

Nondeterministic Finite Automt with empty moves (-NFA) Nondeterministic Finite Automt with empty moves (-NFA) Nondeterminism ~ Prllelism For ny string w, the nondeterministic utomton cn e in suset Q of severl possile sttes. If the finl set contins finl stte, then the utomton ccepts the string. We cn write the NFA in two wys. Stte digrph q q q The utomton processes the input in prllel fshion; its computtionl pth is no longer line, ut more like tree.. Tle δ : Q ( Σ { }) P( Q ) δ q q q {q} {q,q} {q} Nondeterministic Finite Automt with empty moves (-NFA) Nondeterministic Finite Automt with empty moves (-NFA) Exmple,, Exmple,,,, q q q 4 q q q 4 This utomton ccepts, ecuse there is possile pth tht leds to finl stte, nmely: q q q q 4 q 4 (note tht q q q q q is not ccepting) The string gets rejected: on the utomton cn only rech: {q,,q }. Nondeterministic Finite Automt with empty moves (-NFA) Exmple strt A -trnsition is tken without consuming ny chrcter from the input. Wht does the NFA ove ccepts *+* Quiz Nondeterministic Finite Automt with empty moves (-NFA) M : M : M : M 4 : q q q, q q q q Wht re d(q,), d(q,), d(q, ) in ech of M, M, M nd in M 4. 7

Answer Nondeterministic Finite Automt with empty moves (-NFA) M: M: M: M4: q q q, q q M: d(q,) = d(q,) = d(q, ) = M: Sme M: d(q,) = d(q,) =, d(q, ) = {q, } M4: d(q,)={q,q }, d(q,)={,q }, d(q, )= q q Quiz Nondeterministic Finite Automt with empty moves (-NFA) q, q q Which of the following strings is ccepted... 4. Nondeterministic Finite Automt with empty moves (-NFA) Answer. is rejected. No pth leled y empty string from strt stte to n ccept stte.. is ccepted. EG the pth q q. is ccepted. EG the pth q q 4. is ccepted. There is only one ccepted pth: q q, q q q q q q q q q Nondeterministic Finite Automt with empty moves (-NFA) Definition Given -NFA stte s, the -closure(s) is the set of sttes tht re rechle through -trnsiton from s. -closure(s)={q: there is pth from s to q leled } Given set of -NFA sttes T, the -closure(t) is the set of sttes tht re rechle through -trnsiton from ny stte s T. -closure(t)=u s T -closure(s) Nondeterministic Finite Automt with empty moves (-NFA) Nondeterministic Finite Automt with empty moves (-NFA) Exmple : q q Exmple : IF -z f -z i 4 5 6 7-9 -9-9 ny 4 5 9 chrcter error ID 8 NUM -closure(q ) = {q,q, } -closure(q ) = {q, } Wht sttes cn e reched from stte without consuming chrcter -closure( ) = { } 8

Nondeterministic Finite Automt with empty moves (-NFA) Exmple IF -z f -z 4 5 6 i 7-9 -9-9 ny 4 5 9 chrcter error Wht sttes cn e reched from stte without consuming chrcter {,4,9,4} form the -closure of stte ID 8 NUM Nondeterministic Finite Automt with empty moves (-NFA) Exmple IF -z f -z 4 5 6 i 7-9 -9-9 ny 4 5 9 chrcter error Wht re ll the stte closures in this NFA closure() = {,4,9,4} closure(5) = {5,6,8} closure(8) = {6,8} closure(7) = {7,8} closure() = {,,} closure() = {,} closure() = {,} ID 8 NUM Nondeterministic Finite Automt with empty moves (-NFA) Definition: Extension of d δ : Q ( U{ }) P( Q) δ is defined s follows:. δ (q,)=-closure(q). δ (q,w)=-closure(t) where T={p: p d(r,) nd r δ (q,w)}, S, w S* * δ : Q P( Q) Nondeterministic Finite Automt with empty moves (-NFA) Exmple: Extension of d q q δ (q, ) = {q, } -NFA NFA -NFA NFA Theorem: For every lnguge L tht is ccepted y -NFA, there is n NFA tht ccepts L s well. -NFA nd NFA re equivlent computtionl models. Proof: Let M=(Q,S,d,q,F) e -NFA, n equivlent NFA, M =(Q,S,d,q,F ) cn e constructed s follows:. F ' = { F U{ q } F If -closure(q ) Otherwise F F. δ ' ( q, ) = δˆ( q, ) 9

-NFA NFA -NFA NFA Exmple: For the -NFA: q q Construct the equivlent NFA Exmple: For the -NFA: q q Construct the equivlent NFA Answer: Given -NFA Constructed NFA Answer: Given -NFA Constructed NFA Q={q,q, } nd S={,} -closure(q )={q,q, } F F Q={q,q, } nd S={,} F = {q,q } Q={q,q, } nd S={,} -closure(q )={q,q, } F F Q={q,q, } nd S={,} F = {q,q } δ ( q,) = δˆ(,) = {q,q, } ' q δ ( q,) = δˆ(,) = {q, } ' q δ ( q,) = δˆ(,) = { } ' q, q q,, δ ( q,) = δˆ(,) = F ' q δ ( q,) = δˆ(,) = {q, } ' q δ ( q,) = δˆ(,) = { } ' q,, q q,, -NFA NFA -NFA NFA Exmple: For the -NFA: Answer: Given -NFA Q={q,q, } nd S={,} -closure(q )={q,q, } F F δ ( q,) = δˆ(,) = F ' q δ ( q,) = δˆ(,) = F ' q δ ( q,) = δˆ(,) = { } ' q q q Construct the equivlent NFA Constructed NFA Q={q,q, } nd S={,} F = {q,q },, q q,, Exmple: For the -NFA: Construct the equivlent NFA Answer: q q,, q q,, RE -NFA Proof: RE -NFA Theorem: Let r e RE, there exist -NFA tht ccepts L(r). The proof works y induction, using the recursive definition of regulr expressions. RE -NFA F q q q q

Proof: M M r+r L(M) U L(M) RE -NFA M r* L(M)* Exmple The -NFA for symol i is: The -NFA for symol f is: RE -NFA For the regulr expression r=if we uild the -NFA s follows: strt strt i f M M r.r L(M) L(M) The -NFA for the regulr expression if is: strt i f IF Exmple RE -NFA Exmple RE -NFA For the regulr expression r=+* uild the equivlent -NFA The -NFA for symol is: The -NFA for symol is: The -NFA for symol * is: For the regulr expression r=+* uild the equivlent -NFA The -NFA for the regulr expression +* is: * The -NFA for the regulr expression +* is: +* Exmple RE -NFA Exmple RE -NFA Q: Find n NFA for the regulr expression ( )*( ( )*)( )* ( )*( ( )*)( )* Note tht: in this exmple e =

Exercise RE -NFA Construct -NFA for the regulr expression: *+(+)*+*