I have the following regular expression:
(12)*[34]+(5[67])*
How can I convert the expression to a grammar that is left recursive?
I came up with this. I did not follow any methods or anything...
G --> GAB34C
A --> A12 | epsilon
B --> B34 | epsilon
C --> C56 | C57 | epsilon
As commented, 34 is not a correct rule as 3 and 4 are alternates. This not only affects the second rule, but also the first.
Not a problem, but
C56 | C57can be written asC5(6|7)So:
If G must also employ left recursion itself, then put the C-recursion inside G (eliminate C):
And maybe it is more elegant to keep
(3|4)in the definition of B:Script
Here is an implementation with generators in JavaScript. It produces some random strings in the grammar: