File:  [LON-CAPA] / capa / capa51 / pProj / capaLexerDef.flex
Revision 1.9: download - view: text, annotated - select for diffs
Fri Jun 30 21:36:16 2000 UTC (23 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- gave everyone the GPL header

    1: /* main CAPA lexer
    2:    Copyright (C) 1992-2000 Michigan State University
    3: 
    4:    The CAPA system is free software; you can redistribute it and/or
    5:    modify it under the terms of the GNU Library General Public License as
    6:    published by the Free Software Foundation; either version 2 of the
    7:    License, or (at your option) any later version.
    8: 
    9:    The CAPA system is distributed in the hope that it will be useful,
   10:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   12:    Library General Public License for more details.
   13: 
   14:    You should have received a copy of the GNU Library General Public
   15:    License along with the CAPA system; see the file COPYING.  If not,
   16:    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   17:    Boston, MA 02111-1307, USA.  */
   18: 
   19: /*------------------------------------------------------------------------*/
   20: /*         capaLexerDef.flex   created by Isaac Tsai Jul 15 1996          */
   21: /*                             added /END(variable)                       */
   22: /*                             added /HIN  .... /DIS(variable) ...        */
   23: /*                             Jan 15 1998  /{KeyWord}{KeyWord}{KeyWord}  */
   24: /*                               replaced by /DIS                         */
   25: /*   catch "No /END() statement found"                                    */
   26: /*   catch "/DIS(")" and "/DIS(""")" errors "                             */
   27: /*   catch "/LET var = "                                                  */
   28: /*   add a new token EoL to indicate '\n' '\r''\n' and '\r'               */
   29: /*   This file is based on flex 2.5.3, flex 2.3 apparantly cannot take it :-( */
   30: /*   DONE /RMAP() function July 14, 1998 */
   31: /*   DONE /AND /OR answer formats  July 20, 1998 */
   32: /*   DONE /IF ()  /ENDIF   July 26, 1998 */
   33: /*   DONE /WHILE ()  /ENDWHILE August 10 1998 */
   34: /*   DONE /VERB /ENDVERB    Feb 20 1998  */
   35: /*------------------------------------------------------------------------*/
   36: /**************************************************************************/
   37: 
   38: %{
   39: 
   40: #include <stdio.h>
   41: #include <stdlib.h>       /* strtod(), strtol() */
   42: #include <string.h>
   43: #ifdef NeXT
   44: #include <sys/file.h>
   45: #else 
   46: #include <unistd.h>       /* access() */
   47: #endif
   48: 
   49: #define YYSTYPE Symbol*
   50: #include "capaCommon.h"   /* capa_access() */
   51: #include "capaParser.h"   /* _symbol structure def */
   52: #include "capaToken.h"    /* from YACC -d capaGrammarDef.y */
   53: #include "lex_debug.h"    /* defined RETURN(xxx) macro */
   54: 
   55: 
   56: 
   57: /* ============================================== begin of code */
   58: 
   59: #define LEX_BUFLEN  (8*1024)     /* lexical buffer size (for each opened file) */
   60: 
   61: #ifdef  YYLMAX 
   62: #undef  YYLMAX
   63: #define YYLMAX   8192
   64: #endif
   65: 
   66: void yyfatalerror(char*msg);
   67: #define YY_FATAL_ERROR yyfatalerror
   68: 
   69: #ifdef   LEX_DBUG
   70: #define  LLDBUG_PRL1(xx)        { printf("Line %d ",Current_line[Input_idx]); printf(xx); fflush(stdout); }
   71: #define  LLDBUG_PRL2(xx,yy)     { printf("Line %d ",Current_line[Input_idx]); printf(xx,yy); fflush(stdout); }
   72: #define  LLDBUG_PR1(xx)         { printf(xx); fflush(stdout); }
   73: #define  LLDBUG_PR2(xx,yy)      { printf(xx,yy); fflush(stdout); }
   74: #define  LLDBUG_PR3(xx,yy,zz)   { printf(xx,yy,zz); fflush(stdout); }
   75: #else
   76: #define  LLDBUG_PRL1(xx)        { }
   77: #define  LLDBUG_PRL2(xx,yy)     { }
   78: #define  LLDBUG_PR1(xx)         { }
   79: #define  LLDBUG_PR2(xx,yy)      { }
   80: #define  LLDBUG_PR3(xx,yy,zz)   { }
   81: #endif
   82: 
   83: #ifdef   LEX_INPUT_DBUG
   84: #define  LIDBUG_PR1(xx)         { printf(xx); fflush(stdout); }
   85: #define  LIDBUG_PR2(xx,yy)      { printf(xx,yy); fflush(stdout); }
   86: #define  LIDBUG_PR3(xx,yy,zz)   { printf(xx,yy,zz); fflush(stdout); }
   87: #else
   88: #define  LIDBUG_PR1(xx)         { }
   89: #define  LIDBUG_PR2(xx,yy)      { }
   90: #define  LIDBUG_PR3(xx,yy,zz)   { }
   91: #endif
   92: 
   93: #ifdef   USE_DYNAMIC_SYMBOLS
   94: #define  USE_DYNAMIC_LEXBUFS 
   95: #endif
   96: 
   97: Symbol       *yylval;       /* global pointer to symbol */
   98: 
   99: FILE         *(Input_stream[MAX_OPENED_FILE]);             /* <-- perhaps we can use linked list */
  100: char          Opened_filename[MAX_OPENED_FILE][QUARTER_K]; /* <-- perhaps we can use linked list */
  101: int           Input_idx;
  102: int           Lexi_pos[MAX_OPENED_FILE];   /* Current position in the line */
  103: 
  104: #ifdef  USE_DYNAMIC_LEXBUFS
  105: char         *(Lexi_buf[MAX_OPENED_FILE]);          /* Line Buffer for current file  */
  106: 
  107: #else 
  108: char          Lexi_buf[MAX_OPENED_FILE][LEX_BUFLEN+4];          /* Line Buffer for current file  */
  109: 
  110: #endif  /* USE_DYNAMIC_LEXBUFS */
  111: 
  112: char          String_buf[LEX_BUFLEN];              /* Constant String buffer <-- perhaps we can use char pointer  */
  113: char         *Dynamic_buf;
  114: int           Dynamic_buf_max;
  115: int           Dynamic_buf_cur;
  116: 
  117: 
  118: static   int  End_of_input;
  119: static   int  Pcount, Bcount; /* static means only available in this file */
  120: /* --------------------------------------------------------------------------- */
  121: /* GLOBAL VARIABLES                                                            */
  122: /* --------------------------------------------------------------------------- */
  123: int           Lexi_line;                   /* Current source file line number, counting from beginning */
  124: extern int    Current_line[MAX_OPENED_FILE];
  125: 
  126: int           Func_idx;
  127: Symbol        FuncStack[MAX_FUNC_NEST];    /* <-- perhaps we can use linked list */
  128: 
  129: int           Array_idx;
  130: Symbol       *ArraySymbList_p;
  131: Symbol       *ArraySymbLast_p;
  132: Symbol       *FmlSymbList_p;
  133: Symbol       *FmlSymbLast_p;
  134: int           FmlSymb_cnt;
  135: int           Symb_count;
  136: 
  137: int           IFstatus[MAX_FUNC_NEST];     /* <-- perhaps we can use linked list */
  138: int           IFcurrent[MAX_FUNC_NEST];    /* <-- perhaps we can use linked list */
  139: int           IFcount;
  140: WhileLoop_t   WhileStack[MAX_FUNC_NEST];   /* <-- perhaps we can use linked list */
  141: int           While_idx, Wcount;
  142: 
  143: #ifdef  USE_DYNAMIC_SYMBOLS
  144: Symbol       *SymbList_p;
  145: Symbol       *SymbLast_p;
  146: #else
  147: Symbol        SymbArray[MAX_SYMB_COUNT];
  148: #endif /* USE_DYNAMIC_SYMBOLS */
  149: 
  150: 
  151: char         *Current_char_p;                      /* Collect string constant */
  152: extern        char              *EndText_p;
  153: extern        char              *StartText_p;
  154: extern        Problem_t         *LexiProblem_p;
  155: extern        Problem_t         *LastProblem_p;
  156: int           first_run=1;
  157: int           Stop_Parser;
  158: #define  FLEX
  159: 
  160: #define  YY_STACK_USED   1  /* for yy_push_state(), yy_pop_state() */
  161: 
  162: #ifdef   FLEX
  163: 
  164: int      capaL_unput();
  165: int      capaL_input();
  166: 
  167: 
  168: /* YY_INPUT() 
  169:    Controls scanner input.  By default, YY_INPUT reads from the
  170:    file-pointer yyin.  Its action is to place up to max_size
  171:    characters in the character array buf and return in the
  172:    integer variable result either the number of characters read
  173:    or the constant YY_NULL to indicate EOF.  
  174:    max_size is defined to be num_to_read = 8192 in liby
  175:    Following is a sample 
  176:    redefinition of YY_INPUT, in the definitions section of
  177:    the input file:
  178:    
  179:    %{
  180:    #undef YY_INPUT
  181:    #define YY_INPUT(buf,result,max_size)\
  182:    {\
  183:        int c = getchar();\
  184:        result = (c == EOF) ? YY_NULL : (buf[0] = c, 1);\
  185:    }
  186:    %}
  187:  
  188: */
  189: 
  190: /* fgets() reads the input stream until 
  191:    n-1 bytes have been read  OR
  192:    a newline character is read and transferred to string  OR
  193:    an EOF (End-of-File) condition is encountered
  194:    
  195:    The string is then terminated with a NULL character.  
  196:   
  197:   ii = fseek(FILE *stream,0L,SEEK_END) ;
  198:   if(ii!=0) { error }
  199:   leng = ftell(FILE *stream) + 1 ;
  200:   fseek(FILE *stream,0L,SEEK_SET) ;
  201:   Lexi_buf[Input_idx] = (char *)capa_malloc(sizeof(char)*leng,1);
  202:   
  203: */
  204: 
  205: 
  206: #ifdef AVOIDYYINPUT
  207: #define MAX_INCLUDE_DEPTH 10
  208: YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  209: int include_stack_ptr = 0;
  210: #else
  211: #ifdef USE_DYNAMIC_LEXBUFS
  212: #define NEWYYINPUT
  213: #endif
  214: 
  215: #ifdef  NEWYYINPUT
  216: void newyy_input (char *buf,int *result,int max_size);
  217: #define YY_INPUT(buf,result,max_size) newyy_input(buf,&result,max_size)
  218: 
  219: #else
  220: #ifdef  USE_DYNAMIC_LEXBUFS
  221: 
  222: #define  YY_INPUT(buf,result,max_size) \
  223:   { int ii, leng, out_of_char; \
  224:     if (!Lexi_line) { /* was startup */ \
  225:        for(ii=0;ii < MAX_OPENED_FILE;ii++) { \
  226:          Lexi_buf[ii] = NULL; \
  227:          Lexi_pos[ii] = 0; \
  228:          Current_line[ii] = 0; \
  229:        } \
  230:        Input_idx = 0; \
  231:        first_run=0; \
  232:        yyin = Input_stream[Input_idx]; LIDBUG_PR1("<<yy_input() startup>>\n"); \
  233:     } \
  234:     out_of_char = 0; \
  235:     if ( Lexi_buf[Input_idx] == NULL ) { \
  236:       Lexi_buf[Input_idx] = (char *)capa_malloc(sizeof(char)*LEX_BUFLEN+1,1); out_of_char=1; \
  237:     } else { \
  238:       if (!Lexi_buf[Input_idx][Lexi_pos[Input_idx]]) { /* test if the line buffer is empty or at the end */ \
  239:         out_of_char=1; \
  240:       } \
  241:     } \
  242:     if( out_of_char ) { \
  243:       if (fgets(Lexi_buf[Input_idx],LEX_BUFLEN-1,Input_stream[Input_idx])==NULL) { /* read in one line */ \
  244:         LIDBUG_PR2("<<yy_input() fgets() returns NULL, input index=%d>>\n",Input_idx); \
  245:         if( (Input_idx > 0) && ( Lexi_buf[Input_idx][Lexi_pos[Input_idx]] == '\0') ) { \
  246:           LIDBUG_PR2("<<yy_input() close an input stream, input index=%d>>\n",Input_idx); \
  247:           fclose(Input_stream[Input_idx]); \
  248:           capa_mfree((char *)Lexi_buf[Input_idx]); \
  249:           Lexi_buf[Input_idx] = NULL; \
  250:           Input_idx--; \
  251:           yyin = Input_stream[Input_idx]; \
  252:           /* (Lexi_pos[Input_idx])++; */ \
  253:           buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; \
  254:           result = 1; \
  255:         } else { \
  256:           result = YY_NULL; /* End of File */ \
  257:         } \
  258:       } else { /* successfully read in one line */ \
  259:         if (Lexi_buf[Input_idx]==NULL) puts("Whatup?");\
  260:         leng = strlen(Lexi_buf[Input_idx]); \
  261:         LIDBUG_PR3("<<yy_input() read into buffer a line(leng=%d), input index=%d>>\n",leng,Input_idx);  \
  262:         Lexi_pos[Input_idx] = 0; \
  263:         Lexi_line++; \
  264:         Current_line[Input_idx]++; \
  265:         (Lexi_pos[Input_idx])++; \
  266:         buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1];  \
  267:         /* need to take care of return continuation conditions */ \
  268:         /*  so, we return to one-char-at-a-time approach */ \
  269:         /* for(ii=0;ii<leng;ii++) { */ \
  270:         /*  buf[ii] = Lexi_buf[Input_idx][ii]; */ \
  271:         /* } */ \
  272:         /* buf[ii] = '\0'; */ \
  273:         /* printf("YY_INPUT()(Lexi_line=%d,max size=%d)(%c)",Lexi_line,max_size,buf[0]); */ \
  274:         result = 1; \
  275:       } \
  276:     } else { \
  277:       /* LIDBUG_PR2("<<yy_input() increase Lexi_pos, input index=%d>>\n",Input_idx);  */ \
  278:       (Lexi_pos[Input_idx])++; \
  279:       buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; \
  280:       result = 1; \
  281:     } \
  282:     if (Stop_Parser==1) { \
  283:       result = YY_NULL; \
  284:     } \
  285:   }
  286:   
  287: #else 
  288: 
  289: #define  YY_INPUT(buf,result,max_size) \
  290:   { int ii, leng; \
  291:     if (!Lexi_line) { /* was startup */ \
  292:        for(ii=0;ii < MAX_OPENED_FILE;ii++) { \
  293:          Lexi_buf[ii][0]=0; \
  294:          Lexi_pos[ii] = 0; \
  295:          Current_line[ii] = 0; \
  296:        } \
  297:        Input_idx = 0; \
  298:        first_run=0; \
  299:        yyin = Input_stream[Input_idx]; LIDBUG_PR1("<<yy_input() startup>>\n"); \
  300:     } \
  301:     if (!Lexi_buf[Input_idx][Lexi_pos[Input_idx]]) { /* test if the line buffer is empty or at the end */ \
  302:       if (fgets(Lexi_buf[Input_idx],LEX_BUFLEN-1,Input_stream[Input_idx])==NULL) { /* read in one line */ \
  303:         LIDBUG_PR2("<<yy_input() fgets() returns NULL, input index=%d>>\n",Input_idx); \
  304:         if( (Input_idx > 0) && ( Lexi_buf[Input_idx][Lexi_pos[Input_idx]] == '\0') ) { \
  305:           LIDBUG_PR2("<<yy_input() close an input stream, input index=%d>>\n",Input_idx); \
  306:           fclose(Input_stream[Input_idx]); \
  307:           Input_idx--; \
  308:           yyin = Input_stream[Input_idx]; \
  309:           /* (Lexi_pos[Input_idx])++; */ \
  310:           buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; \
  311:           result = 1; \
  312:         } else { \
  313:           result = YY_NULL; /* End of File */ \
  314:         } \
  315:       } else { /* successfully read in one line */ \
  316:         leng = strlen(Lexi_buf[Input_idx]); \
  317:         LIDBUG_PR3("<<yy_input() read into buffer a line(leng=%d), input index=%d>>\n",leng,Input_idx);  \
  318:         Lexi_pos[Input_idx] = 0; \
  319:         Lexi_line++; \
  320:         Current_line[Input_idx]++; \
  321:         (Lexi_pos[Input_idx])++; \
  322:         buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1];  \
  323:         /* need to take care of return continuation conditions */ \
  324:         /*  so, we return to one-char-at-a-time approach */ \
  325:         /* for(ii=0;ii<leng;ii++) { */ \
  326:         /*  buf[ii] = Lexi_buf[Input_idx][ii]; */ \
  327:         /* } */ \
  328:         /* buf[ii] = '\0'; */ \
  329:         /* printf("YY_INPUT()(Lexi_line=%d,max size=%d)(%c)",Lexi_line,max_size,buf[0]); */ \
  330:         result = 1; \
  331:       } \
  332:     } else { \
  333:       /* LIDBUG_PR2("<<yy_input() increase Lexi_pos, input index=%d>>\n",Input_idx);  */ \
  334:       (Lexi_pos[Input_idx])++; \
  335:       buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; \
  336:       result = 1; \
  337:     } \
  338:     if (Stop_Parser==1) { \
  339:       result = YY_NULL; \
  340:     } \
  341:   }
  342: #endif  /* USE_DYNAMIC_LEXBUFS */
  343: #endif /*NEWYYINPUT*/
  344: #endif /*AVOIDYYINPUT*/
  345: 
  346: #else
  347: 
  348: #undef  input
  349: #undef  unput
  350: 
  351: #endif
  352: 
  353: int capa_eof();
  354: %}
  355: 
  356: Alpha      [a-zA-Z_]
  357: KeyChar    [A-Z]
  358: AlphaNum   [a-zA-Z_0-9]
  359: Number     [0-9]
  360: HexNumber  [0-9a-fA-F]
  361: Space      [ \t]
  362: Spaces     ({Space}*)
  363: FileName   (\"[^"\n]*\")
  364: Qchar      ([0-9a-zA-Z \t!?\._,:;'"`~@#$%\^&\+\-\*=|\[\]{}()])
  365: Operator   ([=\+\-\*/%<>!&|,])
  366: Identifier ([a-zA-Z_][a-zA-Z_0-9]*)
  367: EndLine    ([\r][\n]|[\n])
  368: 
  369: %a 10500
  370: %o 15000
  371: %k 10000
  372: %p 10000
  373: %n 1000
  374: %x  S_COMMENT   S_HINT S_HINTEXLAINX  S_IMPORT S_EXPLAIN S_ENDX    S_UNIT   S_IGNORE  
  375: %x  S_SKIP      S_VARIABLE S_LET  S_DEFINE     S_TEXT    S_MAP     S_FIGURE S_ANSWER 
  376: %x  S_STRING    S_ANSCONTINUE     S_TRUE_FALSE_STMT      S_IF_SKIP S_WHILE_SKIP
  377: %x  S_NEXT_LINE S_RANDOMORDER     S_VERB
  378: %array
  379: 
  380: 
  381: 
  382: %%
  383: 
  384: <S_IGNORE>{
  385: {EndLine}                       BEGIN S_IGNORE;
  386: [^\n]*$                         BEGIN S_IGNORE;
  387: <<EOF>>                        {
  388:                                   capa_eof();
  389: #ifndef AVOIDYYINPUT
  390: 				  yyterminate();
  391: #endif
  392:                                }
  393: }
  394: 
  395: <S_COMMENT>[^\n]*{EndLine}      BEGIN S_TEXT;
  396: 
  397: <S_TEXT>{
  398: ^{Spaces}"/LET" |
  399: ^{Spaces}"/BEG"                  { LLDBUG_PRL1("[LET]"); Pcount = 0; init_funcstack(); BEGIN S_LET; RETURN(CAPA_LET); }
  400: ^{Spaces}"/VERB"                 { 
  401:                                    LLDBUG_PRL1("[VERBATIM]");
  402: 				   Dynamic_buf = (char *)capa_malloc(sizeof(char),10);
  403: 				   Dynamic_buf_max = 10;
  404: 				   Dynamic_buf_cur = 0;
  405: 				   Dynamic_buf[0] = '\0';
  406:                                    BEGIN S_VERB; 
  407:                                  }
  408: ^{Spaces}"/HIN"{Alpha}*{Spaces}  { LLDBUG_PRL1("[HIN]"); Current_char_p = String_buf;  BEGIN S_HINT; }
  409: ^{Spaces}"/EXP"{Alpha}*{Spaces}  { LLDBUG_PRL1("[EXP]"); Current_char_p = String_buf;  BEGIN S_EXPLAIN; }
  410: ^{Spaces}"/IMP"{Alpha}*{Space}+  { LLDBUG_PRL1("[IMP]"); BEGIN S_IMPORT; }
  411: ^{Spaces}"/END"                  { LLDBUG_PRL1("[END]");  
  412:                                     if ( (LexiProblem_p !=NULL) && 
  413: 					 (LexiProblem_p->question != NULL) && 
  414: 					 (LexiProblem_p->ans_type == 0)) {
  415: 				      EndText_p=strsave(LexiProblem_p->question);
  416: 				      free_problems(LexiProblem_p);
  417: 				      LexiProblem_p=NULL;
  418: 				    } else {
  419: 				      EndText_p=NULL;
  420: 				    }
  421: 				    End_of_input = 1; BEGIN S_IGNORE;RETURN(CAPA_END); 
  422:                                   }
  423: ^{Spaces}"/START"[^\n]*          { LLDBUG_PRL1("[START]");  
  424:                                     if (LexiProblem_p !=NULL && 
  425: 					LexiProblem_p->question != NULL) {
  426: 				      StartText_p=strsave(LexiProblem_p->question);
  427: 				    } else {
  428: 				      StartText_p=NULL;
  429: 				    }
  430: 				    BEGIN S_TEXT;
  431: 				    RETURN(CAPA_START); 
  432:                                   }
  433:                                   
  434: ^{Spaces}"/END"{Spaces}[\(]{Spaces}      { LLDBUG_PRL1("[END()]"); BEGIN S_ENDX; }
  435: ^"/DEF"                       { Bcount = 0; BEGIN S_DEFINE; RETURN(CAPA_DEF); }
  436: ^{Spaces}"/ANS"               { LLDBUG_PRL2("[ANS(%s)]",yytext); Pcount = 0; BEGIN S_ANSWER; RETURN(CAPA_ANS); }
  437: ^{Spaces}"/SUBJECTIVE"        { LLDBUG_PRL1("[SUBJECTIVE ANSWER]"); Pcount = 0; BEGIN S_ANSWER; RETURN(CAPA_SUBJ); }
  438: ^{Spaces}"/MAP"               { LLDBUG_PRL1("[MAP]");  Pcount = 0; BEGIN S_MAP;   RETURN(CAPA_MAP); }
  439: ^{Spaces}"/RMAP"              { LLDBUG_PRL1("[RMAP]"); Pcount = 0; BEGIN S_MAP;   RETURN(CAPA_RMAP); }
  440: ^{Spaces}"/RQO"{Spaces}*      { LLDBUG_PRL1("[RQO]"); BEGIN S_RANDOMORDER; 
  441:                                 RETURN(CAPA_RQO);}
  442: ^{Spaces}"/ENDWHILE"([^\n])*          { long int  file_pos;       
  443:                                 int   top_item, input_idx;
  444:                                 LLDBUG_PRL2("[ENDWHILE While_idx=<%d>]\n",While_idx);
  445:                                 
  446:                                 top_item = While_idx - 1;
  447:                                 if( top_item < 0 ) { /* strange things must have happened here! */
  448:                                   
  449:                                 } else {
  450:                                   input_idx = WhileStack[top_item].input_idx;
  451:                                   file_pos = WhileStack[top_item].pos_idx;
  452:                                   Current_line[input_idx] = WhileStack[top_item].line_idx;
  453:                                   Lexi_pos[input_idx] = 0;
  454:                                   fseek(Input_stream[input_idx],file_pos,SEEK_SET);
  455:                                   fgets(Lexi_buf[input_idx],LEX_BUFLEN-1,Input_stream[input_idx]);
  456:                                   While_idx--;
  457:                                 }
  458:                                 BEGIN S_TEXT;
  459:                               }
  460: "/WHILE"                      |
  461: ^{Spaces}"/WHILE"             { long int file_pos;
  462:                                 int      leng;
  463:                                 LLDBUG_PRL2("[WHILE While_idx=<%d>]\n",While_idx);
  464:                                 leng = strlen(Lexi_buf[Input_idx]);  /* length of current line */
  465:                                 /* <-- because we use fgets() to read input, 
  466:                                        thus ftell() will give the starting position of next line */
  467:                                 WhileStack[While_idx].input_idx = Input_idx;
  468:                                 file_pos = ftell(Input_stream[Input_idx]);
  469:                                 file_pos -= leng;  /* calibrate the current line length */
  470:                                 WhileStack[While_idx].pos_idx = file_pos;  /* begin of current line */
  471:                                 WhileStack[While_idx].line_idx = Current_line[Input_idx];
  472:                                 While_idx++;  /* advance the stack pointer */
  473:                                 
  474:                                 BEGIN S_TRUE_FALSE_STMT; RETURN(CAPA_WHILE); 
  475:                                 
  476:                               }
  477: ^{Spaces}"/IF"                { IFcount++; IFcurrent[IFcount] = RUN_IF_PORTION;
  478:                                 LLDBUG_PRL2("[IF <IFcount=%d>]",IFcount);  BEGIN S_TRUE_FALSE_STMT; RETURN(CAPA_IF); }
  479: ^{Spaces}"/ELSE"([^\n])*        { LLDBUG_PRL2("[ELSE <IFcount=%d>]\n",IFcount); 
  480:                                 IFcurrent[IFcount] = RUN_ELSE_PORTION;
  481:                                 if( IFstatus[IFcount] == IF_TRUE ) {
  482:                                   LLDBUG_PRL1("[ELSE begin Skip]\n");
  483:                                   BEGIN S_IF_SKIP;
  484:                                 }
  485:                               }
  486: ^{Spaces}"/ENDIF"([^\n])*    { IFcount--; LLDBUG_PRL2("[ENDIF <IFcount=%d>]\n",IFcount); 
  487:                               }
  488: "/AND"                        { LLDBUG_PRL1("[AND]"); BEGIN S_ANSCONTINUE; RETURN(ANS_AND); }
  489: "/DIS"                        { /* since S_VARIABLE treat {Space} as null, so here we do not match ( */
  490:                                 /* so that between /DIS and ( can have as many {Space} as we want */
  491:                                 LLDBUG_PR1("[DIS<]");
  492:                                 init_funcstack();
  493:                                 Pcount = 0; BEGIN S_VARIABLE; 
  494:                                 RETURN(CAPA_VAR); 
  495:                               }
  496: "/OR"                         { LLDBUG_PRL1("[OR]"); BEGIN S_ANSCONTINUE;  RETURN(ANS_OR); }
  497: {EndLine}                     { LLDBUG_PR1("[EoL within S_TEXT]\n"); /* end of the whole text line */ 
  498:                                 RETURN(EoL); }
  499: [\\]{Space}*{EndLine}         { LLDBUG_PR2("[\\EoL continue](%s)",yytext); /* continuation on next line */ }                       
  500: ^{Spaces}"//"[^\n]*$		      { LLDBUG_PRL2("[COMMENT<%s>]\n",yytext);    BEGIN S_COMMENT; }
  501: [^/\n\\]+$  |
  502: [/]         | 
  503: [\\]                          {  yylval = (Symbol *) capa_malloc(1, sizeof(Symbol));
  504:                                 yylval->s_str = strsave(yytext);
  505:                                 LLDBUG_PR2("[TEXT_LINE<%s>]",yytext);
  506: 			        RETURN(TEXT_LINE); 
  507: 			      }
  508: ([^/\n])+[/] |
  509: ([^/\n])+[\\]                 { /* matches anything until a '/' or a '\' */
  510:                                 LLDBUG_PR2("[TEXT_LINE( )<%s>]",yytext);
  511:                                 
  512:                                 yyless(yyleng-1); /* push back the last char */
  513:                                 yylval = (Symbol *) capa_malloc(1, sizeof(Symbol));
  514:                                 yylval->s_str = strsave(yytext);
  515:                                 
  516:                                 BEGIN S_TEXT;
  517:                                 RETURN(TEXT_LINE); 
  518:                               }
  519: <<EOF>>                       { 
  520: #ifdef AVOIDYYINPUT
  521:                                 char    warn_msg[ONE_K];
  522: 
  523:                                 if ( (--include_stack_ptr < 0) || Stop_Parser) {
  524: 				  if (Stop_Parser) {
  525:                                     if ( LexiProblem_p!=NULL &&
  526: 					 LexiProblem_p->question != NULL)
  527: 				      EndText_p=strsave(LexiProblem_p->question);
  528: 				    while (include_stack_ptr >= 0) {
  529: 				      yy_delete_buffer( YY_CURRENT_BUFFER );
  530: 				      yy_switch_to_buffer(
  531: 						 include_stack[include_stack_ptr]);
  532: 				      --include_stack_ptr;
  533: 				    }
  534: 				  } else {
  535: 				    sprintf(warn_msg,
  536: 					    "at End-of-File, a /END is needed.\n");
  537: 				    capa_msg(MESSAGE_ERROR,warn_msg);
  538: 				  }
  539: 				  free_problems(LexiProblem_p);
  540: 				  LexiProblem_p=NULL;
  541: 				  yyterminate();
  542: 				} else {
  543: 				  yy_delete_buffer( YY_CURRENT_BUFFER );
  544: 				  yy_switch_to_buffer(include_stack[include_stack_ptr]);
  545: 				}
  546: #else                              
  547: 				char    warn_msg[ONE_K];
  548:                                 if (!Stop_Parser) { 
  549: 				 sprintf(warn_msg,"at End-of-File, a /END is needed.\n");
  550: 				 capa_msg(MESSAGE_ERROR,warn_msg);
  551: 				} else {
  552: 				  if (LexiProblem_p != NULL &&
  553: 				      LexiProblem_p->question != NULL)
  554: 				    EndText_p=strsave(LexiProblem_p->question);
  555: 				}
  556:                                 capa_eof(); 
  557: 				yyterminate();
  558: #endif
  559:                               }
  560: }
  561: 
  562: 
  563: <S_ENDX>{
  564: {Alpha}{AlphaNum}*    { /* DONE: add codes to handle /END() */
  565: 				char *question_end=NULL;
  566: 				End_of_input = 1;
  567: 				if (EndText_p!=NULL) capa_mfree((char*)EndText_p);
  568: 				if ((LexiProblem_p!=NULL) &&
  569: 				    (LexiProblem_p->question != NULL) &&
  570: 				    (LexiProblem_p->ans_type == 0)) {
  571: 				  question_end=strsave(LexiProblem_p->question);
  572: 				}
  573:                                 if( yyleng > 0 ) {
  574:                                   
  575:                                   LLDBUG_PRL2("[END()<%s>]\n",yytext);
  576:                                   yylval = find_identifier(yytext);
  577:                                 
  578:                                   switch(yylval->s_type) {
  579:                                     case IDENTIFIER:
  580:                                     case I_VAR: case I_CONSTANT:
  581:                                     case R_VAR: case R_CONSTANT:
  582:                                          break;
  583:                                     case S_VAR: case S_CONSTANT:
  584:                                          EndText_p = strsave(yylval->s_str);
  585: 					 if (question_end) {
  586: 					   int leng; char *new_end;
  587: 					   leng = strlen(EndText_p) + 
  588: 					     strlen(question_end) + 1;
  589: 					   new_end = capa_malloc(sizeof(char), leng);
  590: 					   strcat(new_end, question_end);
  591: 					   strcat(new_end, EndText_p);  
  592: 					   capa_mfree(EndText_p);  
  593: 					   capa_mfree(question_end);  
  594: 					   EndText_p=new_end;
  595: 					 }
  596:                                          break;
  597:                                     default: break;
  598:                                   }
  599:                                 }
  600:                                 BEGIN S_IGNORE;   RETURN(CAPA_END);
  601:                               }
  602: {Space}*                      { /* ignore spaces */ }
  603: [\)]                          { /* a right paren */ 
  604:                                 if ( (LexiProblem_p != NULL) &&
  605: 				     (LexiProblem_p->question != NULL) && 
  606: 				     (LexiProblem_p->ans_type == 0)) {
  607: 				  EndText_p=strsave(LexiProblem_p->question);
  608: 				} else {
  609: 				  EndText_p=NULL;
  610: 				}
  611:                                 BEGIN S_IGNORE;   
  612: 				RETURN(CAPA_END);  
  613:                               }
  614: }
  615: 
  616: <S_HINT,S_EXPLAIN>{
  617: [/][Dd][Ii][Ss]{Space}*[\(]{Space}*  {  yy_push_state(S_HINTEXLAINX); }
  618: [^/\n]+[/\\]                    { char  *aptr = yytext;
  619:                                   int    ii;
  620:                                 
  621:                                   yyless(yyleng-1);
  622:                                   for(ii=0;ii<yyleng;ii++) {
  623:                                   *Current_char_p++ = *aptr++;
  624:                                 }
  625:                                 
  626:                               }
  627: [/]                            { *Current_char_p++ = '/'; }
  628: [\\]                           { *Current_char_p++ = '\\'; }
  629: [\\]{Space}*[\n]               { LLDBUG_PR1("[\\CR hint explain continue]"); /* Hint and explain continuation */ }
  630: [^/\n\\]+$                      { char  *aptr = yytext;
  631:                                 int    ii;
  632:                                 for(ii=0;ii<yyleng;ii++) {
  633:                                   *Current_char_p++ = *aptr++;
  634:                                 }
  635:                               }
  636: }
  637: <S_HINT>{
  638: {EndLine}                     {  LLDBUG_PR1("[CR hint]");
  639:                                  yylval = (Symbol *) capa_malloc(1, sizeof(Symbol));
  640: 		                *Current_char_p++ = '\n'; *Current_char_p = '\0';
  641: 	                         yylval->s_str = strsave(String_buf);
  642:                                  BEGIN S_TEXT; RETURN(HINT_LINE); 
  643:                               }
  644: }
  645: <S_EXPLAIN>{
  646: {EndLine}                     {  LLDBUG_PR1("[CR explain]");
  647:                                  yylval = (Symbol *) capa_malloc(1, sizeof(Symbol));
  648: 		                *Current_char_p++ = '\n'; *Current_char_p = '\0';
  649: 	                         yylval->s_str = strsave(String_buf);
  650:                                  BEGIN S_TEXT; RETURN(EXPLAIN_LINE); 
  651:                               }
  652: }
  653: 
  654: <S_HINTEXLAINX>{
  655: {Alpha}{AlphaNum}*            { char   *aptr;
  656:                                 char    tmp_str[QUARTER_K],warn_msg[ONE_K];
  657:                                 int     ii, len;
  658:                                 Symbol *tmp_p;
  659:                                 
  660:                                 tmp_p = find_identifier(yytext);
  661:                                 switch(tmp_p->s_type) {
  662:                                   case IDENTIFIER:
  663:                                         sprintf(warn_msg,"var %s not defined.\n", yytext);
  664:                                         capa_msg(MESSAGE_ERROR,warn_msg);
  665:                                        break;
  666:                                   case I_VAR:
  667:                                   case I_CONSTANT: 
  668:                                          sprintf(tmp_str,"%ld",tmp_p->s_int);
  669:                                          len = strlen(tmp_str);
  670:                                          for(ii=0;ii< len;ii++) {
  671:                                            *Current_char_p++ = tmp_str[ii];
  672:                                          }
  673:                                        break;
  674:                                   case R_VAR:
  675:                                   case R_CONSTANT: 
  676:                                          sprintf(tmp_str,"%g",tmp_p->s_real);
  677:                                          len = strlen(tmp_str);
  678:                                          for(ii=0;ii< len;ii++) {
  679:                                            *Current_char_p++ = tmp_str[ii];
  680:                                          }
  681:                                        break;
  682:                                   case S_VAR:
  683:                                   case S_CONSTANT: 
  684:                                          len = strlen(tmp_p->s_str);
  685:                                          aptr = tmp_p->s_str;
  686:                                          for(ii=0;ii< len;ii++) {
  687:                                            *Current_char_p++ = *aptr++;
  688:                                          }
  689:                                        break;
  690:                                 }
  691:                               }
  692: {Space}+                      { }
  693: [)]                           {  yy_pop_state(); }
  694: }
  695: 
  696: <S_IMPORT>{
  697: {FileName}{Space}*             { parse_filename(yytext);    BEGIN S_SKIP; }
  698: {Identifier}{Space}*           { parse_import_id(yytext);   BEGIN S_SKIP; }
  699: }
  700: 
  701: <S_ANSWER>{
  702: [Pp][Ll][Uu][Ss]             { LLDBUG_PR1("[PLUS]");  RETURN(ANS_PLUS); }
  703: [Mm][Ii][Nn][Uu][Ss]         { LLDBUG_PR1("[MINUS]"); RETURN(ANS_MINUS); }
  704: [Cc][Ss]                     { LLDBUG_PR1("[CS]");    RETURN(ANS_CS); }
  705: [Cc][Ii]                     { LLDBUG_PR1("[CI]");    RETURN(ANS_CI); }
  706: [Mm][Cc]                     { LLDBUG_PR1("[MC]");    RETURN(ANS_MC); }
  707: [Oo][Nn]                     |
  708: [Yy][Ee][Ss]                 { LLDBUG_PR1("[ON]");    RETURN(ANS_ON); }
  709: [Oo][Ff][Ff]                 |
  710: [Nn][Oo]                     { LLDBUG_PR1("[OFF]");   RETURN(ANS_OFF); }
  711: [Ff][Mm][Ll]                 { LLDBUG_PR1("[FORMULA]");   RETURN(ANS_FORMULA); }
  712: [Ff][Mm][Tt]                 { LLDBUG_PR1("[FMT]");   RETURN(ANS_FMT); }
  713: [Uu][Nn][Ff][Mm][Tt]         { LLDBUG_PR1("[UNFMT]"); RETURN(ANS_UNFMT); }
  714: [,:%=@#-]                  { LLDBUG_PR2("[symbol(%s)]",yytext); return(yytext[0]); }
  715: "<"                          { LLDBUG_PR2("[symbol(%s)]",yytext); return(yytext[0]); }
  716: ">"                          { LLDBUG_PR2("[symbol(%s)]",yytext); return(yytext[0]); }
  717: [Ss][Ii][Gg]                 { LLDBUG_PR2("[SIG(%s)]",yytext);  RETURN(ANS_SIG); }
  718: [Tt][Oo][Ll]                 { LLDBUG_PR2("[tol(%s)]",yytext);  RETURN(ANS_TOLERANCE); }
  719: [Ss][Tt][Rr]                 { LLDBUG_PR1("[STR]");   RETURN(ANS_COMPARE); }
  720: [Ww][Gg][Tt]                 { LLDBUG_PR1("[WGT]");   RETURN(ANS_WEIGHT); }
  721: [Pp][Cc][Rr]                 |
  722: [Hh][Gg][Rr]                 { RETURN(ANS_PCREDIT); }
  723: [Pp][Aa][Tt][Hh]             { RETURN(ANS_PATH); }
  724: [Cc][Aa][Ll][Cc]             { RETURN(ANS_CALC); }
  725: [Tt][Rr][Yy]                 |
  726: [Tt][Rr][Ii][Ee][Ss]         { LLDBUG_PR1("[TRY]");     RETURN(ANS_TRY); }
  727: [Uu][Nn][Ii][Tt]             |
  728: [Uu][Nn][Ii][Tt][Ss]         { LLDBUG_PR1("[UNIT]");    RETURN(ANS_UNIT); }
  729: [Bb][Rr]                     { LLDBUG_PR1("[SHOW_BR]"); RETURN(ANS_SHOW_BR);  }
  730: [Vv][Ee][Rr][Bb][Aa][Tt][Ii][Mm] { LLDBUG_PR1("[VERBATIM]"); RETURN(ANS_VERBATIM); }
  731: [Aa][Nn][Ss][Bb][Oo][Xx]     { LLDBUG_PR1("[SHOW_ANS_BOX]"); RETURN(ANS_BOX_SHOW);  }
  732: [Hh][Ii][Nn][Tt]             { LLDBUG_PR1("[HINT]");    RETURN(ANS_HINT); }
  733: [Ee][Xx][Pp][Ll][Aa][Ii][Nn] { LLDBUG_PR1("[EXPLAIN]"); RETURN(ANS_EXPLAIN); }
  734: [Ee][Xx][Tt][Ee][Rr][Nn][Aa][Ll]  { LLDBUG_PR1("[EXTERNAL]"); RETURN(ANS_EXTERNAL); }
  735: [Ee][Vv][Aa][Ll]             |
  736: [Ee][Vv][Aa][Ll][Uu][Aa][Tt][Ee]             { LLDBUG_PR1("[EVAL]"); RETURN(ANS_EVAL); }
  737: [\)]                         { LLDBUG_PR1("[)]"); Pcount--; 
  738:                                if(Pcount==0) {
  739:                                    BEGIN S_ANSCONTINUE; 
  740:                                }
  741:                                return(yytext[0]); 
  742:                              }
  743: }
  744: 
  745: <S_VARIABLE,S_TRUE_FALSE_STMT,S_LET,S_ANSWER,S_MAP>{
  746: {Alpha}{AlphaNum}*             { LLDBUG_PR2("[ID<%s>]",yytext);LLDBUG_PR2("[SYMB CNT=<%d>]", Symb_count); 
  747:                                  yylval = find_identifier(yytext); RETURN(IDENTIFIER); 
  748:                                }
  749: 
  750: {Alpha}{AlphaNum}*{Space}*[(]  { char aline[MAX_FUNC_NAME];
  751:                                  int  i;          
  752:                                    for(i=0;i < (yyleng-1); i++) {
  753:                                      if( yytext[i] == ' ' || yytext[i] == '\t' || 
  754:                                          yytext[i] == 0   || yytext[i] == '(' )    break;
  755:                                      aline[i] = yytext[i];
  756:                                    }
  757:                                    aline[i] = 0;
  758:                                    LLDBUG_PR3("[FUNCT<%s:%d>]",aline,Func_idx);
  759:                             (FuncStack[Func_idx]).s_type = FUNCTION_ID;
  760: 			    (FuncStack[Func_idx]).s_name = strsave(aline); /* free it in parser() */
  761: 				   Func_idx++;
  762: 				   yyless(yyleng-1); /* <-- push back '(' char  */
  763: 				   RETURN(FUNCTION_ID); 
  764:                                }
  765: {Alpha}{AlphaNum}*{Space}*[\[]  {  char aline[MAX_FUNC_NAME];
  766:                                    int  i;   
  767:                                    for(i=0;i < (yyleng-1); i++) {
  768:                                      if( yytext[i] == ' ' || yytext[i] == '\t' || 
  769:                                          yytext[i] == 0   || yytext[i] == '[' )    break;
  770:                                      aline[i] = yytext[i];
  771:                                    }
  772:                                    aline[i] = 0;
  773:                                    LLDBUG_PR2("[ARRAY<%s>]",aline);
  774:                                    
  775:                                    yylval = (Symbol *) capa_malloc(1, sizeof(Symbol)); /* *** */
  776:                                    yylval->s_name = strsave(aline); /* free it in parser() */
  777:                                    yylval->s_type = ARRAY_ID;
  778:                                    
  779:                                    yyless(yyleng-1); /* <-- push back char '[' */
  780:                                    RETURN(ARRAY_ID);
  781:                                }
  782: {Number}*"\."{Number}*[Ee]"+"{Number}+ |
  783: {Number}*"\."{Number}*[Ee]{Number}+    |
  784: {Number}*"\."{Number}*[Ee]"-"{Number}+ |
  785: {Number}+[Ee]"+"{Number}+ |
  786: {Number}+[Ee]{Number}+    |
  787: {Number}+[Ee]"-"{Number}+ |
  788: {Number}+"\."{Number}*    |
  789: "\."{Number}+             {  yylval = (Symbol *) capa_malloc(1, sizeof(Symbol)); /* *** */
  790:                              yylval->s_real = strtod(yytext, (char **) 0);
  791:                              yylval->s_type = R_CONSTANT;
  792:                              LLDBUG_PR2("[REAL<%s>]",yytext);
  793:                              RETURN(R_CONSTANT);
  794: 			  }
  795: 
  796: {Number}+                 {  yylval = (Symbol *) capa_malloc(1, sizeof(Symbol)); /* *** */
  797:                              yylval->s_int = strtol(yytext, (char **) 0, 0);
  798:                              yylval->s_type= I_CONSTANT;
  799:                              LLDBUG_PR2("[INT<%s>]",yytext);
  800:                              RETURN(I_CONSTANT);
  801: 			  }
  802: [\(]                      { LLDBUG_PR1("[dis let ans map (]"); Pcount++; return(yytext[0]); }
  803: [\[]                      { LLDBUG_PR1("[dis let ans map '[']");         return(yytext[0]); }
  804: [\]]                      { LLDBUG_PR1("[dis let ans map ']']");         return(yytext[0]); }
  805: {Space}+                  { /* LLDBUG_PR1("[SP ignored]");  Ignore Spaces */ }
  806: [\"]                      { LLDBUG_PR1("[TF,V,LET,ANS,MAP str\" ]"); 
  807:                             Current_char_p = String_buf; 
  808:                             yy_push_state(S_STRING); 
  809:                           }
  810: }
  811: 
  812: <S_VARIABLE,S_ANSWER>[:]{Number}+[EeFf]   { char  num[ONE_TWO_EIGHT], fmt[SMALL_LINE_BUFFER];
  813:                              int   i;
  814:                              LLDBUG_PR2("[FORMAT<%s>]",yytext);
  815:                              for(i=0;i<yyleng-2;i++) {
  816:                                num[i] = yytext[i+1];
  817:                              }
  818:                              num[yyleng-2] = 0; /* terminate the numerical string */
  819:                              yylval = (Symbol *) capa_malloc(1, sizeof(Symbol));
  820:                              i = strtol(num, (char **) 0, 0);
  821:                              yylval->s_type=FORMAT;
  822:                              switch( yytext[yyleng-1] ) {
  823:                                case 'e': sprintf(fmt,"%%.%de", i);
  824:                                          yylval->s_distype = E_FORMAT; break;
  825:                                case 'E': sprintf(fmt,"%%.%dE", i);
  826:                                          yylval->s_distype = E_FORMAT; break;
  827:                                case 'f': 
  828:                                case 'F': sprintf(fmt,"%%.%df", i);
  829:                                          yylval->s_distype = F_FORMAT; break;
  830:                              }
  831:                              yylval->s_str = strsave(fmt);
  832:                              RETURN(FORMAT);
  833:                            }
  834: 
  835: <S_VARIABLE,S_TRUE_FALSE_STMT,S_LET,S_MAP>{
  836: "=="                { LLDBUG_PR1("[==]"); RETURN(EQ_op);  }
  837: "!="                { LLDBUG_PR1("[!=]"); RETURN(NE_op);  }
  838: ">"                 { LLDBUG_PR1("[>]");  RETURN(GT_op);  }
  839: ">="                { LLDBUG_PR1("[>=]"); RETURN(GE_op);  }
  840: "<"                 { LLDBUG_PR1("[<]");  RETURN(LT_op);  }
  841: "<="                { LLDBUG_PR1("[<=]"); RETURN(LE_op);  }
  842: "&&"                { LLDBUG_PR1("[&&]"); RETURN(AND_op); }
  843: "||"                { LLDBUG_PR1("[||]"); RETURN(OR_op);  }
  844: "//"                { if(Pcount==0) BEGIN S_SKIP;         }
  845: {Operator}          { LLDBUG_PR2("[Op(%c) in VAR,TF_STMT,LET]",yytext[0]); return(yytext[0]); }
  846: }
  847: 
  848: 
  849: <S_RANDOMORDER>{
  850: {Number}+ {
  851:              yylval = (Symbol *) capa_malloc(1, sizeof(Symbol)); 
  852: 	     yylval->s_int = strtol(yytext, (char **) 0, 0);
  853: 	     yylval->s_type= I_CONSTANT;
  854: 	     LLDBUG_PR2("[INT<%s>]",yytext);
  855: 	     RETURN(I_CONSTANT);
  856:           }
  857: [+~,\-!]   { LLDBUG_PR2("[randqo(%s)]",yytext); return(yytext[0]); }
  858: {EndLine} { LLDBUG_PR1("[EoLRQO]"); BEGIN S_TEXT; RETURN(EoL);}
  859: }
  860: 
  861: <S_VARIABLE>{
  862: [\)]                     { LLDBUG_PR1("[)]"); Pcount--; if(Pcount == 0)  BEGIN S_TEXT; return(yytext[0]); }
  863: [\\]{Space}*{EndLine}    { LLDBUG_PR2("[\\EoL continue in S_VARIABLE (DIS?)](%s)",yytext); /* continuation on next line */ }                       
  864: {EndLine}                { LLDBUG_PR1("[EoL within /dis()]\n"); RETURN(EoL); }
  865: .                   { char warn_msg[WARN_MSG_LENGTH]; 
  866:                       sprintf(warn_msg,"When use a VARIABLE, an unexpected char [%c] is encountered.\n",yytext[0]);
  867:                       capa_msg(MESSAGE_ERROR,warn_msg);
  868:                     }
  869: }
  870: 
  871: <S_TRUE_FALSE_STMT>{
  872: [\)]                     { LLDBUG_PRL1("[) in TRUE_FALSE]"); Pcount--; if(Pcount == 0)  BEGIN S_NEXT_LINE; return(yytext[0]); }
  873: [\\]{Space}*{EndLine}    { LLDBUG_PR2("[\\EoL continue in S_TRUE_FALSE_STMT](%s)",yytext); /* continuation on next line */ }                       
  874: {EndLine}                { LLDBUG_PR1("[EoL within /IF()]\n"); RETURN(EoL); }
  875: .                   { char warn_msg[WARN_MSG_LENGTH]; 
  876:                       sprintf(warn_msg,"In /IF(), an unexpected char [%c] is encountered.\n",yytext[0]);
  877:                       capa_msg(MESSAGE_ERROR,warn_msg);
  878:                     }
  879: }
  880: 
  881: <S_STRING>{
  882: [\\][\\]            { char  *aptr = yytext;
  883:                       while( *aptr )   *Current_char_p++ = *aptr++;
  884:                     }
  885: [\\][\"]            { *Current_char_p++ = '"';  }
  886: [\\]{Space}*[\n]    { LLDBUG_PR2("[\\CR continue in S_STRING](%s)",yytext); /* continuation on next line */ }                       
  887: [\"]                { /* end of a string constant --   */
  888:                       yylval = (Symbol *) capa_malloc(1, sizeof(Symbol));
  889: 		      *Current_char_p = '\0';
  890: 	              yylval->s_str = strsave(String_buf);  /* ****   */
  891: 		      yylval->s_type = S_CONSTANT;
  892: 		      /* printf("STRING:%s\n", String_buf); */
  893: 		      LLDBUG_PR2("[%s\"END str]",String_buf);
  894: 		      yy_pop_state();
  895: 		      RETURN(S_CONSTANT); }
  896: {EndLine}           { /* check for termination of string constant */
  897:                       char warn_msg[WARN_MSG_LENGTH];
  898:                       
  899:                       *Current_char_p = '\0';
  900:                       sprintf(warn_msg,"STRING not terminated properly, an EoL encountered in the middle.\n%s\n",String_buf);
  901:                       capa_msg(MESSAGE_ERROR,warn_msg);
  902:                       yy_pop_state();
  903:                     }
  904: .                   { char  *aptr = yytext;
  905:                       while( *aptr )   *Current_char_p++ = *aptr++;
  906:                     }
  907: }
  908: 
  909: <S_LET>[\)]                  { LLDBUG_PR1("[) in LET]"); Pcount--; return(yytext[0]); }
  910: 
  911: <S_SKIP>{
  912: [^\n]+$                      {       }
  913: {EndLine}                    { BEGIN S_TEXT; RETURN(EoL);  }
  914: }
  915: 
  916: <S_LET,S_ANSWER,S_MAP>{
  917: [\\]{Space}*{EndLine}        { LLDBUG_PR1("[\\EoL let ans map]"); /* continuation */ }
  918: {EndLine}                    { LLDBUG_PR1("[EoL END let ans map]\n"); if(Pcount == 0) BEGIN S_TEXT; RETURN(EoL); }
  919: }
  920: 
  921: <S_MAP>{
  922: [;,]                         { LLDBUG_PR2("[%c]",yytext[0]); return(yytext[0]);  }
  923: [\)]                         { LLDBUG_PR1("[) in MAP]"); Pcount--; 
  924:                                if(Pcount==0) {
  925:                                    BEGIN S_SKIP; 
  926:                                }
  927:                                return(yytext[0]); 
  928:                              }
  929: }
  930: 
  931: <S_ANSCONTINUE>{
  932: {Space}+                 { /* ignore white spaces */ }
  933: [\\]{Space}*{EndLine}    { /* continuation */ }
  934: {EndLine}                { /* end of answer and/or other answers */ LLDBUG_PR1("[complete an answer<EoL>]"); 
  935:                            BEGIN S_TEXT; }
  936: "/AND"                   { LLDBUG_PR1("[AND]"); RETURN(ANS_AND); }
  937: "/OR"                    { LLDBUG_PR1("[OR]");  RETURN(ANS_OR);  }
  938: }
  939: 
  940: <S_IF_SKIP>{
  941: ^{Spaces}"/IF"[^\n]*{EndLine}    { IFcount++; LLDBUG_PRL2("[Skip IF <IFcount=%d>]\n",IFcount); 
  942:                                    IFstatus[IFcount] = IF_DONT_CARE;
  943:                                  }
  944: ^{Spaces}"/ELSE"[^\n]*{EndLine}  {  LLDBUG_PRL2("[Skip ELSE <IFcount=%d>]",IFcount);
  945:                             IFcurrent[IFcount]=RUN_ELSE_PORTION; 
  946:                             if( IFstatus[IFcount] == IF_FALSE ) {
  947:                                LLDBUG_PRL1("[ELSE begin TEXT CR]\n");
  948:                                BEGIN S_TEXT;
  949:                             } 
  950:                             if( IFstatus[IFcount] == IF_TRUE ) {
  951:                                LLDBUG_PRL1("[ELSE THIS SHOULD NEVER HAPPEN.]\n");
  952:                             }
  953:                          }
  954: ^{Spaces}"/ENDIF"[^\n]*{EndLine} { IFcount--; LLDBUG_PRL2("[Skip ENDIF <IFcount=%d>]\n",IFcount);
  955:                             if( IFcount == 0 ) {
  956:                                LLDBUG_PRL1("[ENDIF begin TEXT CR]\n");
  957:                                BEGIN S_TEXT;
  958:                             }
  959:                             if( (IFcurrent[IFcount] == RUN_IF_PORTION )&&(IFstatus[IFcount] == IF_TRUE)) {
  960:                                LLDBUG_PRL1("[ENDIF begin TEXT CR]\n");
  961:                                BEGIN S_TEXT;
  962:                             }
  963:                             if( (IFcurrent[IFcount] == RUN_ELSE_PORTION )&&(IFstatus[IFcount] == IF_FALSE)) {
  964:                                LLDBUG_PRL1("[ENDIF begin TEXT CR]\n");
  965:                                BEGIN S_TEXT;
  966:                             }
  967:                          }
  968: {EndLine}                { LLDBUG_PRL1("[SkipIF a CR]\n");       }
  969: [^\n]*$                  { LLDBUG_PRL2("[SkipIF anything <IFcount=%d>]",IFcount);   }
  970: }
  971: <S_NEXT_LINE>{
  972: ([.]*){EndLine}          { /* this ignores everything until it hits an EoL */
  973:                            LLDBUG_PRL2("[<S_NEXT_LINE> skip \'%s\' until EoL]\n",yytext); 
  974:                            BEGIN S_TEXT;
  975:                          }
  976: }
  977: 
  978: <S_WHILE_SKIP>{
  979: ^{Spaces}"/WHILE"[^\n]*{EndLine}        { Wcount++;
  980:                                           LLDBUG_PRL2("[SkipWHILE /WHILE <Wcount=%d>]\n",Wcount);   
  981:                                         }
  982: ^{Spaces}"/ENDWHILE"[^\n]*{EndLine}     { 
  983:                                           if(Wcount==0) {
  984:                                              LLDBUG_PRL2("[SkipWHILE->/ENDWHILE <Wcount=%d>]\n",Wcount);
  985:                                              BEGIN S_TEXT;
  986:                                           } else {
  987:                                              Wcount--;
  988:                                              LLDBUG_PRL2("[SkipWHILE /ENDWHILE <Wcount=%d>]\n",Wcount);
  989:                                           }
  990:                                         }
  991: {EndLine}                { LLDBUG_PRL1("[SkipWHILE a CR]\n");       }                         
  992: [^\n]*$                  { LLDBUG_PRL2("[SkipWHILE anything <Wcount=%d>]",Wcount);   }
  993: }
  994: 
  995: <S_VERB>{
  996: ^{Spaces}"/ENDVERB" { LLDBUG_PRL1("[END VERB]\n"); 
  997:                       yylval = (Symbol *) capa_malloc(1, sizeof(Symbol));
  998: 		      yylval->s_str = strsave(Dynamic_buf);  /* ****   */
  999: 		      yylval->s_type = S_CONSTANT;  
 1000: 		      capa_mfree(Dynamic_buf);
 1001: 		      Dynamic_buf_cur=-1;
 1002: 		      Dynamic_buf_max=0;
 1003: 		      BEGIN S_TEXT;   RETURN(VERBATIM);   
 1004: 		    }
 1005: .*|{EndLine}         { append_dynamic_buf(yytext); }
 1006: }
 1007: 
 1008: %%
 1009: 
 1010: /* ========================================================================================== */
 1011: extern void
 1012: begin_if_skip() { BEGIN S_IF_SKIP; }
 1013: 
 1014: extern void
 1015: begin_while_skip() { Wcount=0; While_idx--; /* while is FALSE, pop it out from stack */ BEGIN S_WHILE_SKIP; }
 1016: 
 1017: extern void
 1018: begin_next_line()  { BEGIN S_NEXT_LINE; }
 1019: 
 1020: extern void
 1021: begin_var() { BEGIN S_VARIABLE; }
 1022: 
 1023: extern void
 1024: begin_let() { BEGIN S_LET; }
 1025: 
 1026: extern void
 1027: begin_def() { BEGIN S_DEFINE; }
 1028: 
 1029: extern void
 1030: begin_ans() { BEGIN S_ANSWER; }
 1031: 
 1032: extern void
 1033: begin_map() { BEGIN S_MAP; }
 1034: 
 1035: extern void
 1036: begin_ignore() { BEGIN S_IGNORE; }
 1037: 
 1038: extern void
 1039: begin_text() { BEGIN S_TEXT; }
 1040: 
 1041: extern void
 1042: begin_question() { LLDBUG_PR1("[<S_TEXT>]"); 
 1043:                    IFcount = 0; While_idx=0; /* initialize some stacks */
 1044:                    End_of_input = 0; YY_FLUSH_BUFFER; BEGIN S_TEXT; }
 1045: 
 1046: extern void
 1047: end_problemset() { End_of_input = 0; YY_FLUSH_BUFFER; BEGIN S_TEXT; }
 1048: 
 1049: 
 1050: /* ========================================================================================== */
 1051: 
 1052: #define  NUM_KEY   2
 1053: int
 1054: match_keyword(key) char *key;
 1055: {
 1056:   char  *keyword[NUM_KEY] = {"/DIS", "/DIR" };
 1057:   int    i;
 1058:   
 1059:   for(i=0;i < NUM_KEY; i++) {
 1060:      if( !strncmp(keyword[i], key, 4) ) {
 1061:         return (1);
 1062:      }
 1063:   }
 1064:   return (0);
 1065: }
 1066: 
 1067: int
 1068: match_functionid(key) char *key;
 1069: {
 1070:   char  *keyword[NUM_KEY] = {"/DIS", "/DIR" };
 1071:   int    i;
 1072:   
 1073:   for(i=0;i < NUM_KEY; i++) {
 1074:      if( !strncmp(keyword[i], key, 4) ) {
 1075:         return (1);
 1076:      }
 1077:   }
 1078:   return (0);
 1079: }
 1080: /* -------------------------------------------------------------------------- */
 1081: /* -------------------------------------------------------------------------- */
 1082: 
 1083: void  init_funcstack() 
 1084: {  
 1085:  int ii;
 1086:  for(ii=0;ii<Func_idx;ii++) {
 1087:    capa_mfree(FuncStack[ii].s_name);
 1088:  }
 1089:  Func_idx = 0;  
 1090: }
 1091: 
 1092: 
 1093: /* -------------------------------------------------------------------------- */
 1094: /* GET THE NEXT CHARACTER OF THE SOURCE FILE                                  */
 1095: /* -------------------------------------------------------------------------- */
 1096: 
 1097: #ifdef  FLEX
 1098: int   capaL_input()
 1099: #else
 1100: int                        /* RETURNS: next character */
 1101: input()                    /* ARGUMENTS: (none)       */
 1102: #endif
 1103: 
 1104: {                          /* LOCAL VARIABLES:        */
 1105:   static  int startup=1;  /*    First call flag      */
 1106:   
 1107:   LLDBUG_PRL1("<<capaL_input() is called>>\n");
 1108:   if (!Lexi_line) { /* was startup */
 1109:        for(Input_idx=0;Input_idx < MAX_OPENED_FILE;Input_idx++) {
 1110:          /* for(ii=0;ii<LEX_BUFLEN;ii++) {
 1111:            Lexi_buf[Input_idx][ii]=0;
 1112:          }
 1113:          */
 1114:          Lexi_buf[Input_idx][0]=0;
 1115:          Lexi_pos[Input_idx] = 0;
 1116:        }
 1117:        Input_idx = 0;
 1118:        startup=0;
 1119:        yyin = Input_stream[Input_idx];
 1120:   }
 1121:   if (!Lexi_buf[Input_idx][Lexi_pos[Input_idx]]) {
 1122:     if (fgets(Lexi_buf[Input_idx],LEX_BUFLEN-1,Input_stream[Input_idx])==NULL) { 
 1123:       /* EOF? */
 1124:       /* no use in flex
 1125:          printf("capaL_input()EOF %s\n",Opened_filename[Input_idx+1]); fflush(stdout); */
 1126:       return (0);
 1127:     }
 1128:     Lexi_pos[Input_idx] = 0;
 1129:     Lexi_line++;
 1130:     printf("input()(%d)\n",Lexi_line);
 1131:   }
 1132:   (Lexi_pos[Input_idx])++;
 1133:   return ( Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1] );
 1134: }
 1135:  
 1136: /******************************************************************************/
 1137: /* PUSH BACK ONE CHARACTER OF THE INPUT                                       */
 1138: /******************************************************************************/
 1139: 
 1140: int                /* RETURNS: nothing     */
 1141: capaL_unput(ch)          /* ARGUMENTS:           */
 1142: register int ch;   /*    Character to push */
 1143: {
 1144:    if (ch)   (Lexi_pos[Input_idx])--;
 1145:  
 1146:    /* unput() stream cannot be re-defined */
 1147:    /* unput(ch); inconsistency between YY_INPUT() and internal matched yytext */
 1148:    return (0);
 1149: 
 1150: }
 1151: 
 1152: 
 1153: /******************************************************/
 1154: 
 1155: #ifndef DMALLOC
 1156: 
 1157: char *       
 1158: strsave(char *s) 
 1159: {            
 1160:    char *p;  
 1161:    if (s==NULL) {return s;}
 1162:    p=capa_malloc(strlen(s)+1,1);
 1163:    strcpy(p,s);
 1164:    return (p);
 1165: }
 1166: 
 1167: #endif
 1168: 
 1169: /* =========================================================================== */
 1170: 
 1171: #ifndef DMALLOC
 1172: char *
 1173: capa_malloc(unsigned num,unsigned sz)
 1174: {
 1175:   char *p;
 1176:   p = calloc(num, sz);
 1177:   bzero(p, num*sz);  /* added Jan 21 1998 */
 1178:   return (p);
 1179: }
 1180: 
 1181: #endif
 1182: 
 1183: #ifndef DMALLOC
 1184: void
 1185: capa_mfree(p) char *p;
 1186: {
 1187:   free(p);
 1188: }
 1189: #endif
 1190: 
 1191: void
 1192: capa_msg(int type, char *p) 
 1193: { int  idx, i, j;
 1194:   int  len;
 1195:   char warn_msg[WARN_MSG_LENGTH];
 1196:   char tmp_line[ONE_TWO_EIGHT];
 1197:   char  *tmp_str;
 1198:   
 1199:   strcpy(warn_msg,"File: ");
 1200:   idx=6;
 1201:   for(i=0;i<=Input_idx;i++) {
 1202:     len=strlen(Opened_filename[i]);
 1203:     for(j=0;j<len;j++) {
 1204:       warn_msg[idx++] = Opened_filename[i][j];
 1205:     }
 1206:     if(i < Input_idx) {
 1207:       warn_msg[idx++]='-';
 1208:       warn_msg[idx++]='>';
 1209:     }
 1210:     warn_msg[idx]=0;
 1211:   }
 1212:   switch (type) {
 1213:     case MESSAGE_ERROR:
 1214:            sprintf(tmp_line,", Line %d: ERROR:", Current_line[Input_idx]);
 1215:            len=strlen(tmp_line);
 1216:            for(j=0;j<len;j++) {
 1217:               warn_msg[idx++] = tmp_line[j];
 1218:            }
 1219:            warn_msg[idx]=0;
 1220:            append_error(warn_msg); append_error(p);
 1221: 	   break;
 1222:     case MESSAGE_WARN:
 1223:     default:
 1224:            sprintf(tmp_line,", Line %d: WARNING:", Current_line[Input_idx]);
 1225:            len=strlen(tmp_line);
 1226:            for(j=0;j<len;j++) {
 1227:              warn_msg[idx++] = tmp_line[j];
 1228:            }
 1229:            warn_msg[idx]=0;
 1230:            j = strlen(warn_msg);
 1231:            len = strlen(p);
 1232:            tmp_str = (char *)capa_malloc(len+j+1,1);
 1233:            for(i=0;i<j;i++) {
 1234:              tmp_str[i]=warn_msg[i];
 1235:            } 
 1236:            for(i=j;i<j+len;i++) {
 1237:              tmp_str[i] = p[i-j];
 1238:            }
 1239:            append_warn(type,tmp_str);
 1240:            capa_mfree(tmp_str);
 1241:            break;
 1242:   }
 1243: }
 1244: 
 1245: /* ======================================================================== */
 1246: void
 1247: capa_warn_header(int type)
 1248: {
 1249:   int  idx, i, j;
 1250:   int  len;
 1251:   char warn_msg[WARN_MSG_LENGTH];
 1252:   char tmp_line[ONE_TWO_EIGHT];
 1253:   
 1254:   strcpy(warn_msg,"File: ");
 1255:   idx=6;
 1256:   for(i=0;i<=Input_idx;i++) {
 1257:     len=strlen(Opened_filename[i]);
 1258:     for(j=0;j<len;j++) {
 1259:       warn_msg[idx++] = Opened_filename[i][j];
 1260:     }
 1261:     if(i < Input_idx) {
 1262:       warn_msg[idx++]='-';
 1263:       warn_msg[idx++]='>';
 1264:     }
 1265:     warn_msg[idx]=0;
 1266:   }
 1267:   switch (type) {
 1268:     case MESSAGE_ERROR:
 1269:       sprintf(tmp_line,", Line %d: ERROR:", Current_line[Input_idx]);
 1270:       
 1271:       break;
 1272:     case MESSAGE_WARN:
 1273:       sprintf(tmp_line,", Line %d: WARNING:", Current_line[Input_idx]);break;
 1274:     default:
 1275:     sprintf(tmp_line,", Line %d: ERROR:", Current_line[Input_idx]);break;
 1276:   }
 1277:   len=strlen(tmp_line);
 1278:   for(j=0;j<len;j++) {
 1279:     warn_msg[idx++] = tmp_line[j];
 1280:   }
 1281:   warn_msg[idx]=0;
 1282:   append_error(warn_msg);
 1283: }
 1284: 
 1285: /* --------------------------------------------------------------------------- */
 1286: #ifdef AVOIDYYINPUT
 1287: 
 1288: void change_file(char *fname)
 1289: {
 1290:   char warn_msg[WARN_MSG_LENGTH];
 1291:   if( capa_access(fname, (F_OK | R_OK)) == -1 ) {
 1292:     sprintf(warn_msg,"/IMP \"%s\", import file does not exist or is not readable.\n",
 1293: 	    fname);
 1294:     capa_msg(MESSAGE_ERROR,warn_msg);
 1295:     return;
 1296:   } 
 1297:   
 1298:   if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
 1299:     sprintf(warn_msg,"Includes nested too deeply" );
 1300:     capa_msg(MESSAGE_ERROR,warn_msg);
 1301:     return;
 1302:   }
 1303: 
 1304:   include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
 1305:   yyin = fopen( fname, "r" );
 1306:   yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ) );
 1307: }
 1308: 
 1309: void
 1310: parse_filename(char *line)
 1311: {
 1312:   char *start, fname[MAX_BUFFER_SIZE],  warn_msg[WARN_MSG_LENGTH];
 1313:   int ii,len;
 1314: 
 1315:   start = index(line, '\"'); /*** hpux complained */
 1316:   if( start == NULL ) {
 1317:     sprintf(warn_msg,"/IMP was not given a filename.\n");
 1318:     capa_msg(MESSAGE_ERROR,warn_msg);
 1319:     return;
 1320:   }
 1321:   start++; len = strlen(start) - 1;
 1322:   ii = 0;
 1323:   while( start[ii] != '\"' ) fname[ii++] = start[ii];
 1324:   fname[ii] = 0;
 1325:   LLDBUG_PR2("[parse_filename<%s>]\n",fname);
 1326:   
 1327:   change_file(fname);
 1328: }
 1329: 
 1330: void
 1331: parse_import_id(char *line)
 1332: {
 1333:   char    fname[QUARTER_K], warn_msg[WARN_MSG_LENGTH];
 1334:   int     ii, dup_open;
 1335:   Symbol *symb_p;
 1336:   int     no_error = 0;
 1337:   
 1338:   ii = 0;
 1339:   while( line[ii] != '\0' && line[ii] != ' ' && line[ii] != '\n' && line[ii] != '\t' ) 
 1340:     fname[ii++] = line[ii]; 
 1341:   fname[ii] = 0;
 1342: 
 1343:   LLDBUG_PR2("[parse_import_id<%s>]\n",fname);
 1344:   symb_p = find_identifier(fname);
 1345:   
 1346:   switch (symb_p->s_type) {
 1347:    case IDENTIFIER:
 1348:      sprintf(warn_msg,"/IMP %s, var is not defined.\n", fname);
 1349:      capa_msg(MESSAGE_ERROR,warn_msg);
 1350:      break;
 1351:    case I_VAR: case I_CONSTANT: case R_VAR: case R_CONSTANT:
 1352:      sprintf(warn_msg,"var cannot be a number.\n");
 1353:      capa_msg(MESSAGE_ERROR,warn_msg);
 1354:      break;
 1355:    case S_VAR:  case S_CONSTANT: sprintf(fname,"%s",symb_p->s_str);
 1356:      no_error = 1;
 1357:      break;
 1358:   }
 1359:   if( no_error ) change_file(fname);
 1360: }
 1361: 
 1362: #else
 1363: void
 1364: parse_filename(char *line)
 1365: {
 1366:   char  *start, fname[QUARTER_K], warn_msg[WARN_MSG_LENGTH];
 1367:   int    ii, len, dup_open;
 1368:   
 1369:   /* printf("IMPORT %s\n", line); */
 1370:   
 1371:   start = index(line, '\"'); /*** hpux complained */
 1372:   if( start != NULL ) {
 1373:     start++; len = strlen(start) - 1;
 1374:     ii = 0;
 1375:     while( start[ii] != '\"' ) {
 1376:       fname[ii] = start[ii]; ii++;
 1377:     }
 1378:     fname[ii] = 0;
 1379:     LLDBUG_PR2("[parse_filename<%s>]\n",fname);
 1380:     if(Input_idx < (MAX_OPENED_FILE -1)) {
 1381:       dup_open = 0;
 1382:       /* -- no need to check duplicated opening a file 
 1383:       for(ii=0;ii<Input_idx;ii++) {
 1384:         if(strcmp(Opened_filename[ii],fname)==0) {
 1385:           dup_open =1;
 1386:         }
 1387:       }
 1388:       */
 1389:       if( !dup_open ) {
 1390:         if( capa_access(fname, (F_OK | R_OK)) == -1 ) {
 1391:           sprintf(warn_msg,"/IMP \"%s\", import file does not exist or is not readable.\n",fname);
 1392:           capa_msg(MESSAGE_ERROR,warn_msg);
 1393:         } else {
 1394:           Input_idx++;
 1395:           Input_stream[Input_idx]=fopen(fname,"r");
 1396:           sprintf(Opened_filename[Input_idx], "%s",fname);
 1397:           Current_line[Input_idx] = 0;
 1398:         }
 1399:       } else {
 1400:         /*
 1401:           sprintf(warn_msg,"/IMP \"%s\", import file has already been imported.\n",fname);
 1402:           capa_msg(MESSAGE_WARN,warn_msg);
 1403:         */
 1404:         if( capa_access(fname, (F_OK | R_OK)) == -1 ) {
 1405:           sprintf(warn_msg,"/IMP \"%s\", import file does not exist or is not readable.\n",fname);
 1406:           capa_msg(MESSAGE_ERROR,warn_msg);
 1407:         } else {
 1408:           Input_idx++;
 1409:           Input_stream[Input_idx]=fopen(fname,"r");
 1410:           sprintf(Opened_filename[Input_idx], "%s",fname);
 1411:           Current_line[Input_idx] = 0;
 1412:         }
 1413:       }
 1414:     } else {
 1415:       sprintf(warn_msg,"/IMP more the %d levels deep ignoring further imports.\n",MAX_OPENED_FILE-1);
 1416:       capa_msg(MESSAGE_WARN,warn_msg);
 1417:     }
 1418:   } else {
 1419:     sprintf(warn_msg,"%s, is not a valid file name.\n",line);
 1420:     capa_msg(MESSAGE_ERROR,warn_msg);
 1421:   }
 1422:   
 1423: }
 1424: 
 1425: void
 1426: parse_import_id(char *line)
 1427: {
 1428:   char    fname[QUARTER_K], warn_msg[WARN_MSG_LENGTH];
 1429:   int     ii, dup_open;
 1430:   Symbol *symb_p;
 1431:   int     no_error = 0;
 1432:   
 1433:     ii = 0;
 1434:     while (line[ii] != '\0' && line[ii] != ' ' && line[ii] != '\n' && line[ii] != '\t') {
 1435:       fname[ii] = line[ii]; ii++;
 1436:     }
 1437:     fname[ii] = 0;
 1438:     LLDBUG_PR2("[parse_import_id<%s>]\n",fname);
 1439:     symb_p = find_identifier(fname);
 1440:     
 1441:     switch (symb_p->s_type) {
 1442:       case IDENTIFIER:
 1443: 	sprintf(warn_msg,"/IMP %s, var is not defined.\n", fname);
 1444: 	capa_msg(MESSAGE_ERROR,warn_msg);
 1445: 	break;
 1446:       case I_VAR: case I_CONSTANT: case R_VAR: case R_CONSTANT:
 1447: 	sprintf(warn_msg,"var cannot be a number.\n");
 1448: 	capa_msg(MESSAGE_ERROR,warn_msg);
 1449: 	break;
 1450:       case S_VAR:
 1451:       case S_CONSTANT:
 1452: 	sprintf(fname,"%s",symb_p->s_str);
 1453: 	no_error = 1;
 1454: 	break;
 1455:     }
 1456:     if( no_error ) {
 1457:       if(Input_idx < (MAX_OPENED_FILE -1) ) {
 1458:           dup_open = 0;
 1459:           /* no need to check duplicated opening a file 
 1460:           for(ii=0;ii<Input_idx;ii++) {
 1461:             if(strcmp(Opened_filename[ii],fname)==0)  dup_open =1; 
 1462:           }
 1463:           */
 1464:           if( !dup_open ) {
 1465:             if( capa_access(fname, (F_OK | R_OK)) == -1 ) {
 1466:               sprintf(warn_msg,"/IMP \"%s\", import file does not exist or is not readable.\n", fname);
 1467:               capa_msg(MESSAGE_ERROR,warn_msg);
 1468:             } else {
 1469:               Input_idx++;
 1470:               Input_stream[Input_idx]=fopen(fname,"r");
 1471:               sprintf(Opened_filename[Input_idx], "%s",fname);
 1472:               Current_line[Input_idx] = 0;
 1473:             }
 1474:           } else {
 1475:             /*  NO warning on duplicated open a file
 1476:             sprintf(warn_msg,"/IMP \"%s\", file has already been imported.\n", fname);
 1477:             capa_msg(MESSAGE_WARN,warn_msg);
 1478:             */
 1479:             if( capa_access(fname, (F_OK | R_OK)) == -1 ) {
 1480:               sprintf(warn_msg,"/IMP \"%s\", import file does not exist or is not readable.\n", fname);
 1481:               capa_msg(MESSAGE_ERROR,warn_msg);
 1482:             } else {
 1483:               Input_idx++;
 1484:               Input_stream[Input_idx]=fopen(fname,"r");
 1485:               sprintf(Opened_filename[Input_idx], "%s",fname);
 1486:               Current_line[Input_idx] = 0;
 1487:             }
 1488: 
 1489:           }
 1490:         } else {
 1491:           sprintf(warn_msg,"/IMP , too many files has been imported. The maximum is %d files.\n",
 1492:               MAX_OPENED_FILE-1);
 1493:           capa_msg(MESSAGE_WARN,warn_msg);
 1494:         }
 1495:       }
 1496: }
 1497: #endif /*AVOIDYYINPUT*/
 1498: 
 1499: void append_dynamic_buf(new_str) char *new_str;
 1500: {
 1501:   int ii,len;
 1502:   
 1503:   if(new_str==NULL) return;
 1504:   len=strlen(new_str);
 1505: #ifdef LEX_DBUG
 1506:   printf("before: len %d; Dynamic_buf_cur %d; Dynamic_buf_max %d\n",
 1507: 	 len,Dynamic_buf_cur,Dynamic_buf_max);
 1508: #endif /* LEX_DBUG */    
 1509:   if (Dynamic_buf_cur+len+1>Dynamic_buf_max) {
 1510:     char *temp_text;
 1511:     Dynamic_buf_max=(Dynamic_buf_cur+len)*2;
 1512:     temp_text=(char*)capa_malloc(sizeof(char),Dynamic_buf_max);
 1513:     strncpy(temp_text,Dynamic_buf,Dynamic_buf_max);
 1514:     free(Dynamic_buf);
 1515:     Dynamic_buf=temp_text;
 1516:   }
 1517:   for(ii=0;ii<len;ii++) {
 1518:     Dynamic_buf[Dynamic_buf_cur+ii]=new_str[ii];
 1519:   }
 1520:   Dynamic_buf_cur += len;
 1521:   Dynamic_buf[Dynamic_buf_cur+1]='\0';
 1522: #ifdef LEX_DBUG
 1523:   printf("after: len %d; Dynamic_buf_cur %d; Dynamic_buf_max %d\n",
 1524: 	 len,Dynamic_buf_cur,Dynamic_buf_max);
 1525:   printf("Dyn_buf %s; added %s\n",Dynamic_buf,new_str);
 1526: #endif /* LEX_DBUG */    
 1527: }
 1528: 
 1529: char* parser_status()
 1530: {
 1531:   char *buf,small[SMALL_LINE_BUFFER];
 1532:   int i,j,totlen=0,len,idx=0;
 1533:   
 1534:   for(i=0;i<=Input_idx;i++) totlen+=strlen(Opened_filename[i])+6;
 1535:   buf=capa_malloc(sizeof(char),totlen);
 1536:   for(i=0;i<=Input_idx;i++) {
 1537:     len=strlen(Opened_filename[i]);
 1538:     for(j=0;j<len;j++) buf[idx++] = Opened_filename[i][j];
 1539:     buf[idx++] = ':';
 1540:     sprintf(small,"%d",Current_line[i]);
 1541:     len=strlen(small);
 1542:     for(j=0;j<len;j++) buf[idx++] = small[j];
 1543:     buf[idx++]=' ';
 1544:     buf[idx]='\0';
 1545:   }
 1546:   return buf;
 1547: }
 1548: 
 1549: void yyfatalerror(char*msg)
 1550: {
 1551:   char    warn_msg[WARN_MSG_LENGTH];
 1552:   sprintf(warn_msg,"Invalid character[\'%s\']\n",yytext);
 1553:   capa_msg(MESSAGE_ERROR,warn_msg);
 1554:   capa_msg(MESSAGE_ERROR,msg);
 1555: }
 1556: void yyerror(char* msg)
 1557: {
 1558:   char    warn_msg[WARN_MSG_LENGTH];
 1559:   sprintf(warn_msg,"%s\n",msg);
 1560:   capa_msg(MESSAGE_ERROR,warn_msg);
 1561: }
 1562: 
 1563: void newyy_input (char *buf,int *result,int max_size) 
 1564: { int ii, leng, out_of_char; 
 1565:  if (!Lexi_line) { /* was startup */ 
 1566:    for(ii=0;ii < MAX_OPENED_FILE;ii++) { 
 1567:      Lexi_buf[ii] = NULL; 
 1568:      Lexi_pos[ii] = 0; 
 1569:      Current_line[ii] = 0; 
 1570:    } 
 1571:    Input_idx = 0; 
 1572:    first_run=0; 
 1573:    yyin = Input_stream[Input_idx]; LIDBUG_PR1("<<yy_input() startup>>\n"); 
 1574:  } 
 1575:  out_of_char = 0; 
 1576:  if ( Lexi_buf[Input_idx] == NULL ) { 
 1577:    Lexi_buf[Input_idx] = (char *)capa_malloc(sizeof(char)*LEX_BUFLEN+1,1); out_of_char=1;
 1578:  } else { 
 1579:    if (!Lexi_buf[Input_idx][Lexi_pos[Input_idx]]) { 
 1580:      /* test if the line buffer is empty or at the end */ 
 1581:      out_of_char=1; 
 1582:    } 
 1583:  }
 1584:  if( out_of_char ) { 
 1585:    if (fgets(Lexi_buf[Input_idx],LEX_BUFLEN-1,Input_stream[Input_idx])==NULL) { 
 1586:      /* read in one line */ 
 1587:      LIDBUG_PR2("<<yy_input() fgets() returns NULL, input index=%d>>\n",Input_idx); 
 1588:      if( (Input_idx > 0) && ( Lexi_buf[Input_idx][Lexi_pos[Input_idx]] == '\0') ) { 
 1589:        LIDBUG_PR2("<<yy_input() close an input stream, input index=%d>>\n",Input_idx); 
 1590:        fclose(Input_stream[Input_idx]); 
 1591:        capa_mfree((char *)Lexi_buf[Input_idx]); 
 1592:        Lexi_buf[Input_idx] = NULL; 
 1593:        Input_idx--; 
 1594:        yyin = Input_stream[Input_idx]; 
 1595:        /* (Lexi_pos[Input_idx])++; */ 
 1596:        buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; 
 1597:        *result = 1; 
 1598:      } else { 
 1599:        *result = YY_NULL; /* End of File */ 
 1600:      } 
 1601:    } else { /* successfully read in one line */ 
 1602:      leng = strlen(Lexi_buf[Input_idx]); 
 1603:      LIDBUG_PR3("<<yy_input() read into buffer a line(leng=%d), input index=%d>>\n",
 1604: 		leng,Input_idx);  
 1605:      Lexi_pos[Input_idx] = 0; 
 1606:      Lexi_line++; 
 1607:      Current_line[Input_idx]++; 
 1608:      (Lexi_pos[Input_idx])++; 
 1609:      buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1];  
 1610:      /* need to take care of return continuation conditions */ 
 1611:      /*  so, we return to one-char-at-a-time approach */ 
 1612:      /* for(ii=0;ii<leng;ii++) { */ 
 1613:      /*  buf[ii] = Lexi_buf[Input_idx][ii]; */ 
 1614:      /* } */ 
 1615:      /* buf[ii] = '\0'; */ 
 1616:      /* printf("YY_INPUT()(Lexi_line=%d,max size=%d)(%c)",Lexi_line,max_size,buf[0]); */ 
 1617:      *result = 1; 
 1618:    } 
 1619:  } else { 
 1620:    /* LIDBUG_PR2("<<yy_input() increase Lexi_pos, input index=%d>>\n",Input_idx);  */ 
 1621:    (Lexi_pos[Input_idx])++; 
 1622:    buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; 
 1623:    *result = 1; 
 1624:  } 
 1625:  if (Stop_Parser==1) *result = YY_NULL;  
 1626: }
 1627: 
 1628: int capa_eof()
 1629: {
 1630: #ifdef AVOIDYYINPUT
 1631:   if ( --include_stack_ptr < 0 ) yyterminate();
 1632:   else {
 1633:     yy_delete_buffer( YY_CURRENT_BUFFER );
 1634:     yy_switch_to_buffer(include_stack[include_stack_ptr]);
 1635:   }
 1636: #else
 1637:   if(Input_idx == 0) {
 1638:     fclose(Input_stream[Input_idx]);
 1639:     capa_mfree((char *)Lexi_buf[Input_idx]); 
 1640:     free_problems(LexiProblem_p);
 1641:     LexiProblem_p=NULL;
 1642:     /* printf("\nCAPA EOF\n"); fflush(stdout); */
 1643:   }
 1644:   return (0);
 1645: #endif /*AVOIDYYINPUT*/
 1646: }
 1647: /* ------------ */
 1648: 
 1649: 
 1650: /* =========================================================== */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>