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

Annotation of OpenXM/src/kan96xx/Kan/yylex_polymake.c, Revision 1.3

1.3     ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/Kan/yylex_polymake.c,v 1.2 2003/11/20 03:25:08 takayama Exp $ */
1.1       takayama    2: /* parser for polymake output */
                      3: /* This program requires
                      4:
                      5:
                      6: */
                      7:
                      8: #include <stdio.h>
                      9: #include "yylex_polymake.h"
                     10: #include "yy_polymake.tab.h"
                     11:
1.3     ! takayama   12: #define mymalloc(n) sGC_malloc(n)
        !            13: /*
1.1       takayama   14: #define mymalloc(n) malloc(n)
1.3     ! takayama   15: */
1.1       takayama   16: /* pm = PolyMake */
                     17: #define PM_emptyLineCode 1
                     18:
                     19:
                     20: static char *S=NULL;
                     21: /* It is assumed that the newline code is only \n (preprocessed).
                     22:    Empty line is marked with 0x1. More than one empty line does not appear. */
                     23: static int Pt=-1;
                     24: static int PMdebug = 0;
                     25:
                     26: /* char *PMlval; */
                     27:
1.3     ! takayama   28: /* The function putstr() uses static variables inside,
        !            29:   so if it is under use, it must not be initialized.
        !            30:   The function putstr2() is for the second use and is identical
        !            31:   function with putstr().
        !            32: */
1.1       takayama   33: static char *putstr(int c);
1.3     ! takayama   34: static char *putstr2(int c);
        !            35:
        !            36: char *pmPutstr(int c) {
        !            37:   return putstr(c);
        !            38: }
        !            39:
        !            40: pmSetS(char *s) {
        !            41:   S = s;
        !            42:   return 0;
        !            43: }
1.1       takayama   44:
                     45: int PMlex() {
                     46:   int type;
                     47:   type = PMlex_aux();
                     48:
                     49:   if (PMdebug) {
                     50:        printf("type=%d ",type);
                     51:        if ((type == PM_number) || (type == PM_keyword)) {
                     52:          printf("value="); pmPrintObject(stdout,PMlval);
                     53:        }
                     54:        printf("\n");
                     55:   }
                     56:
                     57:   return type;
                     58: }
                     59: int PMlex_aux() {
                     60:   if (Pt < 0) {
                     61:        return PM_noToken;
                     62:   }
                     63:   if (S[Pt] == 0) { Pt=-1; return PM_noToken; }
                     64:   while ((S[Pt]<=' ') && (S[Pt]!='\n') && (S[Pt] != PM_emptyLineCode)) Pt++;
                     65:   if (S[Pt] == '\n') { Pt++; return PM_newline; }
                     66:   if (S[Pt] == PM_emptyLineCode) {Pt++; return PM_emptyLine; }
                     67:   if (S[Pt] == '{') { Pt++; return PM_LCurryBrace; }
                     68:   if (S[Pt] == '}') { Pt++; return PM_RCurryBrace; }
                     69:   if (((S[Pt] >= '0') && (S[Pt] <= '9')) || (S[Pt] == '-')) {
                     70:        putstr(-1); putstr(S[Pt++]);
                     71:        while (((S[Pt]>='0') && (S[Pt]<='9')) || (S[Pt] == '/')) putstr(S[Pt++]);
                     72:        PMlval = pmNewStrObject(putstr(0));
                     73:        return PM_number;
                     74:   }
                     75:   if (((S[Pt] >= 'A') && (S[Pt] <= 'Z')) ||
                     76:       ((S[Pt] >= 'a') && (S[Pt] <= 'z')) ||
                     77:       (S[Pt] == '_')) {
                     78:        putstr(-1); putstr(S[Pt++]);
                     79:        while (((S[Pt] >= 'A') && (S[Pt] <= 'Z')) ||
                     80:       ((S[Pt] >= 'a') && (S[Pt] <= 'z')) ||
                     81:       (S[Pt] == '_')) putstr(S[Pt++]);
                     82:        PMlval = pmNewStrObject(putstr(0));
                     83:        return PM_keyword;
                     84:   }
                     85:   Pt++;  return PM_unknown;
                     86: }
                     87:
                     88: #define PUTSTR_INIT 10
                     89: static char *putstr(int c) {
                     90:   static char *s=NULL;
                     91:   static int pt=0;
                     92:   static int limit=0;
                     93:   int i;
                     94:   char *old;
                     95:   if (c < 0) {
                     96:        s = (char *)mymalloc(PUTSTR_INIT);
                     97:        if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(10);}
                     98:        limit = PUTSTR_INIT;
                     99:        pt = 0; s[pt] = 0;
                    100:        return s;
                    101:   }
                    102:   if (s == NULL) putstr(-1);
                    103:   if (pt < limit-1) {
                    104:        s[pt++]=c; s[pt]=0;
                    105:        return s;
                    106:   }else{
                    107:        old = s;
                    108:        limit = 2*limit;
                    109:        s = (char *)mymalloc(limit);
                    110:        if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(10);}
                    111:        for (i=0; i<=pt; i++) {
                    112:          s[i] = old[i];
                    113:        }
                    114:        return putstr(c);
                    115:   }
                    116: }
