32
CSCI 3130: Formal languages and automata theory Tutorial 5 Chin

CSCI 3130: Formal languages and automata theory Tutorial 5

  • Upload
    darby

  • View
    47

  • Download
    0

Embed Size (px)

DESCRIPTION

CSCI 3130: Formal languages and automata theory Tutorial 5. Chin. Reminder. Homework 3 is due tomorrow . Midterm is on next Monday . Review session tomorrow . You can get back homework 2 after this tutorial. Homework 2. We only say a language is regular or non-regular. - PowerPoint PPT Presentation

Citation preview

Page 1: CSCI 3130: Formal languages and automata theory Tutorial 5

CSCI 3130: Formal languagesand automata theory

Tutorial 5

Chin

Page 2: CSCI 3130: Formal languages and automata theory Tutorial 5

Reminder

• Homework 3 is due tomorrow.

• Midterm is on next Monday.• Review session tomorrow.

• You can get back homework 2 after this tutorial.

Page 3: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• We only saya language is regular or non-regular.

• We do not say a string is regular or non-regular

• Never writex is regular

• We only say a string is in a language or not.

Page 4: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• To show a language is not regular(a) You only have to choose one string.(b) The chosen string should be in terms of n.(c) You cannot choose what u, v, w are.

e.g. u = (ab)n-1, v = (ab)n, w = (bc)n. wrong!You have to consider every possible partitions of uvw.

(d) You only have to choose one value for i.

Page 5: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• Problem 1(a) Most answers are incorrect.

Some of you used the following arguments:Replace

with

What if:

q0 NFAa

q0 NFA

q0 NFAa q1

b

Page 6: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• Problem 1(a) There can be multiple accepting states in the DFA

Many states can reach an accepting state on ‘b’

Page 7: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• Problem 1(c) Many students drew the following

x and y are not fixed.

NFANFAq0x y

q0

Page 8: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• Problem 1(d) A few students did the following

Let L1 = {anb: n ≥ 0}, L2 = {an: n ≥ 0}

Then L1L2 = {anban : n ≥ 0}. wrong!

The n in L1 and the n in L2 are independent.

L1L2 = {anbam : n, m ≥ 0}.

Page 9: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• Problem 2(a) Some of you chose the strings (a+b)n(b+c)n

The proof fails if I take bnbn, which is of the above form.

Choose one string is okay. e.g. ancn

Page 10: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• Problem 2(c) L3 = {w : w does not have the same number of as, bs, and cs}

A few students got L3 wrong.

(b) Many students let u = (ac)n-1, v = (ac), w = (abc)n.

You cannot choose what u, v, w are!v can be any nonempty substring of the first n

characters of the string. e.g. v can be the first ‘a’ of the string.

Page 11: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• Problem 3(b) To show two states are distinguishable

Write down on what inputs one of them goes to an accepting state but the other does not.

Page 12: CSCI 3130: Formal languages and automata theory Tutorial 5

Homework 2

• Problem 4(b) Some of you did not handle names without any ‘a’.

(c) A few of you did not handle the names “crystal”.

(d) A few of you missed the case “rin”.

Page 13: CSCI 3130: Formal languages and automata theory Tutorial 5

Pushdown Automata

• NFA with a stack

• Read a, pop b, and push c• Push c to the stack after popping b• a, b, c can be • Use $ to mark stack bottom

q0a, b/c q1

Page 14: CSCI 3130: Formal languages and automata theory Tutorial 5

Pushdown Automata

• PDA = CFG

• Languages that can be represented by PDA/CFG are context-free.

Page 15: CSCI 3130: Formal languages and automata theory Tutorial 5

Pushdown Automata

• Design a PDA for the following. = {a, b, c}L1 = {x : x has fewer a’s than b’s and c’s together}

L2 = {aibjck : i = j or j = k}

Page 16: CSCI 3130: Formal languages and automata theory Tutorial 5

Pushdown Automata

L1 = {x : x has fewer a’s than b’s and c’s together}Lecture: L = {w: w has same number 0s and 1s}

First, treat a as a, b and c as x

q0

0, / 0

, / $

1, / 1

, $ /

0, 1 / 1, 0 /

q1 q2

q0

a, / a

, / $

b, / x

