Formal Languages, Grammars and Automata Lecture 4 Helle Hvid Hansen - - PowerPoint PPT Presentation

formal languages grammars and automata lecture 4
SMART_READER_LITE
LIVE PREVIEW

Formal Languages, Grammars and Automata Lecture 4 Helle Hvid Hansen - - PowerPoint PPT Presentation

Introduction Closure Properties Radboud University Nijmegen Pumping Lemma Formal Languages, Grammars and Automata Lecture 4 Helle Hvid Hansen helle@cs.ru.nl http://www.cs.ru.nl/~helle/ Foundations Group Intelligent Systems Section


slide-1
SLIDE 1

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Formal Languages, Grammars and Automata Lecture 4

Helle Hvid Hansen

helle@cs.ru.nl http://www.cs.ru.nl/~helle/

Foundations Group – Intelligent Systems Section Institute for Computing and Information Sciences Radboud University Nijmegen

23 May 2014

Helle Hvid Hansen 23 May 2014 FLGA 1 / 12

slide-2
SLIDE 2

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Recap Lecture 3

Kleene’s Theorem: L is regular ⇐ ⇒ L ∈ L(DFA). L is regular

def

⇐ ⇒ L = L(e) for some regular expression e

(lecture 3)

⇐ ⇒ L ∈ L(NFA-λ)

λ-elim.

⇐ ⇒ L ∈ L(NFA)

subset constr.

⇐ ⇒ L ∈ L(DFA) Thus, to prove that given L is regular, we can either give a

  • reg. expr. or show that L is accepted by some DFA/NFA/NFA-λ.

Helle Hvid Hansen 23 May 2014 FLGA 2 / 12

slide-3
SLIDE 3

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Today

  • Closure properties of regular languages (ways to prove

regularity)

  • How to prove a language is not regular (Pumping Lemma).

Helle Hvid Hansen 23 May 2014 FLGA 3 / 12

slide-4
SLIDE 4

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Closure Properties of Regular Languages

Regular languages are closed under

  • union: L1, L2 regular ⇒ L1 ∪ L2 regular.
  • concatenation: L1, L2 regular ⇒ L1 · L2 regular.
  • Kleene star: L regular ⇒ L∗ regular.

Proof: (follows directly from definition of regular expressions) Let L1 = L(e1) and L2 = L(e2). Then

  • L1 ∪ L2 = L(e1 + e2)
  • L1 · L2 = L(e1 · e2)
  • L∗

1 = L(e∗ 1)

Helle Hvid Hansen 23 May 2014 FLGA 4 / 12

slide-5
SLIDE 5

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Closure Properties of Regular Languages

Regular languages are also closed under

  • complement: L regular ⇐

⇒ L = Σ∗ \ L regular.

  • intersection: L1, L2 regular ⇒ L1 ∩ L2 regular.
  • reversal: L regular ⇒ LR = {wR | w ∈ L} regular (exercise).

Helle Hvid Hansen 23 May 2014 FLGA 5 / 12

slide-6
SLIDE 6

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Closure Properties of Regular Languages

Regular languages are also closed under

  • complement: L regular ⇐

⇒ L = Σ∗ \ L regular.

  • intersection: L1, L2 regular ⇒ L1 ∩ L2 regular.
  • reversal: L regular ⇒ LR = {wR | w ∈ L} regular (exercise).

Proof of closure under complement: (⇒) Let M = (Q, q0, δ, F) be a DFA such that L = L(M). Taking M = (Q, q0, δ, Q \ F), then M is a DFA with L(M) = L hence L is regular. (⇐) Follows from L = L and (⇒).

Helle Hvid Hansen 23 May 2014 FLGA 5 / 12

slide-7
SLIDE 7

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Closure Properties of Regular Languages

Regular languages are also closed under

  • complement: L regular ⇐

⇒ L = Σ∗ \ L regular.

  • intersection: L1, L2 regular ⇒ L1 ∩ L2 regular.
  • reversal: L regular ⇒ LR = {wR | w ∈ L} regular (exercise).

