File:  [LON-CAPA] / loncom / homework / CAPA-converter / capaLexerDef.flex
Revision 1.7: download - view: text, annotated - select for diffs
Tue Jan 30 21:03:25 2001 UTC (23 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- missing "

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

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