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