Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Regular grammar

From Wikipedia, the free encyclopedia
Type of formal grammar

Intheoretical computer science andformal language theory, aregular grammar is agrammar that isright-regular orleft-regular.While their exact definition varies from textbook to textbook, they all require that

Every regular grammar describes aregular language.

Strictly regular grammars

[edit]

Aright-regular grammar (also calledright-linear grammar) is a formal grammar (N, Σ,P,S) in which all production rules inP are of one of the following forms:

  1. Aa
  2. AaB
  3. A → ε

whereA,B,SN are non-terminal symbols,a ∈ Σ is a terminal symbol, and ε denotes theempty string, i.e. the string of length 0.S is called the start symbol.

In aleft-regular grammar, (also calledleft-linear grammar), all rules obey the forms

  1. Aa
  2. ABa
  3. A → ε

The language described by a given grammar is the set of all strings that contain only terminal symbols and can be derived from the start symbol by repeated application of production rules. Two grammars are calledweakly equivalent if they describe the same language.

Rules of both kinds must not be mixed; for example, the grammar with rule set {SaT,TSb, S→ε } is not regular, and describes the language{aibi:iN}{\displaystyle \{a^{i}b^{i}:i\in \mathbb {N} \}}, which is not regular, either.

Some textbooks and articles disallow empty production rules, and assume that the empty string is not present in languages.

Extended regular grammars

[edit]

Anextended right-regular grammar is one in which all rules obey one of

  1. Aw, whereA is a non-terminal inN andw is in a (possibly empty) string of terminals Σ*
  2. AwB, whereA andB are inN andw is in Σ*.

Some authors call this type of grammar aright-regular grammar (orright-linear grammar)[1] and the type above astrictly right-regular grammar (orstrictly right-linear grammar).[2]

Anextended left-regular grammar is one in which all rules obey one of

  1. Aw, whereA is a non-terminal inN andw is in Σ*
  2. ABw, whereA andB are inN andw is in Σ*.

Examples

[edit]

An example of a right-regular grammarG withN = {S, A}, Σ = {a, b, c},P consists of the following rules

S → aS
S → bA
A → ε
A → cA

and S is the start symbol. This grammar describes the same language as theregular expression a*bc*, viz. the set of all strings consisting of arbitrarily many "a"s, followed by a single "b", followed by arbitrarily many "c"s.

A somewhat longer but more explicit extended right-regular grammarG for the same regular expression is given byN = {S, A, B, C}, Σ = {a, b, c}, whereP consists of the following rules:

S → A
A → aA
A → B
B → bC
C → ε
C → cC

...where each uppercase letter corresponds to phrases starting at the next position in the regular expression.

As an example from the area of programming languages, the set of all strings denoting a floating point number can be described by an extended right-regular grammarG withN = {S,A,B,C,D,E,F}, Σ = {0,1,2,3,4,5,6,7,8,9,+,−,.,e}, where S is the start symbol, andP consists of the following rules:

S → +A      A → 0A      B → 0C      C → 0C      D → +E      E → 0F      F → 0F
S → −AA → 1AB → 1CC → 1CD → −EE → 1FF → 1F
S → AA → 2AB → 2CC → 2CD → EE → 2FF → 2F
A → 3AB → 3CC → 3CE → 3FF → 3F
A → 4AB → 4CC → 4CE → 4FF → 4F
A → 5AB → 5CC → 5CE → 5FF → 5F
A → 6AB → 6CC → 6CE → 6FF → 6F
A → 7AB → 7CC → 7CE → 7FF → 7F
A → 8AB → 8CC → 8CE → 8FF → 8F
A → 9AB → 9CC → 9CE → 9FF → 9F
A → .BC → eDF → ε
A → BC → ε

Expressive power

[edit]

There is a direct one-to-one correspondence between the rules of a (strictly) right-regular grammar and those of anondeterministic finite automaton, such that the grammar generates exactly the language the automaton accepts.[3] Hence, the right-regular grammars generate exactly allregular languages. The left-regular grammars describe the reverses of all such languages, that is, exactly the regular languages as well.

Every strict right-regular grammar is extended right-regular, while every extended right-regular grammar can be made strict by inserting new non-terminals, such that the result generates the same language; hence, extended right-regular grammars generate the regular languages as well. Analogously, so do the extended left-regular grammars.

If empty productions are disallowed, only all regular languages that do not include the empty string can be generated.[4]

While regular grammars can only describe regular languages, theconverse is not true: regular languages can also be described by non-regular grammars.

Mixing left-regular and right-regular rules

[edit]

If mixing of left-regular and right-regular rules is allowed, we still have alinear grammar, but not necessarily a regular one.What is more, such a grammar need not generate a regular language: all linear grammars can be easily brought into this form, and hence, such grammars can generate exactly alllinear languages, including non-regular ones.

For instance, the grammarG withN = {S, A}, Σ = {a, b},P with start symbolS and rules

S → aA
A → Sb
S → ε

generates{aibi:i0}{\displaystyle \{a^{i}b^{i}:i\geq 0\}}, the paradigmatic non-regular linear language.

See also

[edit]

References

[edit]
  1. ^John E. Hopcroft and Jeffrey D. Ullman (1979).Introduction to Automata Theory, Languages, and Computation. Reading/MA: Addison-Wesley.ISBN 0-201-02988-X. Here: p.217 (left, right-regular grammars as subclasses ofcontext-free grammars), p.79 (context-free grammars)
  2. ^Hopcroft and Ullman 1979 (p.229, exercise 9.2) call it a normal form for right-linear grammars.
  3. ^Hopcroft and Ullman 1979, p.218-219, Theorem 9.1 and 9.2
  4. ^Hopcroft and Ullman 1979, p.229, Exercise 9.2

Further reading

[edit]
Each category of languages, except those marked by a*, is aproper subset of the category directly above it.Any language in each category is generated by a grammar and by an automaton in the category in the same line.
String metric
String-searching algorithm
Multiple string searching
Regular expression
Sequence alignment
Data structure
Other
Retrieved from "https://en.wikipedia.org/w/index.php?title=Regular_grammar&oldid=1247335286"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp