Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a regular grammar to regular expression?

Is there an algorithm or tool to convert regular grammar to regular expression?

like image 888
dalibocai Avatar asked Oct 24 '22 12:10

dalibocai


2 Answers

Answer from dalibocai:

My goal is to convert regular grammer to DFA. Finally, I found an excellent tool : JFLAP.

A tutorial is available here: https://www2.cs.duke.edu/csed/jflap/tutorial/framebody.html

like image 77
Stephan Avatar answered Nov 01 '22 08:11

Stephan


The algorithm is pretty straightforward if you can compute an automaton from your regular expression. Once you have your automaton. For instance for (aa*b|c), an automaton would be (arrows go to the right):

          a
         / \
      a  \ / b
-> 0 ---> 1 ---> 2 ->
    \___________/
          c

Then just "enumerate" your transitions as rules. Below, consider that 0, 1, and 2 are nonterminal symbols, and of course a, b and c are the tokens.

0: a1 | c2
1: a1 | b2
2: epsilon

or, if you don't want empty right-hand sides.

0: a1 | c
1: a1 | b

And of course, the route in the other direction provides one means to convert a regular grammar into an automaton, hence a rational expression.

like image 41
akim Avatar answered Nov 01 '22 09:11

akim