, $ /

a, x / b, a /

q1 q3

c, / xc, a /

Page 17: CSCI 3130: Formal languages and automata theory Tutorial 5

Pushdown Automata

L1 = {x : x has fewer a’s than b’s and c’s together}First, treat a as a, b and c as x

Accept if there are still x’s in the stack

q0

a, / a

, / $

b, / x

, $ /

a, x / b, a /

q1 q3

c, / xc, a /

q0

a, / a

, / $

b, / xa, x / b, a /

q1 q3

c, / xc, a /

q2, x /

, x /

, $ /

Page 18: CSCI 3130: Formal languages and automata theory Tutorial 5

Pushdown Automata

L2 = {aibjck : i = j or j = k}Separate it into two cases{aibjck : i = j } U {aibjck : j = k}1.{aibjck : i = j }Lecture: L = {w: w has same number 0s and 1s}

q0

0, / 0

, / $

1, / 1

, $ /

0, 1 / 1, 0 /

q1 q2

Page 19: CSCI 3130: Formal languages and automata theory Tutorial 5

Pushdown Automata

L2 = {aibjck : i = j or j = k}1.{aibjck : i = j }Lecture: L = {w: w has same number 0s and 1s}

add c’s

q0

0, / 0

, / $

1, / 1

, $ /

0, 1 / 1, 0 /

q1 q2

q0

a, / a

, / $

b, / b

, /

a, b / b, a /

q1 q3q2, $ /

c, /

Page 20: CSCI 3130: Formal languages and automata theory Tutorial 5

Pushdown Automata

L2 = {aibjck : i = j or j = k}2. {aibjck : j = k}Lecture: L = {w: w has same number 0s and 1s}

add a’s

q0

0, / 0

, / $

1, / 1

, $ /

0, 1 / 1, 0 /

q1 q2

q0

c, / c

, / $

b, / b

, /

c, b / b, c /

q1 q3q2, $ /

a, /

Page 21: CSCI 3130: Formal languages and automata theory Tutorial 5

Pushdown Automata

L2 = {aibjck : i = j or j = k}Take union

c, / c

, / $

b, / b

, /

c, b / b, c /

q5 q6q4, $ /

a, /

q0

a, / a, / $ b, / b

, /

a, b / b, a /

q1 q3q2, $ /

c, /

Page 22: CSCI 3130: Formal languages and automata theory Tutorial 5

Pumping lemma for Context-free languages

To show a language L is not context free:For every n, choose one z of length ≥ n in L, such that for every way of writing z = uvwxy where|vwx| ≤ n and|vx| ≥ 1,the string uviwxiy is not in L for some i ≥ 0.

• z depends on n• u, w, y can be empty string• v, x can also be empty string, but not both.• i can be 0 (very useful)•You only have to choose one z•Unlike the regular language version, vwx can be anywhere in z.

Page 23: CSCI 3130: Formal languages and automata theory Tutorial 5

TemplateSuppose L is context-free. Let n be the pumping length.

Then z = _______ (z should contain n somewhere) is in L.

Write z = uvwxy, where |vwx| ≤ n and |vx|≥ 1.

Argue! no matter how we write z = uvwxy, the string uv_wx_y is not in L (choose one i in _, i can be 0)

Therefore L is not regular.

Pumping lemma for Context-free languages

Page 24: CSCI 3130: Formal languages and automata theory Tutorial 5

Pumping lemma for CFG

• Show that the following languages are not context-freeL1 = {anbmanbm : m, n ≥ 0}. = {a, b}.