1.3     ! takayama  117: static char *putstr2(int c) {
        !           118:   static char *s=NULL;
        !           119:   static int pt=0;
        !           120:   static int limit=0;
        !           121:   int i;
        !           122:   char *old;
        !           123:   if (c < 0) {
        !           124:        s = (char *)mymalloc(PUTSTR_INIT);
        !           125:        if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(10);}
        !           126:        limit = PUTSTR_INIT;
        !           127:        pt = 0; s[pt] = 0;
        !           128:        return s;
        !           129:   }
        !           130:   if (s == NULL) putstr(-1);
        !           131:   if (pt < limit-1) {
        !           132:        s[pt++]=c; s[pt]=0;
        !           133:        return s;
        !           134:   }else{
        !           135:        old = s;
        !           136:        limit = 2*limit;
        !           137:        s = (char *)mymalloc(limit);
        !           138:        if (s == NULL) {fprintf(stderr,"No more memory.\n"); exit(10);}
        !           139:        for (i=0; i<=pt; i++) {
        !           140:          s[i] = old[i];
        !           141:        }
        !           142:        return putstr(c);
        !           143:   }
        !           144: }
1.1       takayama  145:
                    146: pmPreprocess() {
                    147:   int newp,oldp;
                    148:   int state;
                    149:   int i,j;
                    150:   if (S == NULL) return -1;
                    151:   Pt = 0;
                    152:   for (oldp = newp = 0; S[oldp] != 0; oldp++) {
                    153:        if ((S[oldp] == 0xd) && (S[oldp] == 0xa)) { /* Windows 0d 0a */
                    154:          S[newp++] = '\n'; oldp++;
                    155:        }else{
                    156:          S[newp++] = S[oldp];
                    157:        }
                    158:   }
                    159:   S[newp] = '\0';
                    160:
                    161:   for (oldp = newp = 0; S[oldp] != 0; oldp++) {
                    162:        if (S[oldp] == 0xd) { /* Replace for mac 0xd to 0xa */
                    163:          S[newp++] = '\n';
                    164:        }else{
                    165:          S[newp++] = S[oldp];
                    166:        }
                    167:   }
                    168:   S[newp] = '\0';
                    169:
                    170:   /* Remove #, empty lines, ... */
                    171:   state = 1; newp=0;
                    172:   for (oldp=0; S[oldp] != 0; oldp++) {
                    173:        /* printf("oldp=%d, state=%d, char=%c\n",oldp,state,S[oldp]); */
                    174:        switch(state) {
                    175:        case 0:
                    176:          if (S[oldp] == '\n') {state=1; newp=oldp+1;}
                    177:          break;
                    178:        case 1:
                    179:          if ((S[oldp] == ' ') || (S[oldp] == '\t')) break;
                    180:          if ((S[oldp] == '#') || ((S[oldp]=='_') && (S[oldp-1]<' '))) {
                    181:                /* skip the rest of the line, state=1; */
                    182:                for (; S[oldp] != 0 ; oldp++) {
                    183:                  if (S[oldp] == '\n') {oldp--; break;}
                    184:                }
                    185:                if (S[oldp] == 0) oldp--;
                    186:          }else if (S[oldp] == '\n') {
                    187:                /* replace the empty line by PM_emptyLine */
                    188:                S[newp]= PM_emptyLineCode; j=oldp+1;
                    189:                for (i=newp+1; S[j] != 0; i++) {
                    190:                  S[i] = S[j]; j++;
                    191:                }
                    192:                oldp=newp;
                    193:                S[i] = 0;
                    194:          }else{
                    195:                state = 0;
                    196:          }
                    197:          break;
                    198:        default:
                    199:          break;
                    200:        }
                    201:   }
                    202: }
                    203:
                    204: pmObjectp pmNewStrObject(char *s) {
                    205:   pmObjectp ob;
                    206:   ob = (pmObjectp) mymalloc(sizeof(struct pmObject));
                    207:   if (ob == NULL) {
                    208:        fprintf(stderr,"No more memory.\n");
                    209:        exit(10);
                    210:   }
                    211:   ob->tag = PMobject_str;
                    212:   ob->body = s;
                    213:   return ob;
                    214: }
                    215: pmObjectp pmNewListObject(pmObjectp a) {
                    216:   pmObjectp ob;
                    217:   struct pmList *aa;
                    218:   ob = (pmObjectp) mymalloc(sizeof(struct pmObject));
                    219:   if (ob == NULL) {
                    220:        fprintf(stderr,"No more memory.\n");
                    221:        exit(10);
                    222:   }
                    223:   aa= (struct pmList *)mymalloc(sizeof(struct pmList));
                    224:   if (aa == NULL) {
                    225:        fprintf(stderr,"No more memory.\n");
                    226:        exit(10);
                    227:   }
                    228:   aa->left=a;
                    229:   aa->right=NULL;
                    230:   ob->tag = PMobject_list;
                    231:   ob->body = aa;
                    232:   return ob;
                    233: }
                    234: pmObjectp pmCons(pmObjectp a,struct pmList *b) {
                    235:   pmObjectp ob;
                    236:   struct pmList *t;
                    237:   ob = pmNewListObject(a);
                    238:   t = ob->body;
                    239:   t->right = b;
                    240:   return ob;
                    241: }
