There is yet another way to finitely represent a regular set: by a grammar—which will naturally be called a regular grammar. To motivate the core idea behind grammars, consider, for example, the (inductive) definition of formulae ( 2.11.0.27 ). Moreover, to simplify matters, let us stay in the Boolean domain—that is, we will include only the connectives ¬ and ∨ but no quantifiers—and we will also adopt as atomic formulae the set of Boolean variables, 121 generated by the symbol p with or without primes. Thus, the atomic formulae include
p, p′, p‴, p (n)
where p (n) indicates p with n primes
The alphabet over which we build these simplified well-formed (Boolean) formulae is
(,), ¬, ∨, p, p′, p″, p‴ , . . .
In the inductive clauses of 2.11.0.27 we have included “if
and
are formulae, then so is (
∨
)”. In words this says that
One way to get a “complicated” formula is to take two formulae, and join them
via a “∨”, adding outermost brackets after that .
This generates this idea: Why not use a metavariable , named “〈formula〉”, to stand for any formula , generically, and retell the above italicized sentence as
〈formula〉 ::= (〈formula〉 ∨ 〈formula〉)
The above is an instance of a grammar rule (or grammar production ) in so-called Backus-Naur Form (or BNF ). The symbol “::=” we read as “is defined as”. The syntactic variable 〈formula〉 that names any formula is also called a nonterminal symbol or just a nonterminal . Symbols such as (,), and ∨ that name themselves are called terminal symbols , or just terminals .
A grammar is a finite set of BNF productions. Our entire grammar for the set of formulae has the structure below.
〈formula〉 ::= (〈formula〉 ∨ 〈formula〉)
〈formula〉 ::= (¬ 〈formula〉)
〈formula〉 ::= p
〈formula〉 ::= p′
〈formula〉 ::= p ″
〈formula〉 ::= p ‴
Hmm. The above listing of rules is infinite . How can we make it finite? By finitely generating the variables, rather than taking them from a ready to use, off-the-shelf infinite set! So let us use the nonterminal 〈var〉 to name any variable. A straightforward recursive definition, expressed as BNF rules, for this new metavariable is
〈var〉 ::= p
〈var〉 ::= 〈var〉′
BNF offers another notational convenience: We can group rules with the same left hand side—that is, rules for the same nonterminal —using the separator
to separate right hand side alternatives. “
” is pronounced “or”.
The final grammar therefore is over the alphabet
and its rules are
(1) 〈formula〉 ::= 〈var〉
(〈formula〉 ∨ 〈formula〉)
(¬ 〈formula〉)
(2) 〈var〉 ::= p
〈var〉′
Every one of the two (grouped) rules defines a class of objects in the order listed: formulae, and variables. Since the “primary” object we are interested in defining is a formula , the nonterminal 〈formula〉 is the “important one”. It is called the start (nonterminal) symbol of the grammar.
“Statically”, the rules (1)–(2) recast Definition 2.11.0.27 (after the adopted above simplifications) in a new notation. Clearly, (1) says that a formula is either a variable, or is formed by using simpler (shorter) formulae. Option one is to use two simpler formulae, concatenate them so that we “sandwich” a “∨” between them, and then enclose the result in brackets. Option two is to use one simpler formula, prefix it with a “¬”, and attach outermost brackets to the result. The subsidiary rule tells us how to finitely generate (define) variables.
Grammars do more than “statically” recasting in new notation inductive definitions such as 2.11.0.27 , as we detail below.
Suppose we want to verify that a string over (*) is a formula. We call such verification parsing . To fix ideas, say, that we are checking, specifically,
We start by assuming that it is a formula and proceed to verify this assumption using the rules (1)–(2).
To begin with, we view that the string (3) is “covered” by the nonterminal 〈formula〉. At this highest level of abstraction (lowest level of detail), we just echo our working assumption.
We next refine this “covering” using a well-chosen rule from group (1). The choice is dictated here by the first couple of symbols in (3): The refinement or expansion of the original covering cannot be either 〈var〉 or the right hand side of the third rule in group (1), for the former requires the string to start with a “ p ” and the latter with a “(¬”.
The refinement yields the covering
We next, say, refine the left occurrence of “〈formula〉” in (4), and the “goal”, (3), suggests that we use the first rule of group (1), to obtain
Let us refine “〈var〉”. The goal suggest we choose the first rule in group (2) to obtain
We have uncovered quite a bit of (3)!
Next we use the third rule of (1) to refine “〈formula〉” in (7). Again, the context “(¬” helps us to choose. We obtain
This time we use group (1)—〈formula〉 ::= 〈var〉—to refine the above into
and then
using (2), second rule. One more application of (2), first rule , allows us to uncover our entire goal! So, indeed, (3) is a formula.
These refinements involved, in each case, the rewriting (replacing) of some occurrence of a nonterminal in some string by a right hand side of a rule for said nonterminal. For that reason we also call grammar rules rewriting rules .
We have a better, symbolic, way to depict the several acts of rewriting (refinement): Let us first introduce the relation “
”, pronounced yields , between strings over the mixed alphabet
defined for all α , α′ over the alphabet (**) by
Then we can summarize our rewriting sequence, from 〈formula〉 to (3), via (9), as
〈formula〉
(〈formula〉 ∨ 〈formula〉)
(〈var〉 ∨ 〈formula〉)
![]()
(p ∨ 〈formula〉)( p ∨ (¬ 〈formula〉))
( p ∨ ( ¬ 〈var〉))
![]()
( p ∨ (¬ 〈var〉′))( p ∨ (¬′))
The sequence above is a derivation .
While our goal-driven choices worked deterministically in this example, it is not guaranteed that an arbitrary grammar allows us the luxury of deterministic choices during every derivation that attempts to verify that a given string is derivable (parsing). In general, we may have to “guess” which rule to apply in each step in order to reach our goal. Such guessing will usually be implemented by backtracking, that is, every time we make a choice that further down gets us stuck (a “bad” choice), then we go all the way back to said bad choice and make a different choice from among the available options, until we get it right—or until we have verified that the string is not derivable.
While BNF is very helpful in the definition of specific programming languages via a grammar, in particular, allowing us to utilize mnemonics for nonterminals, such as 〈if stmt〉, 〈begin block〉, etc., 122 the metatheory of grammars benefits from a more abstract, and simpler, notational convention as detailed in the definition below.
3.4.0.6 Definition. (Grammars) A grammar G = 〈
, Σ, S,
〉 is a toolbox where
is a finite set of nonterminals , Σ is a finite set of terminals , where
∩ Σ =
.
S ∈
is the start symbol , and
is the set of rewriting rules or productions . The symbols in
are denoted by upper case ( single ) Latin letters. The symbols of Σ are single-character symbols that are not Latin capital letters. The rules are of the form A → α , where α ∈ (
∪ Σ)*. Note that in the domain of the metatheory of grammars we have simplified the BNF notation “::=” to “→”.
Strings over
∪ Σ will have the generic names α, β, γ, δ with or without primes, while strings over Σ will have the generic names x, y, z, u, v, w with or without primes. Conistent with this convention, symbols of Σ—if they are not non-letters, such as 0, 1, 9, †, #, etc.—will be denoted by a, b, c, d with or without primes.
We define a relation
on (
∪ Σ)*, pronounced yields , by α
β iff, for some γ and γ′ , we have α = γAγ′ and β = γδγ′ were A → δ is in
.
The language generated by G —in symbols L(G) —is the set { x ∈ Σ* : S
* x }. 123
An α such that S
* α is called a sentential form . A chain
A
α 1
α 2
α 3 . . .
α
is a derivation of α from A . If A is the start symbol, then we just say “a derivation”.
If α ∈ Σ*—in which case we prefer to write something like x instead of α —then it is called a sentence (of G ). Thus, L ( G ) is the set of all sentences of G .
In metatheoretical discussions we will feel free to use the BNF “
“ to group together rules with the same left hand side.
3.4.0.7 Definition. The grammar defined above is called a type-2 or context free grammar, for short, CFG. The corresponding language L ( G ) is said to be a type-2 or context free language , for short CFL. The type of grammar is determined by the fact that the left hand side of every rule is a single nonterminal .
If the right hand side of every rule of a CFG is restricted to be of any of the four forms
, a , B , or aB , where- a ∈ Σ and B ∈
, then the grammar is said to be a type-3 or regular grammar .
CFGs and CFLs will be studied further in Section 4.3.
3.4.0.8 Example. Let
= { S }, Σ = {′} and the rules are S → ′
′ S . This grammar is regular. L ( G ) is clearly
Σ + = {′, ″, ‴, . . .}
that is, the set of all non-empty strings of primes.
How “clearly”? Well, by L ( G )’s definition, L ( G ) ⊆ Σ*, but no rule can lead to the generation of
(the length of right hand sides of the two rules is ≥ 1). Thus L ( G ) ⊆ Σ + .
For the converse inclusion, Σ + ⊆ L ( G ), we do induction on the length of strings x ∈ Σ + . For the basis, | x | = 1, we are looking at x = ′. But S
′. Assume now (I.H.) that if | x | = n , then we have S
* x . What about a y of length n + 1? Well, y = ′ x , with | x | = n . Thus,
3.4.1 From a Regular Grammar to a NFA and Back
In this section we, post facto , justify the terminology regular grammar and regular language by proving the two theorems below, which relate the descriptive powers of NFA and type-3 grammars.
3.4.1.1 Lemma. A sentential form α ∉ Σ* of a regular grammar G = 〈
, Σ, S ,
〉 has the form xA where x ∈ Σ* and A is a nonterminal .
Proof Induction on the length n of the derivation S
n α(cf. 1.2.0.21 ). The basis, for n = 0, is immediate since then S = α . We take the obvious I.H. and consider
Now, (1) can be rewritten, using the I.H. , as
S
n xA
xaB
where a ∈ Σ ∪ {
} and B is a nonterminal.
Pause. Why can it not be that the ( n + 1)-st step used a rule of the type A → a , where a ∈ Σ ∪ {
}?
We are done.
3.4.1.2 Theorem. For any regular grammar G = 〈
, Σ, S ,
〉 we can construct a NFA M = 〈 Q , Σ, q 0 , δ , F 〉 such that L ( G ) = L ( M ).
Proof . We take Q =
∪ {
}, where the “checkmark”,
, is a new symbol (not in
∪ Σ) that names the unique accepting state of M —that is, F = {
}. We take q 0 = S . The transitions (the δ ) are chosen as follows. We have a transition
(1)
iff A → a is a rule, where a ∈ Σ ∪ {
}
(2)
iff A → B is a rule
(3)
if A → aB is a rule, where a ∈ Σ
We will show that L ( G ) = L ( M ). First we address ⊆. We will assume that x ∈ L ( G ) and prove that x ∈ L ( M ). As it is natural to do induction on the derivation length of x , it is more convenient to show instead (cf. preceding lemma) that, for any y ∈ Σ* and A ∈
,
if S
n yA , then there is a path in M , labeled y , from S to A (4)
The case for n = 1 means that we have a grammar rule S → yA , where y ∈ Σ ∪ {
} and A is a nonterminal. If y is a terminal then we have a NFA move of type (3) above, else we have one of type (2). We have settled the basis since in either case we have a one-edge path from S to A labeled y .
We now fix n and take (4) as the I.H. Let us next consider a derivation of length n + 1:
S
n yA
yaB
By the I.H. there is a path in M , labeled y , from S to A . This path continues from A to B , with an edge labeled a ((2) or (3) above). Overall, we have a path from S to B labeled ya . We are done in our task.
If now S
* x ∈ Σ*, then (as S ≠ x ) we must have
S
* yA
ya
where x = ya and a ∈ Σ ∪ {
}. The last step of the derivation must be due to the rule A → a . By what we proved above we have a path labeled y in M from S to A . The rule A → a contributes to the tail-end of the path the edge (1) above. The augmented path is labeled x and it ends at the accepting state. Done.
For the other direction, let
We want to show that x ∈ L ( G ). Correspondingly with the previous direction, we will prove the following converse of (4), by induction on computation path length in M , for all y ∈ Σ* and A ∈ Q − F .
For n = 1, there is one edge in M
thus y = a ∈ Σ or y =
. By (2) and (3) above, S → yA is a rule, thus S
yA . We next fix the n and take (6) as the I.H. Suppose now that there is a path of length n + 1 from S to A in M , labeled x . Let a be the label of the rightmost edge of the path, connecting B to A , where a ∈ Σ ∪ {
}. By (2) and (3),
and, by I.H., we have S
* yB , where x = ya . Since by (7) we have B
aA , we get a derivation
S
* yB
yaA
as needed.
With the induction completed, let us return to the hypothesis (5). It means that we have a path from S to
, labeled x . If the path has just one edge, then we have S → x as a rule [cf. (1) above]. Hence S
x as needed. If the path has length at least 2, then its nodes are S , . . . , A ,
, for some A ∈ Q − F . Say, the edge from A to
is labeled a ∈ Σ ∪ {
}—thus A → a is a rule. By what we just proved by induction, it is S
* yA , where x = ya , and, of course, yA
ya . Thus S
* x and we are done.
3.4.1.3 Theorem. For any NFA M = 〈 Q , Σ, q 0 , δ , F 〉 we can construct a regular grammar G = 〈
, Σ, S ,
〉 such that L ( G ) = L ( M ).
Proof . First off, without loss of generality the names of the rejecting states are capital Latin letters, with S as the start state that was generically denoted as “ q 0 ” in the toolbox of M above. We also assume that we have “preprocessed” the given NFA by adding a new, unique accepting state that we name
(cf. Remark 3.3.1.1 ). We do so even if the original NFA had a unique accepting state .
The construction of G and the proof that it behaves as stated is entirely analogous to that of 3.4.1.2 . Thus, we take
= Q − F .
The rules of G are chosen exactly as in (1)–(3) in the preceding proof, reordering the two sides of each “iff” to emphasize that it is the NFA that is given this time: We have a rule
(1) A → a is a rule, where a ∈ Σ ∪ {
}, iff
is a transition
(2) A → B is a rule iff
is a transition
(3) A → aB is a rule, where a ∈ Σ, iff
is a transition
The proof that L ( G ) = L ( M ) can now be read off the proof of 3.4.1.2 .
Since every NFA is also a FA, the above construction, 3.4.1.3 , works if we start with a FA.
3.4.1.4 Example. Let us construct a regular grammar for the FA of Example 3.1.1.4 , which we reproduce below:
To fix ideas, let us assume that we have designated q 0 as accepting and q 1 is rejecting.
Preprocessing gives the NFA below:
The proof of 3.4.1.3 gives us the rules
3.4.2 Epilogue on Regular Languages
We look at some ad hoc additional results on regular languages, now that we have three equivalent ways to finitely define them: automata, regular expressions, and regular grammars. We will present these in the format of examples and will make no effort to be “complete” given our philosophy that was outlined in the Preface of this volume. The reader who wants to explore more on automata and their languages may wish to refer to any of Sipser (1997), Lewis and Papadimitriou (1998), and Hopcroft et al . (2007).
3.4.2.1 Example. Every regular language L over an alphabet Σ is closed under taking prefixes of its members. That is, the language
is also regular. To show regularity, we will look for an α or an M or a G and show that the language in (1) is finitely defined by one of these. Which one we choose is simply a matter of conveniewnce!
So let us take the regular expression avenue toward the proof, and translate the assumption to
For some α over Σ, we have L = L ( α )
By induction on the formation of α (cf. 3.3.0.7 and 1.6.0.13 ) we argue that we can find ( for any ) α such that L = L ( α ) a
such that Init ( L ) = L (
).
Basis .
-
α = a ∈ Σ. Then
= α +
*. 124 -
2. α =
. Then
= α .
Induction steps . If α = β + γ , then
=
+
.
If α = β · γ, then
=
+ β ·
.
If α = β *, then
= β *
.
Another closure property is left as an exercise ( Exercise 3.5.30 ).
3.4.2.2 Example. Given a regular language in one of the three ways that we know of, can we check algorithmically whether it is equal to
? Yes! We use the FA formalism to describe it (if it is not given that way we can certainly convert to this representation) and then check to see if there is any path from the start state to any accepting state. The language is empty if no such path exists.
3.4.2.3 Example. Given a regular language, L , can we check algorithmically whether it is finite or not?
Yes! We assume without loss of generality that the language has been given as L = L ( M ) for some FA M . This allows us to determine a pumping constant C (it equals the number of states in M ; cf. proof of 3.1.3.1 ).
The language is infinite iff it contains strings with lengths greater than or equal to C . But how can we tell whether it does?
Well, if we have infinitely many strings in L , then L must have strings of arbitrarily large lengths, hence, in particular, a string z of smallest possible length , but nevertheless | z | ≥ 2 C . By 3.1.3.1 , we can write this z as uvw with | uv | ≤ C and such that uv i w ∈ L for i ≥ 0. In particular, uw ∈ L .
As z had least length above 2 C , we have that
Now, adding to 2 C ≤ | uvw | the inequality − C ≤ −| v | [cf. 3.1.3.1 , (3)] we get
So, if L is infinite, then we will be able to find an x ∈ L of length—by (1) and (2)
Of course we will have to examine only finitely many strings , those in the lengthrange (3), to find such an x .
But is finding an x in the language L , in the length-range (3), sufficient in order for us to proclaim that L is infinite? Yes, because the left inequality of (3) and 3.1.3.1 imply that this x “pumps”; it leads to infinitely many strings in L . So this is the algorithm:
Using the FA M, check all strings in the range (3) for membership in L. If any is found, then L is infinite; otherwise it is finite .