Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to the Theory of Computation Regular Expressions.

Similar presentations


Presentation on theme: "1 Introduction to the Theory of Computation Regular Expressions."— Presentation transcript:

1 1 Introduction to the Theory of Computation Regular Expressions

2 2 1.1 Finite Automata  Deterministic FA  Non-Deterministic FA  NFA = DFA  -NFA  -NFA = NFA

3 3 1.2 Regular Expressions  Regular Expression  RE  -NFA  DFA  RE

4 4 Regular Expressions  we can use some regular operations to build up expressions describing languages, which are called regular expressions.  Example: 01*+10*

5 5 Inductive Definition of RE  R is a regular expression if R is 1.Ø, the empty set 2., the empty string 3.a, for some a  Σ 4.R 1 +R 2, where R 1 and R 2 are reg. exp. 5.R 1 R 2, where R 1 and R 2 are reg. exp. 6.R 1 *, where R 1 is a regular expression What are the differences between empty set Ø and empty string ? Basis Induction

6 6 Language of a RE  A regular expression R describes the language L(R) L( Ø ) = Ø L() = {} L(a) = {a} L(R 1 +R 2 ) = L(R 1 )L(R 2 ) L(R 1 R 2 ) = L(R 1 )L(R 2 ) L(R 1 *) = (L(R 1 ))* union concatenation closure

7 7 Concatenation of Languages  If L 1 and L 2 are languages, we can define the concatenation L 1 L 2 = {w | w=xy, xL 1, yL 2 }  Examples: {ab, ba}{cd, dc} =? {abcd, abdc, bacd, badc} Ø {ab} =? Ø

8 8 Exponentiation of a Language  L i is the language L concatenated with itself i times.  Recursive definition: Base: L 0 = {} Induction: L i+1 = LL i  Example: {ab, ba} 2 =? {abab, abba, baab, baba} Ø 0 =? {} Ø 2 =? {}

9 9 Kleene Closure  L* =   i=0 L i = L 0  L 1  L 2  …  Examples: {ab, ba}* =? {, ab, ba, abab, abba, … } Ø * =? {} {}* =? {}  L + = L 1  L 2  …

10 10 Example  Order of precedence for operators: ( ) > Closure > Concatenation > Union  L = {w | 0 and 1 alternate in w}, Σ={0, 1}, how to describe L in a regular expression? (01)*+(10)*+0(10)*+1(01)* Or ?

11 11 1.2 Regular Expressions  Regular Expression  RE  -NFA  DFA  RE

12 12 Regular Language  A language is regular if and only if some nondeterministic finite automaton recognizes it.  Main Theorem: A language is regular (i.e.,recognized by a finite automaton) if and only if some regular expression describes it.

13 13 Lemma  Lemma: If a language is described by a regular expression, then it is regular.  Say that we have a regular expression R describing some language A. We show how to convert R into an NFA recognizing A. If an NFA recognizes A then A is regular.

14 14 From RE to -NFA  For every regular expression R, we can construct an -NFA A, s.t. L(A) = L(R).  Proof by structural induction: Ø: : a: a

15 15 From RE to -NFA R+S: RS: R*: R S     R S  R     This is a proof by structural induction!

16 16 Example: (0+1)*1(0+1)     0 1     0 1        0 1  1      0 1   

17 17 Example 1.31: (a+b)*aba     a b     a  b  a

18 18 1.2 Regular Expressions  Regular Expression  RE  -NFA  DFA  RE

19 19 Lemma  Lemma: If a language is regular, then it is described by a regular expression.  Idea: If a language is regular, it is accepted by a DFA. We need to describe a procedure for converting DFAs into equivalent regular expressions.

20 20 From DFA to RE  For every DFA A=(Q, Σ, , q 0, F), there is a regular expression R, s.t. L(R) = L(A).  Proof Let Q = {1, 2, … n} and q 0 = 1; Let R[i, j] k be a RE describing the set of all label paths in A from state i to state j going through the states {1, … k} only. Then the union of all R[1, j] n (jF) will be the final RE describing the language of A

21 21 From DFA to RE We computes the R[i, j] k inductively  Initially (k=0): R[i, i] = +a+b+ …, whenever (i, a)=(i, b)=i, etc. R[i, j] = a+b+ …, if ij and (i, a)=(i, b)=j, etc. R[i, j] = Ø, if ij and there are no direct transitions from state i to state j.

