I assume that you mean converting it to a formal grammar with rules of the form V-> w, where V is a non-terminal and w is a string of terminals / non-terminals. To get started, you can simply say (mixing CFG and regex syntax):
S -> 01+10(11)*
Where S is the symbol of the beginning. Now break it a bit (and add a space for clarity):
S -> 0 A 1 0 B A -> 1+ B -> (11)*
The key is to convert * es and + es to recursion. First, we convert the Kleene star to a plus by inserting an intermediate rule that accepts an empty string:
S -> 0 A 1 0 B A -> 1+ B -> (empty) B -> C C -> (11)+
Finally, we convert the + notation to recursion:
S -> 0 A 1 0 B A -> 1 A -> A 1 B -> (empty) B -> C C -> 11 C -> C 11
To handle x? , just divide it into a rule that creates an empty one, and a rule that creates an x.
Joey adams
source share