home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts1.zip / H / DLGAUTO.H < prev    next >
C/C++ Source or Header  |  1993-09-01  |  10KB  |  468 lines

  1. /* dlgauto.h automaton
  2.  *
  3.  * SOFTWARE RIGHTS
  4.  *
  5.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  6.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  7.  * company may do whatever they wish with source code distributed with
  8.  * PCCTS or the code generated by PCCTS, including the incorporation of
  9.  * PCCTS, or its output, into commerical software.
  10.  * 
  11.  * We encourage users to develop software with PCCTS.  However, we do ask
  12.  * that credit is given to us for developing PCCTS.  By "credit",
  13.  * we mean that if you incorporate our source code into one of your
  14.  * programs (commercial product, research project, or otherwise) that you
  15.  * acknowledge this fact somewhere in the documentation, research report,
  16.  * etc...  If you like PCCTS and have developed a nice tool with the
  17.  * output, please mention that you developed it using PCCTS.  In
  18.  * addition, we ask that this header remain intact in our source code.
  19.  * As long as these guidelines are kept, we expect to continue enhancing
  20.  * this system and expect to make other tools available as they are
  21.  * completed.
  22.  *
  23.  * ANTLR 1.10
  24.  * Will Cohen
  25.  * Purdue University
  26.  * 1989-1993
  27.  */
  28.  
  29. #ifndef ZZDEFAUTO_H
  30. #define ZZDEFAUTO_H
  31.  
  32. zzchar_t    *zzlextext;    /* text of most recently matched token */
  33. zzchar_t    *zzbegexpr;    /* beginning of last reg expr recogn. */
  34. zzchar_t    *zzendexpr;    /* beginning of last reg expr recogn. */
  35. int    zzbufsize;    /* number of characters in zzlextext */
  36. int    zzbegcol = 0;    /* column that first character of token is in*/
  37. int    zzendcol = 0;    /* column that last character of token is in */
  38. int    zzline = 1;    /* line current token is on */
  39. int    zzchar;        /* character to determine next state */
  40. int    zzbufovf;    /* indicates that buffer too small for text */
  41. int    zzcharfull = 0;
  42. static zzchar_t    *zznextpos;/* points to next available position in zzlextext*/
  43. static int     zzclass;
  44.  
  45. #ifdef __STDC__
  46. void    zzerrstd(char *);
  47. void    (*zzerr)(char *)=zzerrstd;/* pointer to error reporting function */
  48. extern int    zzerr_in(void);
  49. #else
  50. void    zzerrstd();
  51. void    (*zzerr)()=zzerrstd;    /* pointer to error reporting function */
  52. extern int    zzerr_in();
  53. #endif
  54.  
  55. static FILE    *zzstream_in=0;
  56. static int    (*zzfunc_in)() = zzerr_in;
  57. static zzchar_t    *zzstr_in=0;
  58.  
  59. static int     zzauto = 0;
  60. static int    zzadd_erase;
  61. static char     zzebuf[70];
  62.  
  63. #ifdef ZZCOL
  64. #define ZZINC (++zzendcol)
  65. #else
  66. #define ZZINC
  67. #endif
  68.  
  69.  
  70. #define ZZGETC_STREAM {zzchar = getc(zzstream_in); zzclass = ZZSHIFT(zzchar);}
  71. #define ZZGETC_FUNC {zzchar = (*zzfunc_in)(); zzclass = ZZSHIFT(zzchar);}
  72. #define ZZGETC_STR {             \
  73.     if (*zzstr_in){                \
  74.         zzchar = *zzstr_in;        \
  75.         ++zzstr_in;                \
  76.     }else{                        \
  77.         zzchar = EOF;            \
  78.     }                            \
  79.     zzclass = ZZSHIFT(zzchar);    \
  80. }
  81.  
  82. #define ZZNEWSTATE    (newstate = dfa[state][zzclass])
  83.  
  84. #ifndef ZZCOPY
  85. #define ZZCOPY    \
  86.     /* Truncate matching buffer to size (not an error) */    \
  87.     if (zznextpos < lastpos){                \
  88.         *(zznextpos++) = zzchar;            \
  89.     }else{                            \
  90.         zzbufovf = 1;                    \
  91.     }
  92. #endif
  93.  
  94. void
  95. #ifdef __STDC__
  96. zzrdstream( FILE *f )
  97. #else
  98. zzrdstream( f )
  99. FILE *f;
  100. #endif
  101. {
  102.     /* make sure that it is really set to something, otherwise just 
  103.        leave it be.
  104.     */
  105.     if (f){
  106.         /* make sure that there is always someplace to get input
  107.            before closing zzstream_in
  108.         */
  109. #if 0
  110.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  111. #endif
  112.         zzline = 1;
  113.         zzstream_in = f;
  114.         zzfunc_in = NULL;
  115.         zzstr_in = 0;
  116.         zzcharfull = 0;
  117.     }
  118. }
  119.  
  120. void
  121. #ifdef __STDC__
  122. zzrdfunc( int (*f)() )
  123. #else
  124. zzrdfunc( f )
  125. int (*f)();
  126. #endif
  127. {
  128.     /* make sure that it is really set to something, otherwise just 
  129.        leave it be.
  130.     */
  131.     if (f){
  132.         /* make sure that there is always someplace to get input
  133.            before closing zzstream_in
  134.         */
  135. #if 0
  136.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  137. #endif
  138.         zzline = 1;
  139.         zzstream_in = NULL;
  140.         zzfunc_in = f;
  141.         zzstr_in = 0;
  142.         zzcharfull = 0;
  143.     }
  144. }
  145.  
  146.  
  147. void
  148. #ifdef __STDC__
  149. zzrdstr( zzchar_t *s )
  150. #else
  151. zzrdstr( s )
  152. zzchar_t *s;
  153. #endif
  154. {
  155.     /* make sure that it is really set to something, otherwise just 
  156.        leave it be.
  157.     */
  158.     if (s){
  159.         /* make sure that there is always someplace to get input
  160.            before closing zzstream_in
  161.         */
  162. #if 0
  163.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  164. #endif
  165.         zzline = 1;
  166.         zzstream_in = NULL;
  167.         zzfunc_in = 0;
  168.         zzstr_in = s;
  169.         zzcharfull = 0;
  170.     }
  171. }
  172.  
  173.  
  174. void
  175. zzclose_stream()
  176. {
  177. #if 0
  178.     fclose( zzstream_in );
  179.     zzstream_in = NULL;
  180.     zzfunc_in = NULL;
  181. #endif
  182. }
  183.  
  184. /* saves dlg state, but not what feeds dlg (such as file position) */
  185. void
  186. #ifdef __STDC__
  187. zzsave_dlg_state(struct zzdlg_state *state)
  188. #else
  189. zzsave_dlg_state(state)
  190. struct zzdlg_state *state;
  191. #endif
  192. {
  193.     state->stream = zzstream_in;
  194.     state->func_ptr = zzfunc_in;
  195.     state->str = zzstr_in;
  196.     state->auto_num = zzauto;
  197.     state->add_erase = zzadd_erase;
  198.     state->lookc = zzchar;
  199.     state->char_full = zzcharfull;
  200.     state->begcol = zzbegcol;
  201.     state->endcol = zzendcol;
  202.     state->line = zzline;
  203.     state->lextext = zzlextext;
  204.     state->begexpr = zzbegexpr;
  205.     state->endexpr = zzendexpr;
  206.     state->bufsize = zzbufsize;
  207.     state->bufovf = zzbufovf;
  208.     state->nextpos = zznextpos;
  209.     state->class_num = zzclass;
  210. }
  211.  
  212. void
  213. #ifdef __STDC__
  214. zzrestore_dlg_state(struct zzdlg_state *state)
  215. #else
  216. zzrestore_dlg_state(state)
  217. struct zzdlg_state *state;
  218. #endif
  219. {
  220.     zzstream_in = state->stream;
  221.     zzfunc_in = state->func_ptr;
  222.     zzstr_in = state->str;
  223.     zzauto = state->auto_num;
  224.     zzadd_erase = state->add_erase;
  225.     zzchar = state->lookc;
  226.     zzcharfull = state->char_full;
  227.     zzbegcol = state->begcol;
  228.     zzendcol = state->endcol;
  229.     zzline = state->line;
  230.     zzlextext = state->lextext;
  231.     zzbegexpr = state->begexpr;
  232.     zzendexpr = state->endexpr;
  233.     zzbufsize = state->bufsize;
  234.     zzbufovf = state->bufovf;
  235.     zznextpos = state->nextpos;
  236.     zzclass = state->class_num;
  237. }
  238.  
  239. void
  240. #ifdef __STDC__
  241. zzmode( int m )
  242. #else
  243. zzmode( m )
  244. int m;
  245. #endif
  246. {
  247.     /* points to base of dfa table */
  248.     if (m<MAX_MODE){
  249.         zzauto = m;
  250.         /* have to redo class since using different compression */
  251.         zzclass = ZZSHIFT(zzchar);
  252.     }else{
  253.         sprintf(zzebuf,"Invalid automaton mode = %d ",m);
  254.         zzerr(zzebuf);
  255.     }
  256. }
  257.  
  258. /* erase what is currently in the buffer, and get a new reg. expr */
  259. void
  260. zzskip()
  261. {
  262.     zzadd_erase = 1;
  263. }
  264.  
  265. /* don't erase what is in the zzlextext buffer, add on to it */
  266. void
  267. zzmore()
  268. {
  269.     zzadd_erase = 2;
  270. }
  271.  
  272. /* substitute c for the reg. expr last matched and is in the buffer */
  273. #ifdef __STDC__
  274. void
  275. zzreplchar(zzchar_t c)
  276. #else
  277. void
  278. zzreplchar(c)
  279. zzchar_t c;
  280. #endif
  281. {
  282.     /* can't allow overwriting null at end of string */
  283.     if (zzbegexpr < &zzlextext[zzbufsize-1]){
  284.         *zzbegexpr = c;
  285.         *(zzbegexpr+1) = '\0';
  286.     }
  287.     zzendexpr = zzbegexpr;
  288.     zznextpos = zzbegexpr + 1;
  289. }
  290.  
  291. /* replace the string s for the reg. expr last matched and in the buffer */
  292. void
  293. #ifdef __STDC__
  294. zzreplstr(register zzchar_t *s)
  295. #else
  296. zzreplstr(s)
  297. register zzchar_t *s;
  298. #endif
  299. {
  300.     register zzchar_t *l= &zzlextext[zzbufsize -1];
  301.  
  302.     zznextpos = zzbegexpr;
  303.     if (s){
  304.          while ((zznextpos <= l) && (*(zznextpos++) = *(s++))){
  305.             /* empty */
  306.         }
  307.         /* correct for NULL at end of string */
  308.         zznextpos--;
  309.     }
  310.     if ((zznextpos <= l) && (*(--s) == 0)){
  311.         zzbufovf = 0;
  312.     }else{
  313.         zzbufovf = 1;
  314.     }
  315.     *(zznextpos) = '\0';
  316.     zzendexpr = zznextpos - 1;
  317. }
  318.  
  319. void
  320. zzgettok()
  321. {
  322.     register int state, newstate;
  323.     /* last space reserved for the null char */
  324.     register zzchar_t *lastpos;
  325.  
  326. skip:
  327.     zzbufovf = 0;
  328.     lastpos = &zzlextext[zzbufsize-1];
  329.     zznextpos = zzlextext;
  330.     zzbegcol = zzendcol+1;
  331. more:
  332.     zzbegexpr = zznextpos;
  333. #ifdef ZZINTERACTIVE
  334.     /* interactive version of automaton */
  335.     /* if there is something in zzchar, process it */
  336.     state = newstate = dfa_base[zzauto];
  337.     if (zzcharfull){
  338.         ZZINC;
  339.         ZZCOPY;
  340.         ZZNEWSTATE;
  341.     }
  342.     if (zzstr_in)
  343.         while (zzalternatives[newstate]){
  344.             state = newstate;
  345.             ZZGETC_STR;
  346.             ZZINC;
  347.             ZZCOPY;
  348.             ZZNEWSTATE;
  349.         }
  350.     else if (zzstream_in)
  351.         while (zzalternatives[newstate]){
  352.             state = newstate;
  353.             ZZGETC_STREAM;
  354.             ZZINC;
  355.             ZZCOPY;
  356.             ZZNEWSTATE;
  357.         }
  358.     else if (zzfunc_in)
  359.         while (zzalternatives[newstate]){
  360.             state = newstate;
  361.             ZZGETC_FUNC;
  362.             ZZINC;
  363.             ZZCOPY;
  364.             ZZNEWSTATE;
  365.         }
  366.     /* figure out if last character really part of token */
  367.     if ((state != dfa_base[zzauto]) && (newstate == DfaStates)){
  368.         zzcharfull = 1;
  369.         --zznextpos;
  370.     }else{
  371.         zzcharfull = 0;
  372.         state = newstate;
  373.     }
  374.     *(zznextpos) = '\0';
  375.     /* Able to transition out of start state to some non err state?*/
  376.     if ( state == dfa_base[zzauto] ){
  377.         /* make sure doesn't get stuck */
  378.         zzadvance();
  379.     }
  380. #else
  381.     /* non-interactive version of automaton */
  382.     if (!zzcharfull)
  383.         zzadvance();
  384.     else
  385.         ZZINC;
  386.     state = dfa_base[zzauto];
  387.     if (zzstr_in)
  388.         while (ZZNEWSTATE != DfaStates){
  389.             state = newstate;
  390.             ZZCOPY;
  391.             ZZGETC_STR;
  392.             ZZINC;
  393.         }
  394.     else if (zzstream_in)
  395.         while (ZZNEWSTATE != DfaStates){
  396.             state = newstate;
  397.             ZZCOPY;
  398.             ZZGETC_STREAM;
  399.             ZZINC;
  400.         }
  401.     else if (zzfunc_in)
  402.         while (ZZNEWSTATE != DfaStates){
  403.             state = newstate;
  404.             ZZCOPY;
  405.             ZZGETC_FUNC;
  406.             ZZINC;
  407.         }
  408.     zzcharfull = 1;
  409.     if ( state == dfa_base[zzauto] ){
  410.         if (zznextpos < lastpos){
  411.             *(zznextpos++) = zzchar;
  412.         }else{
  413.             zzbufovf = 1;
  414.         }
  415.         *zznextpos = '\0';
  416.         /* make sure doesn't get stuck */
  417.         zzadvance();
  418.     }else{
  419.         *zznextpos = '\0';
  420.     }
  421. #endif
  422. #ifdef ZZCOL
  423.     zzendcol -= zzcharfull;
  424. #endif
  425.     zzendexpr = zznextpos -1;
  426.     zzadd_erase = 0;
  427.     (*actions[accepts[state]])();
  428.     switch (zzadd_erase) {
  429.         case 1: goto skip;
  430.         case 2: goto more;
  431.     }
  432. }
  433.  
  434. void
  435. zzadvance()
  436. {
  437.     if (zzstream_in) { ZZGETC_STREAM; zzcharfull = 1; ZZINC;}
  438.     if (zzfunc_in) { ZZGETC_FUNC; zzcharfull = 1; ZZINC;}
  439.     if (zzstr_in) { ZZGETC_STR; zzcharfull = 1; ZZINC;}
  440.     if (!(zzstream_in || zzfunc_in || zzstr_in)){
  441.         zzerr_in();
  442.     }
  443. }
  444.  
  445. void
  446. #ifdef __STDC__
  447. zzerrstd(char *s)
  448. #else
  449. zzerrstd(s)
  450. char *s;
  451. #endif
  452. {
  453.         fprintf(stderr,
  454.                 "%s near line %d (text was '%s')\n",
  455.                 ((s == NULL) ? "Lexical error" : s),
  456.                 zzline,zzlextext);
  457. }
  458.  
  459. int
  460. zzerr_in()
  461. {
  462.     fprintf(stderr,"No input stream, function, or string\n");
  463.     /* return eof to get out gracefully */
  464.     return EOF;
  465. }
  466.  
  467. #endif
  468.