1.2       takayama  242:
                    243: int pmListLength(struct pmList *list) {
                    244:   struct pmList *t;
                    245:   int n;
                    246:   if (list == NULL) return 0;
                    247:   n = 0;
                    248:   t = list;
                    249:   while (t != NULL) {
                    250:        n++; t = t->right;
                    251:   }
                    252:   return n;
                    253: }
                    254:
                    255: pmObjectp pmNewTreeObjecto(pmObjectp s)
                    256: {
                    257:   if ((s == NULL) || (s->tag != PMobject_str)) {
                    258:        warning_yylex_polymake("Invalid argument for pmNewObjecto\n");
                    259:        return pmNewTreeObject("?");
                    260:   }
                    261:   return pmNewTreeObject((char *)(s->body));
                    262: }
                    263: pmObjectp pmNewTreeObject(char *s) {
                    264:   pmObjectp ob;
                    265:   struct pmTree *aa;
                    266:   ob = (pmObjectp) mymalloc(sizeof(struct pmObject));
                    267:   if (ob == NULL) {
                    268:        fprintf(stderr,"No more memory.\n");
                    269:        exit(10);
                    270:   }
                    271:   aa= (struct pmTree *)mymalloc(sizeof(struct pmTree));
                    272:   if (aa == NULL) {
                    273:        fprintf(stderr,"No more memory.\n");
                    274:        exit(10);
                    275:   }
                    276:   aa->nodeName = s;
                    277:   aa->attrList = NULL;
                    278:   aa->childs = NULL;
                    279:   ob->tag = PMobject_tree;
                    280:   ob->body = aa;
                    281:   return ob;
                    282: }
                    283:
                    284: pmObjectp pmAddAttr(pmObjectp c,pmObjectp a) {
                    285:   struct pmTree *tree;
                    286:   if (a->tag != PMobject_tree) {
                    287:        warning_yylex_polymake("pmAddAttr: the argument is not tree object.\n");
                    288:        return a;
                    289:   }
                    290:   tree = a->body;
                    291:   if (tree->attrList == NULL) {
                    292:        tree->attrList = pmNewListObject(c);
                    293:   }else{
                    294:        if (tree->attrList->tag == PMobject_list) {
                    295:          tree->attrList = pmCons(c,(struct pmList *)(tree->attrList->body));
                    296:        }else{
                    297:          warning_yylex_polymake("pmAddAttr: the attrbute list is broken.\n");
                    298:     }
                    299:   }
                    300:   return a;
                    301: }
                    302:
                    303: /* Add c to the tree a */
                    304: pmObjectp pmAddChild(pmObjectp c,pmObjectp a) {
                    305:   struct pmTree *tree;
                    306:   if (a->tag != PMobject_tree) {
                    307:        warning_yylex_polymake("pmAddAttr: the argument is not tree object.\n");
                    308:        return a;
                    309:   }
                    310:   tree = a->body;
                    311:   if (tree->childs == NULL) {
                    312:        tree->childs = pmNewListObject(c);
                    313:   }else{
                    314:        if (tree->childs->tag == PMobject_list) {
                    315:          tree->childs = pmCons(c,(struct pmList *)(tree->childs->body));
                    316:        }else{
                    317:          warning_yylex_polymake("pmAddAttr: the child list is broken.\n");
                    318:     }
                    319:   }
                    320:   return a;
                    321: }
                    322:
                    323: warning_yylex_polymake(char *s) {
                    324:   fprintf(stderr,"Warning: %s",s);
                    325: }
