[BACK]Return to lex.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / parse

Annotation of OpenXM_contrib2/asir2000/parse/lex.c, Revision 1.2

1.2     ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/parse/lex.c,v 1.1.1.1 1999/12/03 07:39:12 noro Exp $ */
1.1       noro        2: #include <ctype.h>
                      3: #include "ca.h"
                      4: #include "al.h"
                      5: #include "base.h"
                      6: #include "parse.h"
                      7: #if !defined(THINK_C)
                      8: #include <sys/types.h>
                      9: #include <sys/stat.h>
                     10: #endif
                     11: #include "y.tab.h"
                     12:
                     13: extern IN asir_infile;
                     14: extern struct oTKWD kwd[];
                     15:
                     16: int afternl();
                     17: int myatoi();
                     18: int aftercomment();
                     19:
                     20: extern int main_parser;
                     21: extern char *parse_strp;
                     22:
                     23: static int skipspace();
                     24: static int Getc();
                     25: static void Ungetc();
                     26: static void Gets();
                     27:
1.2     ! noro       28: #define NBUFSIZ (BUFSIZ*10)
        !            29: #define TBUFSIZ (BUFSIZ)
        !            30:
        !            31: #define REALLOC_NBUF \
        !            32: if ( i >= nbufsize ) {\
        !            33:        nbufsize += NBUFSIZ;\
        !            34:        if ( nbuf == nbuf0 ) {\
        !            35:                nbuf = (char *)MALLOC_ATOMIC(nbufsize);\
        !            36:                bcopy(nbuf0,nbuf,nbufsize-NBUFSIZ);\
        !            37:        } else\
        !            38:                nbuf = REALLOC(nbuf,nbufsize);\
        !            39: }
        !            40:
        !            41: #define REALLOC_TBUF \
        !            42: if ( i >= tbufsize ) {\
        !            43:        tbufsize += TBUFSIZ;\
        !            44:        if ( tbuf == tbuf0 ) {\
        !            45:                tbuf = (char *)MALLOC_ATOMIC(tbufsize);\
        !            46:                bcopy(tbuf0,tbuf,tbufsize-TBUFSIZ);\
        !            47:        } else\
        !            48:                tbuf = REALLOC(tbuf,tbufsize);\
        !            49: }
        !            50:
        !            51: #define READ_ALNUM_NBUF \
        !            52: while ( 1 ) {\
        !            53:        c = Getc();\
        !            54:        if ( isalnum(c) ) {\
        !            55:                REALLOC_NBUF nbuf[i++] = c;\
        !            56:        } else\
        !            57:                break;\
        !            58: }
        !            59:
        !            60: #define READ_DIGIT_NBUF \
        !            61: while ( 1 ) {\
        !            62:        c = Getc();\
        !            63:        if ( isdigit(c) ) {\
        !            64:                REALLOC_NBUF nbuf[i++] = c;\
        !            65:        } else\
        !            66:                break;\
        !            67: }
        !            68:
