47
TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department of Computer and Information Science [email protected] 2012-09-05

TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

TDDD65Introduction to the Theory of Computation

Lecture 3

Gustav NordhDepartment of Computer and Information Science

[email protected]

2012-09-05

Page 2: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Outline

Context-free GrammarsAmbiguityPumping LemmaPushdown AutomataSummary of Context-free Languages

Page 3: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Context-free Languages (CFL)

What can be computed with restricted access to unlimitedmemory?

Page 4: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Context-free Languages (CFL)

What can be computed with restricted access to unlimitedmemory?

control

...

stack

0 0 1 1 · · ·input tape

Page 5: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Context-free Languages (CFL)

What can be computed with restricted access to unlimitedmemory?

control0

...

stack

0 0 1 1 · · ·input tape

Page 6: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Context-free Languages (CFL)

What can be computed with restricted access to unlimitedmemory?

control00

...

stack

0 0 1 1 · · ·input tape

Page 7: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Context-free Languages (CFL)

What can be computed with restricted access to unlimitedmemory?

control00

...

stack

0 0 1 1 · · ·input tape

A push-down automaton (PDA) is a NFA with a stack

Page 8: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Context-free Languages (CFL)

Recall: DFAs correspond to regular expressionsPush-down automatas (PDAs) correspond to Context-freeGrammars (CFGs)

Page 9: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Context-free Languages (CFL)

Noam Chomsky (1928 -)

Page 10: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Context-free Grammars: Motivation

Describing (parts of) natural languagesDescribing the syntax of programming languages

Page 11: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

ExampleS → AS → BA→ 0A1A→ εB → aBaB → bBbB → aB → bB → ε

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

Page 12: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

ExampleS → AS → BA→ 0A1A→ εB → aBaB → bBbB → aB → bB → ε

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

Page 13: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

A string is in the language of the grammar if it can be generatedby:

1 Writing down the start variable2 Replacing a variable that is written down by the right hand

side of a rule starting with that variable3 Repeating Step 2 until no variable remains

S ⇒ B ⇒ aBa⇒ abBba⇒ abba

Page 14: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

A string is in the language of the grammar if it can be generatedby:

1 Writing down the start variable

2 Replacing a variable that is written down by the right handside of a rule starting with that variable

3 Repeating Step 2 until no variable remainsS ⇒ B ⇒ aBa⇒ abBba⇒ abba

Page 15: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

A string is in the language of the grammar if it can be generatedby:

1 Writing down the start variable2 Replacing a variable that is written down by the right hand

side of a rule starting with that variable

3 Repeating Step 2 until no variable remainsS ⇒ B ⇒ aBa⇒ abBba⇒ abba

Page 16: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

A string is in the language of the grammar if it can be generatedby:

1 Writing down the start variable2 Replacing a variable that is written down by the right hand

side of a rule starting with that variable3 Repeating Step 2 until no variable remains

S ⇒ B ⇒ aBa⇒ abBba⇒ abba

Page 17: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

A string is in the language of the grammar if it can be generatedby:

1 Writing down the start variable2 Replacing a variable that is written down by the right hand

side of a rule starting with that variable3 Repeating Step 2 until no variable remains

S ⇒ B

⇒ aBa⇒ abBba⇒ abba

Page 18: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

A string is in the language of the grammar if it can be generatedby:

1 Writing down the start variable2 Replacing a variable that is written down by the right hand

side of a rule starting with that variable3 Repeating Step 2 until no variable remains

S ⇒ B ⇒ aBa

⇒ abBba⇒ abba

Page 19: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

A string is in the language of the grammar if it can be generatedby:

1 Writing down the start variable2 Replacing a variable that is written down by the right hand

side of a rule starting with that variable3 Repeating Step 2 until no variable remains

S ⇒ B ⇒ aBa⇒ abBba

⇒ abba

Page 20: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Example of a Context-free Grammars (CFG)

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

A string is in the language of the grammar if it can be generatedby:

1 Writing down the start variable2 Replacing a variable that is written down by the right hand

side of a rule starting with that variable3 Repeating Step 2 until no variable remains

