Theory of Computer Science
C5. Context-free Languages: Normal Form and PDA
Gabriele R¨ oger
University of Basel
April 1, 2019
Theory of Computer Science
April 1, 2019 — C5. Context-free Languages: Normal Form and PDA
C5.1 Context-free Grammars and ε-Rules C5.2 Chomsky Normal Form
C5.3 Push-Down Automata
C5.4 Summary
Overview
Automata &
Formal Languages
Languages
& Grammars
Regular Languages
Context-free Languages
ε-rules
Chomsky Normal Form
PDAs
Pumping Lemma Closure Properties
Decidability Context-sensitive &
Type-0 Languages
C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules
C5.1 Context-free Grammars and
ε-Rules
C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules
Repetition: Context-free Grammars
Definition (Context-free Grammar)
A context-free grammar is a 4-tuple hΣ, V , P , S i with
1
Σ finite alphabet of terminal symbols,
2
V finite set of variables (with V ∩ Σ = ∅),
3
P ⊆ (V × (V ∪ Σ) + ) ∪ {hS, εi} finite set of rules,
4
If S → ε ∈ P , then all other rules in V × ((V \ {S}) ∪ Σ) + .
5
S ∈ V start variable.
Rule X → ε is only allowed if X = S and S never occurs on a right-hand side.
With regular grammars, this restriction could be lifted.
How about context-free grammars?
C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules
Overview
Automata &
Formal Languages
Languages
& Grammars
Regular Languages
Context-free Languages
ε-rules
Chomsky Normal Form
PDAs
Pumping Lemma Closure Properties
Decidability Context-sensitive &
Type-0 Languages
C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules
ε-Rules
Theorem
For every grammar G with rules P ⊆ V × (V ∪ Σ) ∗ there is a context-free grammar G 0 with L(G ) = L(G 0 ).
Proof.
Let G = hΣ, V , P , S i be a grammar with P ⊆ V × (V ∪ Σ) ∗ . Let V ε = {A ∈ V | A ⇒ ∗ ε}. We can find this set V ε by first collecting all variables A with rule A → ε ∈ P and then successively adding additional variables B if there is a rule B → A 1 A 2 . . . A k ∈ P and the variables A i are already in the set
for all 1 ≤ i ≤ k. . . .
C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules
ε-Rules
Theorem
For every grammar G with rules P ⊆ V × (V ∪ Σ) ∗ there is a context-free grammar G 0 with L(G ) = L(G 0 ).
Proof (continued).
Let P 0 be the rule set that is constructed from P by
I adding rules that obviate the need for A → ε rules:
for every existing rule B → w with B ∈ V , w ∈ (V ∪ Σ) + , let I ε be the set of positions where w contains a variable A ∈ V ε . For every non-empty set I 0 ⊆ I ε , add a new rule B → w 0 , where w 0 is constructed from w by removing the variables at all positions in I 0 .
I removing all rules of the form A → ε (after the previous step).
. . .
C5. Context-free Languages: Normal Form and PDA Context-free Grammars andε-Rules
ε-Rules
Theorem
For every grammar G with rules P ⊆ V × (V ∪ Σ) ∗ there is a context-free grammar G 0 with L(G ) = L(G 0 ).
Proof (continued).
Then L(G ) \ {ε} = L(hΣ, V , P 0 , Si) and P 0 contains no rule A → ε. If the start variable S of G is not in V ε , we are done.
Otherwise, let S 0 be a new variable and construct P 00 from P 0 by
1
replacing all occurrences of S on the right-hand side of rules with S 0 ,
2
adding the rule S 0 → w for every rule S → w , and
3
adding the rule S → ε.
Then L(G ) = L(hΣ, V ∪ {S 0 }, P 00 , S i).
C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form
C5.2 Chomsky Normal Form
C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form
Overview
Automata &
Formal Languages
Languages
& Grammars
Regular Languages
Context-free Languages
ε-rules
Chomsky Normal Form
PDAs
Pumping Lemma Closure Properties
Decidability Context-sensitive &
Type-0 Languages
C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form
Chomsky Normal Form: Motivation
As in logical formulas (and other kinds of structured objects), normal forms for grammars are useful:
I they show which aspects are critical for defining grammars and which ones are just syntactic sugar
I they allow proofs and algorithms to be restricted
to a limited set of grammars (inputs): those in normal form
Hence we now consider a normal form for context-free grammars.
C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form
Chomsky Normal Form: Definition
Definition (Chomsky Normal Form)
A context-free grammar G is in Chomsky normal form (CNF) if all rules have one of the following three forms:
I A → BC with variables A, B, C , or
I A → a with variable A, terminal symbol a, or
I S → ε with start variable S . German: Chomsky-Normalform
in short: rule set P ⊆ (V × (VV ∪ Σ)) ∪ {hS, εi}
C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form
Chomsky Normal Form: Theorem
Theorem
For every context-free grammar G there is a context-free grammar G 0 in Chomsky normal form with L(G) = L(G 0 ).
Proof.
The following algorithm converts the rule set of G into CNF:
Step 1: Eliminate rules of the form A → B with variables A, B . If there are sets of variables {B 1 , . . . , B k } with rules
B 1 → B 2 , B 2 → B 3 , . . . , B k−1 → B k , B k → B 1 , then replace these variables by a new variable B.
Define a strict total order < on the variables such that A → B ∈ P
implies that A < B. Iterate from the largest to the smallest
variable A and eliminate all rules of the form A → B while adding
rules A → w for every rule B → w with w ∈ (V ∪ Σ) + . . . .
C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form
Chomsky Normal Form: Theorem
Theorem
For every context-free grammar G there is a context-free grammar G 0 in Chomsky normal form with L(G) = L(G 0 ).
Proof (continued).
Step 2: Eliminate rules with terminal symbols on the
Step 2:
right-hand side that do not have the form A → a.
For every terminal symbol a ∈ Σ add a new variable A a and the rule A a → a.
Replace all terminal symbols in all rules that do not have
the form A → a with the corresponding newly added variables. . . .
C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form
Chomsky Normal Form: Theorem
Theorem
For every context-free grammar G there is a context-free grammar G 0 in Chomsky normal form with L(G) = L(G 0 ).
Proof (continued).
Step 3: Eliminate rules of the form A → B 1 B 2 . . . B k with k > 2 For every rule of the form A → B 1 B 2 . . . B k with k > 2, add new variables C 2 , . . . , C k−1 and replace the rule with
A → B 1 C 2 C 2 → B 2 C 3
.. .
C k−1 → B k−1 B k
C5. Context-free Languages: Normal Form and PDA Chomsky Normal Form
Chomsky Normal Form: Length of Derivations
Observation
Let G be a grammar in Chomsky normal form,
and let w ∈ L(G ) be a non-empty word generated by G .
Then all derivations of w have exactly 2|w | − 1 derivation steps.
Proof.
Exercises
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
C5.3 Push-Down Automata
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Overview
Automata &
Formal Languages
Languages
& Grammars
Regular Languages
Context-free Languages
ε-rules
Chomsky Normal Form
PDAs
Pumping Lemma Closure Properties
Decidability Context-sensitive &
Type-0 Languages
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Limitations of Finite Automata
q
00 q
1q
20,1
0
I Language L is regular.
⇐⇒ There is a finite automaton that accepts L.
I What information can a finite automaton “store”
about the already read part of the word?
I Infinite memory would be required for
L = {x 1 x 2 . . . x n x n . . . x 2 x 1 | n > 0, x i ∈ {a, b}}.
I therefore: extension of the automata model with memory
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Stack
A stack is a data structure following the last-in-first-out (LIFO) principle supporting the following operations:
I push: puts an object on top of the stack
I pop: removes the object at the top of the stack
I peek: returns the top object without removing it
Push Pop
German: Keller, Stapel
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Visually
Input tape
I n p u t
Read head
Push-down automaton
Stack access
Stack
German: Kellerautomat, Eingabeband, Lesekopf, Kellerzugriff
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Definition
Definition (Push-down Automaton)
A push-down automaton (PDA) is a 6-tuple M = hQ , Σ, Γ, δ, q 0 , #i with
I Q finite set of states
I Σ the input alphabet
I Γ the stack alphabet
I δ : Q × (Σ ∪ {ε}) × Γ → P f (Q × Γ ∗ ) the transition function (where P f is the set of all finite subsets)
I q 0 ∈ Q the start state
I # ∈ Γ the bottommost stack symbol
German: Kellerautomat, Eingabealphabet, Kelleralphabet,
German: Uberf¨ ¨ uhrungsfunktion
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Transition Function
Let M = hQ, Σ, Γ, δ, q 0 , #i be a push-down automaton.
What is the Intuitive Meaning of the Transition Function δ?
I hq 0 , B 1 . . . B k i ∈ δ(q, a, A): If M is in state q, reads symbol a and has A as the topmost stack symbol,
then M can transition to q 0 in the next step while replacing A with B 1 . . . B k (afterwards B 1 is the topmost stack symbol)
q a, A → B
1. . . B
kq
0I special case a = ε is allowed (spontaneous transition)
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Example
q q
0a, A → AA a, B → AB a, # → A#
b, A → BA b, B → BB b, # → B#
a, A → ε b, B → ε
a,A → ε b,B → ε ε, # → ε
M = h{q, q 0 }, { a, b }, { A, B, # }, δ, q, # i with
δ(q, a, A) = {hq, AAi, hq
0, εi} δ(q, b, A) = {hq, BAi} δ(q, ε, A) = ∅ δ(q, a, B) = {hq, ABi} δ(q, b, B) = {hq, BBi, hq
0, εi} δ(q, ε, B) = ∅ δ(q, a, #) = {hq, A#i} δ(q, b, #) = {hq, B#i} δ(q, ε, #) = ∅ δ(q
0, a, A) = {hq
0, εi} δ(q
0, b, A) = ∅ δ(q
0, ε, A) = ∅ δ(q
0, a, B) = ∅ δ(q
0, b, B) = {hq
0, εi} δ(q
0, ε, B) = ∅
δ(q
0, a, #) = ∅ δ(q
0, b, #) = ∅ δ(q
0, ε, #) = {hq
0, εi}
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Configuration
Definition (Configuration of a Push-down Automaton)
A configuration of a push-down automaton M = hQ, Σ, Γ, δ, q 0 , #i is given by a triple c ∈ Q × Σ ∗ × Γ ∗ .
German: Konfiguration Example
I n p u t
q
Configuration
hq, ut, BAC#i.
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Steps
Definition (Transition/Step of a Push-down Automaton)
We write c ` M c 0 if a push-down automaton M = hQ, Σ, Γ, δ, q 0 , #i can transition from configuration c to configuration c 0 in one step.
Exactly the following transitions are possible:
hq, a 1 . . . a n , A 1 . . . A m i ` M
hq 0 , a 2 . . . a n , B 1 . . . B k A 2 . . . A m i if hq 0 , B 1 . . . B k i ∈ δ(q, a 1 , A 1 ) hq 0 , a 1 a 2 . . . a n , B 1 . . . B k A 2 . . . A m i
if hq 0 , B 1 . . . B k i ∈ δ(q, ε, A 1 ) German: Ubergang ¨
If M is clear from context, we only write c ` c 0 .
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Reachability of Configurations
Definition (Reachable Configuration)
Configuration c 0 is reachable from configuration c in PDA M (c ` ∗ M c 0 ) if there are configurations c 0 , . . . , c n (n ≥ 0) where
I c 0 = c ,
I c i ` M c i+1 for all i ∈ {0, . . . , n − 1}, and
I c n = c 0 .
German: c
0ist in M von c erreichbar
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Recognized Words
Definition (Recognized Word of a Push-down Automaton) PDA M = hQ, Σ, Γ, δ, q 0 , #i recognizes the word w = a 1 . . . a n
iff the configuration hq, ε, εi (word processed and stack empty) for some q ∈ Q is reachable from the start configuration hq 0 , w , # i.
M recognizes w iff hq 0 , w , #i ` ∗ M hq, ε, εi for some q ∈ Q.
German: M erkennt w , Startkonfiguration
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Recognized Word Example
q q
0a, A → AA a, B → AB a, # → A#
b, A → BA b, B → BB b, # → B#
a, A → ε b, B → ε
a,A → ε b,B → ε ε, # → ε
example: this PDA recognizes bbabbabb blackboard
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
Push-down Automata: Accepted Language
Definition (Accepted Language of a Push-down Automaton) Let M be a push-down automaton with input alphabet Σ.
The language accepted by M is defined as
L(M ) = {w ∈ Σ ∗ | M recognizes w }.
example: blackboard
C5. Context-free Languages: Normal Form and PDA Push-Down Automata
PDAs Accept Exactly the Context-free Languages
Theorem
A language L is context-free if and only if
L is accepted by a push-down automaton.
C5. Context-free Languages: Normal Form and PDA Summary
C5.4 Summary
C5. Context-free Languages: Normal Form and PDA Summary
Summary
I Every context-free language has a grammar in Chomsky normal form. All rules have form
I
A → BC with variables A, B, C, or
I
A → a with variable A, terminal symbol a, or
I