问题
Given an arbitrary context-free grammar, how can I check whether it describes a regular language?
I'm not looking for exam "tricks". I'm looking for a foolproof mechanical test that I can code.
If it helps, here's an example of a CFG that I might receive as an input. Specifically, notice that the answer must be much more complicated than just looking for left- or right-recursion, since the presence of another type of recursion does not automatically imply the grammar is irregular.
S: A B C D X
A: A a
A:
B: b B
B:
C: c C c
C: c
D: D d D
D: d
X: x Y
X:
Y: y X
Y:
回答1:
There is no such mechanical procedure because the problem of determining whether a CFG defines a regular language is undecidable.
This result is a simple application of Greibach's Thereom.
来源:https://stackoverflow.com/questions/31643381/how-to-determine-if-a-context-free-grammar-describes-a-regular-language