30
CSE 105 THEORY OF COMPUTATION Fall 2016 http://cseweb.ucsd.edu/classes/fa16/cse105-abc/

CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

CSE 105 THEORY OF COMPUTATION

Fall 2016

http://cseweb.ucsd.edu/classes/fa16/cse105-abc/

Page 2: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Today's learning goals Sipser Ch 2

• Define push down automata

• Trace the computation of a push down automaton

• Design a push down automaton recognizing a given

language

• Compare class of regular languages and class of CFLs

Page 3: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Notation:

Terminology: sequence of

rule applications is

derivation

Context-free language Sipser p. 104

The language of a CFG (V, Σ, R, S) is

{ w in Σ* | Starting with the Start variable and applying one or more

rules, can get to w}

Page 4: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Context-free languages

Regular languages vs. CFL

Regular languages

Page 5: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Designing CFGs Sipser p. 104

Given a language L over Σ, to prove L is a context-free

language need to build a CFG that generates it.

How?

• Express L as union of simpler languages

• If L is regular, design DFA and convert into CFG

• Exploit any recursive structure in L.

Page 6: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

ExampleDesign a CFG recognizing the language

{anbm | n ≠ m}

On Thursday, we showed that the language {anbn | n ≥ 0} is a CFL. How can we use its

CFG for this example?

A. Use the same CFG but reverse the role of variables and terminals.

B. Use the same set of variables, terminals, and start state, but reverse the RHS and LHS

of each rule.

C. We can't use it directly: the two sets are not related.

D. We can't use it directly: the class of CFLs is not closed under complementation.

E. I don't know.

Page 7: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

ExampleDesign a CFG recognizing the language

{anbm | n ≠ m} = {anbm | n > m} U {anbm | n < m}

Simpler problem: Design a CFG generating the language

{anbm | n > m}

Page 8: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

ExampleDesign a CFG recognizing the language

{anbm | n ≠ m} = {anbm | n > m} U {anbm | n < m}

Simpler problem: Design a CFG generating the language

{anbm | n > m}

G1 = ( {S1}, {a,b}, R, S1) with rules S1 aS1b | aS1 | a

Page 9: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

ExampleDesign a CFG recognizing the language

{anbm | n ≠ m} = {anbm | n > m} U {anbm | n < m}

CFG generating the language {anbm | n > m}

is G1 = ( {S1}, {a,b}, R, S1) with rules S1 aS1b | aS1 | a

CFG generating the language {anbm | n < m}

is G2 = ( {S2}, {a,b}, R, S2) with rules S2 aS2b | S2b | b

Page 10: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

ExampleFor {anbm | n > m}: G1 = ( {S1}, {a,b}, R, S1) with S1 aS1b | aS1 | a

For {anbm | n < m}: G2 = ( {S2}, {a,b}, R, S2) with S2 aS2b | S2b | b

What is CFG generating {anbm | n ≠ m} ?

A. G1 U G2

B. ( {S1, S2}, {a,b}, R, S1) with S1 aS1b | aS1 | a, S2 aS2b | S2b | b

C. ( {S1, S2}, {a,b}, R, S2) with S1 aS1b | aS1 | aS2, S2 aS2b | S2b | b

D. ( {S, S1, S2}, {a,b}, R, S) with S S1 | S2 , S1 aS1b | aS1 | a, S2 aS2b | S2b | b

E. I don't know.

Page 11: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

An alternative … Sipser p. 109

• NFA + stack

Page 12: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Pushdown automata Sipser p. 109

• NFA + stack

At each step

1. Transition to new state

based on current state, letter

read, and top letter of stack.

2. (Possibly) push or pop a

letter to (or from) top of stack

Page 13: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Pushdown automata Sipser p. 109

• NFA + stack

Accept a string if

there is some sequence of states

and some sequence of stack

contents which processes the

entire input string and ends in an

accepting state.

Page 14: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

State diagram for PDA Sipser p. 109

If hand-drawn or in Sipser

State transition labelled a, b c means

"when machine reads an a from the input and the top

symbol of the stack is a b, it may replace the b with a c."

In JFLAP use ; instead of

Page 15: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

State diagram for PDA Sipser p. 109

If hand-drawn or in Sipser

State transition labelled a, b c means

"when machine reads an a from the input and the top

symbol of the stack is a b, it may replace the b with a c."

What edge label would indicate "Read a 0, don't pop anything from stack, don’t push

anything to the stack"?

A. 0, ε ε

B. ε, 0 ε

C. ε, ε 0

D. ε ε, 0

E. I don't know.

Page 16: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Useful trickWhat would ε, ε $ mean?

