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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!brunix!brunix!ted
  3. From: ted@cs.brown.edu (Tony Davis)
  4. Subject: Re: C++ Grammar 
  5. Message-ID: <1992Sep12.021515.23609@cs.brown.edu>
  6. Sender: news@cs.brown.edu
  7. Organization: Brown University Department of Computer Science
  8. References: <Bu07v4.F66@csfb1.fir.fbc.com> <1992Sep6.232016.6573@cssc-syd.tansu.com.au> <BuD9Lx.JoD@unx.sas.com>
  9. Date: Sat, 12 Sep 1992 02:15:15 GMT
  10. Lines: 66
  11.  
  12. In article <BuD9Lx.JoD@unx.sas.com> sasghm@theseus.unx.sas.com (Gary Merrill) writes:
  13. > |> kboyle@csfb1.fir.fbc.com (Kevin T. Boyle) writes:
  14. > |> >        I am interested in writing a syntax parser for C++ and
  15. > |> >    I wonder if anyone has a YACC compatible grammar that they
  16. > |> >    could supply me.  Your help would be very appreciated.
  17.  
  18. > The point is that the grammars you will find that *look* like
  19. > yacc grammars for C++ are not LR(k) for any k.  You can write
  20. > a yaccable grammar that *looks* like a grammar for C++, but yacc
  21. > won't (can't) generate a correct C++ parser from it.  An LALR(1)
  22. > parser generator will not create for you a parser capable of
  23. > parsing a grammar that is not LR(k).
  24.  
  25. This is accurate but somewhat misleading.  An LALR(1) parser
  26. *USED IN ISOLATION* is not capable of parsing a language that
  27. is not LR(k).  However, yacc grammars are rarely used in
  28. isolation.  Even to parse C, the parsers require a symbol table
  29. because C is not context-free.
  30.  
  31. > Roskind has apparently constructed a truly yaccable C++ grammar.
  32. > I believe I know how this was done and considered doing it myself,
  33. > but rejected the approach for several reasons.  If I am correct,
  34. > you would discover that the grammar differs in some significant
  35. > respects from what you are thinking of as a C++ grammar.  There
  36. > was a posting a while ago on comp.compilers that confirms this
  37. > hypothesis (from someone who has seen and used the grammar).  I
  38. > suggest you look at that posting.
  39.  
  40. I'm not familiar with the posting referred to, but the biggest
  41. problem I've found with the Roskind grammar is it's complexity.
  42. There is a good deal of circumlocution in the grammar to get
  43. yacc to resolve ambiguities properly.  Once that has been done
  44. it is hard to associate the grammar rules with the semantic
  45. constructs one expects to deal with in a C++ parser.  Roskind
  46. mentions one ambiguity that is not resolved entirely accurately,
  47. but I believe he is right in concluding that (1) the difference
  48. between the parser and 'true C++' is insignificant, and (2) his
  49. grammar probably resolves that ambiguity more accurately than
  50. most or all production parsers anyway.  If one were producing
  51. a production parser they would have to evaluate this ambiguity
  52. carefully.  However, for other tools it can be ignored because
  53. code that exercises the ambiguity is likely never to appear in
  54. practice.
  55.  
  56. Another common approach to parsing C++ with yacc is to make
  57. the lexicl analyzer more powerful.  With this approach yacc
  58. can indeed be used to parse non-LR(k) languages.  Granted,
  59. this might be called cheating since the lexical analyzer is
  60. doing enough parsing so that the language that the yacc portion
  61. of the system must parse IS now LR(k).  Strictly speaking
  62. yacc does not parse a non-LR(k) language, but the parser as
  63. a whole does.
  64.  
  65. A C++ parser that one of my advisors has recently produced takes
  66. this approach.  The lexer does look ahead to produce artificial
  67. tokens which yacc uses as ques to yield a correct parse (modulo
  68. any bugs that remain in the system).  Soon I will be making that
  69. parser available for ftp.  When it becomes available I will
  70. announce it here.
  71.  
  72.  
  73.  
  74. Tony Davis
  75. ted@cs.brown.edu
  76.  
  77.