Generating Context-free Grammars from Languages

douglasb

Diamond Member
Apr 11, 2005
3,157
0
76
Does anybody have experience with this? Specifically, I would really like to know if there is some sort of process that can be followed to create a CFG when given a language. I can do it intuitively for simple languages (equal number of a's and b's, simple palindromes, etc.) but I really don't know of a good method to do this for more difficult cases.

For example, how would I go about creating a CFG for the following language:
Code:
a^i b^j c^k : i,j,k >= 0 and (i != j OR j != k)

Intuitively, I know that this means that there can't be an equal number of a's, b's, and c's. There can be:
- equal number of a's and b's, but different number of c's
- equal number of a's and c's, but different number of b's
- equal number of b's and c's, but different number of a's
- different number of a's, b's, and c's

Does this mean that I need 4 non-terminals? What is the best way to approach this sort of problem?
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Things like counting the number of occurrences of a terminal/non-terminal are difficult to do in a parser. In this case I'd do the validation of the number of a, b, and c tokens in a semantic analysis phase.
 

douglasb

Diamond Member
Apr 11, 2005
3,157
0
76
Things like counting the number of occurrences of a terminal/non-terminal are difficult to do in a parser. In this case I'd do the validation of the number of a, b, and c tokens in a semantic analysis phase.

This all has to be done in the CFG. The next step is turning it into a push-down automata. I can sometimes intuitively create a PDA, but I don't really know how to convert that into a CFG.