home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / compiler / 1556 < prev    next >
Encoding:
Internet Message Format  |  1992-09-15  |  4.8 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!rutgers!faatcrl!iecc!compilers-sender
  2. From: sasghm@unx.sas.com (Gary Merrill)
  3. Newsgroups: comp.compilers
  4. Subject: Re: Backtracking yacc
  5. Keywords: yacc, parse, LL(1)
  6. Message-ID: <92-09-074@comp.compilers>
  7. Date: 14 Sep 92 13:02:00 GMT
  8. References: <92-09-062@comp.compilers> <92-09-059@comp.compilers>
  9. Sender: compilers-sender@iecc.cambridge.ma.us
  10. Reply-To: Gary Merrill <sasghm@unx.sas.com>
  11. Organization: Compilers Central
  12. Lines: 79
  13. Approved: compilers@iecc.cambridge.ma.us
  14.  
  15.  
  16. > Such theorems are rarely of practical value; in practice you will find
  17. > that rewriting an LR(k) grammar to be LR(1) (or whatever) will involve
  18. > contorting the original syntax in ways that are perverse. While such a
  19. > contortion might be acceptable for parsing alone, attempting to add simple
  20. > semantic actions or, worse, trying to maintain the grammar for an evolving
  21. > language can lead to premature greying. The Roskind C++ grammar is a
  22. > notorious example.
  23.  
  24. I can second this with some enthusiasm.  I in fact have written an LL(1)
  25. parser generator that I transformed into a "grammar munger", and
  26. ultimately into a tool that does the following:
  27.  
  28.       1. Sucks in a grammar (in more or less yacc-like format).
  29.       2. Applies a load of transformations in an attempt to get the
  30.          grammar as close to LL(1) as possible.
  31.       3. Generates an (LL(1)-like) parse table that contains conflicts.
  32.       4. Emits C code for a recursive descent parser using that table,
  33.          where this code contains built-in backtracking code.
  34.  
  35. (Don't get the idea that this is a well-cooked product.  I haven't tested
  36. it that much, but it does seem to work.  And at least it produces a table
  37. and parser when run on our yacc grammar for C++ -- about 750 productions.)
  38. I had hopes that the resulting RD parser would be maintainable in the
  39. absence of the generating tool, but I suppose I should have known better.
  40. The resulting grammar was quite counter-intuitive in having odd
  41. "artificial" semantic categories.  I expect things might be a bit better
  42. with an LR(1)-ish grammar as the result, but not enough so it would make
  43. much difference.
  44.  
  45. In cases such as these I think the primary theorem is something like, "You
  46. can have a grammar that is pretty natural, or you can have a grammar that
  47. really works: take your pick."
  48.  
  49. > As for backtracking, there are additional problems: again, what about
  50. > semantic processing? If you are merely parsing or constructing a parse
  51. > tree, you can get away with throwing away an incorrect guess. But as soon
  52. > as you introduce side effects of even the simplest nature, you are left
  53. > with the task of figuring out how to undo actions associated with the
  54. > wrong guess. Even for a language as simle as Ansi C, you must perform
  55. > certain semantic actions just to parse and C++ is even worse.
  56.  
  57. Yes, exactly.  This is why my changes to Berkeley yacc include treating
  58. the "usual" ( { ... } ) actions as *conditional* and implementing
  59. *unconditional* actions ( [ ... ] ) that will be executed even during
  60. trial parsing.
  61.  
  62. > A far better approach is to have available a set of tools which allow the
  63. > disambiguation at the point where it is first encountered. LADE, for
  64. > example, allows context-sensitive tests to be performed. These can be used
  65. > to either a) test arbitrary semantic information to decide on a path to
  66. > take, or b) parse ahead in parse-only mode to find some disambiguating
  67. > symbol, then return and resume normal, action-associated, parsing. While b
  68. > approaches a back-tracking capability, it is not, in fact, backtracking
  69. > since it returns to the originating point regardless of what symbols were
  70. > found in the scan- ahead.
  71.  
  72. Such tests can, of course, be performed in the lexical analyzer and you
  73. can use the results of these tests to drive the invocations of your
  74. parser.  However, it would be *much* nicer to have a parser tool that
  75. would take care of all this.  I have not looked at LADE, but I have
  76. literature pertaining to it on my desk.  I think that if we decide to
  77. replace our parser at some time in the future, however, we likely will use
  78. RD.
  79.  
  80. Note that there are *still* some constructs in some languages (even in
  81. C++) that require *some* semantic processing even during trial parsing in
  82. order to parse correctly. (Think of defining a type in an argument
  83. declaration list and then using that type in a later declaration in the
  84. list -- this is an error in C++, but in order to perform a truly correct
  85. parse you need to record the type definition so you can diagnose it and
  86. not issue a spurious diagnostic on its subsequent use.)
  87. ---
  88. Gary H. Merrill  [Principal Systems Developer, C Compiler Development]
  89. SAS Institute Inc. / SAS Campus Dr. / Cary, NC  27513 / (919) 677-8000
  90. sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm
  91. -- 
  92. Send compilers articles to compilers@iecc.cambridge.ma.us or
  93. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  94.