Converting Regular Expressions to NFAs Empty string   is a regular expression denoting  {  } a is a regular expression denoting {a} for any a in 

Slides:



Advertisements
Similar presentations
Lexical Analysis IV : NFA to DFA DFA Minimization
Advertisements

CSE 311 Foundations of Computing I
CSC 361NFA vs. DFA1. CSC 361NFA vs. DFA2 NFAs vs. DFAs NFAs can be constructed from DFAs using transitions: Called NFA- Suppose M 1 accepts L 1, M 2 accepts.
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Lecture # 5. Topics Minimization of DFA Examples What are the Important states of NFA? How to convert a Regular Expression directly into a DFA ?
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Lecture 3UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 3.
Fall 2006Costas Busch - RPI1 Regular Expressions.
CS 310 – Fall 2006 Pacific University CS310 Converting NFA to DFA Sections:1.2 Page 54 September 15, 2006.
Chapter 3: Lexical Analysis
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
Lecture 3: Closure Properties & Regular Expressions Jim Hook Tim Sheard Portland State University.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Regular.
1 Single Final State for NFAs and DFAs. 2 Observation Any Finite Automaton (NFA or DFA) can be converted to an equivalent NFA with a single final state.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
Fall 2006Costas Busch - RPI1 Non-Deterministic Finite Automata.
1 Non-Deterministic Automata Regular Expressions.
Costas Busch - LSU1 Non-Deterministic Finite Automata.
CSE 311: Foundations of Computing Fall 2014 Lecture 23: State Minimization, NFAs.
CS 540 Spring CS 540 Spring 2013 GMU2 The Course covers: Lexical Analysis Syntax Analysis Semantic Analysis Runtime environments Code Generation.
Regular Expressions (RE) Empty set Φ A RE denotes the empty set Empty string λ A RE denotes the set {λ} Symbol a A RE denotes the set {a} Alternation M.
Thompson Construction, Subset Construction Thompson Construction, Subset Construction Continue….. LECTURE 7.
CH3.1 CSE4100 Chapter 3: Lexical Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371 Fairfield.
Theory of Computation, Feodor F. Dragan, Kent State University 1 Regular expressions: definition An algebraic equivalent to finite automata. We can build.
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong NFA to DFA.
Lexical Analysis Constructing a Scanner from Regular Expressions.
Topic #3: Lexical Analysis EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Overview of Previous Lesson(s) Over View  An NFA accepts a string if the symbols of the string specify a path from the start to an accepting state.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
Lexical Analysis III : NFA to DFA DFA Minimization Lecture 5 CS 4318/5331 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper.
Thompson Construction Prepared by: Anupam Prakash 04CS1017.
Prof. Busch - LSU1 NFAs accept the Regular Languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
Lecture 5 Sept 06, 2012 Regular expressions – examples Converting DFA to regular expression. (same works for NFA to r.e. conversion.) Converting R.E. to.
Regular Expressions and Languages A regular expression is a notation to represent languages, i.e. a set of strings, where the set is either finite or contains.
 Regular Grammar and Regular Language [Def 3.1] Regular Grammar(use to in lexical analysis) Type 3 grammar(regular grammar, RG) Type 3 grammar(regular.
CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs.
CMSC 330: Organization of Programming Languages Theory of Regular Expressions NFAs  DFAs.
Lecture # 12. Nondeterministic Finite Automaton (NFA) Definition: An NFA is a TG with a unique start state and a property of having single letter as label.
CMSC 330: Organization of Programming Languages Theory of Regular Expressions Finite Automata.
Regular Expressions Costas Busch - LSU.
Lexical Analysis – Part II EECS 483 – Lecture 3 University of Michigan Wednesday, September 13, 2006.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2007.
CSE 311 Foundations of Computing I Lecture 24 FSM Limits, Pattern Matching Autumn 2011 CSE 3111.
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
1 Chapter 3 Regular Languages.  2 3.1: Regular Expressions (1)   Regular Expression (RE):   E is a regular expression over  if E is one of:
 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
Compilers Lexical Analysis 1. while (y < z) { int x = a + b; y += x; } 2.
Chapter 3: Lexical Analysis and Flex
Chapter 2 Finite Automata
The time complexity for e-closure(T).
Two issues in lexical analysis
Recognizer for a Language
Review: NFA Definition NFA is non-deterministic in what sense?
Chapter 3: Lexical Analysis
Decision Properties of Regular Languages
Lecture 4: Lexical Analysis II: From REs to DFAs
CSE 311: Foundations of Computing
Animated Conversion of Regular Expressions to C Code
Theory of Computation Lecture #
This method is used for converting an
Finite Automata & Language Theory
Chapter 3. Lexical Analysis (2)
Presentation transcript:

Converting Regular Expressions to NFAs Empty string   is a regular expression denoting  {  } a is a regular expression denoting {a} for any a in  i  f start i a f Thompson’s Construction

Converting Regular Expressions to NFAs If P and Q are regular expressions with NFAs  N p, N q : P | Q (union) PQ (concatenation) NpNp NqNq if    start NqNq NpNp i f

Converting Regular Expressions to NFAs If Q is a regular expression with NFA N q : Q* (closure)  NqNq f  i start  

Example (ab* | a*b)* Starting with: 43 a*b b a start ab* 12 a b start 12 a ab* | a*b     a b b start

Example (ab* | a*b)* 12 a ab* | a*b     a b b start 12 a (ab* | a*b)* 78        b b a start

Properties of Construction 1.N(r) has #of states  2*(#symbols + #operators) of r 2.N(r) has exactly one start and one accepting state 3.Each state of N(r) has at most one outgoing edge a  or at most two outgoing  -transition Let r be a regular expression, with NFA N(r), then

Detailed Example r 13 r 12 r5r5 r3r3 r 11 r4r4 r9r9 r 10 r8r8 r7r7 r6r6 r0r0 r1r1 r2r2 b * c a a | ( ) b | * c (ab*c) | (a(b|c*)) Parse Tree for this regular expression: What is the NFA? Let’s construct it !

Detailed Example – Construction(1) r3:r3: a r0:r0: b r2:r2: c b     r1:r1:r 4 : r 1 r 2 b     c r 5 : r 3 r 4 b     ac (ab*c) | (a(b|c*))

Detailed Example – Construction(2) r 11 : a r7:r7: b r6:r6: c c     r 9 : r 7 | r 8   b r 10 : r 9 c     r8:r8: c     r 12 : r 11 r 10   b a   (ab*c) | (a(b|c*))

Detailed Example – Final Step r 13 : r 5 | r 12 b     ac c       b a    

Converting NFAs to DFAs (subset construction) Idea: Each state in the new DFA will correspond to some set of states from the NFA. The DFA will be in state {s 0,s 1,…} after input if the NFA could be in any of these states for the same input. Input: NFA N with state set S N, alphabet , start state s N, final states F N, transition function T N : S N x {  U  }  S N Output: DFA D with state set S D, alphabet , start state s D =  -closure(s N ), final states F D, transition function T D : S D x   S D

Terminology:  -closure  -closure(T) = T + all NFA states reachable from any state in T using only  transitions b  a  b b a  -closure({1,2,5}) = {1,2,5}  -closure({4}) = {1,4}  -closure({3}) = {1,3,4}  -closure({3,5}) = {1,3,4,5}

Illustrating Conversion – An Example First we calculate:  -closure(0) (i.e., state 0)  -closure(0) = {0, 1, 2, 4, 7} (all states reachable from 0 on  -moves) Let A={0, 1, 2, 4, 7} be a state of new DFA, D         a a b b b start Start with NFA: (a | b)*abb 

Conversion Example – continued (1) b :  -closure(move(A,b)) =  -closure(move({0,1,2,4,7},b)) adds {5} ( since move(4,b)=5) From this we have :  -closure({5}) = {1,2,4,5,6,7} (since 5  6  1  4, 6  7, and 1  2 all by  -moves) Let C={1,2,4,5,6,7} be a new state. Define Dtran[A,b] = C. 2 nd, we calculate : a :  -closure(move(A,a)) and b :  -closure(move(A,b)) a :  -closure(move(A,a)) =  -closure(move({0,1,2,4,7},a))} adds {3,8} ( since move(2,a)=3 and move(7,a)=8) From this we have :  -closure({3,8}) = {1,2,3,4,6,7,8} (since 3  6  1  4, 6  7, and 1  2 all by  -moves) Let B={1,2,3,4,6,7,8} be a new state. Define Dtran[A,a] = B.

