home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16497 < prev    next >
Encoding:
Text File  |  1992-11-18  |  3.9 KB  |  110 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: Need Yacc & Lex retargeted to C++
  5. In-Reply-To: ameesh@lsil.com's message of 16 Nov 92 20:16:58 GMT
  6. Message-ID: <KANZE.92Nov18120946@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <1992Nov13.154107.21091@murdoch.acc.Virginia.EDU>
  10.     <1992Nov16.201658.2862@lsil.com>
  11. Date: 18 Nov 92 12:09:46
  12. Lines: 96
  13.  
  14. In article <1992Nov16.201658.2862@lsil.com>, Ameesh Desai writes:
  15.  
  16. |> In article 21091@murdoch.acc.Virginia.EDU,
  17. |> cgh6m@darwin.clas.Virginia.EDU (Craig G. Hocker) writes:
  18.  
  19. |> >This seems to be a topic of some interest, so I will mention a 
  20. |> >C++ Parser Class Kit I came across by accident yesterday while
  21. |> >downloading another file. Nobody has mentioned it.
  22. |> >
  23. |> >anonymous ftp to  export.lcs.mit.edu
  24. |> >
  25. |> >    contrib/yacc.shar.Z
  26. |> >
  27. |> >
  28. |> >warning: it's a beta version, but the author does seem to be actively
  29. |> >soliciting people to modify and send him enhancements.
  30. |> >
  31. |> >I can't comment on it, since I haven't tried to do anything with it
  32. |> >yet.
  33.  
  34. |> The implementation seems to encapsulate only the global yacc variables
  35. |> within a C++ class. In effect providing a C++ "wrapper" to yacc calls
  36. |> and vars.
  37.  
  38. |> The problem with yacc is not its variables. Its making yacc understand
  39. |> C++ class definations. You should ideally be able to write stuff
  40. |> like...
  41.  
  42. |> %{
  43.  
  44. |> class A {   // some C++ class
  45. |> ...
  46. |> public:
  47. |>     A();        // see note below ...
  48. |>             //  this would cause problems with yacc
  49. |> };
  50.  
  51. |> %}
  52.  
  53. |> %union
  54. |> {
  55. |> int ival;
  56. |> float fval;
  57. |> char *sval
  58. |> A aclass;         // make this work !
  59. |> }
  60.  
  61. |> %type <aclass> rule_that_returns_a_class
  62.  
  63. |> ....
  64. |> %%
  65.  
  66. |> The fact that a class contains a ctor/dtor should not prevent it from
  67. |> being returned. The current yacc implementation depends on union for
  68. |> declaring the return type, and C++ will not allow a class with a
  69. |> ctor/dtor to be part of the union !
  70.  
  71.     Nota bene: my comments here concern the classical yacc, and
  72.     not the C++ Parser Class Kit, which I haven't seen.
  73.  
  74. Actually, there is no rule in yacc that says that the semantic values
  75. have to be declared as a union.  This is just a facility that was
  76. offered, as it corresponds to the most typical case in C.  The actual
  77. type of the semantic value can be anything.
  78.  
  79. In an object oriented approach, one would probably want a polymorphic
  80. type, rather than a union.  In C++, this implies a (possibly
  81. intelligent) reference or pointer.
  82.  
  83. There is another point to consider, however.  The code generated by
  84. the traditional yacc maintains these values on a stack, implemented as
  85. a simple C-style array.  This means that if you declare the type as a
  86. class (intelligent reference or pointer), you must provide a default
  87. constructor, which will be called for every element in the array
  88. before parsing starts.  You must also provide an assignment operator.
  89. In addition, none of the elements will be destructed before the end of
  90. parsing, which could result in the locking up of ressourses.
  91.  
  92. For these reasons, until now, I have always declared the semantic type
  93. to be a C pointer to the base class of all of my semantic values.  The
  94. scanner will construct the actuall desired class (with "new"); it is
  95. up to the individual actions to destruct (with "delete") the
  96. individual values.  (This requires some understanding of what's going
  97. on to do it at the right moment, but isn't really that hard.)
  98.  
  99. It shouldn't be to difficult, however, to modify the parser to use a
  100. real class (a container type generated from a template?) to implement
  101. the semantic stack.  If this is done, there should be no problem using
  102. intelligent references (the envelope classes in Coplien) as the
  103. semantic values.  (Warning: I have not actually tried this yet.  I'm
  104. just thinking out loud.)
  105. --
  106. James Kanze            GABI Software, Sarl.
  107. email: kanze@us-es.sel.de    8 rue du Faisan
  108.                 67000 Strasbourg
  109.                 France
  110.