As we know, given a regular grammar, we have algorithm to get its regular expression.
But if the given grammar is context-free grammar (but it only generates regular language), like
Is there any existing algorithm that can get the regular expression in general?
Thanks!
The set of all regular languages is a subset of context free languages. So if you have a context free grammar (CFG) that generates a regular languages, you most certainly can convert it to a regular expression (RE), regular grammar (RG), or finite automata (FA).
Context-free grammars (CFGs) are used to describe context-free languages. A context-free grammar is a set of recursive rules used to generate patterns of strings. A context-free grammar can describe all regular languages and more, but they cannot describe all possible languages.
Regular Expressions describe exactly the regular languages. If E is a regular expression, then L(E) is the regular language it defines. For each regular expression E, we can create a DFA A such that L(E) = L(A).
In the most general sense, there is no solution. The problem of determining whether a CFG is regular is undecidable (Greibach Theorem, last 3 pages of http://www.cis.upenn.edu/~jean/gbooks/PCPh04.pdf ) If we could convert CFGs to Regular Expressions, we could use that algorithm on any grammar and use its success/failure to determine whether the language is regular.
So instead, when a CFG is known to produce a regular language, either its language is already known (and therefore directly convertible to a RegEx), or there's some property of the grammar to exploit. Each property has its own algorithm for converting to a RegEx.
For example, if the grammar is right linear, every production is of the form A->bC or A->a. This can be converted to a NFA where:
1) There is a state for every non-terminal, plus an accept state.
2) The start symbol S is the start state.
3) A->bC is a transition from A to B on input b
4) A->a is a transition from A to the accept state on input a.
This NFA can then be converted to a regular expression via state elimination (pages 5-8 of http://www.math.uaa.alaska.edu/~afkjm/cs351/handouts/regular-expressions.pdf ). An analogous process for left-linear grammars would have start and accept states exchanged.
Beyond that, one could exploit closure properties of regular languages. For example, the language in the question is not linear, but it can be written as S->S'b, S'->aA. Now S' is right-linear, and S is the concatenation of two disjoint linear grammars. Concatenate the two expressions for the final expression. Similar logic for union.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With