19

Can a context-free grammar include "dead states" from an automaton, such as

$$G = \big(\{a, b, c\}, \{A, B, C\}, \{A\to aB, B\to b, B\to C, C\to cC\}, A\big)\,?$$

The production rules $B\to C$ and $C\to cC$ will loop forever and never generate a word. Is this allowed or MUST production rules end with an terminal at some point?

David Richerby
  • 82,470
  • 26
  • 145
  • 239
cmtjk
  • 293
  • 2
  • 5

2 Answers2

24

Context-free grammars are allowed to contain unproductive rules. This is accepted, because every CFG generates the same language as some proper CFG which contains no unproductive rules, no empty string productions, and no cycles; so it is safe to assume that a CFG is proper without loss of generality.

ilke444
  • 507
  • 5
  • 10
3

Yes of course. Every NFA can be written as a CFG. And building a DFA with a 'dead state' (the term I was taught, is 'sink') is trivial.

Example: $$G = (\{a\}, \{A\}, \{A\to A\},A)$$ is a CFG that describes the empty language over the alphabet $\{a\}$.

Analogous to the NFA with only one non-accepting starting state and only a self-transition with $\epsilon$.

David Richerby
  • 82,470
  • 26
  • 145
  • 239
David
  • 39
  • 1