Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
TDDD65Introduction to the Theory of Computation
Lecture 3
Gustav NordhDepartment of Computer and Information Science
2012-09-05
Outline
Context-free GrammarsAmbiguityPumping LemmaPushdown AutomataSummary of Context-free Languages
Context-free Languages (CFL)
What can be computed with restricted access to unlimitedmemory?
Context-free Languages (CFL)
What can be computed with restricted access to unlimitedmemory?
control
...
stack
0 0 1 1 · · ·input tape
Context-free Languages (CFL)
What can be computed with restricted access to unlimitedmemory?
control0
...
stack
0 0 1 1 · · ·input tape
Context-free Languages (CFL)
What can be computed with restricted access to unlimitedmemory?
control00
...
stack
0 0 1 1 · · ·input tape
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
Context-free Languages (CFL)
Recall: DFAs correspond to regular expressionsPush-down automatas (PDAs) correspond to Context-freeGrammars (CFGs)
Context-free Languages (CFL)
Noam Chomsky (1928 -)
Context-free Grammars: Motivation
Describing (parts of) natural languagesDescribing the syntax of programming languages
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 | ε
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 | ε
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
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
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
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
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
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
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
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
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
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
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
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
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
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}
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}
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}
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.
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.
CFG, Ambiguity
Example
S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε
CFG, Ambiguity
Example
S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε
Is this grammar ambiguous?
CFG, Ambiguity
Example
S → A | BA→ 0A1 | εB → aBa | bBb | a | b | ε
Yes!S ⇒ B ⇒ εS ⇒ A⇒ ε
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
Pushdown Automata
control
...
stack
0 0 1 1 · · ·input tape
Pushdown Automata
control0
...
stack
0 0 1 1 · · ·input tape
Pushdown Automata
control00
...
stack
0 0 1 1 · · ·input tape
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
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}∗}.
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}∗}.
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.
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.
CFL, pushdown automata
TheoremA language is context-free if and only if some pushdownautomaton recognizes it
nonCFLs
L = {0n1n2n | n ≥ 0} is not a CFLWhy?
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
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
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