200 likes | 425 Vues
Constructing an Equivalent Regular Grammar from a Regular Expression. First of all you must remember the definition of a regular expression and the theorem: for any regular expression e , there exists a regular grammar G recognizing exactly the language described by e . Algorithm.
E N D
Constructing an Equivalent Regular Grammar from a Regular Expression
First of all you must remember the definition of a regular expression and the theorem: for any regular expressione, there exists a regular grammar Grecognizing exactly the language described by e.
Algorithm 1. if the regular expression is simply 0, we can show that G, with no production rules, is an equivalent regular grammar.
Algorithm 2. if the regular expression is simply 1, we can show that G, with one production rule S (where S is the start symbol), is an equivalent regular grammar.
Algorithm 3. if the regular expression is simply a (a being any terminal), we can show that G, with one production ruleS->a (where S is the start symbol), is an equivalent regular grammar.
Algorithm 4. if the regular expression is of the form e+f, where both e and f are regular expressions, use this algorithm to contruct two regular grammars G1 and G2 equivalent to e and f respectively. Now use regular grammar union on these two grammars to construct a regular grammar G which is equivalent to e+f.
Algorithm 5. if the regular expression is of the form ef, where both e and f are regular expressions, use this algorithm to contruct two regular grammars G1 and G2 equivalent to e and f respectively. Now use regular grammar catenation on these two grammars to construct a regular grammar G which is equivalent to ef.
Algorithm 6. if the regular expression is of the form e*, where e is a regular expression, use this algorithm to construct a regular grammar G equivalent to e. Now use regular grammar Kleene star closure to construct a regular grammar G' which is equivalent to e*.
Algorithm 7. if the regular expression is of the forme+, where e is a regular expression, use this algorithm to construct a regular grammar G equivalent to e. Now use regular grammar Kleene plus closure to construct a regular grammar G' which is equivalent to e+.
Example 1 • Consider the regular expression (a + b)*a. We will now construct a regular grammar for this regular expression. For every terminal symbol a, we create a regular grammar with the rule S->a, start symbol S. We then apply the transformations to these regular grammars, progressively constructing the regular grammar.
Example 1 • First consider the expression a + b. We create two regular grammars: S1-> aandS2-> bwhereS1 and S2 are the start symbols. Clearly, these grammars recognize the regular expressionsa and b respectively.
Example 1 • Now, we apply the union transformation for regular grammars to get:S3->a | bS1->aS2->b where S3 is the start symbol. This grammar obviously recognizesa + b.
Example 1 • Next, we consider the expression (a + b)*.We already have a regular grammar for (a + b),so now we apply the Kleene star transformation on the regular grammar:
S4->a | b | ε S3-> a | b S1-> aS3 S2 -> bS3where S4 is the start symbol.
Example 1 • Recall that we need a regular grammar that recognizes(a + b)*a. We thus consider again the regular expression a. Again, we create a regular grammar that describes the language:S5->awhere S5 is the start symbol.
Example 1 • We now construct the catenation of the regular grammar describing (a + b)* together with this one. We simply apply the transformation that catenates two regular grammars, to get:
Algorithm S4->a | b | ε S3->aS5 | bS5 S1->aS3 S2->bS3 S5->a where S4 is the start symbol.
Algorithm • This regular grammar is equivalent to the regular expression (a + b)*a.
Example 2 • Please check the website in the references.
References • http://www.inrialpes.fr/vasy/people/Gordon.Pace/Research/Software/Relic/Transformations/RE/toRG.html