1.1       takayama  326: void pmPrintObject(FILE *fp,pmObjectp p) {
                    327:   int n,i;
                    328:   struct pmList *list;
                    329:   struct pmList *t;
1.2       takayama  330:   struct pmTree *tree;
1.1       takayama  331:   if (p == NULL) {
                    332:        /* fprintf(stderr,"NULL "); */
                    333:        return;
                    334:   }
                    335:   /* fprintf(stderr,"tag=%d ",p->tag); */
                    336:   switch (p->tag) {
                    337:   case PMobject_str:
                    338:        fprintf(fp,"%s",(char *)(p->body));
                    339:        break;
                    340:   case PMobject_list:
                    341:        list = (struct pmList *)(p->body);
                    342:        if (list == NULL) break;
1.2       takayama  343:        n = pmListLength(list);
1.1       takayama  344:        t = list;
                    345:        fprintf(fp,"[");
                    346:        for (i=0; i<n; i++) {
                    347:          pmPrintObject(fp,t->left);
                    348:          if (i != n-1) fprintf(fp,",");
                    349:          if (t == NULL) break; else t = t->right;
                    350:        }
                    351:        fprintf(fp,"]");
1.2       takayama  352:        break;
                    353:   case PMobject_tree:
                    354:        tree = p->body;
                    355:        fprintf(fp,"polymake.%s(",tree->nodeName);
                    356:        /* Ignore attribute list */
                    357:        if (tree->childs == NULL) {n = 0; t = NULL; }
                    358:        else {
                    359:          t = tree->childs->body;
                    360:          n = pmListLength(t);
                    361:        }
                    362:        for (i=0; i<n; i++) {
                    363:          pmPrintObject(fp,t->left);
                    364:          t = t->right;
                    365:          if (i != n-1) fprintf(fp,",");
                    366:        }
                    367:        fprintf(fp,")");
1.1       takayama  368:        break;
                    369:   default:
                    370:        fprintf(stderr,"Unknown object tag %d.\n",p->tag);
                    371:        /* sleep(100);  to call debugger. */
                    372:        break;
                    373:   }
                    374: }
                    375:
                    376: PMerror() {
                    377: }

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