1.1       noro       69: yylex()
                     70: {
                     71: #define yylvalp (&yylval)
                     72:        register int c,c1;
                     73:        register int *ptr;
                     74:        char *cptr;
                     75:        int d,i,j;
1.2     ! noro       76:        char nbuf0[NBUFSIZ],tbuf0[TBUFSIZ];
        !            77:        char *nbuf, *tbuf;
        !            78:        int nbufsize, tbufsize;
1.1       noro       79:        N n,n1;
                     80:        Q q;
                     81:        Obj r;
                     82:
1.2     ! noro       83:        /* initialize buffer pointers */
        !            84:        nbuf = nbuf0; tbuf = tbuf0;
        !            85:        nbufsize = NBUFSIZ; tbufsize = TBUFSIZ;
        !            86:
1.1       noro       87:        switch ( c = skipspace() ) {
                     88:                case EOF :
                     89:                        asir_terminate(2); break;
                     90:                case '0' :
                     91:                        while ( ( c = Getc() ) == '0' );
                     92:                        if ( c == '.' ) {
                     93:                                Ungetc(c); c = '0';
                     94:                        } else if ( c == 'x' ) {
                     95:                                for ( i = 0; i < 8; i++ )
                     96:                                        nbuf[i] = '0';
1.2     ! noro       97:                                READ_ALNUM_NBUF
        !            98:                                Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
1.1       noro       99:                                hexton(nbuf,&n1);
                    100:                                NTOQ(n1,1,q); r = (Obj)q;
                    101:                                yylvalp->p = (pointer)r;
                    102:                                return ( FORMULA );
                    103:                        } else if ( c == 'b' ) {
                    104:                                for ( i = 0; i < 32; i++ )
                    105:                                        nbuf[i] = '0';
1.2     ! noro      106:                                READ_ALNUM_NBUF
        !           107:                                Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
1.1       noro      108:                                binaryton(nbuf,&n1);
                    109:                                NTOQ(n1,1,q); r = (Obj)q;
                    110:                                yylvalp->p = (pointer)r;
                    111:                                return ( FORMULA );
                    112:                        } else if ( !isdigit(c) ) {
                    113:                                yylvalp->p = 0; Ungetc(c);
                    114:                                return ( FORMULA );
                    115:                        }
                    116:                        break;
                    117:                case '\'' :
                    118:                        for ( i = 0; ; i++ ) {
                    119:                                c = Getc();
                    120:                                if ( c == '\'' )
                    121:                                        break;
                    122:                                if ( c == '\\' )
                    123:                                        c = Getc();
1.2     ! noro      124:                                REALLOC_TBUF tbuf[i] = c;
1.1       noro      125:                        }
1.2     ! noro      126:                        REALLOC_TBUF tbuf[i] = 0;
1.1       noro      127:                        cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
                    128:                        yylvalp->p = (pointer)cptr;
                    129:                        return LCASE; break;
                    130:                case '"' :
                    131:                        i = 0;
                    132:                        do {
                    133:                                c = Getc();
                    134:                                if ( c == '\\' ) {
                    135:                                        c1 = Getc();
                    136:                                        if ( c1 == 'n' )
                    137:                                                c1 = '\n';
1.2     ! noro      138:                                        REALLOC_NBUF nbuf[i++] = c1;
        !           139:                                } else {
        !           140:                                        REALLOC_NBUF nbuf[i++] = c;
        !           141:                                }
1.1       noro      142:                        } while ( c != '"' );
1.2     ! noro      143:                        nbuf[i-1] = 0; /* REALLOC_NBUF is not necessary */
1.1       noro      144:                        cptr = (char *)MALLOC(strlen(nbuf)+1);
                    145:                        strcpy(cptr,nbuf); yylvalp->p = (pointer) cptr;
                    146:                        return ( STR ); break;
                    147:                case '>': case '<': case '=': case '!':
                    148:                        if ( (c1 = Getc()) == '=' )
                    149:                                switch ( c ) {
                    150:                                        case '>': yylvalp->i = (int)C_GE; break;
                    151:                                        case '<': yylvalp->i = (int)C_LE; break;
                    152:                                        case '=': yylvalp->i = (int)C_EQ; break;
                    153:                                        case '!': yylvalp->i = (int)C_NE; break;
                    154:                                        default: break;
                    155:                                }
                    156:                        else if ( (c == '<' && c1 == '<') || (c == '>' && c1 == '>') )
                    157:                                return c;
                    158:                        else {
                    159:                                Ungetc(c1);
                    160:                                switch ( c ) {
                    161:                                        case '>': yylvalp->i = (int)C_GT; break;
                    162:                                        case '<': yylvalp->i = (int)C_LT; break;
                    163:                                        default: return c; break;
                    164:                                }
                    165:                        }
                    166:                        return CMP; break;
                    167:                case '+': case '-': case '*': case '/': case '%': case '^':
                    168:                case '|': case '&':
                    169:                        switch ( c ) {
                    170:                                case '+': yylvalp->p = (pointer)addfs; break;
                    171:                                case '-': yylvalp->p = (pointer)subfs; break;
                    172:                                case '*': yylvalp->p = (pointer)mulfs; break;
                    173:                                case '/': yylvalp->p = (pointer)divfs; break;
                    174:                                case '%': yylvalp->p = (pointer)remfs; break;
                    175:                                case '^': yylvalp->p = (pointer)pwrfs; break;
                    176:                                default: break;
                    177:                        }
                    178:                        if ( (c1 = Getc()) == c )
                    179:                                switch ( c ) {
                    180:                                        case '+': case '-': return SELF; break;
                    181:                                        case '|': return OR; break;
                    182:                                        case '&': return AND; break;
                    183:                                        default: Ungetc(c1); return c; break;
                    184:                                }
                    185:                        else if ( c1 == '=' )
                    186:                                return BOPASS;
                    187:                        else if ( (c == '-') && (c1 == '>') )
                    188:                                return POINT;
                    189:                        else {
                    190:                                Ungetc(c1); return c;
                    191:                        }
                    192:                        break;
                    193:                default :
                    194:                        break;
                    195:        }
                    196:        if ( isdigit(c) ) {
                    197:                for ( i = 0; i < DLENGTH; i++ )
                    198:                        nbuf[i] = '0';
1.2     ! noro      199:                REALLOC_NBUF nbuf[i++] = c;
        !           200:                READ_DIGIT_NBUF
1.1       noro      201:                if ( c == '.' ) {
                    202:                        double dbl;
                    203:                        Real real;
                    204:                        double atof();
                    205:                        extern int bigfloat;
                    206:
1.2     ! noro      207:                        REALLOC_NBUF nbuf[i++] = c;
        !           208:                        READ_DIGIT_NBUF
1.1       noro      209:                        if ( c == 'e' ) {
1.2     ! noro      210:                                REALLOC_NBUF nbuf[i++] = c;
        !           211:                                c = Getc();
        !           212:                                if ( (c == '+') || (c == '-') ) {
        !           213:                                        REALLOC_NBUF nbuf[i++] = c;
        !           214:                                } else
        !           215:                                        Ungetc(c);
        !           216:                                READ_DIGIT_NBUF
1.1       noro      217:                        }
1.2     ! noro      218:                        Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
1.1       noro      219: #if PARI
                    220:                        if ( !bigfloat ) {
                    221:                                dbl = (double)atof(nbuf+DLENGTH);
                    222:                                MKReal(dbl,real); r = (Obj)real;
                    223:                        } else
                    224:                                strtobf(nbuf,(BF *)&r);
                    225: #else
                    226:                        dbl = (double)atof(nbuf+DLENGTH);
                    227:                        MKReal(dbl,real); r = (Obj)real;
                    228: #endif
                    229:                } else {
                    230:                        Ungetc(c);
                    231:                        i -= DLENGTH; d = (i%DLENGTH?i/DLENGTH+1:i/DLENGTH);
                    232:                        n = NALLOC(d); PL(n) = d;
                    233:                        for ( j = 0, ptr = BD(n); j < d; j++ ,i -= DLENGTH )
                    234:                                ptr[j] = myatoi(nbuf+i);
                    235:                        bnton(DBASE,n,&n1);
                    236:                        NTOQ(n1,1,q); r = (Obj)q;
                    237: /*                     optobj(&r); */
                    238:                }
                    239:                yylvalp->p = (pointer)r;
                    240:                return ( FORMULA );
                    241:        } else if ( isalpha(c) ) {
1.2     ! noro      242:                i = 0;
        !           243:                tbuf[i++] = c;
        !           244:                while ( 1 ) {
        !           245:                        c = Getc();
        !           246:                        if ( isalpha(c)||isdigit(c)||(c=='_') ) {
        !           247:                                REALLOC_TBUF tbuf[i++] = c;
        !           248:                        } else
        !           249:                                break;
        !           250:                }
        !           251:                REALLOC_TBUF tbuf[i] = 0; Ungetc(c);
1.1       noro      252:                if ( isupper(tbuf[0]) ) {
                    253:                        cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
                    254:                        yylvalp->p = (pointer)cptr;
                    255:                        return UCASE;
                    256:                } else {
                    257:                        for ( i = 0; kwd[i].name && strcmp(tbuf,kwd[i].name); i++ );
                    258:                        if ( kwd[i].name ) {
                    259:                                yylvalp->i = asir_infile->ln;
                    260:                                return kwd[i].token;
                    261:                        } else {
                    262:                                cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
                    263:                                yylvalp->p = (pointer)cptr;
                    264:                                return LCASE;
                    265:                        }
                    266:                }
                    267:        } else if ( c == '@' ) {
                    268:                if ( isdigit(c = Getc()) ) {
1.2     ! noro      269:                        i = 0;
        !           270:                        nbuf[i++] = c;
        !           271:                        READ_DIGIT_NBUF
        !           272:                        Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
        !           273:                        yylvalp->i = atoi(nbuf);
1.1       noro      274:                        return ANS;
                    275:                } else if ( c == '@' ) {
                    276:                        yylvalp->i = MAX(0,APVS->n-1);
                    277:                        return ANS;
                    278:                } else if ( c == '>' ||  c == '<' ||  c == '=' || c == '!' ) {
                    279:                        if ( (c1 = Getc()) == '=' )
                    280:                                switch ( c ) {
                    281:                                        case '>': yylvalp->i = (int)L_GE; break;
                    282:                                        case '<': yylvalp->i = (int)L_LE; break;
                    283:                                        case '=': yylvalp->i = (int)L_EQ; break;
                    284:                                        case '!': yylvalp->i = (int)L_NE; break;
                    285:                                        default: break;
                    286:                                }
                    287:                        else {
                    288:                                Ungetc(c1);
                    289:                                switch ( c ) {
                    290:                                        case '>': yylvalp->i = (int)L_GT; break;
                    291:                                        case '<': yylvalp->i = (int)L_LT; break;
                    292:                                        case '=': yylvalp->i = (int)L_EQ; break;
                    293:                                        case '!': yylvalp->i = (int)L_NOT; return FOP_NOT; break;
                    294:                                        default: break;
                    295:                                }
                    296:                        }
                    297:                        return LOP;
                    298:                } else if ( c == '|' ||  c == '&' ) {
                    299:                        if ( (c1 = Getc()) != c )
                    300:                                Ungetc(c1);
                    301:                        switch ( c ) {
                    302:                                case '|': yylvalp->i = (int)L_OR;
                    303:                                        return FOP_OR; break;
                    304:                                case '&': yylvalp->i = (int)L_AND;
                    305:                                        return FOP_AND; break;
                    306:                        }
                    307:                } else if ( isalpha(c) ) {
1.2     ! noro      308:                        i = 0;
        !           309:                        tbuf[i++] = '@';
        !           310:                        tbuf[i++] = c;
        !           311:                        while ( 1 ) {
        !           312:                                c = Getc();
        !           313:                                if ( isalpha(c) ) {
        !           314:                                        REALLOC_TBUF tbuf[i++] = c;
        !           315:                                } else
        !           316:                                        break;
        !           317:                        }
        !           318:                        Ungetc(c); REALLOC_TBUF tbuf[i] = 0;
1.1       noro      319:                        if ( !strcmp(tbuf,"@p") )
                    320:                                return GFPNGEN;
                    321:                        else if ( !strcmp(tbuf,"@i") ) {
                    322:                                extern pointer IU;
                    323:
                    324:                                yylvalp->p = IU;
                    325:                                return FORMULA;
                    326:                        } else if ( !strcmp(tbuf,"@true") ) {
                    327:                                yylvalp->p = F_TRUE;
                    328:                                return FORMULA;
                    329:                        } else if ( !strcmp(tbuf,"@false") ) {
                    330:                                yylvalp->p = F_FALSE;
                    331:                                return FORMULA;
                    332:                        } else if ( !strcmp(tbuf,"@impl") ) {
                    333:                                yylvalp->i = (int)L_IMPL;
                    334:                                return FOP_IMPL;
                    335:                        } else if ( !strcmp(tbuf,"@repl") ) {
                    336:                                yylvalp->i = (int)L_REPL;
                    337:                                return FOP_REPL;
                    338:                        } else if ( !strcmp(tbuf,"@equiv") ) {
                    339:                                yylvalp->i = (int)L_EQUIV;
                    340:                                return FOP_EQUIV;
                    341:                        } else {
                    342:                                cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
                    343:                                yylvalp->p = (pointer)cptr;
                    344:                                return LCASE;
                    345:                        }
                    346:                } else {
                    347:                        Ungetc(c);
                    348:                        return GF2NGEN;
                    349:                }
                    350:        } else
                    351:                return ( c );
                    352: }
                    353:
                    354: static int skipspace() {
                    355:        int c,c1;
                    356:
                    357:        for ( c = Getc(); ; )
                    358:                switch ( c ) {
                    359:                        case ' ': case '\t':
                    360:                                c = Getc(); break;
                    361:                        case '\n':
                    362:                                c = afternl();  break;
                    363:                        case '/':
                    364:                                if ( (c1 = Getc()) == '*' )
                    365:                                        c = aftercomment();
                    366:                                else {
                    367:                                        Ungetc(c1); return c;
                    368:                                }
                    369:                                break;
                    370:                        default:
                    371:                                return c; break;
                    372:                }
                    373: }
                    374:
                    375: int afternl() {
                    376:        int c,ac,i,quote;
                    377:        char *ptr;
                    378:        char *av[BUFSIZ];
                    379:        static int ilevel = 0;
                    380:        char buf[BUFSIZ];
                    381:
                    382:        if ( !ilevel )
                    383:                asir_infile->ln++;
                    384:        while ( (c = Getc()) == '#' ) {
                    385:                Gets(buf);
                    386:                for ( quote = 0, ptr = buf; *ptr; ptr++ )
                    387:                        if ( *ptr == '"' )
                    388:                                quote = quote ? 0 : 1;
                    389:                        else if ( quote && (*ptr == ' ') )
                    390:                                *ptr = '_';
                    391:                stoarg(buf,&ac,av);
                    392:                if ( ac == 3 )
                    393:                        if ( (i = atoi(av[2])) == 1 )
                    394:                                ilevel++;
                    395:                        else if ( i == 2 )
                    396:                                ilevel--;
                    397:                if ( !ilevel )
                    398:                        asir_infile->ln = atoi(av[0]);
                    399:        }
                    400:        return c;
                    401: }
                    402:
                    403: int aftercomment() {
                    404:        int c,c1;
                    405:
                    406:        for ( c = Getc(); ; ) {
                    407:                c1 = Getc();
                    408:                if ( (c == '*') && (c1 == '/') )
                    409:                        return Getc();
                    410:                else
                    411:                        c = c1;
                    412:        }
                    413: }
                    414:
                    415: int myatoi(s)
                    416: char *s;
                    417: {
                    418:        int i,r;
                    419:        for ( i = 0, r = 0; i < DLENGTH; i++ ) r = r * 10 + ( s[i] - '0' );
                    420:        return ( r );
                    421: }
                    422:
                    423: extern int ox_do_copy;
                    424:
                    425: void yyerror(s)
                    426: char *s;
                    427: {
                    428:        if ( main_parser )
                    429:                if ( ox_do_copy ) {
                    430:                        /* push errors to DebugStack */
                    431:                } else {
                    432:                        if ( asir_infile->fp == stdin )
                    433:                                fprintf(stderr,"%s\n",s);
                    434:                        else
                    435:                                fprintf(stderr,"\"%s\", near line %d: %s\n",asir_infile->name,asir_infile->ln,s);
                    436:                }
                    437:        else
                    438:                fprintf(stderr,"exprparse : %s\n",s);
                    439: }
                    440:
                    441: int echoback;
                    442:
                    443: extern int read_exec_file, do_fep, do_file;
                    444:
                    445: int readline_getc();
                    446: void readline_ungetc();
                    447: int Egetc();
                    448: void Eungetc();
                    449:
                    450: unsigned char encrypt_char(unsigned char);
                    451: unsigned char decrypt_char(unsigned char);
                    452:
                    453: int Egetc(fp)
                    454: FILE *fp;
                    455: {
                    456:        int c;
                    457:
                    458:        if ( fp ) {
                    459:                c = getc(fp);
                    460:                if ( c == EOF )
                    461:                        return c;
                    462:                if ( asir_infile->encoded )
                    463:                        c = decrypt_char((unsigned char)c);
                    464:                return c;
                    465:        } else {
                    466:                c = *parse_strp++;
                    467:                if ( !c )
                    468:                        return EOF;
                    469:                else
                    470:                        return c;
                    471:        }
                    472: }
                    473:
                    474: void Eungetc(c,fp)
                    475: int c;
                    476: FILE *fp;
                    477: {
                    478:        if ( fp ) {
                    479:                if ( asir_infile->encoded )
                    480:                        c = (int)encrypt_char((unsigned char)c);
                    481:                ungetc(c,fp);
                    482:        } else
                    483:                *--parse_strp = c;
                    484: }
                    485:
                    486: static int Getc() {
                    487:        int c;
                    488:
                    489:        if ( main_parser ) {
                    490:                while ( 1 ) {
                    491:                        if ((c = Egetc(asir_infile->fp)) == EOF)
                    492:                                if ( NEXT(asir_infile) ) {
                    493:                                        closecurrentinput();
                    494:                                        c = Getc();
                    495:                                        break;
                    496:                                } else if ( read_exec_file || do_file )
                    497:                                        asir_terminate(2);
                    498:                                else {
                    499:                                        if ( asir_infile->fp )
                    500:                                                clearerr(asir_infile->fp);
                    501:                                }
                    502:                        else
                    503:                                break;
                    504:                }
                    505:                if ( echoback )
                    506:                        fputc(c,asir_out);
                    507:        } else
                    508:                c = *parse_strp++;
                    509:        return ( c );
                    510: }
                    511:
                    512: static void Ungetc(c) {
                    513:        if ( main_parser ) {
                    514:                Eungetc(c,asir_infile->fp);
                    515:                if ( echoback )
                    516:                        fputc('',asir_out);
                    517:        } else
                    518:                *--parse_strp = c;
                    519: }
                    520:
                    521: static void Gets(s)
                    522: char *s;
                    523: {
                    524:        int c;
                    525:
                    526:        while ( (c = Getc()) != '\n' )
                    527:                *s++ = c;
                    528:        *s = 0;
                    529: }

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