Proof of closure under complement: (⇒) Let M = (Q, q0, δ, F) be a DFA such that L = L(M). Taking M = (Q, q0, δ, Q \ F), then M is a DFA with L(M) = L hence L is regular. (⇐) Follows from L = L and (⇒). Note: The definition of M can also be applied to NFAs, but if M is an NFA then it is not necessarily the case that L(M) = L. (Why?)

Helle Hvid Hansen 23 May 2014 FLGA 5 / 12

slide-8
SLIDE 8

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Closure Properties of Regular Languages

Proof of closure under intersection: Two ways:

1 Use closure under complement, union and De Morgan laws:

L1 ∩ L2 = L1 ∪ L2

Helle Hvid Hansen 23 May 2014 FLGA 6 / 12

slide-9
SLIDE 9

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Closure Properties of Regular Languages

Proof of closure under intersection: Two ways:

1 Use closure under complement, union and De Morgan laws:

L1 ∩ L2 = L1 ∪ L2

2 Idea: Let Mi = (Qi, qi, δi, Fi) be DFA accepting Li for

i = 1, 2. Run M1 and M2 in parallel, and accept a word if both M1 and M2 accept. We define the product DFA M = (Q, q, δ, F) where Q = Q1 × Q2 = {(p1, p2) | p1 ∈ Q1, p2 ∈ Q2} q = (q1, q2) δ((p1, p2), a) = (δ(p1, a), δ2(p2, a)) F = F1 × F2 (both components accept) Then L(M) = L(M1) ∩ L(M2) = L1 ∩ L2.

Helle Hvid Hansen 23 May 2014 FLGA 6 / 12

slide-10
SLIDE 10

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

How to prove a language is not regular?

To prove L is NOT regular, we must show that there is NO finite automaton that accepts L. There are many finite automata to check (infinitely many)...

Helle Hvid Hansen 23 May 2014 FLGA 7 / 12

slide-11
SLIDE 11

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

How to prove a language is not regular?

To prove L is NOT regular, we must show that there is NO finite automaton that accepts L. There are many finite automata to check (infinitely many)... Observation: Suppose L = L(M) for some DFA with k states. Take a word w = a1 · · · an, where ai ∈ Σ, of length n ≥ k, and consider the run on w in M: q0

a1

− → q1

a2

− → · · ·

ak

− → qk

  • k+1 states

ak+1

− → · · ·

an

− → qn There must be j, l ∈ {0, . . . , k} such that qj = ql.

Helle Hvid Hansen 23 May 2014 FLGA 7 / 12

slide-12
SLIDE 12

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

How to prove a language is not regular?

To prove L is NOT regular, we must show that there is NO finite automaton that accepts L. There are many finite automata to check (infinitely many)... Observation: Suppose L = L(M) for some DFA with k states. Take a word w = a1 · · · an, where ai ∈ Σ, of length n ≥ k, and consider the run on w in M: q0

a1

− → q1

a2

− → · · ·

ak

− → qk

  • k+1 states

ak+1

− → · · ·

an

− → qn There must be j, l ∈ {0, . . . , k} such that qj = ql. There is a “loop” through qj whose transitions are labelled by aj+1 · · · al. So: w ∈ L iff a1 · · · aj(aj+1 · · · al)ial+1 · · · an ∈ L for all i ∈ N. We say aj+1 · · · al is a pumpable substring of w (before index k).

Helle Hvid Hansen 23 May 2014 FLGA 7 / 12

slide-13
SLIDE 13

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

The Pumping Lemma

Pumping Property A language L has the Pumping Property (PP) if there is a k ∈ N such that for all words w ∈ L, if |w| ≥ k then there is a decomposition w = uvz with |uv| = k′ ≤ k and |v| > 0, w = a1 · · · aj

u

aj+1 · · · ak′

  • v

ak′+1 · · · an

  • z