22 22 From DFA to RE R[i, j] k is computed as:  A path that goes through the states {1, … k} only either never goes through state k, in which case the path's label is in the language of R[i, j] k-1 or goes through k one or more times. In this case:  R[i, k] k-1 contains the portion of the path that goes from i to k for the first time.  (R[k, k] k-1 )* contains the portion of the path (possibly empty) from the first k visit to the last.  R[k, j] k-1 contains the portion of the path from the last k visit to state j.  R[i, j] k = R[i, j] k-1 + R[i, k] k-1 (R[k, k] k-1 )* R[k, j] k-1

23 23 Example  L(A) = {x0y | x{1}*, y{0, 1}*} 1 0,1 0 1 2 R[1, 1] 0 +1 R[1, 2] 0 0 R[2, 1] 0 Ø R[2, 2] 0 +0+1 k = 0

24 24 Example Cont ’ d  We need the following simplification rules: (+R)* = R* R+RS* = RS* Ø R = R Ø = Ø (Annihilation) Ø +R = R+ Ø = R (Identity)

25 25 Example Cont ’ d R[1, 1] 0 +1 R[1, 2] 0 0 R[2, 1] 0 Ø R[2, 2] 0 +0+1 R[1, 1] 1 +1+(+1)(+1)*(+1) = 1* R[1, 2] 1 0+(+1)(+1)*0 = 1*0 R[2, 1] 1 Ø + Ø (+1)*(+1) = Ø R[2, 2] 1 +0+1+ Ø (+1)*0 = +0+1 k = 1

26 26 Example Cont ’ d R[1, 1] 1 1* R[1, 2] 1 1*0 R[2, 1] 1 Ø R[2, 2] 1 +0+1 R[1, 1] 2 1*+ 1*0(+0+1)* Ø = 1* R[1, 2] 2 1*0+1*0(+0+1)*(+0+1) = 1*0(0+1)* R[2, 1] 2 Ø +(+0+1)(+0+1)* Ø = Ø R[2, 2] 2 +0+1+(+0+1)(+0+1)*(+0+1) = 0+1 k = 2

27 27 Example Cont ’ d  The final regular expression for A is R[1, 2] 2 = 1*0(0+1)*

28 28 Generalized nondeterministic FAs  A generalized nondeterministic FA is a nondeterministic FA wherein the transition arrows may have any regular expressions as labels. Further we require GNFA satisfy the following three conditions:  The start state has transition arrows going to every other state but no arrows coming in from any other state.  There is only a single accept state, and it has arrows coming in from every other state but no arrows going to any other state. Furthermore, the accept state is not the same as the start state.  Except for the start and accept states, one arrow goes from every state to every other state and also from each state to itself.

29 29 Example: GNFA q start q accept ab*  bab (aa)*a* aa

30 30 GNFA  Label the edges of the FA with regular expressions instead of symbols.  Add a new start state, q start, with an  edge to the old start state and a new accept state, q accept, with  arrows from the old accept states.  Idea: eliminate the states step by step except q start and q accept, the regular expression on the label is the answer.

31 31 DFA -> RE

32 32 Crucial step  We do so by selecting a state, ripping it out of the machine, and repairing the remainder so that the same language is still recognized. Any state will do, provided that it is not the start or accept state.  Let's call the removed state qrip.

33 33 Crucial step qi qj q0 qiqj R4 R1 R3 R2 R1(R2)*R3+R4 What if qi = qj?

34 34 Crucial step qi q0 qi R4 R1 R3 R2 R1(R2)*R3+R4 if qi = qj?

35 35 Example 1: two states DFA

36 36 Example 2: three states DFA

37 37 Example 2: three states DFA

38 38 Algebraic Laws for RE  Regexs E and F are equivalent: L(E) = L(F).  (E + F) + G = E + (F + G) Union is associative  E + F = F + E Union is commutative  Ø + E = E + Ø = E Ø is identity for union  E + E = E Union is idempotent

39 39 Algebraic Laws for RE  (E F) G = E (F G) Concatenation is associative   E = E  = E  is right and left identity for concatenation  Ø E = E Ø = Ø Ø is right and left annihilator for concatenation  E (F + G) = E F + E G Concatenation is left distributive over union  (F + G) E = F E + G E Concatenation is right distributive over union

40 40 Algebraic Laws for RE  Ø * =   * =   (E*)* = E* Closure is idempotent  E* = EE* + 

41 41 Assignment 2 1. Convert 0*(100*)* to an -NFA 2. Eliminate the  arcs in your answer 3. Convert the following FA into RE: s t q p 0 0 1 1 1 1 0 0


Download ppt "1 Introduction to the Theory of Computation Regular Expressions."

Similar presentations


Ads by Google