A. Without reading any input or popping any symbol from stack, push $

B. If the input string and stack are both empty strings, push $

C. At the end of reading the input string, push $ to top of stack

D. I don't know.

Page 17: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Useful trick

Why is this useful?

Commonly used from initial state (at start of computation) to record top of stack with a special symbol)… we'll see applications soon!

What would ε, ε $ mean?

A. Without reading any input or popping any symbol from stack, push $

B. If the input string and stack are both empty strings, push $

C. At the end of reading the input string, push $ to top of stack

D. I don't know.

Page 18: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Formal definition of PDA Sipser Def 2.13 p. 111

Page 19: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Designing a PDAL = { 0i1i+1 | i ≥ 0 }

Informal description of PDA:

Read symbols from the input. As each 0 is read, push it onto the

stack. As soon as 1s are seen, pop a 0 off the stack for each 1

read. If the stack becomes empty and there is exactly one 1 left

to read, read that 1 and accept the input. If the stack becomes

empty and there are either zero or more than one 1s left to read,

or if the 1s are finished while the stack still contains 0s, or if any

0s appear in the input following 1s, reject the input.

Page 20: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Designing/Tracing a PDAL = { 0i1i+1 | i ≥ 0 }

What are the contents of the stack

after processing 001?

A. (TOP) $00

B. (TOP) 00$

C. (TOP) 0$

D. (TOP) 100$

E. I don't know.

Page 21: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

PDAs and CFGs are equivalently expressive

Theorem 2.20: A language is context-free if and only some

nondeterministic PDA recognizes it.

Consequences

- Quick proof that every regular language is context free

- To prove closure of class of CFLs under a given operation, can choose two

modes of proof (via CFGs or PDAs) depending on which is easier

Page 22: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

ExampleL = { aibjck | i=j or i=k, with i,j,k≥0 }

Which of the following strings are

not in L?

A. b

B. abc

C. abbcc

D. aabcc

E. I don't know.

Page 23: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

ExampleL = { aibjck | i=j or i=k, with i,j,k≥0 }

To design a CFG that generates L…

L = { aibjck | i=j, with i,j,k≥0 } U { aibjck | i=k, with i,j,k≥0 }

Page 24: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

ExampleL = { aibjck | i=j or i=k, with i,j,k≥0 }

To design a CFG that generates L…

L = { aibjck | i=j, with i,j,k≥0 } U { aibjck | i=k, with i,j,k≥0 }

S Sc | T

T aTb | ε

Page 25: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

ExampleL = { aibjck | i=j or i=k, with i,j,k≥0 }

To design a CFG that generates L…

L = { aibjck | i=j, with i,j,k≥0 } U { aibjck | i=k, with i,j,k≥0 }

S T | aSc

T Tb | ε

Page 26: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Designing a PDAL = { aibjck | i=j or i=k, with i,j,k≥0 }

Informal description of PDA:

How would you design an algorithm that, given a string, decides if it is in this set?

- What information do you need to track?

- How much memory do you need?

- Are you using non-determinism?

Page 27: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Designing a PDAL = { aibjck | i=j or i=k, with i,j,k≥0 }

Informal description of PDA:

• The PDA pushes a $ to indicate the top of the stack, then starts reading a's, pushing each one on to the stack.

• The PDA guesses when it's reached the end of the a's and whether to match the number of a's to the number of b's or the number of c's.

• If trying to match number of b's with number of a's: PDA pops off a's for each b read. If there are more a's on the stack but no more b's being read, reject. When the end of the stack ($) is reached, the number of a's matches the number of b's. If this is the end of the input or if any number of c's is read at this point, accept; otherwise, reject.

• If trying to match the number of c's with number of a's: first read any number of b's without changing stack contents and then nondeterministically guess when to start reading c's. For each c read, pop one a off the stack. When the end of the stack ($) is reached the number of a's and c's so far match.

Page 28: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Designing a PDAL = { aibjck | i=j or i=k, with i,j,k≥0 }

Formal definition of PDA:

Page 29: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Conventions for PDAs• Can "test for end of stack" without providing details

• We can always push the end-of-stack symbol, $, at the start.

• Can "test for end of input" without providing details

• Can transform PDA to one where accepting states are only those

reachable when there are no more input symbols.

• Don't always need to provide a state transition diagram!

Page 30: CSE 105 Theory of Computation - Home | Computer Science · 2016-10-26 · CSE 105 THEORY OF COMPUTATION Fall 2016 ... •Compare class of regular languages and class of CFLs. Notation:

Context-free languages

Overview

Regular languages

???