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