home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13472 < prev    next >
Encoding:
Text File  |  1992-09-10  |  4.3 KB  |  95 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!decwrl!concert!sas!mozart.unx.sas.com!sasghm
  3. From: sasghm@theseus.unx.sas.com (Gary Merrill)
  4. Subject: Re: C++ Grammar 
  5. Originator: sasghm@theseus.unx.sas.com
  6. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  7. Message-ID: <BuD9Lx.JoD@unx.sas.com>
  8. Date: Thu, 10 Sep 1992 14:29:56 GMT
  9. References: <Bu07v4.F66@csfb1.fir.fbc.com> <1992Sep6.232016.6573@cssc-syd.tansu.com.au>
  10. Nntp-Posting-Host: theseus.unx.sas.com
  11. Organization: SAS Institute Inc.
  12. Lines: 81
  13.  
  14.  
  15. In article <1992Sep6.232016.6573@cssc-syd.tansu.com.au>, mattw@cssc-syd.tansu.com.au (Matthew Watson) writes:
  16. |> kboyle@csfb1.fir.fbc.com (Kevin T. Boyle) writes:
  17. |> 
  18. |> 
  19. |> 
  20. |> >        I am interested in writing a syntax parser for C++ and
  21. |> >    I wonder if anyone has a YACC compatible grammar that they
  22. |> >    could supply me.  Your help would be very appreciated.
  23. |> There is a grammar available at the same place you get c++-mode for emacs
  24. |> from, ftp.cme.nist.gov:pub/gnu/?? I think. Sorry if this isn't right but
  25. |> I know there is one out there. Try using archie.
  26. |> -- 
  27. |> 
  28. |> -------------------------------------------------------------------------------
  29. |>      -  Matthew Watson -         | MHSnet: mattw@cssc-syd.tansu.com.au
  30. |>      Australian and Overseas       |
  31.  
  32. There have been enough requests of this sort that someone ought to
  33. emphasize a couple of important points.  In a significant sense,
  34. the concept of a yacc compatible grammar for C++ is incoherent.
  35. I take it that what most people mean by this is "A grammar like
  36. the ones you see in the various C++ books that I could run yacc on."
  37. My impression is that most people think this is like asking for
  38. a yaccable C grammar.
  39.  
  40. But the syntax of C++ is quite different in various places from the
  41. syntax of C.  Note that in _The_Annotated_C++_Reference_Manual_ it
  42. is remarked that
  43.  
  44.     The general cases cannot be resolved without backtracking,
  45.     nested grammars or similar advanced parsing strategies.
  46.  
  47. The point is that the grammars you will find that *look* like
  48. yacc grammars for C++ are not LR(k) for any k.  You can write
  49. a yaccable grammar that *looks* like a grammar for C++, but yacc
  50. won't (can't) generate a correct C++ parser from it.  An LALR(1)
  51. parser generator will not create for you a parser capable of
  52. parsing a grammar that is not LR(k).
  53.  
  54. Now a disclaimer for the record (and for obvious legal reasons).
  55. I have not looked at the yacc grammar for g++.  I have not seen
  56. the C++ yacc grammar developed by Roskind.  However, ...
  57.  
  58. If you noticed a prior posting of mine concerning how various
  59. C++ compilers handle syntactic ambiguities in C++ you will have
  60. noticed that g++ does not do well at all.  Since their parser
  61. is a yacc parser this is not surprising.  If you use a grammar
  62. similar to this, you will be able to parse a language that is
  63. *close* to C++ in a lot of respects, but this language is *not*
  64. C++.
  65.  
  66. Roskind has apparently constructed a truly yaccable C++ grammar.
  67. I believe I know how this was done and considered doing it myself,
  68. but rejected the approach for several reasons.  If I am correct,
  69. you would discover that the grammar differs in some significant
  70. respects from what you are thinking of as a C++ grammar.  There
  71. was a posting a while ago on comp.compilers that confirms this
  72. hypothesis (from someone who has seen and used the grammar).  I
  73. suggest you look at that posting.
  74.  
  75. The grammar we use for our C++ compiler is a yacc grammar, and
  76. we use a version of yacc to generate the parser -- sort of.  I have
  77. made several modifications to yacc to facilitate the parsing of
  78. C++ and of course the resulting yacc parser *still* does not
  79. parse C++ (remember, it *can't*).  We successfully parse C++
  80. by using backtracking (or maybe it's "advanced parsing strategies"
  81. -- depends on your point of view).  To see how this is done
  82. with a recursive descent parser see the article by Mike Ball from
  83. Taumetric in the latest _C++_Report_.
  84.  
  85. Apparently a number of people are getting interested in writing
  86. various tools for C++ (Editors that do syntax checking, for
  87. example?  Software metrics?) and feel that a good and easy place
  88. to start would be from a yacc parser for C++.  Good luck.
  89.  
  90.  
  91. -- 
  92. Gary H. Merrill  [Principal Systems Developer, C Compiler Development]
  93. SAS Institute Inc. / SAS Campus Dr. / Cary, NC  27513 / (919) 677-8000
  94. sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm
  95.