54 Regulr Lnguges nd Finite Automt Find simple regulr expression for the regulr lnguge recognized by this NFA. Hint: Trnsform the NFA into DFA, nd then find the minimum-stte DFA. Chllenge 12. Wht cn you sy bout the regulr lnguge ccepted by DFA in which ll sttes re finl? 11.4 Regulr Lnguge Topics We ve lredy seen chrcteriztions of regulr lnguges by regulr expressions, lnguges ccepted by DFAs, nd lnguges ccepted by NFAs. In this section we ll introduce still nother chrcteriztion of regulr lnguges in terms of certin restricted grmmrs. We ll discuss some properties of regulr lnguges tht cn be used to find lnguges tht re not regulr. Regulr Grmmrs A regulr lnguge cn be described by specil kind of grmmr in which the productions tke certin form. A grmmr is clled regulr grmmr if ech production tkes one of the following forms, where the cpitl letters re nonterminls nd w is nonempty string of terminls: S! ", S! w, S! T, S! wt. The thing to keep in mind here is tht only one nonterminl cn pper on the right side of production, nd it must pper t the right end of the right side. For exmple, the productions A! Bc nd S! TU re re not prt of regulr grmmr. But the production A! bca is OK. The most importnt spect of grmmr writing is knowledge of the lnguge under discussion. We should lso remember tht grmmrs re not unique. So we shouldn t be surprised when two people come up with two different grmmrs for the sme lnguge. To strt things off, we ll look t few regulr grmmrs for some simple regulr lnguges. Ech line of the following list describes regulr lnguge in terms of regulr expression nd regulr grmmr. As you look through the following list, cover up the grmmr column with your hnd nd try to dis-
11.4 Regulr Lnguge Topics 55 cover your own version of regulr grmmr for the regulr lnguge of ech regulr expression. Regulr Expression * S! " S Regulr Grmmr ( + b)* S! " S bs * + b* S! " A B A! A B! b bb *b b* (b)* S! b S S! ba A! " A S! " bs The lst three exmples in the preceding list involve products of lnguges. Most problems occur in trying to construct regulr grmmr for lnguge tht is the product of lnguges. Let s look t n exmple to see whether we cn get some insight into constructing such grmmrs. Suppose we wnt to construct regulr grmmr for the lnguge of the regulr expression *bc*. First we observe tht the strings of *bc* strt with either the letter or the letter b. We cn represent this property by writing down the following two productions, where S is the strt symbol: S! S b C. These productions llow us to derive strings of the form bc, bc, bc, nd so on. Now ll we need is definition for C to derive the lnguge of c*. The following two productions do the job: C! " c C. Therefore regulr grmmr for *bc* cn be written s follows: S! S b C C! " c C. EXAMPLE 1 Smple Regulr Grmmrs
56 Regulr Lnguges nd Finite Automt We ll consider some regulr lnguges, ll of which consist of strings of s followed by strings of b s. The lrgest lnguge of this form is the lnguge { m b n m, n! N}, which is represented by the regulr expression *b*. A regulr grmmr for this lnguge cn be written s follows: S " # S B B " b bb. Let s look t four sublnguges of { m b n m, n! N} tht re defined by whether ech string contins occurrences of or b. The following list shows ech lnguge together with regulr expression nd regulr grmmr. Lnguge Expression Regulr Grmmr { m b n m! 0 nd n > 0} *bb* S " S B B " b bb. { m b n m > 0 nd n! 0} *b* S " A A " A B B " # bb. { m b n m > 0 nd n > 0} *bb* S " A A " A B B " b bb. { m b n m > 0 or n > 0} *b* + *bb* S " A bb A " # A B B " # b B. $ Any regulr lnguge hs regulr grmmr; conversely, ny regulr grmmr genertes regulr lnguge. To see this, we ll give two lgorithms: one to trnsform n NFA to regulr grmmr nd the other to trnsform regulr grmmr to n NFA, where the lnguge ccepted by the NFA is identicl to the lnguge generted by the regulr grmmr. NFA to Regulr Grmmr (11.11) Perform the following steps to construct regulr grmmr tht genertes the lnguge of given NFA: 1. Renme the sttes to set of uppercse letters. 2. The strt symbol is the NFA s strt stte. 3. For ech stte trnsition from I to J lbeled with, crete the production I " J.
11.4 Regulr Lnguge Topics 57 4. For ech stte trnsition from I to J lbeled with!, crete the production I " J. 5. For ech finl stte K, crete the production K "!. It s esy to see tht the lnguge of the NFA nd the lnguge of the constructed grmmr re the sme. Just notice tht ech stte trnsition in the NFA corresponds exctly with production in the grmmr so tht the cceptnce pth in the NFA for some string corresponds to derivtion by the grmmr for the sme string. Let s do n exmple. EXAMPLE 2 From NFA to Regulr Grmmr Let s see how (11.11) trnsforms the following NFA into regulr grmmr: Strt S I b K! J The lgorithm tkes this NFA nd constructs the following regulr grmmr with strt symbol S: S " I J I " bk J " J K K "!. For exmple, to ccept the string, the NFA follows the pth S, J, J, K with edges lbeled!,,, respectively. The grmmr derives this string with the following sequence of productions: S " J, J " J, J " K, K "!. # Now let s look t the converse problem of constructing n NFA from regulr grmmr. For the opposite trnsformtion we ll first tke regulr grmmr nd rewrite it so tht ll the productions hve one of two forms S " x or S " xt, where x is either! or single letter. Let s see how to do this so tht we don t lose ny generlity. For exmple, if we hve production like
58 Regulr Lnguges nd Finite Automt A! bcb, we cn replce it by the following two productions, where C is new nonterminl: A! bc nd C! cb. Now let s look t n lgorithm tht does the job of trnsforming regulr grmmr into n NFA. Regulr Grmmr to NFA (11.12) Perform the following steps to construct n NFA tht ccepts the lnguge of given regulr grmmr: 1. If necessry, trnsform the grmmr so tht ll productions hve the form A! x or A! xb, where x is either single letter or ". 2. The strt stte of the NFA is the grmmr s strt symbol. 3. For ech production I! J, construct stte trnsition from I to J lbeled with the letter. 4. For ech production I! J, construct stte trnsition from I to J lbeled with ". 5. If there re productions of the form I! for some letter, then crete single new stte symbol F. For ech production I!, construct stte trnsition from I to F lbeled with. 6. The finl sttes of the NFA re F together with ll I for which there is production I! ". It s esy to see tht the lnguge of the NFA is the sme s the lnguge of the given regulr grmmr becuse the productions used in the derivtion of ny string correspond exctly with the stte trnsitions on the pth of cceptnce for the string. Here s n exmple. EXAMPLE 3 From Regulr Grmmr to NFA Let s use (11.12) to trnsform the following regulr grmmr into n NFA: S! S bi I! I. Since there is production I!, we need to introduce new stte F, which then gives us the following NFA:
11.4 Regulr Lnguge Topics 59 b Strt S I F! Properties of Regulr Lnguges We need to fce the fct tht not ll lnguges re regulr. To see this, let s look t clssic exmple. Suppose we wnt to find DFA or NFA to recognize the following lnguge. { n b n n! 0}. After few ttempts t trying to find DFA or n NFA or regulr expression or regulr grmmr, we might get the ide tht it cn t be done. But how cn we be sure tht lnguge is not regulr? We cn try to prove it. A proof usully proceeds by ssuming tht the lnguge is regulr nd then trying to find contrdiction of some kind. For exmple, we might be ble to find some property of regulr lnguges tht the given lnguge doesn t stisfy. So let s look t few properties of regulr lnguges. One useful property of regulr lnguges comes from the observtion tht ny DFA for n infinite regulr lnguge must contin loop to recognize infinitely mny strings. For exmple, suppose DFA with four sttes ccepts the 4-letter string bcd. To ccept bcd the DFA must enter five sttes. For exmple, if the sttes of the DFA re numbered 0, 1, 2, nd 3, where 0 is the strt stte nd 3 is the finl stte, then there must be pth through the DFA strting t 0 nd ending t 3 with edges lbeled, b, c, nd d. For exmple, if the pth is 0, 1, 2, 1, 3, then the following grph represents portion of the DFA tht contins the the pth to ccept bcd. 2 Strt c 0 1 b d 3 Of course, the loop 1, 2, 1 cn be trveled ny number of times. For exmple, the pth 0, 1, 2, 1, 2, 1, 3 ccepts the string bcbcd. So the DFA will ccept the strings, d, bcd, bcbcd,..., (bc) n d,.... This is the property tht we wnt to describe. We ll generlize the ide illustrted in our little exmple. Suppose DFA with m sttes recognizes n infinite regulr lnguge. If s is string