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

images

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 images and images are formulae, then so is ( images ∨ images )”. 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.

images 〈formula〉 ::= (〈formula〉 ∨ 〈formula〉)
images 〈formula〉 ::= (¬ 〈formula〉)
images 〈formula〉 ::= p
images 〈formula〉 ::= p′
images 〈formula〉 ::= p ″
images 〈formula〉 ::= p ‴
     images

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 images to separate right hand side alternatives. “ images ” is pronounced “or”.

The final grammar therefore is over the alphabet

images

and its rules are

(1) 〈formula〉 ::= 〈var〉 images (〈formula〉 ∨ 〈formula〉) images (¬ 〈formula〉)

(2) 〈var〉 ::= p images 〈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.

images Grammars do more than “statically” recasting in new notation inductive definitions such as 2.11.0.27 , as we detail below. images

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,

images

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

images

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

images

Let us refine “〈var〉”. The goal suggest we choose the first rule in group (2) to obtain

images

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

images

This time we use group (1)—〈formula〉 ::= 〈var〉—to refine the above into

images

and then

images

using (2), second rule. One more application of (2), first rule , allows us to uncover our entire goal! So, indeed, (3) is a formula.

images 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 . Images

We have a better, symbolic, way to depict the several acts of rewriting (refinement): Let us first introduce the relation “ images ”, pronounced yields , between strings over the mixed alphabet

images

defined for all α , α′ over the alphabet (**) by

images

Then we can summarize our rewriting sequence, from 〈formula〉 to (3), via (9), as

〈formula〉 images (〈formula〉 ∨ 〈formula〉) images (〈var〉 ∨ 〈formula〉) images
(p ∨ 〈formula〉) images ( p ∨ (¬ 〈formula〉)) images ( p ∨ ( ¬ 〈var〉)) images
( p ∨ (¬ 〈var〉′)) images ( p ∨ (¬′))

The sequence above is a derivation .

Images 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. Images

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 = 〈 images , Σ, S, images 〉 is a toolbox where images is a finite set of nonterminals , Σ is a finite set of terminals , where images ∩ Σ = images .

S ∈ images is the start symbol , and images is the set of rewriting rules or productions . The symbols in images 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 α ∈ ( images ∪ Σ)*. Note that in the domain of the metatheory of grammars we have simplified the BNF notation “::=” to “→”.

Strings over images ∪ Σ 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 images on ( images ∪ Σ)*, pronounced yields , by α images β iff, for some γ and γ′ , we have α = γAγ′ and β = γδγ′ were A → δ is in images .

The language generated by G —in symbols L(G) —is the set { x ∈ Σ* : S images * x }. 123

An α such that S images * α is called a sentential form . A chain

A images α 1 images α 2 images α 3 . . . images α

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 . images

Images In metatheoretical discussions we will feel free to use the BNF “ images “ to group together rules with the same left hand side. Images

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 images , a , B , or aB , where- a ∈ Σ and B ∈ images , then the grammar is said to be a type-3 or regular grammar . images

CFGs and CFLs will be studied further in Section 4.3.

3.4.0.8 Example. Let images = { S }, Σ = {′} and the rules are S → ′ images ′ 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 images (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 images ′. Assume now (I.H.) that if | x | = n , then we have S images * x . What about a y of length n + 1? Well, y = ′ x , with | x | = n . Thus,

images

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 = 〈 images , Σ, S , images 〉 has the form xA where x ∈ Σ* and A is a nonterminal .

Proof Induction on the length n of the derivation S images n α(cf. 1.2.0.21 ). The basis, for n = 0, is immediate since then S = α . We take the obvious I.H. and consider

images

Now, (1) can be rewritten, using the I.H. , as

S images n xA images xaB

where a ∈ Σ ∪ { images } 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 ∈ Σ ∪ { images }? images

We are done. images

3.4.1.2 Theorem. For any regular grammar G = 〈 images , Σ, S , images 〉 we can construct a NFA M = 〈 Q , Σ, q 0 , δ , F 〉 such that L ( G ) = L ( M ).

Proof . We take Q = images ∪ { images }, where the “checkmark”, images , is a new symbol (not in images ∪ Σ) that names the unique accepting state of M —that is, F = { images }. We take q 0 = S . The transitions (the δ ) are chosen as follows. We have a transition

(1)

images

iff A → a is a rule, where a ∈ Σ ∪ { images }

(2)

images

iff A → B is a rule

(3)

images

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 ∈ images ,

if S images 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 ∈ Σ ∪ { images } 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 images n yA images 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 images * x ∈ Σ*, then (as S ≠ x ) we must have

S images * yA images ya

where x = ya and a ∈ Σ ∪ { images }. 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

images

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 .

images

For n = 1, there is one edge in M

images

thus y = a ∈ Σ or y = images . By (2) and (3) above, S → yA is a rule, thus S images 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 ∈ Σ ∪ { images }. By (2) and (3),

images

and, by I.H., we have S images * yB , where x = ya . Since by (7) we have B images aA , we get a derivation

S images * yB images yaA

as needed.

With the induction completed, let us return to the hypothesis (5). It means that we have a path from S to images , labeled x . If the path has just one edge, then we have S → x as a rule [cf. (1) above]. Hence S images x as needed. If the path has length at least 2, then its nodes are S , . . . , A , images , for some A ∈ Q − F . Say, the edge from A to images is labeled a ∈ Σ ∪ { images }—thus A → a is a rule. By what we just proved by induction, it is S images * yA , where x = ya , and, of course, yA images ya . Thus S images * x and we are done. images

3.4.1.3 Theorem. For any NFA M = 〈 Q , Σ, q 0 , δ , F 〉 we can construct a regular grammar G = 〈 images , Σ, S , images 〉 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 images (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 images = 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 ∈ Σ ∪ { images }, iff

images

is a transition

(2) A → B is a rule iff

images

is a transition

(3) A → aB is a rule, where a ∈ Σ, iff

images

is a transition

The proof that L ( G ) = L ( M ) can now be read off the proof of 3.4.1.2 . images

Images Since every NFA is also a FA, the above construction, 3.4.1.3 , works if we start with a FA. Images

3.4.1.4 Example. Let us construct a regular grammar for the FA of Example 3.1.1.4 , which we reproduce below:

images

To fix ideas, let us assume that we have designated q 0 as accepting and q 1 is rejecting.

Preprocessing gives the NFA below:

images

The proof of 3.4.1.3 gives us the rules

images

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

images

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 images such that Init ( L ) = L ( images ).

Basis .

  1. α = a ∈ Σ. Then images = α + images *. 124
  2. 2. α = images . Then images = α .

Induction steps . If α = β + γ , then images = images + images .
     If α = β · γ, then images = images + β · images .
     If α = β *, then images = β * images .

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 images ? 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. images

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

images

Now, adding to 2 C ≤ | uvw | the inequality − C ≤ −| v | [cf. 3.1.3.1 , (3)] we get

images

So, if L is infinite, then we will be able to find an x ∈ L of length—by (1) and (2)

images

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 . images