File:  [LON-CAPA] / capa / capa51 / pProj / capaLexerDef.flex
Revision 1.6: download - view: text, annotated - select for diffs
Tue Feb 22 18:19:02 2000 UTC (24 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- new random distribution functions

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

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