Annotation of capa/capa51/pProj/capaLexerDef.flex, revision 1.4

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

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