S ⇒ B ⇒ aBa⇒ abBba⇒ abba

Page 21: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Definition of Context-free Grammar (CFG)

DefinitionA context-free grammar (CFG) is a 4-tuple (V ,Σ,R,S) where

V is a finite set of variablesΣ is a finite set of terminalsR is a finite set of rulesS ∈ V is the start variable

Page 22: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

The language of a CFG

If u, v , and w are strings of variables and terminals, and A→ wis a rule of the grammar we say that uAv yields uwv , writtenuAv ⇒ uwv .

u derives v written u ∗⇒ v if u ⇒ u1 ⇒ u2 ⇒ · · · ⇒ uk ⇒ v

DefinitionThe language of a CFG G = (V ,Σ,R,S) is

{w ∈ Σ∗ | S ∗⇒ w}

written L(G).

DefinitionA language that is generated by some context-free grammar iscalled a context-free language

Page 23: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

The language of a CFG

If u, v , and w are strings of variables and terminals, and A→ wis a rule of the grammar we say that uAv yields uwv , writtenuAv ⇒ uwv .u derives v written u ∗⇒ v if u ⇒ u1 ⇒ u2 ⇒ · · · ⇒ uk ⇒ v

DefinitionThe language of a CFG G = (V ,Σ,R,S) is

{w ∈ Σ∗ | S ∗⇒ w}

written L(G).

DefinitionA language that is generated by some context-free grammar iscalled a context-free language

Page 24: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

The language of a CFG

If u, v , and w are strings of variables and terminals, and A→ wis a rule of the grammar we say that uAv yields uwv , writtenuAv ⇒ uwv .u derives v written u ∗⇒ v if u ⇒ u1 ⇒ u2 ⇒ · · · ⇒ uk ⇒ v

DefinitionThe language of a CFG G = (V ,Σ,R,S) is

{w ∈ Σ∗ | S ∗⇒ w}

written L(G).

DefinitionA language that is generated by some context-free grammar iscalled a context-free language

Page 25: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

The language of a CFG

If u, v , and w are strings of variables and terminals, and A→ wis a rule of the grammar we say that uAv yields uwv , writtenuAv ⇒ uwv .u derives v written u ∗⇒ v if u ⇒ u1 ⇒ u2 ⇒ · · · ⇒ uk ⇒ v

DefinitionThe language of a CFG G = (V ,Σ,R,S) is

{w ∈ Σ∗ | S ∗⇒ w}

written L(G).

DefinitionA language that is generated by some context-free grammar iscalled a context-free language

Page 26: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

The language of a CFG

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

ExampleThe language generated byA→ 0A1 | εis LA = {0n1n | n ≥ 0}

ExampleThe language generated byB → aBa | bBb | a | b | εis LB = {s ∈ {a,b}∗ | s is a palindrome}

Page 27: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

The language of a CFG

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

ExampleThe language generated byA→ 0A1 | εis LA = {0n1n | n ≥ 0}

ExampleThe language generated byB → aBa | bBb | a | b | εis LB = {s ∈ {a,b}∗ | s is a palindrome}

Page 28: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

The language of a CFG

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

ExampleThe language generated byA→ 0A1 | εis LA = {0n1n | n ≥ 0}

ExampleThe language generated byB → aBa | bBb | a | b | εis LB = {s ∈ {a,b}∗ | s is a palindrome}

Page 29: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFG, Ambiguity

DefinitionA derivation of a string w in a grammar G is a leftmostderivation if at every step the leftmost remaining variable is theone being replaced

DefinitionA string s is derived ambiguously in a CFG G if it has twodifferent leftmost derivations. A CFG G is ambiguous if itgenerates some string ambiguously.

Page 30: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFG, Ambiguity

DefinitionA derivation of a string w in a grammar G is a leftmostderivation if at every step the leftmost remaining variable is theone being replaced

DefinitionA string s is derived ambiguously in a CFG G if it has twodifferent leftmost derivations. A CFG G is ambiguous if itgenerates some string ambiguously.

Page 31: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFG, Ambiguity

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

