File:  [LON-CAPA] / capa / capa51 / pProj / capaLexerDef.flex
Revision 1.8: download - view: text, annotated - select for diffs
Tue Jun 27 18:28:49 2000 UTC (23 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- new improved makefiles
- fixed random_*
- added ability to use tth fore tex().

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

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