Conversion Example – continued (2) 3 rd, we calculate for state B on {a,b} a :  -closure(move(B,a)) =  -closure(move({1,2,3,4,6,7,8},a))} = {1,2,3,4,6,7,8} = B Define Dtran[B,a] = B. b :  -closure(move(B,b)) =  -closure(move({1,2,3,4,6,7,8},b))} = {1,2,4,5,6,7,9} = D Define Dtran[B,b] = D. 4 th, we calculate for state C on {a,b} a :  -closure(move(C,a)) =  -closure(move({1,2,4,5,6,7},a))} = {1,2,3,4,6,7,8} = B Define Dtran[C,a] = B. b :  -closure(move(C,b)) =  -closure(move({1,2,4,5,6,7},b))} = {1,2,4,5,6,7} = C Define Dtran[C,b] = C.

Conversion Example – continued (3) 5 th, we calculate for state D on {a,b} a :  -closure(move(D,a)) =  -closure(move({1,2,4,5,6,7,9},a))} = {1,2,3,4,6,7,8} = B Define Dtran[D,a] = B. b :  -closure(move(D,b)) =  -closure(move({1,2,4,5,6,7,9},b))} = {1,2,4,5,6,7,10} = E Define Dtran[D,b] = E. Finally, we calculate for state E on {a,b} a :  -closure(move(E,a)) =  -closure(move({1,2,4,5,6,7,10},a))} = {1,2,3,4,6,7,8} = B Define Dtran[E,a] = B. b :  -closure(move(E,b)) =  -closure(move({1,2,4,5,6,7,10},b))} = {1,2,4,5,6,7} = C Define Dtran[E,b] = C.