k′ ≤ k such that for all i ∈ N, uviz ∈ L. In words, L has the PP if there is a k ∈ N such that all words w ∈ L of length k or more have a pumpable substring before position k in w.

Helle Hvid Hansen 23 May 2014 FLGA 8 / 12

slide-14
SLIDE 14

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

The Pumping Lemma

Pumping Property A language L has the Pumping Property (PP) if there is a k ∈ N such that for all words w ∈ L, if |w| ≥ k then there is a decomposition w = uvz with |uv| = k′ ≤ k and |v| > 0, w = a1 · · · aj

u

aj+1 · · · ak′

  • v

ak′+1 · · · an

  • z

k′ ≤ k such that for all i ∈ N, uviz ∈ L. In words, L has the PP if there is a k ∈ N such that all words w ∈ L of length k or more have a pumpable substring before position k in w. Pumping Lemma: L is regular ⇒ L has the Pumping Property. So we have: L does not have the PP ⇒ L is not regular.

Helle Hvid Hansen 23 May 2014 FLGA 8 / 12

slide-15
SLIDE 15

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Games with the Demon

L does not have the PP: For all k ∈ N there exists a w ∈ L such that |w| ≥ k, and for all decompositions w = uvz with |uv| ≤ k and |v| > 0, there exists an i ∈ N such that uviz ∈ L.

Helle Hvid Hansen 23 May 2014 FLGA 9 / 12

slide-16
SLIDE 16

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Games with the Demon

L does not have the PP: For all k ∈ N there exists a w ∈ L such that |w| ≥ k, and for all decompositions w = uvz with |uv| ≤ k and |v| > 0, there exists an i ∈ N such that uviz ∈ L. Game between Demon and You: Demon wants to show that PP holds. You want to show that PP does not hold.

Helle Hvid Hansen 23 May 2014 FLGA 9 / 12

slide-17
SLIDE 17

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Games with the Demon

L does not have the PP: For all k ∈ N there exists a w ∈ L such that |w| ≥ k, and for all decompositions w = uvz with |uv| ≤ k and |v| > 0, there exists an i ∈ N such that uviz ∈ L. Game between Demon and You: Demon wants to show that PP holds. You want to show that PP does not hold. Demon (“for all”-player) You (“there exists”-player)

  • 1. picks k ∈ N
  • 2. pick w ∈ L : |w| ≥ k
  • 3. picks u, v, z ∈ Σ∗ : such that

(w = uvz ∧ |uv| ≤ k ∧ |v| > 0)

  • 4. pick i ∈ N
  • 5. Demon wins the game if uviz ∈ L. You win if uviz ∈ L.

Helle Hvid Hansen 23 May 2014 FLGA 9 / 12

