home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15990 < prev    next >
Encoding:
Text File  |  1992-11-09  |  6.6 KB  |  205 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!EE.Stanford.EDU!sierra!mcgrant
  3. From: mcgrant@rascals.stanford.edu (Michael C. Grant)
  4. Subject: Re: yacc and C++
  5. In-Reply-To: hm@cs.brown.edu's message of 9 Nov 92 01:38:42 GMT
  6. Message-ID: <MCGRANT.92Nov9125219@rascals.stanford.edu>
  7. Sender: usenet@EE.Stanford.EDU (Usenet)
  8. Organization: Information Systems Laboratory, Stanford University
  9. References: <1992Nov9.013842.28215@cs.brown.edu>
  10. Date: 9 Nov 92 12:52:19
  11. Lines: 192
  12.  
  13. In article <1992Nov9.013842.28215@cs.brown.edu>
  14. hm@cs.brown.edu (Harry Mamaysky) writes:
  15.  
  16.    So, does any one have a better way of using YACC with C++?
  17.    Does a better way exist?
  18.    Does a (free) YACC like tool exist for C++? If so, where?
  19.    Please respond either by email or to comp.lang.c++. If any one out
  20.    there has a nicer solution, I am most eager to hear about it.
  21.    Harry.
  22.    hm@cs.brown.edu
  23.  
  24. Well, here's a hack that I put together so that 1) yacc uses new instead
  25. of malloc, and 2) I can use a class for the YYSTYPE (and overload operator=
  26. for example). It's not modified at all for speed, just for correctness.
  27.  
  28. I've only tried this patch on the yacc++ included with the Sun Sparcstation.
  29.  
  30. To use the (context) patch, put the file in yaccpar.diff, name your file
  31. with a .Y (instead of .y) suffix, and add the following suffix entry
  32. to your Makefile:
  33.  
  34. .Y.o:
  35.         yacc++ -d $(YFLAGS) $*.Y
  36.         rm -f $*.tab.H
  37.         mv y.tab.h $*.tab.H
  38.         patch y.tab.c yaccpar.diff -o $*.tab.C
  39.         rm -f y.tab.c
  40.         $(CCC) $(CFLAGS) $(YCFLAGS) -c -o $*.o $*.tab.C
  41.  
  42. This also simulates the bison naming scheme; i.e. if my file is parse.Y
  43. then the output is parse.tab.H and parse.tab.C.
  44.  
  45. I have no idea if this is a near-optimal solution but it worked for me
  46. and took me very little time to do.
  47.  
  48. Nobody is allowed to sue me or get ticked off at me if this doesn't
  49. work for them. So caveat emptor.
  50.  
  51. Mike
  52.  
  53. *** /usr/lang/SC1.0/yaccpar    Wed Feb 13 23:22:42 1991
  54. --- yaccpar.C    Thu Oct 29 00:21:14 1992
  55. ***************
  56. *** 26,33 ****
  57.       goto yynewstate;\
  58.   }
  59.   #define YYRECOVERING()    (!!yyerrflag)
  60. - #define YYCOPY(to, from, type) \
  61. -     (type *) memcpy(to, (char *) from, yynewmax * sizeof(type))
  62.   #ifndef YYDEBUG
  63.   #    define YYDEBUG    1    /* make debugging available */
  64.   #endif
  65. --- 26,31 ----
  66. ***************
  67. *** 38,47 ****
  68.   ** if you have function redefined error messages here.
  69.   */
  70.   
  71. - #ifdef __cplusplus
  72.   EXTERN_FUNCTION ( extern int printf,    (const char*, DOTDOTDOT) );
  73.   EXTERN_FUNCTION ( extern void *memcpy,  (void *, const void *, int) );
  74. - #endif
  75.   
  76.   
  77.   /*
  78. --- 36,43 ----
  79. ***************
  80. *** 57,64 ****
  81.   /*
  82.   ** static variables used by the parser
  83.   */
  84. ! static YYSTYPE yy_yyv[YYMAXDEPTH], *yyv = yy_yyv;    /* value stack */
  85. ! static int yy_yys[YYMAXDEPTH], *yys = yy_yys;    /* state stack */
  86.   
  87.   static YYSTYPE *yypv;            /* top of value stack */
  88.   static int *yyps;            /* top of state stack */
  89. --- 53,60 ----
  90.   /*
  91.   ** static variables used by the parser
  92.   */
  93. ! static YYSTYPE* yyv = new YYSTYPE [YYMAXDEPTH]; /* value stack */
  94. ! static int*     yys = new int     [YYMAXDEPTH]; /* state stack */
  95.   
  96.   static YYSTYPE *yypv;            /* top of value stack */
  97.   static int *yyps;            /* top of state stack */
  98. ***************
  99. *** 66,72 ****
  100.   static int yystate;            /* current state */
  101.   static int yytmp;            /* extra var (lasts between blocks) */
  102.   
  103. - #if defined(__cplusplus) || defined(__STDC__) || defined(lint)
  104.   static int __yaccpar_lint_hack__ = 0;   
  105.                   /* if you change the value from 0 to
  106.                   something else, make sure you know
  107. --- 62,67 ----
  108. ***************
  109. *** 73,79 ****
  110.                   what to do with yyerrlab reference.
  111.                   This is a hack - to make sure C++ and
  112.                   lint are happy with the 4.1 yacc code. */
  113. - #endif
  114.   
  115.   int yynerrs;            /* number of errors */
  116.   
  117. --- 68,73 ----
  118. ***************
  119. *** 102,108 ****
  120.       yyerrflag = 0;
  121.       yychar = -1;
  122.   
  123. - #if defined(__cplusplus) || defined(__STDC__) || defined(lint)
  124.   /*
  125.      Note that the following can never be executed but simply to please
  126.      lint and C++
  127. --- 96,101 ----
  128. ***************
  129. *** 112,118 ****
  130.           case 1: goto yyerrlab;
  131.           case 2: goto yynewstate;
  132.       }
  133. - #endif
  134.   
  135.       {
  136.           register YYSTYPE *yy_pv;    /* top of value stack */
  137. --- 105,110 ----
  138. ***************
  139. *** 179,219 ****
  140.           if ( ++yy_ps >= &yys[ yymaxdepth ] )    /* room on stack? */
  141.           {
  142.                           /*
  143. !                         ** reallocate and recover.  Note that pointers
  144.                           ** have to be reset, or bad things will happen
  145.                           */ 
  146.                           int yyps_index = (yy_ps - yys);
  147.                           int yypv_index = (yy_pv - yyv);
  148.                           int yypvt_index = (yypvt - yyv);
  149. !                         int yynewmax;
  150.   
  151. !             yynewmax = yymaxdepth + YYMAXDEPTH;
  152. !             if (yymaxdepth == YYMAXDEPTH)    /* first time growth */
  153. !             {
  154. !                 YYSTYPE *newyyv = (YYSTYPE*)malloc(yynewmax*sizeof(YYSTYPE));
  155. !                 int *newyys = (int*)malloc(yynewmax*sizeof(int));
  156. !                 if (newyys != 0 && newyyv != 0)
  157. !                 {
  158. !                     yys = YYCOPY(newyys, yys, int);
  159. !                     yyv = YYCOPY(newyyv, yyv, YYSTYPE);
  160. !                 }
  161. !                 else
  162. !                     yynewmax = 0;    /* failed */
  163. !             }
  164. !             else                /* not first time */
  165. !             {
  166. !                 yyv = (YYSTYPE*)realloc((char*)yyv,
  167. !                                     yynewmax * sizeof(YYSTYPE));
  168. !                 yys = (int*)realloc((char*)yys,
  169. !                     yynewmax * sizeof(int));
  170. !                 if (yys == 0 || yyv == 0)
  171. !                     yynewmax = 0;    /* failed */
  172. !             }
  173. !             if (yynewmax <= yymaxdepth)    /* tables not expanded */
  174. !             {
  175. !                 yyerror( "yacc stack overflow" );
  176. !                 YYABORT;
  177. !             }
  178.               yymaxdepth = yynewmax;
  179.   
  180.                           yy_ps = yys + yyps_index;
  181. --- 171,192 ----
  182.           if ( ++yy_ps >= &yys[ yymaxdepth ] )    /* room on stack? */
  183.           {
  184.                           /*
  185. !                         ** grow the stack. Note that pointers
  186.                           ** have to be reset, or bad things will happen
  187.                           */ 
  188.                           int yyps_index = (yy_ps - yys);
  189.                           int yypv_index = (yy_pv - yyv);
  190.                           int yypvt_index = (yypvt - yyv);
  191. !                         int yynewmax = yymaxdepth + YYMAXDEPTH;
  192.   
  193. !                         YYSTYPE* newyyv = new YYSTYPE [yynewmax];
  194. !                         int*     newyys = new int     [yynewmax];
  195. !                         for ( register int k = 0 ; k < yymaxdepth ; k++ ) {
  196. !                                 newyyv[k] = yyv[k];
  197. !                                 newyys[k] = yys[k];
  198. !                         }
  199. !                         delete [] yys; yys = newyys;
  200. !                         delete [] yyv; yyv = newyyv;
  201.               yymaxdepth = yynewmax;
  202.   
  203.                           yy_ps = yys + yyps_index;
  204.  
  205.