Conversion Example – continued (4) Dstates Input Symbol ab A B C B B D C B C E B C D B E A C BDE startbb b b b a a a a This gives the transition table Dtran for the DFA of:

Algorithm For Subset Construction push all states in T onto stack; initialize  -closure(T) to T; while stack is not empty do begin pop t, the top element, off the stack; for each state u with edge from t to u labeled  do if u is not in  -closure(T) do begin add u to  -closure(T) ; push u onto stack end computing the  -closure

Algorithm For Subset Construction – (2) initially,  -closure(s 0 ) is only (unmarked) state in Dstates; while there is unmarked state T in Dstates do begin mark T; for each input symbol a do begin U :=  -closure(move(T,a)); if U is not in Dstates then add U as an unmarked state to Dstates; Dtran[T,a] := U end

Example 2: Subset Construction NFA  b a b a,b start NFA N with State set S N = {1,2,3,4,5}, Alphabet  = {a,b} Start state s N =1, Final states F N ={5}, Transition function T N : S N x {    }  S N ab  ,

Example 2: Subset Construction NFA 1,2 T  -closure(move(T, a))  -closure(move(T, b)) {1,2}  b a b a,b start

Example 2: Subset Construction NFA 1,24,5 3,5 b a T  -closure(move(T, a))  -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5} {4,5}  b a b a,b start

Example 2: Subset Construction  b a b a,b NFA 1,24,5 3,54 b a b T  -closure(move(T, a))  -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5}-{4} {4,5} {4} start

Example 2: Subset Construction  b a b a,b NFA 1,24,55 3,54 a,bb a b T  -closure(move(T, a))  -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5}-{4} {4,5}{5} {4} {5} start

Example 2: Subset Construction  b a b a,b NFA 1,24,55 3,54 a,b b a b T  -closure(move(T, a))  -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5}-{4} {4,5}{5} {4}{5} -- start

Example 2: Subset Construction  b a b a,b NFA T  -closure(move(T, a))  -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5}-{4} {4,5}{5} {4}{5} {5}{5}-- All final states since the NFA final state is included start 1,24,55 3,54 a,b b a b start

Example 3: Subset Construction b  a b b a NFA  start

Example 3: Subset Construction b  a b b a 1 1,3,4 2 1,4,5 a b b NFADFA  1,3, 4,5 a a a b b b start

Example 4: Subset Construction  b  1,2,4 NFADFA a b a 5 3,43, b b a b a b b a start

Converting DFAs to REs 1.Combine serial links by concatenation 2.Combine parallel links by alternation 3.Remove self-loops by Kleene closure 4.Select a node (other than initial or final) for removal. Replace it with a set of equivalent links whose path expressions correspond to the in and out links 5.Repeat steps 1-4 until the graph consists of a single link between the entry and exit nodes.

Example d a b c d 0 da d b b c da|b|cd 0 da d b b|c parallel edges become alternation start a b c

Example 345 d (a|b|c) d d 0 a b (b|c) d da|b|cd 0 da d b b|c serial edges become concatenation start

Example 345 d (a|b|c) d d 0 a b (b|c) d 345 d (a|b|c) d d 0 a b(b|c)da Find paths that can be “shortened” start

Example 345 d (a|b|c) d d 0 a b(b|c)da 345 d (a|b|c) d (b(b|c)da)*d 0 a 5 d (a|b|c) d(b(b|c)da)*d 0 a eliminate self-loops serial edges become concatenation start

Relationship among RE, NFA, DFA The set of strings recognized by an NFA can be described by a Regular Expression. The set of strings described by a Regular Expression can be recognized by an NFA. The set of strings recognized by an DFA can be described by a Regular Expression. The set of strings described by a Regular Expression can be recognized by an DFA. DFAs, NFAs, and Regular Expressions all have the same “power”. They describe “Regular Sets” (“Regular Languages”) The DFA may have a lot more states than the NFA.