21
cl Informatics 1 School of Informatics, University of Edinburgh NFA and regex the Boolean algebra of languages non-deterministic machines regular expressions 1

NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

clInformatics 1 School of Informatics, University of Edinburgh

NFA and regex

• the Boolean algebra of languages

• non-deterministic machines

• regular expressions

1

Page 2: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

The intersection of two regular languages is regular

2

Run both machines in parallel?

Build one machine that simulates two

machines running in parallel!

Keep track of the state of each

machine.

1

1

1

0 00 2

1

0

1

1 10

0

0

Page 3: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

The intersection of two regular languages is regular

3

1

0

0

1

1

1 10

0

0

1

0

0

1

0

10

20

100

11

0

0

1

11

21

01

1

|| =

0

0

1

2

10

mod 3

mod 2

0 1

3 2 3 2

0 0 0 0 1 11 0 2 0 0 12 0 1 0 2 10 1 0 0 1 11 1 2 0 0 12 1 1 0 2 1

1

Page 4: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

The intersection of two regular languages is regular

4

1

1 10

0

0|| =

1

0

0

1

0

4

2

10

11

0

0

1

1

5

3

1

0

mod 3

mod 2

mod 6

0 13 2 6 3 2 6

0 0 0 0 0 0 1 1 11 0 4 2 0 2 0 1 32 0 2 1 0 4 2 1 50 1 3 0 0 0 1 1 11 1 1 2 0 2 0 1 32 1 5 1 0 4 2 1 5

1

0

0

10

1

2

10

1

Page 5: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

The intersection of two regular languages is regular

5

1

1 10

0

0|| =

1

0

0

1

0

4

2

10

11

0

0

1

1

5

3

1

0

mod 3

mod 2

mod 6

0 13 2 6 3 2 6

0 0 0 0 0 0 1 1 11 1 1 2 0 2 0 1 32 0 2 1 0 4 2 1 50 1 3 0 0 0 1 1 11 0 4 2 0 2 0 1 32 1 5 1 0 4 2 1 5

1

0

0

10

1

2

10

1

Page 6: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

The regular languages A ⊆ Σ* form a Boolean Algebra

• Since they are closed under intersection and complement.

6

Page 7: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Determinism

• Proof Add a new “black hole” state, ● For every pair (s, a) for which there is no state t with a transition T(s, a, t), add a transition T(s, a, ●). This includes a transition T(●, a, ●) for each a ∈ Σ . You cannot escape from the black hole. The black hole ● is not an accepting state.

This machine accepts the same language as the original.

7

For this machine there is exactly one trace for each input

string

Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol.

Page 8: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Non DeterminismIn a non-deterministic machine (NFA), each state may have any number of transitions with the same input symbol, leaving to different successor states.

8

0 1

1

1

0 0

2 0 1

0 0 0,1

1 2

2

Page 9: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Non DeterminismIn a non-deterministic machine (NFA), each state may have any number of transitions with the same input symbol, leaving to different successor states.

9

0 1

1

1

0 0

20 1

0 0 0,11 22

0,1 0,2 0,10,2 0 0,1

0 0,1

010

11

0

0,2

Page 10: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Non DeterminismWe can simulate a non-deterministic machine using a deterministic machine – by keeping track of the set of states the NFA could possibly be in.

10

0 1

1

1

0 0

20 1

0 0 0,11 22

0,1 0,2 0,10,2 0 0,1

0 0,1

010

11

0

0,2

Page 11: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Internal TransitionsWe sometimes add an internal transition ε to a non-deterministic machine (NFA)This is a state change that consumes no input.

11

0 1

10 0

2

0 1 ε0 0 1

1 2 0

2

ε

0 1

1

1

0 0

2

Page 12: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Internal TransitionsWe sometimes add internal transitions – labelled ε – to a non-deterministic machine (NFA). This is a state change that consumes no input. It introduces non-determinism in the observed behaviour of the machine.

12

0 1

10 0

2

0 1 ε0 0 11 2 02

ε

0ε* 1ε*0 0 1,01 22

Page 13: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

NFA single start stateany number of accepting states

S

13

R

Page 14: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

sequence RS

ε

14

SRε

Page 15: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

alternation R|S

ε

ε

ε

15

SR

ε

ε

Page 16: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

iteration R*

ε

16

R

εε

Page 17: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Demohttp://ivanzuzak.info/noam/webapps/fsm_simulator/

17

0 1

1

1

0 0

20 1

0 0 0,11 22

0,1 0,2 0,10,2 0 0,1

0 0,1

010

11

0

0,2

Page 18: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Is there a regular expression for every FSM?

18

0 1

a b

2

c

Let Li be the language accepted if i is the accepting state

L0 = εL1 = L0 aL2 = L1 b | L0 c

L2 = L0 a b | ε cL2 = ε a b | ε cL2 = a b | c

0 1

a b

2

c

0 1

a b

2

c

L0

L1

L2

Page 19: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Is there a regular expression for every FSM?

19

L1 = L2 bL2 = L3 b | L1 aL3 = ε | L1 b

2

3

1 b

b

a

b

= ε | L2 b bL2 = (ε | L2 b b) b | L2 b a

= b | L2 b b b | L2 b a= b | L2 (b b b | b a)

Page 20: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Is there a regular expression for every FSM?

20

2

3

1 b

b

a

b

L2 = b | L2 (b b b | b a)

L2 = b (b b b | b a)*

L1 = L2 b = b (b b b | b a)* b

L3 = ε | L2 b b = ε | b (b b b | b a)* bb

Page 21: NFA and regex€¦ · NFA and regex • the Boolean ... Can always convert to an equivalent DFA for which every state has exactly one transition leaving the state for each input symbol

Informatics 1 School of Informatics, University of Edinburgh

Arden’s Lemma

21

If R and S are regular expressions then the equation

X = R | X S has a solution X = R S*

If ε ∉ L(S) then this solution is unique. L2 = b | L2 (b b b | b a)L2 = b (b b b | b a)*