Compilation principle-mutual conversion between regular grammar and regular expression


Preface

In the process of lexical analysis, if each type of word is regarded as a language, the morphology of most words can be described by regular grammar. In addition to regular grammars, regular expressions can also be used to describe words accordingly. Regular grammars and regular expressions have the same ability and can be transformed into each other. Regular expressions are more intuitive than regular grammar, and sometimes regular expressions are preferred to express regular languages.


1. Regular grammar

1. Definition

Regular grammar is explained in this article ( Principle of Compilation-Definition and Classification of Grammar ), and I will talk about it again here:

  1. In the regular grammar G = (V, T, P, S), for ∀α —> β∈P, α β has the form A —> w or A —> wB(A —> w or A —> Bw), Among them A, B ∈ V, w ∈ T+.
  2. Regular grammar describes the regular language on T.

2. Examples

Example: Grammar of identifiers in lexical analysis:

example


Two, regular expression

1. Definition

Definition: Let ∑ be an alphabet, then the regular expression on ∑ and the regular language it represents can be recursively defined as follows:
⑴ Ø is a regular expression on ∑, which represents the empty set;
⑵ ε is on ∑ A regular expression of, which represents the language {ε};
⑶ For ∀a(a∈∑), a is a regular expression on ∑, and the regular language it represents is {a};
⑷ Assuming that r and s are both The regular expressions on ∑, the languages ​​they represent are L® and L(s) respectively, then:

  1. (r) is also a regular expression on ∑, and the language it represents is L( r );
  2. (r|s) is also a regular expression on ∑, and the language it represents is L( r )∪L(s); (and operation)
  3. (r•s) is also a regular expression on ∑, and the language it represents is L(r)L(s); (connection operation)
  4. (r*) is also a regular expression on ∑, the language it represents is (L(r))*; (Kling closure operation)

⑸ The expression constructed using the above rules is a regular expression on ∑.

2. Examples

Example: Regular expression expression of identifier in lexical analysis:

Insert picture description here


Three, conversion rules

1. Convert regular grammar to regular expression

Insert picture description here
The specific conversion steps are:

  1. According to the regular grammar G, the regular expression simultaneous equations are constructed.
    Assuming that the regular grammar G is right linear, and the right part of each production contains only one terminal symbol, there are the following equation construction rules:
    Insert picture description here

  2. Solve the simultaneous equations and find the equivalent regular expression r.
    Use substitution elimination method to eliminate other variables except the start symbol S in the equations one by one, and finally get the solution about the start symbol S.
    The rules for substitution and elimination are as follows:
    Insert picture description here

  3. Seek the result.
    If the final equation for S is the following form,
    S=α1|α2|...|αh
    , delete all αi(1≤i≤n) that still contain grammatical variables on the right side of the equation, and the result obtained is equivalent to G The regular expression.
    If any αi (1≤i≤n) contains grammatical variables, then Ø is a regular expression equivalent to G.

2. Convert regular expressions to regular grammar

Given a regular expression r, construct a regular definition formula as follows, and gradually convert it into a regular grammar.
Introduce the start symbol S, starting from the following regular definition:
S—>r
decompose S—>r into a new regular definition according to the following rules, and introduce new grammatical variables as needed during the decomposition process.

Insert picture description here


Four, conversion example

1. Convert regular grammar to regular expression

Insert picture description here
process:
Insert picture description here

2. Convert regular expressions to regular grammar

Example 1. Conversion of identifier definition:
(1). Introduce S
(2).S→ (|)*
(3). Decompose into
S→A
A→(|)A|ε

Example 2. (a | b) * a (a | b) (a | b)

Converted into regular grammar:
(1).S->Aa|Ab
(2).A->Ba|Bb
(3).B->Ca
(4).C->Ca|Cb|ε


to sum up

Regular expressions are equivalent to regular grammars:
for any regular grammar, there is
a regular expression that defines the same language ; for any regular expression, there is a regular grammar that defines the same language.

Guess you like

Origin blog.csdn.net/weixin_43824348/article/details/111499366