Page 32: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFG, Ambiguity

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

Is this grammar ambiguous?

Page 33: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFG, Ambiguity

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

Yes!S ⇒ B ⇒ εS ⇒ A⇒ ε

Page 34: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFG, Ambiguity

Example

S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε

Equivalent and unambiguous:

Example

S → A | B | εA→ 0A1 | 01B → aBa | bBb | a | b | aa | bb

Page 35: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Pushdown Automata

control

...

stack

0 0 1 1 · · ·input tape

Page 36: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Pushdown Automata

control0

...

stack

0 0 1 1 · · ·input tape

Page 37: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Pushdown Automata

control00

...

stack

0 0 1 1 · · ·input tape

Page 38: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFL, pushdown automata, definition

DefinitionA pushdown automaton (PDA) is a 6-tuple (Q,Σ, Γ, δ,q0,F )where

Q is the finite set of statesΣ is the input alphabetΓ is the stack alphabetδ : Q × (Σ ∪ {ε})× (Γ ∪ {ε})→ P(Q × (Γ ∪ {ε})) is thetransition functionq0 ∈ Q is the start stateF ⊆ Q is the set of accept states

Page 39: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFL, pushdown automata

Pushdown automata are nondeterministic!

TheoremThere are languages recognized by PDAs that are notrecognized by any deterministic PDA. For example thelanguage {wwR | w ∈ {0,1}∗}.

Page 40: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFL, pushdown automata

Pushdown automata are nondeterministic!TheoremThere are languages recognized by PDAs that are notrecognized by any deterministic PDA. For example thelanguage {wwR | w ∈ {0,1}∗}.

Page 41: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFL, pushdown automata

ExampleDescribe a pushdown automaton recognizing the language{0n1n | n ≥ 0}

1 Start pushing the 0’s read on the stack.2 When the first 1 appears, start popping a 0 from the stack

for each 1 that is read.3 Should a 0 appear as input in this stage, then reject the

string.4 If the input is finished and 0’s remains on the stack, or if

the stack is emptied before the input is finished, then rejectthe string.

5 Otherwise, accept the string.

Page 42: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFL, pushdown automata

ExampleDescribe a pushdown automaton recognizing the language{0n1n | n ≥ 0}

1 Start pushing the 0’s read on the stack.2 When the first 1 appears, start popping a 0 from the stack

for each 1 that is read.3 Should a 0 appear as input in this stage, then reject the

string.4 If the input is finished and 0’s remains on the stack, or if

the stack is emptied before the input is finished, then rejectthe string.

5 Otherwise, accept the string.

Page 43: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

CFL, pushdown automata

TheoremA language is context-free if and only if some pushdownautomaton recognizes it

Page 44: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

nonCFLs

L = {0n1n2n | n ≥ 0} is not a CFLWhy?

Page 45: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

nonCFLs

L = {0n1n2n | n ≥ 0} is not a CFLWhy?

Imagine a PDA that recognize LWhen reading a string the PDA needs to keep track of thenumber of 0’s so that it can check that the same number of1’s and 2’s followThe number of 0’s is unbounded so the PDA needs to useits stack for thisTo check that the same number of 1’s follow, the PDAneeds to empty its stackNow, the PDA has no way of checking that the samenumber of 2’s follow

Page 46: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Pumping Lemma for CFLs

LemmaIf L is a CFL, then there exists a positive integer p (the pumpinglength) such that every string s ∈ L, |s| ≥ p, can be partitionedinto five pieces, s = uvxyz, such that the following conditionshold:

|vy | > 0,|vxy | ≤ p, andfor each i ≥ 0, uv ixy iz ∈ L

Page 47: TDDD65 Introduction to the Theory of Computation - Lecture 3TDDD65/lectures/contextfree12.pdf · TDDD65 Introduction to the Theory of Computation Lecture 3 Gustav Nordh Department

Summary of Context-free Languages

The context-free languages are the languages generatedby context-free grammarsA context-free grammar is ambiguous if the same stringcan be derived using two different left-most derivationsA language is context-free iff it is recognized by a PDAThere are simple languages that are not context-free