home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / compiler / 1894 < prev    next >
Encoding:
Internet Message Format  |  1992-11-17  |  7.7 KB

  1. Path: sparky!uunet!ogicse!das-news.harvard.edu!spdcc!iecc!compilers-sender
  2. From: pab@cs.arizona.edu (Peter A. Bigot)
  3. Newsgroups: comp.compilers
  4. Subject: Re: Theoretical CFG question
  5. Keywords: parse
  6. Message-ID: <92-11-085@comp.compilers>
  7. Date: 16 Nov 92 20:00:27 GMT
  8. Article-I.D.: comp.92-11-085
  9. References: <92-11-067@comp.compilers>
  10. Sender: compilers-sender@iecc.cambridge.ma.us
  11. Reply-To: pab@cs.arizona.edu (Peter A. Bigot)
  12. Organization: U of Arizona CS Dept, Tucson
  13. Lines: 167
  14. Approved: compilers@iecc.cambridge.ma.us
  15.  
  16. norlin@essex.ecn.uoknor.edu (Norman Lin) writes:
  17. >The problem was:
  18. >                                               *
  19. >For i>=1, let b(i) denote the string in  1(0+1)   that is the
  20. >binary representation of i.
  21. >
  22. >Construct a CFG generating
  23. >          +
  24. >   {0,1,#}    -   {b(1)#b(2)# ... #b(n) | n>=1}
  25.  
  26. I'll refrain from specific comments about a professor who isn't interested
  27. in addressing questions about problems nobody in a class can solve (did
  28. you try asking hir in office hours?).
  29.  
  30. This is problem 4.3 in Hopcroft and Ullman.  Note that although the
  31. complement of the language is not a CFL (pump on it), CFL's aren't closed
  32. under complement; they are closed under union, though, so you can simply
  33. define CFGs for each way a string could fail to be in the form
  34. b_1#b_2#...#b_n.
  35.  
  36. I've attached my own solution to this from a couple years back (this is
  37. ugly TeX before I switched to cleaner LaTeX; the idea should be present).
  38. The conclusion of the class (and the professor), however, was that the
  39. problem was way too hard; and if the even numbered b_i were given in
  40. reverse, the resulting grammar would be feasible (easier to generate
  41. incorrect mirror images).  As noted, the actual grammar below is not
  42. correct.
  43.  
  44. \def\re#1{
  45.         \ifmmode{\rm\bf #1}
  46.         \else{\bf #1}
  47.         \fi}
  48.  
  49. \proofsect {Method}: The idea is to split $L$ into several languages, each
  50. of which can be constructed using a context free grammar, then merging the
  51. grammars to form one for $L$.
  52.  
  53. {\advance\leftskip by 0.25in
  54. \item{1} Strings which do not begin with $b_1$ are not in the language.\par
  55. \item{2} Strings in which one of the purported $b_i$ does not begin with a
  56. $\re1$ are not in the language.\par
  57. \item{3} Strings in which some sequence purported to be $b_i\#b_{i+1}$ has an
  58. error are not in the language.  I assume that $b_i$ is well formed, and the
  59. error is in $b_{i+1}$; without this assumption, correctness is harder to
  60. ensure (variations in $b_i$ may make the supposedly ill-formed $b_{i+1}$
  61. correct).  This set can be further subdivided:\par
  62. {\advance\leftskip by 0.25in
  63. \item{3a} If we assume that $b_i = \re1^n$, then $b_{i+1}$ should be
  64. $\re1\re0^n$.  The error is either in the leading \re1 or in the
  65. sequence of $n$ \re0s of $b_{i+1}$.\par
  66. \item{3b}.  Otherwise, $b_i$ is of the form $\rho01^n$ where $n \ge 0$. 
  67. $b_{i+1}$ should then be $\rho10^n$.  The error can occur in four places:
  68. extra characters before the prefix, or an incorrect character match in
  69. the prefix, the \re1 immediately following the prefix, or the trailing
  70. \re0s.\par
  71. }}
  72.  
  73. \hang A grammar which was derived from this partitioning is attached. 
  74. Nonterminals are enclosed in angle brackets, and productions can be combined
  75. using a vertical bar when the nonterminal being rewritten is the same.
  76. (Although this grammar does not seem to generate strings which are not in $L$,
  77. there are strings of $L$ which it will not generate.  Right idea; wrong
  78. details---this really is a horrible problem.)
  79.  
  80. // Solution to problem 4.3, Hopcroft & Ullman
  81. //
  82. 01#  // symbols in CFG
  83. <S>  // External nonterminals
  84. <S>  // Start symbol
  85.  
  86. <S> -> <L1>     // $w where w != (1 or 1#)
  87.      | <L2>     // #w where w [1] != 1
  88.      | <H><L3><T>       // there is a bi#bj where i+1 <> j in the mix
  89.  
  90. // Odds and ends--sequences of ones, digits, numbers, anything
  91. <O> -> 1<O>
  92.      | //epsilon
  93. <D> -> 1
  94.      | 0
  95. <N> -> <D><N>
  96.      | //epsilon
  97. <Q> -> 0
  98.      | 1
  99.      | #
  100. <W> -> <Q><W>
  101.      | //epsilon
  102.  
  103. // Sequence starts with something other than b1
  104. <L1> -> 0<W>
  105.       | #<W>
  106.       | 1<D><W>
  107.  
  108. // There is an ill-formed integer somewhere in the mix
  109. <L2> -> <W>#0<W>
  110.       | <W>##<W>
  111.  
  112. // Head and tail of string: ensure that <H>w<T> makes w a bi#bj sequence.
  113. <H> -> <W>#
  114.      | //epsilon
  115. <T> -> #<W>
  116.      | //epsilon
  117.  
  118. <L3> -> <La>    // Inverse of 1^n#10^n,  intersect 1*#<W>
  119.       | <Lb>    // Inverse of p01^n#p10^n
  120.  
  121. <La> -> 1<La>0  // 1s and 0s still balance
  122.       | 1<La_F>1        // Bad 1 on right
  123.       | #0<N>   // Error--0 starts right
  124.       | #1<D><N>        // Too much on right
  125.       | <O>#    // Too many 1s on left
  126. <La_F> -> 1<La_F><Q>    // Maintain balance of 1s/0s
  127.         | #<N>          // Tack on whatever you want on right
  128.  
  129. // Inverse of p01^n#p10^n
  130. // Assume error in prefix: 0 on left, 1 on right
  131. <P-L-A0> -> <D><P-L-A0><D> | 0 // Error is center 0; generate prefix around it
  132. <P-L-B0> -> <D><P-L-A0>0                // Generate 0 following prefix
  133. <P-L-C0> -> <D><P-L-C0>1 | <P-L-B0>     // Generate trailing 1s in left side
  134. <P-L-D0> -> <D><P-L-C0>#<N>             // Generate separating hash
  135. <P-L-E0> -> <D><P-L-E0><D> | <P-L-D0>   // Generate upper end of right prefix
  136. <P-L-A1> -> <D><P-L-A1><D> | 1 // Error is center 1; generate prefix around it
  137. <P-L-B1> -> <D><P-L-A1>0                // Generate 0 following prefix
  138. <P-L-C1> -> <D><P-L-C1>1 | <P-L-B1>     // Generate trailing 1s in left side
  139. <P-L-D1> -> <D><P-L-C1>#<N>             // Generate separating hash
  140. <P-L-E1> -> <D><P-L-E1><D> | <P-L-D1>   // Generate upper end of right prefix
  141. <P-R-A1> -> <D><P-R-A1><D> | 1 // Error is center 1; generate prefix around it
  142. <P-R-B1> -> <N>#<P-R-A1><D>             // Separating hash
  143. <P-R-C1> -> 1<P-R-C1><D> | <P-R-B1>     // Tail 1s of left side
  144. <P-R-D1> -> 0<P-R-C1><D>                // After prefix 0 on left side
  145. <P-R-E1> -> <D><P-R-E1><D> | <P-R-D1>   // Right edge of left prefix
  146. <P-R-A0> -> <D><P-R-A0><D> | 0 // Error is center 0; generate prefix around it
  147. <P-R-B0> -> <N>#<P-R-A0><D>             // Separating hash
  148. <P-R-C0> -> 1<P-R-C0><D> | <P-R-B0>     // Tail 1s of left side
  149. <P-R-D0> -> 0<P-R-C0><D>                // After prefix 0 on left side
  150. <P-R-E0> -> <D><P-R-E0><D> | <P-R-D0>   // Right edge of left prefix
  151.  
  152. // Assume error at first zero after prefix: pairs with 0 on right
  153. <C-L-A0> -> <D><C-L-A0>1 | 0            // 0 after prefix
  154. <C-L-B0> -> <D><C-L-A0>#                // Trailing 1s plus hash
  155. <C-L-C0> -> <D><C-L-C0><D> | <C-L-B0>   // Part of right prefix
  156. <C-R-Z0> -> <D><C-R-Z0>0 | 0            // Make "1" after prefix a 0
  157. <C-R-Y0> -> #<C-R-Z0>0                  // Separating hash
  158. <C-R-X0> -> 1<C-R-X0>0 | <C-R-Y0>       // Last 1s of left side
  159.  
  160. // Assume error in trailing 1s on left side: pairs with 1 on right
  161.  
  162. <T-A> -> 1<T-A><D>              // Last 1s of left tail, last Xs of right tail
  163.      | #<N>1                    // Sep, prefix, 0, 1s, and error
  164.  
  165. <Lb> -> <P-L-A0><P-R-E1>            // Break before end of left prefix
  166.       | <P-L-C0><P-R-C1>            // Break somewhere in trailing 1s on left
  167.       | <P-L-E0><P-R-A1>            // Break somewhere in left of right prefix
  168.       | <P-L-A1><P-R-E0>            // Break before end of left prefix
  169.       | <P-L-C1><P-R-C0>            // Break somewhere in trailing 1s on left
  170.       | <P-L-E1><P-R-A0>            // Break somewhere in left of right prefix
  171.         // Error in first zero after left side prefix
  172.       | <C-L-A0><C-R-X0>            // Break on left side
  173.       | <C-L-C0><C-R-Z0>            // Break on right side
  174.         // Error in trailing 1s of left side after prefix
  175.       | <N>1<T-A>
  176.  
  177. --
  178.                      Peter A. Bigot -- pab@cs.arizona.edu
  179.           Dept. of Computer Science, University of Arizona, Tucson AZ
  180. -- 
  181. Send compilers articles to compilers@iecc.cambridge.ma.us or
  182. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  183.