Lecture Notes | 15CS54 – ATC | Module 4
6. Decidable questions
6.1 Membership
Given a context free language L and a string w, there exists a decision procedure that answers
the questions, “is w in L?”. There are two approaches:
• Find a context-free grammar to generate it
• Find a PDA to accept it
Using a Grammar to Decide
We begin by considering the first alternative. We show a straightforward algorithm for deciding
whether a string w is in a language L:
Using facts about every derivation that is produced by a grammar in Chomsky normal form, we
can construct an algorithm that explores a finite number of derivation paths and finds one that
derives a particular string w iff such a path exists.
decideCFLusingGrammar(L: CFL, w: string) =
1. If given a PDA, build G so that L(G) = L(M).
2. If w = ε then if SG is nullable then accept, else reject.
3. If w ≠ ε then:
3.1 Construct G′ in Chomsky normal form such that L(G′) = L(G) – {ε}.
3.2 If G derives w, it does so in 2⋅|w| - 1 steps. Try all derivations in G of 2|w| - 1 steps.
If one of them derives w, accept. Otherwise reject.
Using a PDA to Decide
It is also possible to solve the membership problem using PDAs. We take a two-step approach.
We first show that. for every context-free language L it is possible to build a PDA that accepts
L - { ɛ } and that has no ε-transitions. Then we show that every PDA with no ε-transitions is
guaranteed to halt.
While not all PDAs halt, it is possible for any context-free language L, to craft a PDA M that is
guaranteed to halt on all inputs and that accepts all strings in L and rejects all strings that are
not in L.
cfgtoPDAnoeps( G: context-free grammar) =
1. Convert G to Greibach normal form, producing G'.
2. From G' build the PDA M.
Step 2 can be implemented as follows.
Prepared by Harivinod N www.techjourney.in Page| 17
Lecture Notes | 15CS54 – ATC | Module 4
Halting Behavior of PDAs without ε-Transitions
A PDA Without ε-Transitions must halt.
The final algorithm can be listed as follows.
decideCFLusingPDA(L: CFL, w: string) =
1. If L is specified as a PDA, use PDAtoCFG to construct a grammar G such that L(G) =
L(M).
2. If L is specified as a grammar G, simply use G.
3. If w = ε then if SG is nullable then accept, otherwise reject.
4. If w ≠ ε then:
4.1 From G, construct G′ such that L(G′) = L(G) – {ε} and G′ is in Greibach normal form.
4.2 From G′ construct a PDA M such that L(M) = L(G′) and M′ has no ε-transitions.
4.3 All paths of M are guaranteed to halt within a finite number of steps. So, run M on w.
Accept if it accepts and reject otherwise.
6.2. Decidability of Emptiness and Finiteness
Prepared by Harivinod N www.techjourney.in Page| 18
Lecture Notes | 15CS54 – ATC | Module 4
Proof
Prepared by Harivinod N www.techjourney.in Page| 19
Lecture Notes | 15CS54 – ATC | Module 4
7. The Undecidable Questions
• Given a context free language L, is L = Σ*?
• Given a context free language L, is the complement of L context-free?
• Given a context free language L, is L regular?
• Given a context free language L1 and L2,, is L1 = L2?
• Given a context free language L1 and L2, is L1 ⊆ L2?
• Given a context free language L1 and L2, is L1 ∩ L2 = ∅?
• Given a context free language L, is L inherently ambiguous?
• Given a context free grammar G, is G ambiguous?
Prepared by Harivinod N www.techjourney.in Page| 20