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