It is possible to write an algorithm that generates a CFG that parses a list of words, with maxim m
rules, or if it is imposible to parse all words, to parse maximum words possible?
Do you have any ideas about how should I start the algorithm?
All input words will be in the [a-z]
alphabet.
Asked
Active
Viewed 393 times
-2

tairqammar
- 151
- 3
- 10
-
2This question is somewhat vague. Can you provide an example of a string that could be parsed using this grammar? – Anderson Green Jan 05 '19 at 18:11
1 Answers
1
It is definitely possible to write such an algorithm. Without going through the details, you can enumerate all possible CFGs (unique up to renaming of the nonterminals) with m or fewer rules over the desired alphabet, and check each one to see whether it accepts your strings. If you do this in order of number of rules, you will stop on the first smallest CFG that works, if any. To see whether a CFG works, you can roll your own or derive a PDA from the CFG and then run the PDA on all inputs to see whether it accepts them. There are algorithms to generate PDA parsers from CFGs which you should be able to use.
This process is not particularly efficient but it is effective and suffices to show that it is possible.

Patrick87
- 27,682
- 3
- 38
- 73