patternMinor
complexity of determining whether a language given by context free grammar is empty
Viewed 0 times
determiningfreeemptylanguagegrammarcontextwhethergivencomplexity
Problem
I know that it is decidable problem to check whether given context free grammar represents empty language -- for instance, AFAIR one could convert it to Chomsky normal form, and then check if any word of length $\leq 2^n$ (or maybe $\leq 2^{n+1}$, I'm not sure) belongs to the language, where $n$ is IIRC the number of nonterminals in CNF. If not, then no longer words belong either and the grammar is empty.
The above algorithm has the unpleasant property of having exponential complexity. The questions that interest me are:
The above algorithm has the unpleasant property of having exponential complexity. The questions that interest me are:
- Is there a polynomial algorithm to check whether given CFG represents empty language?
- What's the (asymptotically) best known algorithm for that?
- What's the simplest polynomial algorithm for that (not necessarily having best known complexity)?
Solution
First, omit all terminal symbols. Then the language is empty iff the grammar does not generate the empty string.
An algorithm to check acceptance of the empty string is straightforward. A non-terminal $A$ is nullable, i.e., $A\Rightarrow^* \lambda$ iff $A \to w$ where each symbol in $w$ is nullable.
Determine the symbols nullable with a derivation tree of depth $k$, $k=0,1,\dots$. First find all $A$ such that $A\to \lambda$. Remove these $A$ from the right-hand side of productions and repeat. Axiom $S$ is nullable iff the language is non-empty.
This can be implemented in linear time, making lists of all productions that contain a certain letter at their right-hand side.
Seems to be equivalent to HORN-SAT.
An algorithm to check acceptance of the empty string is straightforward. A non-terminal $A$ is nullable, i.e., $A\Rightarrow^* \lambda$ iff $A \to w$ where each symbol in $w$ is nullable.
Determine the symbols nullable with a derivation tree of depth $k$, $k=0,1,\dots$. First find all $A$ such that $A\to \lambda$. Remove these $A$ from the right-hand side of productions and repeat. Axiom $S$ is nullable iff the language is non-empty.
This can be implemented in linear time, making lists of all productions that contain a certain letter at their right-hand side.
Seems to be equivalent to HORN-SAT.
Context
StackExchange Computer Science Q#32191, answer score: 9
Revisions (0)
No revisions yet.