L2 = {x#y#z : yR is a substring of xz; x, y, z {a,b}*}.∈

= {a, b, #}.

Page 25: CSCI 3130: Formal languages and automata theory Tutorial 5

Pumping lemma for CFG

L1 = {anbmanbm : m, n ≥ 0}. = {a, b}.

Suppose L is context-free. Let n be the pumping length.Then z = _anbnanbn_ (z should contain n somewhere) is in L.Write z = uvwxy, where |vwx| ≤ n and |vx|≥ 1.

Argue! no matter how we write z = uvwxy, the string uv_wx_y is not in L (choose one i in _, i can be 0)

How to argue?

Page 26: CSCI 3130: Formal languages and automata theory Tutorial 5

Pumping lemma for CFG

L1 = {anbmanbm : m, n ≥ 0}. = {a, b}.

Then z = _anbnanbn_ (z should contain n somewhere) is in L.Write z = uvwxy, where |vwx| ≤ n and |vx|≥ 1.

What can vwx be if |vwx| ≤ n? vwx can be…1.part of the first an

2.part of the first anbn

3.part of the first bn

4.part of the middle bnan

5.part of the second an

6.part of the second anbn

7.part of the last bn

a……ab……ba……ab……b

Page 27: CSCI 3130: Formal languages and automata theory Tutorial 5

Pumping lemma for CFG

L1 = {anbmanbm : m, n ≥ 0}. = {a, b}.Then z = _anbnanbn_ (z should contain n somewhere) is in L.Write z = uvwxy, where |vwx| ≤ n and |vx|≥ 1.1.If vwx is part of the first an

uv2wx2y has less a’s in the first group than the second group2.If vwx is part of the first anbn

v and x cannot be both emptyIf v is not empty, v must contain some a, uv0wx0y has less a’s in the first group than the second groupIf x is not empty, x must contain some b,uv0wx0y has less b’s in the first group than the second group

The rest are similar, do explain them in the homework/exam.

a……ab……ba……ab……b

Page 28: CSCI 3130: Formal languages and automata theory Tutorial 5

Pumping lemma for CFG

L2 = {x#y#z : yR is a substring of xz; x, y, z {a,b}*}.∈

Suppose L is context-free. Let n be the pumping length.Then z = _an#bnan#bn_ (z should contain n somewhere) is in L.Write z = uvwxy, where |vwx| ≤ n and |vx|≥ 1.

Argue! no matter how we write z = uvwxy, the string uv_wx_y is not in L (choose one i in _, i can be 0)

How to argue?

Page 29: CSCI 3130: Formal languages and automata theory Tutorial 5

Pumping lemma for CFGL2 = {x#y#z : yR is a substring of xz; x, y, z {a,b}*}.∈

Then z = _an#bnan#bn_ (z should contain n somewhere) is in L.Write z = uvwxy, where |vwx| ≤ n and |vx|≥ 1.

What can vwx be if |vwx| ≤ n? vwx can be…0. If v and x contain ‘#’,

then we can always pump it and the string will contain more than 2 #s.1.part of the first an

2.part of the first an#bn

3.part of the first bn

4.part of the middle bnan

5.part of the second an

6.vwx is part of the last an#bn

7.vwx is part of the last bn

a……a#b……ba……a#b……b

Page 30: CSCI 3130: Formal languages and automata theory Tutorial 5

Pumping lemma for CFG

L2 = {x#y#z : yR is a substring of xz; x, y, z {a,b}*}.∈Then z = _an#bnan#bn_ (z should contain n somewhere) is in L.Write z = uvwxy, where |vwx| ≤ n and |vx|≥ 1.0. If v and x contain ‘#’,

then we can always pump it and the string will contain more than 2 #s, which is not in L2.

1.If vwx is part of the first an,uv0wx0y = X#Y#Z contains less a’s in X than in Y.thus the YR cannot be a substring of XZ.

a……a#b……ba……a#b……b

Page 31: CSCI 3130: Formal languages and automata theory Tutorial 5

Pumping lemma for CFGL2 = {x#y#z : yR is a substring of xz; x, y, z {a,b}*}.∈Then z = _an#bnan#bn_ (z should contain n somewhere) is in L.Write z = uvwxy, where |vwx| ≤ n and |vx|≥ 1.0. If v and x contain ‘#’,

then we can always pump it and the string will contain more than 2 #s, which is not in L2.2.If vwx is part of the first an#bn,

v and x cannot be both emptyIf v is not empty, v must contain some a, uv0wx0y = X#Y#Z contains less a’s in X than in Y.thus the YR cannot be a substring of XZ.

If x is not empty, x must contain some b,uv2wx2y = X#Y#Z contains less b’s in X than in Y.thus the YR cannot be a substring of XZ.

The rest are similar, do explain them in the homework/exam.

a……a#b……ba……a#b……b

Page 32: CSCI 3130: Formal languages and automata theory Tutorial 5

End

• Questions?