GME  13
DLexer.cpp
Go to the documentation of this file.
00001 /* DLexer.c
00002  *
00003  * SOFTWARE RIGHTS
00004  *
00005  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
00006  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
00007  * company may do whatever they wish with source code distributed with
00008  * PCCTS or the code generated by PCCTS, including the incorporation of
00009  * PCCTS, or its output, into commerical software.
00010  *
00011  * We encourage users to develop software with PCCTS.  However, we do ask
00012  * that credit is given to us for developing PCCTS.  By "credit",
00013  * we mean that if you incorporate our source code into one of your
00014  * programs (commercial product, research project, or otherwise) that you
00015  * acknowledge this fact somewhere in the documentation, research report,
00016  * etc...  If you like PCCTS and have developed a nice tool with the
00017  * output, please mention that you developed it using PCCTS.  In
00018  * addition, we ask that this header remain intact in our source code.
00019  * As long as these guidelines are kept, we expect to continue enhancing
00020  * this system and expect to make other tools available as they are
00021  * completed.
00022  *
00023  * ANTLR 1.33
00024  * Terence Parr
00025  * Parr Research Corporation
00026  * with Purdue University and AHPCRC, University of Minnesota
00027  * 1989-1998
00028  */
00029 
00030 #define ZZINC {if ( track_columns ) (++_endcol);}
00031 
00032 #define ZZGETC {ch = input->nextChar(); cl = ZZSHIFT(ch);}
00033 
00034 #define ZZNEWSTATE      (newstate = dfa[state][cl])
00035 
00036 #ifndef ZZCOPY
00037 #define ZZCOPY  \
00038         /* Truncate matching buffer to size (not an error) */   \
00039         if (nextpos < lastpos){                         \
00040                 *(nextpos++) = ch;                      \
00041         }else{                                                  \
00042                 bufovf = 1;                                     \
00043         }
00044 #endif
00045 
00046 void DLGLexer::
00047 mode( int m )
00048 {
00049         /* points to base of dfa table */
00050         if (m<MAX_MODE){
00051                 automaton = m;
00052                 /* have to redo class since using different compression */
00053                 cl = ZZSHIFT(ch);
00054         }else{
00055                 sprintf((char *)ebuf,"Invalid automaton mode = %d ",m);
00056                 errstd(ebuf);
00057         }
00058 }
00059 
00060 ANTLRTokenType DLGLexer::
00061 nextTokenType(void)
00062 {
00063         register int state, newstate;
00064         /* last space reserved for the null char */
00065         register DLGChar *lastpos;
00066         ANTLRTokenType tk;
00067 
00068 skip:
00069         bufovf = 0;
00070         lastpos = &_lextext[_bufsize-1];
00071         nextpos = _lextext;
00072         _begcol = _endcol+1;
00073 more:
00074         _begexpr = nextpos;
00075         if ( interactive ) {
00076                 /* interactive version of automaton */
00077                 /* if there is something in ch, process it */
00078                 state = newstate = dfa_base[automaton];
00079                 if (charfull){
00080                         ZZINC;
00081                         ZZCOPY;
00082                         ZZNEWSTATE;
00083                 }
00084                 while (alternatives[newstate]){
00085                         state = newstate;
00086                         ZZGETC;
00087                         ZZINC;
00088                         ZZCOPY;
00089                         ZZNEWSTATE;
00090                 }
00091                 /* figure out if last character really part of token */
00092                 if ((state != dfa_base[automaton]) && (newstate == DfaStates)){
00093                         charfull = 1;
00094                         --nextpos;
00095                 }else{
00096                         charfull = 0;
00097                         state = newstate;
00098                 }
00099                 *(nextpos) = '\0';
00100                 /* Able to transition out of start state to some non err state?*/
00101                 if ( state == dfa_base[automaton] ){
00102                         /* make sure doesn't get stuck */
00103                         advance();
00104                 }
00105         }
00106         else { /* non-interactive version of automaton */
00107                 if (!charfull)
00108                         advance();
00109                 else
00110                         ZZINC;
00111                 state = dfa_base[automaton];
00112                 while (ZZNEWSTATE != DfaStates) {
00113                         state = newstate;
00114                         ZZCOPY;
00115                         ZZGETC;
00116                         ZZINC;
00117                 }
00118                 charfull = 1;
00119                 if ( state == dfa_base[automaton] ){
00120                         if (nextpos < lastpos){
00121                                 *(nextpos++) = ch;
00122                         }else{
00123                                 bufovf = 1;
00124                         }
00125                         *nextpos = '\0';
00126                         /* make sure doesn't get stuck */
00127                         advance();
00128                 }else{
00129                         *nextpos = '\0';
00130                 }
00131         }
00132         if ( track_columns ) _endcol -= charfull;
00133         _endexpr = nextpos -1;
00134         add_erase = 0;
00135 #ifdef OLD
00136         tk = (ANTLRTokenType)
00137                  (*actions[accepts[state]])(this);      // must pass this manually
00138                                                                                         // actions is not a [] of pointers
00139                                                                                         // to member functions.
00140 #endif
00141         tk = (this->*actions[accepts[state]])();
00142 
00143 // MR1
00144 // MR1 11-Apr-97  Help for tracking DLG results
00145 // MR1
00146 
00147 #ifdef DEBUG_LEXER
00148 
00149 /* MR1 */        if (debugLexerFlag) {
00150 /* MR1 */          if (parser != NULL) {
00151 /* MR1 */            printf("\ntoken name=%s",parser->parserTokenName(tk));
00152 /* MR1 */          } else {
00153 /* MR1 */            printf("\ntoken nnumber=%d",tk);
00154 /* MR1 */          };
00155 /* MR1 */          printf(" lextext=(%s) mode=%d",
00156 /* MR1 */                (_lextext[0]=='\n' && _lextext[1]==0) ?
00157 /* MR1 */                       "newline" : _lextext,
00158 /* MR1 */                               automaton);
00159 /* MR1 */          if (interactive && !charfull) {
00160 /* MR1 */            printf(" char=empty");
00161 /* MR1 */          } else {
00162 /* MR1 */            if (ch=='\n') {
00163 /* MR1 */              printf(" char=newline");
00164 /* MR1 */            } else {
00165 /* MR1 */              printf(" char=(%c)",ch);
00166 /* MR1 */            };
00167 /* MR1 */          };
00168 /* MR1 */          printf(" %s\n",
00169 /* MR1 */                (add_erase==1 ? "skip()" :
00170 /* MR1 */                 add_erase==2 ? "more()" :
00171 /* MR1 */                 ""));
00172 /* MR1 */        };
00173 
00174 #endif
00175 
00176         switch (add_erase) {
00177                 case 1: goto skip;
00178                 case 2: goto more;
00179         }
00180         return tk;
00181 }
00182 
00183 void DLGLexer::
00184 advance()
00185 {
00186         if ( input==NULL ) err_in();
00187         ZZGETC; charfull = 1; ZZINC;
00188 }