Theory of Computing
THE HYPERTEXTBOOK
Chapter 2. Finite State Automata, Regular Languages, Regular Expresions, and Regular Grammars
Section 5.  Regular Grammars

## A Formal Definition of Regular Grammars

OK, it's time now to put on our theoretician's hat, buckle down, and come up with a formal definition for a regular grammar.  We've seen what they look like and have a concept of their form.  Now we need to capture all of that in with formal notation.  We should be getting pretty good at this by now. In any case, we know we can do it!

Let's give it a try.

#### Definition:  Regular Grammar

A regular grammar is a mathematical object, G, with four components, G = (N, Σ, P, S), where

• N is a nonempty, finite set of nonterminal symbols,
• Σ is a finite set of terminal symbols , or alphabet, symbols,
• P is a set of grammar rules, each of one having one of the forms
• AaB
• Aa
• Aε, for A, B ∈ N, aΣ, and ε the empty string, and
• SN is the start symbol. □

Notice that this definition captures all of the components of a regular grammar that we have heretofore identified (heretofor, how often does one get to use that word?).

This definition is pretty useless by itself.  It tells us what a regular grammar is, but not what it does. Sort of like some people we know. The purpose of a regular grammar is to specify how to form grammatically correct strings in the language the grammar represents. All strings in Σ* that can be produced from the start symbol by application of the rules of the grammar are in the language of this grammar, all other strings are not. Let's see how we might capture that in a definition.

First we need yet another definition.  We have talked about how strings in a language can be derived by applying the rules of the grammar to nonterminals, starting with the start symbol S.  We now need to make that concept formal.   We do this by defining the operation "derives in one step " as we have used it in our previous examples and exercises by way of the ⇒ arrow.  For example, we have said that

010110A derives in one step 0101101S

or simply

010110A ⇒ 0101101S

if there is a rule A → 1S in the grammar we are using.  (Notice that because of the way regular grammars are defined, all intermediate strings on the way to a string in the language have the form "zero or more terminals followed by one nonterminal".)

#### Definition: Derivations in a Regular Grammar

Let G = (N, Σ, P, S) be a regular grammar. We will define three different notations that we use for derivations in G:

⇒      (single step derivations)

$\stackrel{k}{⇒}$       (k step derivations)

$\stackrel{*}{⇒}$       (derivations of 0 or more steps)

Part 1.  The definition of ⇒. We define the operation ⇒, called "derives in one step" to be the mapping from strings of the form a1...anA to strings of the form a1... anα, or

a1...anAa1... anα

if and only if there is a rule

Aα

in P, where each of the a1 are terminal symbols in Σ and A is a nonterminal in N.  (Of course, by the definition of regular grammars, α must have one of the forms bB, b, or ε since α is the right hand side of a rule or a regular grammar).

Part 2.  The definition of $\stackrel{k.}{⇒}$ We further define the operation $\stackrel{k}{⇒}$ , called a k-step derivation, to be the extension of ⇒ to k consecutive single-step derivations.  Thus, there is a derivation

a1...anA $\stackrel{k}{⇒}$ a1...an, an+1...an+k-1α

if and only if there are k single-step derivations in G

a1...anAa1...anan+1A1 ⇒ ... ⇒ a1...anan+1...an+k-1Ak-1a1...anan+1...an+k-1α

for k ≥ 0.  If k = 0 this just means that no rules are applied, so the string being derived from does not change:

a1...anA $\stackrel{0}{⇒}$ a1...anA

Part 3.  The definition of $\stackrel{*}{⇒}$. We define $\stackrel{*}{⇒}$ to stand for zero or more single step derivations in sequence. □

Finally, we are able to give a formal definition for the set of all strings generated by a grammar G.  We call this set the language of G.

#### Definition: The Language Generated by a Regular Grammar

Let G = (N, Σ, P, S) be a regular grammar.  We define the language generated by G to be L(G)

L(G) = {w | S $\stackrel{*}{⇒}$ w, where wΣ*

All this formal definition says is that the language of a regular grammar is the set of all strings over the alphabet Σ that can be derived from the start symbol by application of the grammar rules starting with S.  □

Although this appears to be a lot of words and unnecessary mathematical notation, it turns out to be unbelievably convenient.  From now on we can simply discuss the derivation of strings in a regular grammar by way of the production rules (→) and the three notations for derivations (⇒, $\stackrel{k}{⇒}$ , and $\stackrel{*}{⇒}$).