How to build CFG based on a given regular expression

I am trying to figure out how to build CFG (context free grammar) based on this regular expression. For example, a (ab) * (a | b) I think there is an algorithm that can be followed, but it is really confusing. here is what i got so far:

    S->aAB; 
    A->aAb|empty;
    B->a|b;

Does this look right? Any help would be appreciated.

+4
source share
2 answers

Another way to build context-free grammar for a given regular expression:

  • Build a finite state machine that accepts the same language as the regular expression.
  • , , ( 1:1) X -> t Y - X Y t. CFG , F F -> epsilon. CFG , X F t X -> t ( X -> t F). , - , , .

, FSA ( , , ):

FSA for language <code> a (ab) * (a | b) </ code>

:

S -> a A1
A1 -> a A2
A2 -> b B3
B3 -> a A2
B3 -> a A4
B3 -> b B5
A1 -> a A4
A1 -> b B5
A4 -> epsilon
B5 -> epsilon
epsilon -> 

, , :

A1 -> a
A1 -> b
B3 -> a
B3 -> b

, , , , , , , , .

+3

CFG , a, (ab)* (a|b).

(a|b) B -> a | b .

(ab)* ab, abab, ababab .. , A -> abA | empty .

, :

S -> aAB
A -> abA | empty
B -> a | b

: A -> aAb | empty ab, aabb, aaabbb .., .

+2

Source: https://habr.com/ru/post/1539125/


All Articles