File:  [LON-CAPA] / loncom / homework / CAPA-converter / capaLexerDef.flex
Revision 1.9: download - view: text, annotated - select for diffs
Thu Nov 15 21:42:42 2001 UTC (22 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- from Ohio University cleanups to the capa-convertor.
   - /MAP is properly translated now
   - <answergroup> deleted
   - <numericalresponse> now
   - /> instead of </tagname> in multiple places

    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: int           sccount;		           /* Semi-colon count for MAP and RMAP  */
  128: 
  129: #ifdef  USE_DYNAMIC_SYMBOLS
  130: Symbol       *SymbList_p;
  131: Symbol       *SymbLast_p;
  132: #else
  133: Symbol        SymbArray[MAX_SYMB_COUNT];
  134: #endif /* USE_DYNAMIC_SYMBOLS */
  135: 
  136: 
  137: char         *Current_char_p;                      /* Collect string constant */
  138: extern        char              *EndText_p;
  139: extern        char              *StartText_p;
  140: extern        Problem_t         *LexiProblem_p;
  141: extern        Problem_t         *LastProblem_p;
  142: int           first_run=1;
  143: int           Stop_Parser;
  144: static        int dosend=1;
  145: static        int firstparam=1;
  146: #define  FLEX
  147: 
  148: #define  YY_STACK_USED   1  /* for yy_push_state(), yy_pop_state() */
  149: 
  150: #ifdef   FLEX
  151: 
  152: int      capaL_unput();
  153: int      capaL_input();
  154: 
  155: 
  156: /* YY_INPUT() 
  157:    Controls scanner input.  By default, YY_INPUT reads from the
  158:    file-pointer yyin.  Its action is to place up to max_size
  159:    characters in the character array buf and return in the
  160:    integer variable result either the number of characters read
  161:    or the constant YY_NULL to indicate EOF.  
  162:    max_size is defined to be num_to_read = 8192 in liby
  163:    Following is a sample 
  164:    redefinition of YY_INPUT, in the definitions section of
  165:    the input file:
  166:    
  167:    %{
  168:    #undef YY_INPUT
  169:    #define YY_INPUT(buf,result,max_size)\
  170:    {\
  171:        int c = getchar();\
  172:        result = (c == EOF) ? YY_NULL : (buf[0] = c, 1);\
  173:    }
  174:    %}
  175:  
  176: */
  177: 
  178: /* fgets() reads the input stream until 
  179:    n-1 bytes have been read  OR
  180:    a newline character is read and transferred to string  OR
  181:    an EOF (End-of-File) condition is encountered
  182:    
  183:    The string is then terminated with a NULL character.  
  184:   
  185:   ii = fseek(FILE *stream,0L,SEEK_END) ;
  186:   if(ii!=0) { error }
  187:   leng = ftell(FILE *stream) + 1 ;
  188:   fseek(FILE *stream,0L,SEEK_SET) ;
  189:   Lexi_buf[Input_idx] = (char *)capa_malloc(sizeof(char)*leng,1);
  190:   
  191: */
  192: 
  193: 
  194: #ifdef AVOIDYYINPUT
  195: #define MAX_INCLUDE_DEPTH 10
  196: YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  197: int include_stack_ptr = 0;
  198: #else
  199: #ifdef USE_DYNAMIC_LEXBUFS
  200: #define NEWYYINPUT
  201: #endif
  202: 
  203: #ifdef  NEWYYINPUT
  204: void newyy_input (char *buf,int *result,int max_size);
  205: #define YY_INPUT(buf,result,max_size) newyy_input(buf,&result,max_size)
  206: 
  207: #else
  208: #ifdef  USE_DYNAMIC_LEXBUFS
  209: 
  210: #define  YY_INPUT(buf,result,max_size) \
  211:   { int ii, leng, out_of_char; \
  212:     if (!Lexi_line) { /* was startup */ \
  213:        for(ii=0;ii < MAX_OPENED_FILE;ii++) { \
  214:          Lexi_buf[ii] = NULL; \
  215:          Lexi_pos[ii] = 0; \
  216:          Current_line[ii] = 0; \
  217:        } \
  218:        Input_idx = 0; \
  219:        first_run=0; \
  220:        yyin = Input_stream[Input_idx]; LIDBUG_PR1("<<yy_input() startup>>\n"); \
  221:     } \
  222:     out_of_char = 0; \
  223:     if ( Lexi_buf[Input_idx] == NULL ) { \
  224:       Lexi_buf[Input_idx] = (char *)capa_malloc(sizeof(char)*LEX_BUFLEN+1,1); out_of_char=1; \
  225:     } else { \
  226:       if (!Lexi_buf[Input_idx][Lexi_pos[Input_idx]]) { /* test if the line buffer is empty or at the end */ \
  227:         out_of_char=1; \
  228:       } \
  229:     } \
  230:     if( out_of_char ) { \
  231:       if (fgets(Lexi_buf[Input_idx],LEX_BUFLEN-1,Input_stream[Input_idx])==NULL) { /* read in one line */ \
  232:         LIDBUG_PR2("<<yy_input() fgets() returns NULL, input index=%d>>\n",Input_idx); \
  233:         if( (Input_idx > 0) && ( Lexi_buf[Input_idx][Lexi_pos[Input_idx]] == '\0') ) { \
  234:           LIDBUG_PR2("<<yy_input() close an input stream, input index=%d>>\n",Input_idx); \
  235:           fclose(Input_stream[Input_idx]); \
  236:           capa_mfree((char *)Lexi_buf[Input_idx]); \
  237:           Lexi_buf[Input_idx] = NULL; \
  238:           Input_idx--; \
  239:           yyin = Input_stream[Input_idx]; \
  240:           /* (Lexi_pos[Input_idx])++; */ \
  241:           buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; \
  242:           result = 1; \
  243:         } else { \
  244:           result = YY_NULL; /* End of File */ \
  245:         } \
  246:       } else { /* successfully read in one line */ \
  247:         if (Lexi_buf[Input_idx]==NULL) puts("Whatup?");\
  248:         leng = strlen(Lexi_buf[Input_idx]); \
  249:         LIDBUG_PR3("<<yy_input() read into buffer a line(leng=%d), input index=%d>>\n",leng,Input_idx);  \
  250:         Lexi_pos[Input_idx] = 0; \
  251:         Lexi_line++; \
  252:         Current_line[Input_idx]++; \
  253:         (Lexi_pos[Input_idx])++; \
  254:         buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1];  \
  255:         /* need to take care of return continuation conditions */ \
  256:         /*  so, we return to one-char-at-a-time approach */ \
  257:         /* for(ii=0;ii<leng;ii++) { */ \
  258:         /*  buf[ii] = Lexi_buf[Input_idx][ii]; */ \
  259:         /* } */ \
  260:         /* buf[ii] = '\0'; */ \
  261:         /* printf("YY_INPUT()(Lexi_line=%d,max size=%d)(%c)",Lexi_line,max_size,buf[0]); */ \
  262:         result = 1; \
  263:       } \
  264:     } else { \
  265:       /* LIDBUG_PR2("<<yy_input() increase Lexi_pos, input index=%d>>\n",Input_idx);  */ \
  266:       (Lexi_pos[Input_idx])++; \
  267:       buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; \
  268:       result = 1; \
  269:     } \
  270:     if (Stop_Parser==1) { \
  271:       result = YY_NULL; \
  272:     } \
  273:   }
  274:   
  275: #else 
  276: 
  277: #define  YY_INPUT(buf,result,max_size) \
  278:   { int ii, leng; \
  279:     if (!Lexi_line) { /* was startup */ \
  280:        for(ii=0;ii < MAX_OPENED_FILE;ii++) { \
  281:          Lexi_buf[ii][0]=0; \
  282:          Lexi_pos[ii] = 0; \
  283:          Current_line[ii] = 0; \
  284:        } \
  285:        Input_idx = 0; \
  286:        first_run=0; \
  287:        yyin = Input_stream[Input_idx]; LIDBUG_PR1("<<yy_input() startup>>\n"); \
  288:     } \
  289:     if (!Lexi_buf[Input_idx][Lexi_pos[Input_idx]]) { /* test if the line buffer is empty or at the end */ \
  290:       if (fgets(Lexi_buf[Input_idx],LEX_BUFLEN-1,Input_stream[Input_idx])==NULL) { /* read in one line */ \
  291:         LIDBUG_PR2("<<yy_input() fgets() returns NULL, input index=%d>>\n",Input_idx); \
  292:         if( (Input_idx > 0) && ( Lexi_buf[Input_idx][Lexi_pos[Input_idx]] == '\0') ) { \
  293:           LIDBUG_PR2("<<yy_input() close an input stream, input index=%d>>\n",Input_idx); \
  294:           fclose(Input_stream[Input_idx]); \
  295:           Input_idx--; \
  296:           yyin = Input_stream[Input_idx]; \
  297:           /* (Lexi_pos[Input_idx])++; */ \
  298:           buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; \
  299:           result = 1; \
  300:         } else { \
  301:           result = YY_NULL; /* End of File */ \
  302:         } \
  303:       } else { /* successfully read in one line */ \
  304:         leng = strlen(Lexi_buf[Input_idx]); \
  305:         LIDBUG_PR3("<<yy_input() read into buffer a line(leng=%d), input index=%d>>\n",leng,Input_idx);  \
  306:         Lexi_pos[Input_idx] = 0; \
  307:         Lexi_line++; \
  308:         Current_line[Input_idx]++; \
  309:         (Lexi_pos[Input_idx])++; \
  310:         buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1];  \
  311:         /* need to take care of return continuation conditions */ \
  312:         /*  so, we return to one-char-at-a-time approach */ \
  313:         /* for(ii=0;ii<leng;ii++) { */ \
  314:         /*  buf[ii] = Lexi_buf[Input_idx][ii]; */ \
  315:         /* } */ \
  316:         /* buf[ii] = '\0'; */ \
  317:         /* printf("YY_INPUT()(Lexi_line=%d,max size=%d)(%c)",Lexi_line,max_size,buf[0]); */ \
  318:         result = 1; \
  319:       } \
  320:     } else { \
  321:       /* LIDBUG_PR2("<<yy_input() increase Lexi_pos, input index=%d>>\n",Input_idx);  */ \
  322:       (Lexi_pos[Input_idx])++; \
  323:       buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; \
  324:       result = 1; \
  325:     } \
  326:     if (Stop_Parser==1) { \
  327:       result = YY_NULL; \
  328:     } \
  329:   }
  330: #endif  /* USE_DYNAMIC_LEXBUFS */
  331: #endif /*NEWYYINPUT*/
  332: #endif /*AVOIDYYINPUT*/
  333: 
  334: #else
  335: 
  336: #undef  input
  337: #undef  unput
  338: 
  339: #endif
  340: 
  341: int capa_eof();
  342: 
  343: %}
  344: 
  345: Alpha      [a-zA-Z_]
  346: KeyChar    [A-Z]
  347: AlphaNum   [a-zA-Z_0-9]
  348: Number     [0-9]
  349: HexNumber  [0-9a-fA-F]
  350: Space      [ \t]
  351: Spaces     ({Space}*)
  352: FileName   (\"[^"\n]*\")
  353: Qchar      ([0-9a-zA-Z \t!?\._,:;'"`~@#$%\^&\+\-\*=|\[\]{}()])
  354: Operator   ([=\+\-\*/%<>!&|,])
  355: Identifier ([a-zA-Z_][a-zA-Z_0-9]*)
  356: EndLine    ([\r][\n]|[\n])
  357: 
  358: %a 10500
  359: %o 15000
  360: %k 10000
  361: %p 10000
  362: %n 1000
  363: %x  S_COMMENT   S_HINT S_HINTEXLAINX  S_IMPORT S_EXPLAIN S_ENDX    S_UNIT   S_IGNORE  
  364: %x  S_SKIP      S_VARIABLE S_LET  S_DEFINE     S_TEXT    S_MAP     S_FIGURE S_ANSWER 
  365: %x  S_STRING    S_ANSCONTINUE     S_TRUE_FALSE_STMT      S_IF_SKIP S_WHILE_SKIP
  366: %x  S_NEXT_LINE S_VERB   S_ECHO S_STRINGINANS
  367: %array
  368: 
  369: 
  370: 
  371: %%
  372: 
  373: <S_IGNORE>{
  374: {EndLine}                       BEGIN S_IGNORE;
  375: [^\n]*$                         BEGIN S_IGNORE;
  376: <<EOF>>                        {
  377:                                   capa_eof();
  378: #ifndef AVOIDYYINPUT
  379: 				  yyterminate();
  380: #endif
  381:                                }
  382: }
  383: 
  384: <S_COMMENT>{
  385: {EndLine}{Spaces}"//"[^\n]*$ {LLDBUG_PRL2("[COMMENT<%s>]\n",yytext); 
  386: 			      send("# %s\n",&yytext[2]);
  387:                              }
  388: [^\n]*{EndLine}      {
  389:                        send("\n"); BEGIN S_TEXT;
  390:                      }
  391: }
  392: 
  393: 
  394: <S_TEXT>{
  395: ^{Spaces}"/LET" |
  396: 	   ^{Spaces}"/BEG"                  { LLDBUG_PRL1("[LET]"); Pcount = 0; BEGIN S_LET; start_mode(MODE_SCRIPT,NULL);
  397: }
  398: ^{Spaces}"/VERB"                 { 
  399:                                    LLDBUG_PRL1("[VERBATIM]");
  400:                                    BEGIN S_VERB; 
  401: 				   start_mode(MODE_OUTTEXT,NULL);
  402: 				   send("<PRE>\n");
  403:                                  }
  404: ^{Spaces}"/HIN"{Alpha}*{Spaces}  { LLDBUG_PRL1("[HIN]"); 
  405:                                    start_mode(MODE_HINT, "");
  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("<numericalresponse 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; sccount = 0; BEGIN S_MAP; start_mode(MODE_SCRIPT,NULL);send("&map("); }
  452: ^{Spaces}"/RMAP"              { LLDBUG_PRL1("[RMAP]"); Pcount = 0; sccount = 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}*            {send("${%s}",yytext);}
  660: {Space}+                      { }
  661: [)]                           {  yy_pop_state(); }
  662: }
  663: 
  664: <S_IMPORT>{
  665: {FileName}{Space}*             { end_mode();send("<import>/res/ohiou/%s</import>\n",yytext); BEGIN S_SKIP; }
  666: {Identifier}{Space}*           { end_mode();send("<import>$%s</import>\n",yytext);  BEGIN S_SKIP; }
  667: }
  668: 
  669: <S_ANSWER>{
  670: [Pp][Ll][Uu][Ss]             { LLDBUG_PR1("[PLUS]"); add_delayed("+");}
  671: [Mm][Ii][Nn][Uu][Ss]         { LLDBUG_PR1("[MINUS]"); add_delayed("-");}
  672: 
  673: [Cc][Ss]                     { LLDBUG_PR1("[CS]"); send("cs");}
  674: [Cc][Ii]                     { LLDBUG_PR1("[CI]"); send("ci");}
  675: [Mm][Cc]                     { LLDBUG_PR1("[MC]"); send("mc");}
  676: [Ff][Mm][Ll]                 { LLDBUG_PR1("[FORMULA]"); send("fml"); }
  677: 
  678: [Oo][Nn]                     |
  679: [Yy][Ee][Ss]                 { LLDBUG_PR1("[ON]"); send("on");}
  680: [Oo][Ff][Ff]                 |
  681: [Nn][Oo]                     { LLDBUG_PR1("[OFF]"); send("off");}
  682: [Ff][Mm][Tt]                 { LLDBUG_PR1("[FMT]"); }
  683: [Uu][Nn][Ff][Mm][Tt]         { LLDBUG_PR1("[UNFMT]"); }
  684: 
  685: [,=]                  { LLDBUG_PR2("[symbol(%s)]",yytext);}
  686: [%]                  { LLDBUG_PR2("[symbol(%s)]",yytext); 
  687:                        if (dosend==1) send("%s",yytext);
  688:                        if (dosend==2) add_delayed("%s",yytext);
  689:                      }
  690: [:@#-]                  { LLDBUG_PR2("[symbol(%s)]",yytext); 
  691:                           if (dosend==1) send("%s",yytext);
  692:                           if (dosend==2) add_delayed("%s",yytext);
  693:                         }
  694: "<"                          { LLDBUG_PR2("[symbol(%s)]",yytext);
  695:                                if (dosend==1) send("%s",yytext);
  696:                                if (dosend==2) add_delayed("%s",yytext);
  697:                              }
  698: ">"                          { LLDBUG_PR2("[symbol(%s)]",yytext);
  699:                                if (dosend==1) send("%s",yytext);
  700:                                if (dosend==2) add_delayed("%s",yytext);
  701:                              }
  702: 
  703: [Pp][Cc][Rr]                 |
  704: [Hh][Gg][Rr]                 { if (firstparam) firstparam=0; else add_delayed("\" />\n\t");
  705:                                add_delayed("<responseparam name=\"hgr\" type=\"on|off\" default=\""); 
  706:                                dosend=2;
  707:                              }
  708: [Tt][Oo][Ll]                 { LLDBUG_PR2("[tol(%s)]",yytext);
  709:                                if (firstparam) firstparam=0; else add_delayed("\" />\n\t");
  710: 			       add_delayed("<responseparam name=\"tol\" type=\"tolerance\" description=\"Numerical Tolerance\" default=\""); 
  711:                                dosend=2;
  712:                              }
  713: [Ss][Ii][Gg]                 { 
  714:                                LLDBUG_PR2("[SIG(%s)]",yytext); 
  715:                                if (firstparam) firstparam=0; else add_delayed("\" />\n\t");
  716: 			       add_delayed("<responseparam name=\"sig\" type=\"int,range,0-12\" description=\"Significant Figures\" default=\""); 
  717:                                dosend=2;
  718:                              }
  719: 
  720: [Ss][Tt][Rr]                 { LLDBUG_PR1("[STR]"); send("\" str=\""); dosend=1; }
  721: [Ee][Vv][Aa][Ll]             |
  722: [Ee][Vv][Aa][Ll][Uu][Aa][Tt][Ee] { LLDBUG_PR1("[EVAL]");send("\" eval="); dosend=1;}
  723: [Uu][Nn][Ii][Tt]             |
  724: [Uu][Nn][Ii][Tt][Ss]         { LLDBUG_PR1("[UNIT]"); send("\" units=\""); dosend=1;}
  725: 
  726: [Ee][Xx][Tt][Ee][Rr][Nn][Aa][Ll]  { LLDBUG_PR1("[EXTERNAL]"); dosend=0; }
  727: [Aa][Nn][Ss][Bb][Oo][Xx]     { LLDBUG_PR1("[SHOW_ANS_BOX]"); dosend=0; }
  728: [Vv][Ee][Rr][Bb][Aa][Tt][Ii][Mm] { LLDBUG_PR1("[VERBATIM]"); dosend=0; }
  729: [Bb][Rr]                     { LLDBUG_PR1("[SHOW_BR]"); dosend=0; }
  730: [Pp][Aa][Tt][Hh]             { send("\" path=\""); dosend=0; }
  731: [Cc][Aa][Ll][Cc]             { send("\" calc=\""); dosend=0; }
  732: 
  733: [Ee][Xx][Pp][Ll][Aa][Ii][Nn] { LLDBUG_PR1("[EXPLAIN]"); dosend=0; }
  734: [Hh][Ii][Nn][Tt]             { LLDBUG_PR1("[HINT]"); dosend=0; }
  735: [Tt][Rr][Yy]                 |
  736: [Tt][Rr][Ii][Ee][Ss]         { LLDBUG_PR1("[TRY]"); dosend=0; }
  737: [Ww][Gg][Tt]                 { LLDBUG_PR1("[WGT]"); dosend=0; }
  738: 
  739: [\)]                         { LLDBUG_PR1("[)]"); Pcount--; 
  740:                                if(Pcount==0) {
  741:                                    BEGIN S_ANSCONTINUE; 
  742:                                }
  743:                                send("\">\n\t");
  744: 			       dosend=1;
  745: 			       flush_delayed();
  746: 			       if (!firstparam) send("\" />");
  747: 			       send("\n\t<textline />\n</numericalresponse>\n");
  748:                              }
  749: }
  750: 
  751: <S_VARIABLE,S_TRUE_FALSE_STMT,S_LET,S_MAP,S_ANSWER>{
  752: {Alpha}{AlphaNum}*             { LLDBUG_PR2("[ID<%s>]",yytext);
  753:                                  LLDBUG_PR2("[SYMB CNT=<%d>]", Symb_count); 
  754:                                  if (dosend==1) send("$%s",yytext); 
  755:                                  if (dosend==2) add_delayed("$%s",yytext); 
  756:                                }
  757: 
  758: {Alpha}{AlphaNum}*{Space}*[(]  { if (dosend==1) send("&%s",yytext);
  759:                                  if (dosend==2) add_delayed("&%s",yytext);
  760: 				 Pcount++;
  761:                                }
  762: {Alpha}{AlphaNum}*{Space}*[\[]  {  char aline[MAX_FUNC_NAME];
  763:                                    int  i;   
  764:                                    for(i=0;i < (yyleng-1); i++) {
  765:                                      if( yytext[i] == ' ' || yytext[i] == '\t' || 
  766:                                          yytext[i] == 0   || yytext[i] == '[' )    break;
  767:                                      aline[i] = yytext[i];
  768:                                    }
  769:                                    aline[i] = 0;
  770:                                    LLDBUG_PR2("[ARRAY<%s>]",aline);
  771:                                    
  772:                                    yylval = (Symbol *) capa_malloc(1, sizeof(Symbol)); /* *** */
  773:                                    yylval->s_name = strsave(aline); /* free it in parser() */
  774:                                    yylval->s_type = ARRAY_ID;
  775:                                    
  776:                                    yyless(yyleng-1); /* <-- push back char '[' */
  777:                                    RETURN(ARRAY_ID);
  778:                                }
  779: {Number}*"\."{Number}*[Ee]"+"{Number}+ |
  780: {Number}*"\."{Number}*[Ee]{Number}+    |
  781: {Number}*"\."{Number}*[Ee]"-"{Number}+ |
  782: {Number}+[Ee]"+"{Number}+ |
  783: {Number}+[Ee]{Number}+    |
  784: {Number}+[Ee]"-"{Number}+ |
  785: {Number}+"\."{Number}*    |
  786: "\."{Number}+             {  LLDBUG_PR2("[REAL<%s>]",yytext);
  787: 			     if(dosend==1) send("%s",yytext);
  788: 			     if(dosend==2) add_delayed("%s",yytext);
  789: 			  }
  790: 
  791: {Number}+                 {  LLDBUG_PR2("[INT<%s>]",yytext);
  792:                              if (dosend==1) send("%s",yytext);
  793:                              if (dosend==2) add_delayed("%s",yytext);
  794: 			  }
  795: [\[]                      { LLDBUG_PR1("[dis let ans map '[']");         return(yytext[0]); }
  796: [\]]                      { LLDBUG_PR1("[dis let ans map ']']");         return(yytext[0]); }
  797: {Space}+                  { /* LLDBUG_PR1("[SP ignored]");  Ignore Spaces */ }
  798: }
  799: 
  800: <S_VARIABLE,S_TRUE_FALSE_STMT,S_MAP,S_LET>{
  801: [\"]                      { LLDBUG_PR1("[TF,V,LET,MAP str\" ]"); 
  802:                             Current_char_p = String_buf; 
  803: 			    send("'");
  804:                             yy_push_state(S_STRING);
  805:                           }
  806: }
  807: 
  808: <S_ANSWER>{
  809: [\"]                      { LLDBUG_PR1("[ANS str\" ]"); 
  810:                             Current_char_p = String_buf; 
  811:                             yy_push_state(S_STRINGINANS);
  812:                           }
  813: }
  814: 
  815: <S_VARIABLE,S_TRUE_FALSE_STMT,S_MAP,S_ANSWER>{
  816: [\(]                      { LLDBUG_PR1("[dis let ans map (]"); 
  817:                             Pcount++; 
  818: 			    if (Pcount > 1 ) {
  819: 			      if (dosend==1) send(yytext);
  820: 			      if (dosend==2) add_delayed(yytext);
  821: 			    } 
  822:                           }
  823: }
  824: 
  825: <S_LET>{
  826: [\(]                      { LLDBUG_PR1("[dis let ans map (]"); 
  827:                             Pcount++; 
  828: 			    send(yytext);
  829:                           }
  830: }
  831: 
  832: <S_VARIABLE>[:]{Number}+[EeFf]   { 
  833:                              end_delayed();
  834:                              send("&format(");
  835:                              flush_delayed();
  836:                              send(",'%s')",yytext+1);
  837:                            }
  838: <S_ANSWER>[:]{Number}+[EeFf]   { 
  839:                              if (dosend) send("\" format=\"%s",yytext+1);
  840:                            }
  841: 
  842: <S_MAP>{
  843: [;]   { 
  844:         if (sccount==0) {
  845:           send(",[\\");
  846:           sccount++;
  847:         } else if (sccount==1) {
  848:           send("],[");
  849:           sccount++;
  850:         }
  851:        }
  852: [,]     {
  853:         if (sccount==1) {
  854:           send(",\\");
  855:         } else {
  856:           send(",");
  857:         }
  858:       }
  859: [\)]  {
  860:         LLDBUG_PR1("[) in MAP]"); Pcount--; 
  861:         if(Pcount==0) {
  862:           BEGIN S_SKIP; 
  863:         }
  864:         /* you might need a ; in the string below */
  865:         send("]%c;\n",yytext[0]);
  866:         sccount=0;
  867:       }
  868: }
  869: 
  870: <S_VARIABLE,S_TRUE_FALSE_STMT,S_LET,S_MAP>{
  871: "=="                { LLDBUG_PR1("[==]"); send(yytext);  }
  872: "!="                { LLDBUG_PR1("[!=]"); send(yytext);  }
  873: ">"                 { LLDBUG_PR1("[>]");  send(yytext);  }
  874: ">="                { LLDBUG_PR1("[>=]"); send(yytext);  }
  875: "<"                 { LLDBUG_PR1("[<]");  send(yytext);  }
  876: "<="                { LLDBUG_PR1("[<=]"); send(yytext);  }
  877: "&&"                { LLDBUG_PR1("[&&]"); send(yytext);  }
  878: "||"                { LLDBUG_PR1("[||]"); send(yytext);  }
  879: "//"                { if(Pcount==0) {
  880: 			 send("; #");
  881: 			 BEGIN S_ECHO;
  882:                       }
  883:                     }
  884: {Operator}          { LLDBUG_PR2("[Op(%c) in VAR,TF_STMT,LET]",yytext[0]); send(yytext); }
  885: }
  886: 
  887: 
  888: 
  889: <S_VARIABLE>{
  890: [\)]                     { LLDBUG_PR1("[)]"); 
  891:                            Pcount--; 
  892:                            if(Pcount == 0) {
  893:                               BEGIN S_TEXT; 
  894:                               flush_delayed();
  895:                            } else {
  896:                               send(yytext); 
  897:                            }
  898:                          }
  899: [\\]{Space}*{EndLine}    { LLDBUG_PR2("[\\EoL continue in S_VARIABLE (DIS?)](%s)",yytext); /* continuation on next line */ }                       
  900: {EndLine}                { LLDBUG_PR1("[EoL within /dis()]\n"); }
  901: .                   { char warn_msg[WARN_MSG_LENGTH]; 
  902:                       sprintf(warn_msg,"When use a VARIABLE, an unexpected char [%c] is encountered.\n",yytext[0]);
  903:                       capa_msg(MESSAGE_ERROR,warn_msg);
  904:                     }
  905: }
  906: 
  907: <S_TRUE_FALSE_STMT>{
  908: [\)]                     { LLDBUG_PRL1("[) in TRUE_FALSE]"); Pcount--; if(Pcount == 0)  BEGIN S_NEXT_LINE; return(yytext[0]); }
  909: [\\]{Space}*{EndLine}    { LLDBUG_PR2("[\\EoL continue in S_TRUE_FALSE_STMT](%s)",yytext); /* continuation on next line */ }                       
  910: {EndLine}                { LLDBUG_PR1("[EoL within /IF()]\n"); RETURN(EoL); }
  911: .                   { char warn_msg[WARN_MSG_LENGTH]; 
  912:                       sprintf(warn_msg,"In /IF(), an unexpected char [%c] is encountered.\n",yytext[0]);
  913:                       capa_msg(MESSAGE_ERROR,warn_msg);
  914:                     }
  915: }
  916: 
  917: <S_STRING>{
  918: [\\][\\]            { /*char  *aptr = yytext;
  919: 			while( *aptr )   *Current_char_p++ = *aptr++;*/
  920:                       send(yytext);
  921:                     }
  922: [\\][\"]            { /**Current_char_p++ = '"';*/ send("\\\"");  }
  923: [\\]{Space}*[\n]    { LLDBUG_PR2("[\\CR continue in S_STRING](%s)",yytext); /* continuation on next line */ }                       
  924: [\"]                { /* end of a string constant --   */
  925:                       send("'");
  926: 		      yy_pop_state();
  927: 		    }
  928: {EndLine}           { /* check for termination of string constant */
  929:                       char warn_msg[WARN_MSG_LENGTH];
  930:                       
  931:                       sprintf(warn_msg,"STRING not terminated properly, an EoL encountered in the middle.\n%s\n",String_buf);
  932:                       capa_msg(MESSAGE_ERROR,warn_msg);
  933:                       yy_pop_state();
  934:                     }
  935: .                   { /*char  *aptr = yytext;
  936: 			while( *aptr )   *Current_char_p++ = *aptr++;*/
  937:                       send(yytext);
  938:                     }
  939: }
  940: 
  941: <S_STRINGINANS>{
  942: [\\][\\]            { /*char  *aptr = yytext;
  943: 			while( *aptr )   *Current_char_p++ = *aptr++;*/
  944:                       if (dosend==1) send("%s",yytext);
  945: 		      if (dosend==2) add_delayed("%s",yytext);
  946:                     }
  947: [\\][\"]            { /**Current_char_p++ = '"';*/ 
  948:                       if (dosend==1) send("%s",yytext);
  949: 		      if (dosend==2) add_delayed("%s",yytext);
  950:                     }
  951: [\\]{Space}*[\n]    { LLDBUG_PR2("[\\CR continue in S_STRING](%s)",yytext); /* continuation on next line */ }                       
  952: [\"]                { /* end of a string constant --   */
  953: 		      yy_pop_state();
  954: 		    }
  955: {EndLine}           { /* check for termination of string constant */
  956:                       char warn_msg[WARN_MSG_LENGTH];
  957:                       
  958:                       sprintf(warn_msg,"STRING not terminated properly, an EoL encountered in the middle.\n%s\n",String_buf);
  959:                       capa_msg(MESSAGE_ERROR,warn_msg);
  960:                       yy_pop_state();
  961:                     }
  962: .                   { /*char  *aptr = yytext;
  963: 			while( *aptr )   *Current_char_p++ = *aptr++;*/
  964:                       if (dosend==1) send("%s",yytext);
  965: 		      if (dosend==2) add_delayed("%s",yytext);
  966:                     }
  967: }
  968: 
  969: <S_LET>[\)]                  { LLDBUG_PR1("[) in LET]"); Pcount--;send(yytext); }
  970: 
  971: <S_SKIP>{
  972: [^\n]+$                      {       }
  973: {EndLine}                    { BEGIN S_TEXT; }
  974: }
  975: 
  976: <S_ECHO>{
  977: [^\n]+$                      { send(yytext); }
  978: {EndLine}                    { send(yytext); BEGIN S_TEXT; }
  979: }
  980: 
  981: <S_LET,S_ANSWER,S_MAP>{
  982: [\\]{Space}*{EndLine}        { LLDBUG_PR1("[\\EoL let ans map]"); /* continuation */ }
  983: {EndLine}                    { LLDBUG_PR1("[EoL END let ans map]\n"); 
  984:                                if(Pcount == 0) BEGIN S_TEXT; 
  985:                                send(";%s",yytext); 
  986:                              }
  987: }
  988: 
  989: <S_ANSCONTINUE>{
  990: {Space}+                 { /* ignore white spaces */ }
  991: [\\]{Space}*{EndLine}    { /* continuation */ }
  992: {EndLine}                { /* end of answer and/or other answers */ LLDBUG_PR1("[complete an answer<EoL>]"); 
  993:                            BEGIN S_TEXT; }
  994: "/AND"                   { LLDBUG_PR1("[AND]"); RETURN(ANS_AND); }
  995: "/OR"                    { LLDBUG_PR1("[OR]");  RETURN(ANS_OR);  }
  996: }
  997: 
  998: <S_IF_SKIP>{
  999: ^{Spaces}"/IF"[^\n]*{EndLine}    { IFcount++; LLDBUG_PRL2("[Skip IF <IFcount=%d>]\n",IFcount); 
 1000:                                    IFstatus[IFcount] = IF_DONT_CARE;
 1001:                                  }
 1002: ^{Spaces}"/ELSE"[^\n]*{EndLine}  {  LLDBUG_PRL2("[Skip ELSE <IFcount=%d>]",IFcount);
 1003:                             IFcurrent[IFcount]=RUN_ELSE_PORTION; 
 1004:                             if( IFstatus[IFcount] == IF_FALSE ) {
 1005:                                LLDBUG_PRL1("[ELSE begin TEXT CR]\n");
 1006:                                BEGIN S_TEXT;
 1007:                             } 
 1008:                             if( IFstatus[IFcount] == IF_TRUE ) {
 1009:                                LLDBUG_PRL1("[ELSE THIS SHOULD NEVER HAPPEN.]\n");
 1010:                             }
 1011:                          }
 1012: ^{Spaces}"/ENDIF"[^\n]*{EndLine} { IFcount--; LLDBUG_PRL2("[Skip ENDIF <IFcount=%d>]\n",IFcount);
 1013:                             if( IFcount == 0 ) {
 1014:                                LLDBUG_PRL1("[ENDIF begin TEXT CR]\n");
 1015:                                BEGIN S_TEXT;
 1016:                             }
 1017:                             if( (IFcurrent[IFcount] == RUN_IF_PORTION )&&(IFstatus[IFcount] == IF_TRUE)) {
 1018:                                LLDBUG_PRL1("[ENDIF begin TEXT CR]\n");
 1019:                                BEGIN S_TEXT;
 1020:                             }
 1021:                             if( (IFcurrent[IFcount] == RUN_ELSE_PORTION )&&(IFstatus[IFcount] == IF_FALSE)) {
 1022:                                LLDBUG_PRL1("[ENDIF begin TEXT CR]\n");
 1023:                                BEGIN S_TEXT;
 1024:                             }
 1025:                          }
 1026: {EndLine}                { LLDBUG_PRL1("[SkipIF a CR]\n");       }
 1027: [^\n]*$                  { LLDBUG_PRL2("[SkipIF anything <IFcount=%d>]",IFcount);   }
 1028: }
 1029: <S_NEXT_LINE>{
 1030: ([.]*){EndLine}          { /* this ignores everything until it hits an EoL */
 1031:                            LLDBUG_PRL2("[<S_NEXT_LINE> skip \'%s\' until EoL]\n",yytext); 
 1032:                            BEGIN S_TEXT;
 1033:                          }
 1034: }
 1035: 
 1036: <S_WHILE_SKIP>{
 1037: ^{Spaces}"/WHILE"[^\n]*{EndLine}        { Wcount++;
 1038:                                           LLDBUG_PRL2("[SkipWHILE /WHILE <Wcount=%d>]\n",Wcount);   
 1039:                                         }
 1040: ^{Spaces}"/ENDWHILE"[^\n]*{EndLine}     { 
 1041:                                           if(Wcount==0) {
 1042:                                              LLDBUG_PRL2("[SkipWHILE->/ENDWHILE <Wcount=%d>]\n",Wcount);
 1043:                                              BEGIN S_TEXT;
 1044:                                           } else {
 1045:                                              Wcount--;
 1046:                                              LLDBUG_PRL2("[SkipWHILE /ENDWHILE <Wcount=%d>]\n",Wcount);
 1047:                                           }
 1048:                                         }
 1049: {EndLine}                { LLDBUG_PRL1("[SkipWHILE a CR]\n");       }                         
 1050: [^\n]*$                  { LLDBUG_PRL2("[SkipWHILE anything <Wcount=%d>]",Wcount);   }
 1051: }
 1052: 
 1053: <S_VERB>{
 1054: ^{Spaces}"/ENDVERB" { LLDBUG_PRL1("[END VERB]\n"); 
 1055: 		      BEGIN S_TEXT;
 1056: 		      puts("\n</PRE>\n");
 1057: 		      end_mode();
 1058: 		    }
 1059: .*|{EndLine}         { send(yytext); }
 1060: }
 1061: 
 1062: %%
 1063: 
 1064: /* ========================================================================================== */
 1065: extern void
 1066: begin_if_skip() { BEGIN S_IF_SKIP; }
 1067: 
 1068: extern void
 1069: begin_while_skip() { Wcount=0; While_idx--; /* while is FALSE, pop it out from stack */ BEGIN S_WHILE_SKIP; }
 1070: 
 1071: extern void
 1072: begin_next_line()  { BEGIN S_NEXT_LINE; }
 1073: 
 1074: extern void
 1075: begin_var() { BEGIN S_VARIABLE; }
 1076: 
 1077: extern void
 1078: begin_let() { BEGIN S_LET; }
 1079: 
 1080: extern void
 1081: begin_def() { BEGIN S_DEFINE; }
 1082: 
 1083: extern void
 1084: begin_ans() { BEGIN S_ANSWER; }
 1085: 
 1086: extern void
 1087: begin_map() { BEGIN S_MAP; }
 1088: 
 1089: extern void
 1090: begin_ignore() { BEGIN S_IGNORE; }
 1091: 
 1092: extern void
 1093: begin_text() { BEGIN S_TEXT; }
 1094: 
 1095: extern void
 1096: begin_question() { LLDBUG_PR1("[<S_TEXT>]"); 
 1097:                    IFcount = 0; While_idx=0; /* initialize some stacks */
 1098:                    End_of_input = 0; YY_FLUSH_BUFFER; BEGIN S_TEXT; }
 1099: 
 1100: extern void
 1101: end_problemset() { End_of_input = 0; YY_FLUSH_BUFFER; BEGIN S_TEXT; }
 1102: 
 1103: 
 1104: /* ========================================================================================== */
 1105: 
 1106: #define  NUM_KEY   2
 1107: int
 1108: match_keyword(key) char *key;
 1109: {
 1110:   char  *keyword[NUM_KEY] = {"/DIS", "/DIR" };
 1111:   int    i;
 1112:   
 1113:   for(i=0;i < NUM_KEY; i++) {
 1114:      if( !strncmp(keyword[i], key, 4) ) {
 1115:         return (1);
 1116:      }
 1117:   }
 1118:   return (0);
 1119: }
 1120: 
 1121: int
 1122: match_functionid(key) char *key;
 1123: {
 1124:   char  *keyword[NUM_KEY] = {"/DIS", "/DIR" };
 1125:   int    i;
 1126:   
 1127:   for(i=0;i < NUM_KEY; i++) {
 1128:      if( !strncmp(keyword[i], key, 4) ) {
 1129:         return (1);
 1130:      }
 1131:   }
 1132:   return (0);
 1133: }
 1134: /* -------------------------------------------------------------------------- */
 1135: /* -------------------------------------------------------------------------- */
 1136: 
 1137: void  init_funcstack() 
 1138: {  
 1139:  int ii;
 1140:  for(ii=0;ii<Func_idx;ii++) {
 1141:    capa_mfree(FuncStack[ii].s_name);
 1142:  }
 1143:  Func_idx = 0;  
 1144: }
 1145: 
 1146: 
 1147: /* -------------------------------------------------------------------------- */
 1148: /* GET THE NEXT CHARACTER OF THE SOURCE FILE                                  */
 1149: /* -------------------------------------------------------------------------- */
 1150: 
 1151: #ifdef  FLEX
 1152: int   capaL_input()
 1153: #else
 1154: int                        /* RETURNS: next character */
 1155: input()                    /* ARGUMENTS: (none)       */
 1156: #endif
 1157: 
 1158: {                          /* LOCAL VARIABLES:        */
 1159:   static  int startup=1;  /*    First call flag      */
 1160:   
 1161:   LLDBUG_PRL1("<<capaL_input() is called>>\n");
 1162:   if (!Lexi_line) { /* was startup */
 1163:        for(Input_idx=0;Input_idx < MAX_OPENED_FILE;Input_idx++) {
 1164:          /* for(ii=0;ii<LEX_BUFLEN;ii++) {
 1165:            Lexi_buf[Input_idx][ii]=0;
 1166:          }
 1167:          */
 1168:          Lexi_buf[Input_idx][0]=0;
 1169:          Lexi_pos[Input_idx] = 0;
 1170:        }
 1171:        Input_idx = 0;
 1172:        startup=0;
 1173:        yyin = Input_stream[Input_idx];
 1174:   }
 1175:   if (!Lexi_buf[Input_idx][Lexi_pos[Input_idx]]) {
 1176:     if (fgets(Lexi_buf[Input_idx],LEX_BUFLEN-1,Input_stream[Input_idx])==NULL) { 
 1177:       /* EOF? */
 1178:       /* no use in flex
 1179:          printf("capaL_input()EOF %s\n",Opened_filename[Input_idx+1]); fflush(stdout); */
 1180:       return (0);
 1181:     }
 1182:     Lexi_pos[Input_idx] = 0;
 1183:     Lexi_line++;
 1184:     printf("input()(%d)\n",Lexi_line);
 1185:   }
 1186:   (Lexi_pos[Input_idx])++;
 1187:   return ( Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1] );
 1188: }
 1189:  
 1190: /******************************************************************************/
 1191: /* PUSH BACK ONE CHARACTER OF THE INPUT                                       */
 1192: /******************************************************************************/
 1193: 
 1194: int                /* RETURNS: nothing     */
 1195: capaL_unput(ch)          /* ARGUMENTS:           */
 1196: register int ch;   /*    Character to push */
 1197: {
 1198:    if (ch)   (Lexi_pos[Input_idx])--;
 1199:  
 1200:    /* unput() stream cannot be re-defined */
 1201:    /* unput(ch); inconsistency between YY_INPUT() and internal matched yytext */
 1202:    return (0);
 1203: 
 1204: }
 1205: 
 1206: 
 1207: /******************************************************/
 1208: 
 1209: #ifndef DMALLOC
 1210: 
 1211: char *       
 1212: strsave(char *s) 
 1213: {            
 1214:    char *p;  
 1215:    if (s==NULL) {return s;}
 1216:    p=capa_malloc(strlen(s)+1,1);
 1217:    strcpy(p,s);
 1218:    return (p);
 1219: }
 1220: 
 1221: #endif
 1222: 
 1223: /* =========================================================================== */
 1224: 
 1225: #ifndef DMALLOC
 1226: char *
 1227: capa_malloc(unsigned num,unsigned sz)
 1228: {
 1229:   char *p;
 1230:   p = calloc(num, sz);
 1231:   bzero(p, num*sz);  /* added Jan 21 1998 */
 1232:   return (p);
 1233: }
 1234: 
 1235: #endif
 1236: 
 1237: #ifndef DMALLOC
 1238: void
 1239: capa_mfree(p) char *p;
 1240: {
 1241:   free(p);
 1242: }
 1243: #endif
 1244: 
 1245: void
 1246: capa_msg(int type, char *p) 
 1247: { int  idx, i, j;
 1248:   int  len;
 1249:   char warn_msg[WARN_MSG_LENGTH];
 1250:   char tmp_line[ONE_TWO_EIGHT];
 1251:   char  *tmp_str;
 1252:   
 1253:   strcpy(warn_msg,"File: ");
 1254:   idx=6;
 1255:   for(i=0;i<=Input_idx;i++) {
 1256:     len=strlen(Opened_filename[i]);
 1257:     for(j=0;j<len;j++) {
 1258:       warn_msg[idx++] = Opened_filename[i][j];
 1259:     }
 1260:     if(i < Input_idx) {
 1261:       warn_msg[idx++]='-';
 1262:       warn_msg[idx++]='>';
 1263:     }
 1264:     warn_msg[idx]=0;
 1265:   }
 1266:   switch (type) {
 1267:     case MESSAGE_ERROR:
 1268:            sprintf(tmp_line,", Line %d: ERROR:", Current_line[Input_idx]);
 1269:            len=strlen(tmp_line);
 1270:            for(j=0;j<len;j++) {
 1271:               warn_msg[idx++] = tmp_line[j];
 1272:            }
 1273:            warn_msg[idx]=0;
 1274:            append_error(warn_msg); append_error(p);
 1275: 	   break;
 1276:     case MESSAGE_WARN:
 1277:     default:
 1278:            sprintf(tmp_line,", Line %d: WARNING:", Current_line[Input_idx]);
 1279:            len=strlen(tmp_line);
 1280:            for(j=0;j<len;j++) {
 1281:              warn_msg[idx++] = tmp_line[j];
 1282:            }
 1283:            warn_msg[idx]=0;
 1284:            j = strlen(warn_msg);
 1285:            len = strlen(p);
 1286:            tmp_str = (char *)capa_malloc(len+j+1,1);
 1287:            for(i=0;i<j;i++) {
 1288:              tmp_str[i]=warn_msg[i];
 1289:            } 
 1290:            for(i=j;i<j+len;i++) {
 1291:              tmp_str[i] = p[i-j];
 1292:            }
 1293:            append_warn(type,tmp_str);
 1294:            capa_mfree(tmp_str);
 1295:            break;
 1296:   }
 1297: }
 1298: 
 1299: /* ======================================================================== */
 1300: void
 1301: capa_warn_header(int type)
 1302: {
 1303:   int  idx, i, j;
 1304:   int  len;
 1305:   char warn_msg[WARN_MSG_LENGTH];
 1306:   char tmp_line[ONE_TWO_EIGHT];
 1307:   
 1308:   strcpy(warn_msg,"File: ");
 1309:   idx=6;
 1310:   for(i=0;i<=Input_idx;i++) {
 1311:     len=strlen(Opened_filename[i]);
 1312:     for(j=0;j<len;j++) {
 1313:       warn_msg[idx++] = Opened_filename[i][j];
 1314:     }
 1315:     if(i < Input_idx) {
 1316:       warn_msg[idx++]='-';
 1317:       warn_msg[idx++]='>';
 1318:     }
 1319:     warn_msg[idx]=0;
 1320:   }
 1321:   switch (type) {
 1322:     case MESSAGE_ERROR:
 1323:       sprintf(tmp_line,", Line %d: ERROR:", Current_line[Input_idx]);
 1324:       
 1325:       break;
 1326:     case MESSAGE_WARN:
 1327:       sprintf(tmp_line,", Line %d: WARNING:", Current_line[Input_idx]);break;
 1328:     default:
 1329:     sprintf(tmp_line,", Line %d: ERROR:", Current_line[Input_idx]);break;
 1330:   }
 1331:   len=strlen(tmp_line);
 1332:   for(j=0;j<len;j++) {
 1333:     warn_msg[idx++] = tmp_line[j];
 1334:   }
 1335:   warn_msg[idx]=0;
 1336:   append_error(warn_msg);
 1337: }
 1338: 
 1339: /* --------------------------------------------------------------------------- */
 1340: #ifdef AVOIDYYINPUT
 1341: 
 1342: void change_file(char *fname)
 1343: {
 1344:   char warn_msg[WARN_MSG_LENGTH];
 1345:   
 1346:   if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
 1347:     sprintf(warn_msg,"Includes nested too deeply" );
 1348:     capa_msg(MESSAGE_ERROR,warn_msg);
 1349:     return;
 1350:   }
 1351: 
 1352:   include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
 1353:   yyin = fopen( fname, "r" );
 1354:   yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ) );
 1355: }
 1356: 
 1357: void
 1358: parse_filename(char *line)
 1359: {
 1360:   char *start, fname[MAX_BUFFER_SIZE],  warn_msg[WARN_MSG_LENGTH];
 1361:   int ii,len;
 1362: 
 1363:   start = index(line, '\"'); /*** hpux complained */
 1364:   if( start == NULL ) {
 1365:     sprintf(warn_msg,"/IMP was not given a filename.\n");
 1366:     capa_msg(MESSAGE_ERROR,warn_msg);
 1367:     return;
 1368:   }
 1369:   start++; len = strlen(start) - 1;
 1370:   ii = 0;
 1371:   while( start[ii] != '\"' ) fname[ii++] = start[ii];
 1372:   fname[ii] = 0;
 1373:   LLDBUG_PR2("[parse_filename<%s>]\n",fname);
 1374:   
 1375:   change_file(fname);
 1376: }
 1377: 
 1378: void
 1379: parse_import_id(char *line)
 1380: {
 1381:   char    fname[QUARTER_K], warn_msg[WARN_MSG_LENGTH];
 1382:   int     ii, dup_open;
 1383:   Symbol *symb_p;
 1384:   int     no_error = 0;
 1385:   
 1386:   ii = 0;
 1387:   while( line[ii] != '\0' && line[ii] != ' ' && line[ii] != '\n' && line[ii] != '\t' ) 
 1388:     fname[ii++] = line[ii]; 
 1389:   fname[ii] = 0;
 1390: 
 1391:   LLDBUG_PR2("[parse_import_id<%s>]\n",fname);
 1392:   /*symb_p = find_identifier(fname);*/
 1393:   
 1394:   switch (symb_p->s_type) {
 1395:    case IDENTIFIER:
 1396:      sprintf(warn_msg,"/IMP %s, var is not defined.\n", fname);
 1397:      capa_msg(MESSAGE_ERROR,warn_msg);
 1398:      break;
 1399:    case I_VAR: case I_CONSTANT: case R_VAR: case R_CONSTANT:
 1400:      sprintf(warn_msg,"var cannot be a number.\n");
 1401:      capa_msg(MESSAGE_ERROR,warn_msg);
 1402:      break;
 1403:    case S_VAR:  case S_CONSTANT: sprintf(fname,"%s",symb_p->s_str);
 1404:      no_error = 1;
 1405:      break;
 1406:   }
 1407:   if( no_error ) change_file(fname);
 1408: }
 1409: 
 1410: #else
 1411: void
 1412: parse_filename(char *line)
 1413: {
 1414:   char  *start, fname[QUARTER_K], warn_msg[WARN_MSG_LENGTH];
 1415:   int    ii, len, dup_open;
 1416:   
 1417:   /* printf("IMPORT %s\n", line); */
 1418:   
 1419:   start = index(line, '\"'); /*** hpux complained */
 1420:   if( start != NULL ) {
 1421:     start++; len = strlen(start) - 1;
 1422:     ii = 0;
 1423:     while( start[ii] != '\"' ) {
 1424:       fname[ii] = start[ii]; ii++;
 1425:     }
 1426:     fname[ii] = 0;
 1427:     LLDBUG_PR2("[parse_filename<%s>]\n",fname);
 1428:     if(Input_idx < (MAX_OPENED_FILE -1)) {
 1429:       dup_open = 0;
 1430:       /* -- no need to check duplicated opening a file 
 1431:       for(ii=0;ii<Input_idx;ii++) {
 1432:         if(strcmp(Opened_filename[ii],fname)==0) {
 1433:           dup_open =1;
 1434:         }
 1435:       }
 1436:       */
 1437:       if( !dup_open ) {
 1438:           Input_idx++;
 1439:           Input_stream[Input_idx]=fopen(fname,"r");
 1440:           sprintf(Opened_filename[Input_idx], "%s",fname);
 1441:           Current_line[Input_idx] = 0;
 1442:       } else {
 1443:         /*
 1444:           sprintf(warn_msg,"/IMP \"%s\", import file has already been imported.\n",fname);
 1445:           capa_msg(MESSAGE_WARN,warn_msg);
 1446:         */
 1447:           Input_idx++;
 1448:           Input_stream[Input_idx]=fopen(fname,"r");
 1449:           sprintf(Opened_filename[Input_idx], "%s",fname);
 1450:           Current_line[Input_idx] = 0;
 1451:       }
 1452:     } else {
 1453:       sprintf(warn_msg,"/IMP more the %d levels deep ignoring further imports.\n",MAX_OPENED_FILE-1);
 1454:       capa_msg(MESSAGE_WARN,warn_msg);
 1455:     }
 1456:   } else {
 1457:     sprintf(warn_msg,"%s, is not a valid file name.\n",line);
 1458:     capa_msg(MESSAGE_ERROR,warn_msg);
 1459:   }
 1460:   
 1461: }
 1462: 
 1463: void
 1464: parse_import_id(char *line)
 1465: {
 1466:   char    fname[QUARTER_K], warn_msg[WARN_MSG_LENGTH];
 1467:   int     ii, dup_open;
 1468:   Symbol *symb_p;
 1469:   int     no_error = 0;
 1470:   
 1471:     ii = 0;
 1472:     while (line[ii] != '\0' && line[ii] != ' ' && line[ii] != '\n' && line[ii] != '\t') {
 1473:       fname[ii] = line[ii]; ii++;
 1474:     }
 1475:     fname[ii] = 0;
 1476:     LLDBUG_PR2("[parse_import_id<%s>]\n",fname);
 1477:     /*symb_p = find_identifier(fname);*/
 1478:     
 1479:     switch (symb_p->s_type) {
 1480:       case IDENTIFIER:
 1481: 	sprintf(warn_msg,"/IMP %s, var is not defined.\n", fname);
 1482: 	capa_msg(MESSAGE_ERROR,warn_msg);
 1483: 	break;
 1484:       case I_VAR: case I_CONSTANT: case R_VAR: case R_CONSTANT:
 1485: 	sprintf(warn_msg,"var cannot be a number.\n");
 1486: 	capa_msg(MESSAGE_ERROR,warn_msg);
 1487: 	break;
 1488:       case S_VAR:
 1489:       case S_CONSTANT:
 1490: 	sprintf(fname,"%s",symb_p->s_str);
 1491: 	no_error = 1;
 1492: 	break;
 1493:     }
 1494:     if( no_error ) {
 1495:       if(Input_idx < (MAX_OPENED_FILE -1) ) {
 1496:           dup_open = 0;
 1497:           /* no need to check duplicated opening a file 
 1498:           for(ii=0;ii<Input_idx;ii++) {
 1499:             if(strcmp(Opened_filename[ii],fname)==0)  dup_open =1; 
 1500:           }
 1501:           */
 1502:           if( !dup_open ) {
 1503:               Input_idx++;
 1504:               Input_stream[Input_idx]=fopen(fname,"r");
 1505:               sprintf(Opened_filename[Input_idx], "%s",fname);
 1506:               Current_line[Input_idx] = 0;
 1507:           } else {
 1508:             /*  NO warning on duplicated open a file
 1509:             sprintf(warn_msg,"/IMP \"%s\", file has already been imported.\n", fname);
 1510:             capa_msg(MESSAGE_WARN,warn_msg);
 1511:             */
 1512:               Input_idx++;
 1513:               Input_stream[Input_idx]=fopen(fname,"r");
 1514:               sprintf(Opened_filename[Input_idx], "%s",fname);
 1515:               Current_line[Input_idx] = 0;
 1516:           }
 1517:         } else {
 1518:           sprintf(warn_msg,"/IMP , too many files has been imported. The maximum is %d files.\n",
 1519:               MAX_OPENED_FILE-1);
 1520:           capa_msg(MESSAGE_WARN,warn_msg);
 1521:         }
 1522:       }
 1523: }
 1524: #endif /*AVOIDYYINPUT*/
 1525: 
 1526: void append_dynamic_buf(new_str) char *new_str;
 1527: {
 1528:   int ii,len;
 1529:   
 1530:   if(new_str==NULL) return;
 1531:   len=strlen(new_str);
 1532: #ifdef LEX_DBUG
 1533:   printf("before: len %d; Dynamic_buf_cur %d; Dynamic_buf_max %d\n",
 1534: 	 len,Dynamic_buf_cur,Dynamic_buf_max);
 1535: #endif /* LEX_DBUG */    
 1536:   if (Dynamic_buf_cur+len+1>Dynamic_buf_max) {
 1537:     char *temp_text;
 1538:     Dynamic_buf_max=(Dynamic_buf_cur+len)*2;
 1539:     temp_text=(char*)capa_malloc(sizeof(char),Dynamic_buf_max);
 1540:     strncpy(temp_text,Dynamic_buf,Dynamic_buf_max);
 1541:     free(Dynamic_buf);
 1542:     Dynamic_buf=temp_text;
 1543:   }
 1544:   for(ii=0;ii<len;ii++) {
 1545:     Dynamic_buf[Dynamic_buf_cur+ii]=new_str[ii];
 1546:   }
 1547:   Dynamic_buf_cur += len;
 1548:   Dynamic_buf[Dynamic_buf_cur+1]='\0';
 1549: #ifdef LEX_DBUG
 1550:   printf("after: len %d; Dynamic_buf_cur %d; Dynamic_buf_max %d\n",
 1551: 	 len,Dynamic_buf_cur,Dynamic_buf_max);
 1552:   printf("Dyn_buf %s; added %s\n",Dynamic_buf,new_str);
 1553: #endif /* LEX_DBUG */    
 1554: }
 1555: 
 1556: char* parser_status()
 1557: {
 1558:   char *buf,small[SMALL_LINE_BUFFER];
 1559:   int i,j,totlen=0,len,idx=0;
 1560:   
 1561:   for(i=0;i<=Input_idx;i++) totlen+=strlen(Opened_filename[i])+6;
 1562:   buf=capa_malloc(sizeof(char),totlen);
 1563:   for(i=0;i<=Input_idx;i++) {
 1564:     len=strlen(Opened_filename[i]);
 1565:     for(j=0;j<len;j++) buf[idx++] = Opened_filename[i][j];
 1566:     buf[idx++] = ':';
 1567:     sprintf(small,"%d",Current_line[i]);
 1568:     len=strlen(small);
 1569:     for(j=0;j<len;j++) buf[idx++] = small[j];
 1570:     buf[idx++]=' ';
 1571:     buf[idx]='\0';
 1572:   }
 1573:   return buf;
 1574: }
 1575: 
 1576: void yyfatalerror(char*msg)
 1577: {
 1578:   char    warn_msg[WARN_MSG_LENGTH];
 1579:   sprintf(warn_msg,"Invalid character[\'%s\']\n",yytext);
 1580:   capa_msg(MESSAGE_ERROR,warn_msg);
 1581:   capa_msg(MESSAGE_ERROR,msg);
 1582: }
 1583: void yyerror(char* msg)
 1584: {
 1585:   char    warn_msg[WARN_MSG_LENGTH];
 1586:   sprintf(warn_msg,"%s\n",msg);
 1587:   capa_msg(MESSAGE_ERROR,warn_msg);
 1588: }
 1589: 
 1590: void newyy_input (char *buf,int *result,int max_size) 
 1591: { int ii, leng, out_of_char; 
 1592:  if (!Lexi_line) { /* was startup */ 
 1593:    for(ii=0;ii < MAX_OPENED_FILE;ii++) { 
 1594:      Lexi_buf[ii] = NULL; 
 1595:      Lexi_pos[ii] = 0; 
 1596:      Current_line[ii] = 0; 
 1597:    } 
 1598:    Input_idx = 0; 
 1599:    first_run=0; 
 1600:    yyin = Input_stream[Input_idx]; LIDBUG_PR1("<<yy_input() startup>>\n"); 
 1601:  } 
 1602:  out_of_char = 0; 
 1603:  if ( Lexi_buf[Input_idx] == NULL ) { 
 1604:    Lexi_buf[Input_idx] = (char *)capa_malloc(sizeof(char)*LEX_BUFLEN+1,1); out_of_char=1;
 1605:  } else { 
 1606:    if (!Lexi_buf[Input_idx][Lexi_pos[Input_idx]]) { 
 1607:      /* test if the line buffer is empty or at the end */ 
 1608:      out_of_char=1; 
 1609:    } 
 1610:  }
 1611:  if( out_of_char ) { 
 1612:    if (fgets(Lexi_buf[Input_idx],LEX_BUFLEN-1,Input_stream[Input_idx])==NULL) { 
 1613:      /* read in one line */ 
 1614:      LIDBUG_PR2("<<yy_input() fgets() returns NULL, input index=%d>>\n",Input_idx); 
 1615:      if( (Input_idx > 0) && ( Lexi_buf[Input_idx][Lexi_pos[Input_idx]] == '\0') ) { 
 1616:        LIDBUG_PR2("<<yy_input() close an input stream, input index=%d>>\n",Input_idx); 
 1617:        fclose(Input_stream[Input_idx]); 
 1618:        capa_mfree((char *)Lexi_buf[Input_idx]); 
 1619:        Lexi_buf[Input_idx] = NULL; 
 1620:        Input_idx--; 
 1621:        yyin = Input_stream[Input_idx]; 
 1622:        /* (Lexi_pos[Input_idx])++; */ 
 1623:        buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; 
 1624:        *result = 1; 
 1625:      } else { 
 1626:        *result = YY_NULL; /* End of File */ 
 1627:      } 
 1628:    } else { /* successfully read in one line */ 
 1629:      leng = strlen(Lexi_buf[Input_idx]); 
 1630:      LIDBUG_PR3("<<yy_input() read into buffer a line(leng=%d), input index=%d>>\n",
 1631: 		leng,Input_idx);  
 1632:      Lexi_pos[Input_idx] = 0; 
 1633:      Lexi_line++; 
 1634:      Current_line[Input_idx]++; 
 1635:      (Lexi_pos[Input_idx])++; 
 1636:      buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1];  
 1637:      /* need to take care of return continuation conditions */ 
 1638:      /*  so, we return to one-char-at-a-time approach */ 
 1639:      /* for(ii=0;ii<leng;ii++) { */ 
 1640:      /*  buf[ii] = Lexi_buf[Input_idx][ii]; */ 
 1641:      /* } */ 
 1642:      /* buf[ii] = '\0'; */ 
 1643:      /* printf("YY_INPUT()(Lexi_line=%d,max size=%d)(%c)",Lexi_line,max_size,buf[0]); */ 
 1644:      *result = 1; 
 1645:    } 
 1646:  } else { 
 1647:    /* LIDBUG_PR2("<<yy_input() increase Lexi_pos, input index=%d>>\n",Input_idx);  */ 
 1648:    (Lexi_pos[Input_idx])++; 
 1649:    buf[0] = Lexi_buf[Input_idx][Lexi_pos[Input_idx]-1]; 
 1650:    *result = 1; 
 1651:  } 
 1652:  if (Stop_Parser==1) *result = YY_NULL;  
 1653: }
 1654: 
 1655: int capa_eof()
 1656: {
 1657: #ifdef AVOIDYYINPUT
 1658:   if ( --include_stack_ptr < 0 ) yyterminate();
 1659:   else {
 1660:     yy_delete_buffer( YY_CURRENT_BUFFER );
 1661:     yy_switch_to_buffer(include_stack[include_stack_ptr]);
 1662:   }
 1663: #else
 1664:   if(Input_idx == 0) {
 1665:     fclose(Input_stream[Input_idx]);
 1666:     capa_mfree((char *)Lexi_buf[Input_idx]); 
 1667:     /*free_problems(LexiProblem_p);*/
 1668:     LexiProblem_p=NULL;
 1669:     /* printf("\nCAPA EOF\n"); fflush(stdout); */
 1670:   }
 1671:   end_mode();
 1672:   return (0);
 1673: #endif /*AVOIDYYINPUT*/
 1674: }
 1675: /* ------------ */
 1676: 
 1677: 
 1678: /* =========================================================== */

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