slide-18
SLIDE 18

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Games with the Demon II

  • L has the PP iff Demon has a winning strategy (i.e. he can

pick a k such that no matter which w you choose, he can choose u, v, z such that no matter which i you choose, Demon wins.

Helle Hvid Hansen 23 May 2014 FLGA 10 / 12

slide-19
SLIDE 19

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Games with the Demon II

  • L has the PP iff Demon has a winning strategy (i.e. he can

pick a k such that no matter which w you choose, he can choose u, v, z such that no matter which i you choose, Demon wins.

  • L does not have the PP iff You have a winning strategy (i.e.

no matter which k Demon chooses, you can pick w such that no matter which decomposition u, v, z Demon chooses, you can choose i such that you win.

Helle Hvid Hansen 23 May 2014 FLGA 10 / 12

slide-20
SLIDE 20

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Applying the Pumping Lemma

We show that L = {anbn | n ≥ 0} is not regular. Play against the Demon:

  • Let k ∈ N (chosen by Demon).

Helle Hvid Hansen 23 May 2014 FLGA 11 / 12

slide-21
SLIDE 21

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Applying the Pumping Lemma

We show that L = {anbn | n ≥ 0} is not regular. Play against the Demon:

  • Let k ∈ N (chosen by Demon).
  • Take w = akbk, then w ∈ L and |w| ≥ k.

Helle Hvid Hansen 23 May 2014 FLGA 11 / 12

slide-22
SLIDE 22

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Applying the Pumping Lemma

We show that L = {anbn | n ≥ 0} is not regular. Play against the Demon:

  • Let k ∈ N (chosen by Demon).
  • Take w = akbk, then w ∈ L and |w| ≥ k.
  • Let uvz = w with |uv| = k′ ≤ k and |v| > 0 (chosen by

Demon). Then we have that: u = aj, v = am, z = ak−k′bk, where j + m = k′ and m ≥ 1.

Helle Hvid Hansen 23 May 2014 FLGA 11 / 12

slide-23
SLIDE 23

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Applying the Pumping Lemma

We show that L = {anbn | n ≥ 0} is not regular. Play against the Demon:

  • Let k ∈ N (chosen by Demon).
  • Take w = akbk, then w ∈ L and |w| ≥ k.
  • Let uvz = w with |uv| = k′ ≤ k and |v| > 0 (chosen by

Demon). Then we have that: u = aj, v = am, z = ak−k′bk, where j + m = k′ and m ≥ 1.

  • Take i = 0, then uviz = uz = ak−mbk /

∈ L.

Helle Hvid Hansen 23 May 2014 FLGA 11 / 12

slide-24
SLIDE 24

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Applying the Pumping Lemma

We show that L = {anbn | n ≥ 0} is not regular. Play against the Demon:

  • Let k ∈ N (chosen by Demon).
  • Take w = akbk, then w ∈ L and |w| ≥ k.
  • Let uvz = w with |uv| = k′ ≤ k and |v| > 0 (chosen by

Demon). Then we have that: u = aj, v = am, z = ak−k′bk, where j + m = k′ and m ≥ 1.

  • Take i = 0, then uviz = uz = ak−mbk /

∈ L. I win! (In fact, any i = 1 will work).

Helle Hvid Hansen 23 May 2014 FLGA 11 / 12

slide-25
SLIDE 25

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Applying the Pumping Lemma

We show that L = {anbn | n ≥ 0} is not regular. Play against the Demon:

  • Let k ∈ N (chosen by Demon).
  • Take w = akbk, then w ∈ L and |w| ≥ k.
  • Let uvz = w with |uv| = k′ ≤ k and |v| > 0 (chosen by

Demon). Then we have that: u = aj, v = am, z = ak−k′bk, where j + m = k′ and m ≥ 1.

  • Take i = 0, then uviz = uz = ak−mbk /

∈ L. I win! (In fact, any i = 1 will work). So L does not have the PP, hence L is not regular.

Helle Hvid Hansen 23 May 2014 FLGA 11 / 12

slide-26
SLIDE 26

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Concluding

Summary:

  • Prove L is regular: Construct automata, closure properties.
  • Prove L is not regular: Pumping Lemma, closure properties.
  • Note: PP can not be used to prove that a language is regular:

L has the Pumping Property ⇒ L is regular. I.e., there are non-regular languages that have the PP.

  • Small Exercise Set 4 (for tutorial on 6 June).

Helle Hvid Hansen 23 May 2014 FLGA 12 / 12

slide-27
SLIDE 27

Introduction Closure Properties Pumping Lemma

Radboud University Nijmegen

Concluding

Summary:

  • Prove L is regular: Construct automata, closure properties.
  • Prove L is not regular: Pumping Lemma, closure properties.
  • Note: PP can not be used to prove that a language is regular:

L has the Pumping Property ⇒ L is regular. I.e., there are non-regular languages that have the PP.

  • Small Exercise Set 4 (for tutorial on 6 June).

Midterm Test (plus short enquete)

  • starts at 15:00 in HG00.304
  • starts at 15:15 in HG01.060 (extra time)

GOOD LUCK!

Helle Hvid Hansen 23 May 2014 FLGA 12 / 12