Search code examples
grammarregular-language

Is it, and if so why, wrong that these two regular grammars are different?


I'm tasked with writing a regular grammar based on a regular expression.

Given the regular expression a*b can be written as S -> b | aS

Is it incorrect that ba* as a regular grammar is S -> b | Sa?

I'm told the correct answer is in fact S -> bA, A -> ^| aA but I don't see the difference myself.

An explanation would be greatly appreciated!


Solution

  • IIRC, both your answer and the one being called "correct" are correct. See this. What you have constructed is a "left regular grammar", while the proponent(s) of the "correct" answer obviously prefer a "right regular grammar". There are other arbitrary rules that may be held more or less pedantically, like the "no empty productions" rule, but they don't really affect the class of regular languages, just the compactness of the grammar you use for a particular language, as your example highlights - a single production with two alternatives vs. two productions, one with a single clause, and one with two alternatives, one of which is empty.