[BACK]Return to parserpass0.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / Kan

Annotation of OpenXM/src/kan96xx/Kan/parserpass0.c, Revision 1.1

1.1     ! maekawa     1: /* parserpass0.c */
        !             2: /*  In this preprocessor, for example, the expression
        !             3:       x^2+y^2-4+x y;
        !             4:     is transformed into
        !             5:       x3^2+x4^2-4+x3*x4;
        !             6: */
        !             7: /*  1992/03/05 */
        !             8: #include <stdio.h>
        !             9: #include "datatype.h"
        !            10: #include "extern2.h"
        !            11:
        !            12: static int isSymbol0();
        !            13: static int isNumber0();
        !            14: typedef enum {INIT,GET,PUT} actionType;
        !            15:
        !            16: struct tokens{
        !            17:   char *token;
        !            18:   int kind;
        !            19: };
        !            20:
        !            21: #define KAZU 1  /* used in kind of tokens */
        !            22: #define ID   2
        !            23:
        !            24: #define BUF0LIMIT 2000
        !            25: #define STRBUFMAX 20000
        !            26: static char *String0;
        !            27: static int Strp0 = 0;
        !            28: static char Buf0org[BUF0LIMIT];
        !            29: static char *Buf0 = Buf0org ;
        !            30: static int Buf0limit = BUF0LIMIT ;
        !            31: static int Exist0 = 0;
        !            32:
        !            33: static struct tokens getoken_pass0();
        !            34: static struct tokens flush();
        !            35:
        !            36:
        !            37: static int get0()
        !            38: /* get a letter from String0 */
        !            39: {
        !            40:   int c;
        !            41:   c = String0[Strp0++];
        !            42:   if (c == '\0') {
        !            43:     Strp0--;return(EOF);
        !            44:   } else return(c);
        !            45: }
        !            46:
        !            47: static put0(c)
        !            48: int c;
        !            49: /* put a letter on Buf0 */
        !            50: {
        !            51:   char *new; int i;
        !            52:   Buf0[Exist0++] = c;
        !            53:   if (Exist0 >= Buf0limit-1)  {
        !            54:     new = (char *) sGC_malloc(sizeof(char *)*Buf0limit*2) ;
        !            55:     if (new == (char *)NULL) {
        !            56:       fprintf(stderr,"No more memory in parserpass0.c\n");
        !            57:       exit(18);
        !            58:     }
        !            59:     fprintf(stderr,"\nSystem Message: Increasing Buf0 to %d in parserpass0.c\n",Buf0limit*2);
        !            60:     for (i=0; i<Buf0limit; i++) {
        !            61:       new[i] = Buf0[i];
        !            62:     }
        !            63:     Buf0 = new; Buf0limit *= 2;
        !            64:   }
        !            65: }
        !            66:
        !            67: static struct tokens flush()
        !            68: {
        !            69:   char *token;
        !            70:   struct tokens r;
        !            71:   if (Exist0<=0) {
        !            72:     fprintf(stderr,"\n flush() is called without data. \n");
        !            73:     r.token = (char *)NULL; r.kind = -1;
        !            74:     return(r);
        !            75:   }
        !            76:   Buf0[Exist0] = '\0';
        !            77:   Exist0 = 0;
        !            78:   token = (char *)sGC_malloc((strlen(Buf0)+1)*sizeof(char));
        !            79:   strcpy(token,Buf0);
        !            80:   r.token = token;
        !            81:   if (isSymbol0(token[0])) r.kind = token[0];
        !            82:   else if (isNumber0(token[0])) r.kind =KAZU;
        !            83:   else r.kind = ID;
        !            84:   return(r);
        !            85: }
        !            86:
        !            87: static isSpace0(c)
        !            88: int c;
        !            89: {
        !            90:   if (c <= ' ') return(1);
        !            91:   else return(0);
        !            92: }
        !            93:
        !            94: static int isSymbol0(c)
        !            95: int c;
        !            96: {
        !            97:   if ((c == '+') ||
        !            98:       (c == '-') ||
        !            99:       (c == '*') ||
        !           100:       (c == '/') ||
        !           101:       (c == '^') ||
        !           102:       (c == ';') ||
        !           103:       (c == '(') ||
        !           104:       (c == ')'))
        !           105:     return(1);
        !           106:   else return(0);
        !           107: }
        !           108:
        !           109: static int isNumber0(c)
        !           110: int c;
        !           111: {
        !           112:   if ((c>='0') && (c<='9')) return(1);
        !           113:   else return(0);
        !           114: }
        !           115:
        !           116: static struct tokens getoken_pass0(kind)
        !           117: actionType kind;
        !           118: {
        !           119:   static int c;
        !           120:   static struct tokens rnull;
        !           121:   if (kind == INIT) {
        !           122:     Strp0 = 0;
        !           123:     Exist0 = 0;
        !           124:     c = get0();
        !           125:     rnull.token = (char *)NULL; rnull.kind = -1;
        !           126:     return(rnull);
        !           127:   }
        !           128:
        !           129:   for (;;) {
        !           130:     if (c == EOF) {
        !           131:       if (Exist0) return(flush());
        !           132:       else return(rnull);
        !           133:     } else if (isSpace0(c)) {
        !           134:       if (Exist0) {
        !           135:        c = get0(); return(flush());
        !           136:       }else {
        !           137:        while (isSpace0(c=get0())) ;
        !           138:       }
        !           139:     } else if (isSymbol0(c)) {
        !           140:       if (Exist0) return(flush());
        !           141:       else {
        !           142:        put0(c);
        !           143:        c = get0();
        !           144:        return(flush());
        !           145:       }
        !           146:     } else if (isNumber0(c)) {
        !           147:       put0(c);
        !           148:       c = get0();
        !           149:       while (isNumber0(c)) {
        !           150:        put0(c);
        !           151:        c = get0();
        !           152:       }
        !           153:       return(flush());
        !           154:     } else { /* identifier */
        !           155:       put0(c);
        !           156:       c =get0();
        !           157:       while ((!isSymbol0(c)) &&
        !           158:             (!isSpace0(c))  &&
        !           159:             (c != EOF)) {
        !           160:        put0(c);
        !           161:        c = get0();
        !           162:       }
        !           163:       return(flush());
        !           164:     }
        !           165:   }
        !           166: }
        !           167:
        !           168: /* This function put the string into the Buf */
        !           169: static char Buftmp[STRBUFMAX];
        !           170: static char *Buf = Buftmp;
        !           171: static int Buflimit = STRBUFMAX;
        !           172:
        !           173: static putstr(str,kind)
        !           174: char str[]; /* string to be outputted */
        !           175: actionType kind; /* kind is INIT or PUT */
        !           176: {
        !           177:   static int ptr;
        !           178:   int i;
        !           179:   int k; char *newbuf;
        !           180:   if (kind == INIT) {
        !           181:     ptr = 0;
        !           182:     return;
        !           183:   }
        !           184:
        !           185:   i=0;
        !           186:   while (str[i]!='\0') {
        !           187:     Buf[ptr++] = str[i++];
        !           188:     Buf[ptr] = '\0';
        !           189:     if (ptr >= Buflimit-2) {
        !           190:       fprintf(stderr,"\nSystem Message: Limit is exceeded in putstr() (parserpass0.c) \n");
        !           191:       fprintf(stderr,"Increasing Buf to %d.\n",2*Buflimit);
        !           192:       newbuf = (char *) sGC_malloc(2*Buflimit);
        !           193:       if (newbuf == (char *)NULL) {
        !           194:        fprintf(stderr,"\nNo memory in putstr() in parserpass0.c\n");
        !           195:        exit(20);
        !           196:       }
        !           197:       for (k=0; k<Buflimit; k++) {
        !           198:        newbuf[k] = Buf[k];
        !           199:       }
        !           200:       Buf = newbuf;
        !           201:       Buflimit *= 2;
        !           202:     }
        !           203:   }
        !           204: }
        !           205:
        !           206:
        !           207:
        !           208: char *str2strPass0(string,ringp)
        !           209: char *string;
        !           210: struct ring *ringp;
        !           211: {
        !           212:
        !           213:   struct tokens ptoken;  /* previous token */
        !           214:   struct tokens ctoken;  /* current token */
        !           215:   struct tokens nulltoken;
        !           216:   char *result;
        !           217:   int i;
        !           218:   char work[100];
        !           219:   int inputTranslation = 1;
        !           220:   int n;
        !           221:
        !           222:   n = ringp->n;
        !           223:
        !           224:   nulltoken.token = (char *)NULL;
        !           225:   nulltoken.kind  = -1;
        !           226:   putstr("",INIT);
        !           227:   String0 = (char *)sGC_malloc((strlen(string)+2)*sizeof(char));
        !           228:   strcpy(String0,string);
        !           229:   getoken_pass0(INIT);
        !           230:
        !           231:   ptoken = nulltoken;
        !           232:   for (;;) {
        !           233:     ctoken = getoken_pass0(GET);
        !           234:
        !           235:     /*   ** ----> ^ */
        !           236:     if (ctoken.kind == '*' && ptoken.kind == '*') {
        !           237:       ptoken.kind = '^';
        !           238:       ptoken.token = (char *)sGC_malloc(sizeof(char)*5);
        !           239:       strcpy(ptoken.token,"^");
        !           240:       ctoken = getoken_pass0(GET);
        !           241:     }
        !           242:
        !           243:     if (ctoken.token == (char *)NULL) {
        !           244:       fprintf(stderr,"\n Unexpected eof in str2strPass0() (parserpass0.c).   ; is expected. \n");
        !           245:       return((char *)NULL);
        !           246:     }
        !           247:     /* output the ptoken.token */
        !           248:     if (inputTranslation && (ptoken.kind == ID)) {
        !           249:       /* Do the input translation for identifier*/
        !           250:       for (i=0; i<n; i++) {
        !           251:        if (strcmp(ptoken.token,ringp->x[i]) == 0) {
        !           252:          sprintf(work,"x%d",i);
        !           253:          putstr(work,PUT);
        !           254:          i = -1;
        !           255:          break; /* break for */
        !           256:        }
        !           257:        if (strcmp(ptoken.token,ringp->D[i]) == 0) {
        !           258:          sprintf(work,"d%d",i);
        !           259:          putstr(work,PUT);
        !           260:          i = -1;
        !           261:          break; /* break for */
        !           262:        }
        !           263:       }
        !           264:       if (strlen(ptoken.token)>0  && (ptoken.token)[0] == '@') {
        !           265:        putstr(ptoken.token,PUT);
        !           266:        i = -1;
        !           267:       }
        !           268:       if (i != -1) { /* very dirty code */
        !           269:        fprintf(stderr,"\n Undefined identifier %s. \n",ptoken.token);
        !           270:        putstr(ptoken.token,PUT);
        !           271:       }
        !           272:       /* end of input translations */
        !           273:     }else {
        !           274:       if (ptoken.token != (char *)NULL) {
        !           275:        putstr(ptoken.token,PUT);
        !           276:       }
        !           277:     }
        !           278:     /* if ctoken.token is ";" then end */
        !           279:     if (strcmp(ctoken.token,";") == 0) {
        !           280:       putstr(ctoken.token,PUT);
        !           281:       if (ptoken.token != (char *)NULL) sGC_free(ptoken.token);
        !           282:       if (ctoken.token != (char *)NULL) sGC_free(ctoken.token);
        !           283:       result = (char *)sGC_malloc((strlen(Buf)+2)*sizeof(char));
        !           284:       strcpy(result,Buf);
        !           285:       return(result);
        !           286:     }
        !           287:
        !           288:     if (((ptoken.kind == ID) || (ptoken.kind == KAZU) || (ptoken.kind == ')'))
        !           289:        &&
        !           290:        ((ctoken.kind == ID) || (ctoken.kind == KAZU) || (ctoken.kind == '(')))
        !           291:       {
        !           292:        putstr(" * ",PUT);
        !           293:       }
        !           294:     if (ptoken.token != (char *)NULL) sGC_free(ptoken.token);
        !           295:     ptoken = ctoken;
        !           296:   }
        !           297: }
        !           298:
        !           299:
        !           300:
        !           301: /*
        !           302: main() {
        !           303:   char str[200];
        !           304:   struct ring r;
        !           305:   static char *x[]={"x","y","z"};
        !           306:   static char *D[]={"Dx","Dy","Dz"};
        !           307:   r.n = 3;
        !           308:   r.x = x; r.D = D;
        !           309:   gets(str);
        !           310:   printf("%s\n",str2strPass0(str,&r));
        !           311: }
        !           312: */

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