Topics
Introduction
An algorithm for deciding whether a CFL is empty
Helper algorithm: remove useless nonterminals
Algorithm for deciding whether or not a CFL is
infinite
We addressed the issue of decidability for regular languages in Chapter 11. We showed that:
The method is a grammar method:
S ® XY | AX, A ® a, Y ® BY | BB, B ® b
| 1. Find nullable non-terminals (there are none) | |
| 2. Remove l-productions (there are none) | |
| 3. Underline all occurrences of nonterminals | S ® XY | AX, A ® a, Y ® BY | BB, B ® b |
| 4. Underline all occurrences of nonterminals any of whose right hand sides consists solely of underlined symbols. This is the heart of the algorithm. | We have A ® a,
and B ® b,
so underline all A’s and B’s:
S ® XY | AX, A®a, Y ®BY | BB, B®b |
| 5. Repeat 4, until no new nonterminals can be underlined. | We have Y ® BB,
so underline all Y’s:
S ® XY | AX, A®a, Y®BY | BB, B®b |
| 6. Repeat 4. No new underlinings. Stop. | |
| 7. S is not underlined, so the language produces no sentences, and is empty. | |
| The underlined symbols are those that can derive a string of terminals |
A useless nonterminal is one that can never be used in the derivation of a sentence. This can happen in two ways:
1. do the "emptiness" algorithm.
| 2. underline S (on lhs) | S ® ABa, A ® Xb, B ® bAA, X ® bA | aaa, Y ® b |
| 3. For each production with an underline, find the
nonterminals on the rhs of that production. Underline any lhs
occurrence of such a nonterminal.
This is the heart of the algorithm. |
We have S ® ABa, so
we underline A and B, where they occur as a lhs:
S ® ABa, A ® Xb, B ® bAA, X ® bA | aaa, Y ® b |
| 4. Repeat 3 | We have A ®
Xb,
B ®
bAA, so underline X (A is already underlined):
S ® ABa, A ® Xb, B ® bAA, X ® bA | aaa, Y ® b |
| 5. Repeat 3 | No new nonterminals underlined. Stop.
Y remains non-underlined. |
6. Remove all productions with Y (on lhs or rhs):
We will assume that our CFL is generated by a CFG that is l-production free.
Recall that self-embedded nonterminals are those that can derive themselves:
N Þ* …N…
If a CFL has no self-embedded nonterminals:
So all we need to do is determine if there are any self-embedded nonterminals.
Here’s the algorithm. We will use as an example grammar:
2. For each non-terminal, find those nonterminals it can derive. Let’s
do it for B.
| 3. Examine the rhs for each B ® production. For each nonterminal found there, underline that nonterminal everywhere it appears as a lhs. These are the nonterminals that can be derived from B in one derivation step. | We have B ® bAA, so we want to underline
all lhs A’s:
S ® ABa, A ® Xb, B ® bAA, X ® bA | aaa |
| 4. For each production with a lhs underlined, underline lhs occurrences of nonterminals in the corresponding rhs. This is the heart of the algorithm. | We have A ®
Xb, so underline lhs X’s:
S ® ABa, A ® Xb, B ® bAA, X ® bA | aaa |
| 5. Repeat 4. No new underlinings. Stop. |
The underlined non-terminals are those than can be derived from B in 1 or more steps. They are: {A, X}. B is not self-embedding.
If we do 3-5 for A, we find that A can derive {X, A}. Thus A is self-embedding, and the language is infinite.
Note: