Annotation of OpenXM/src/kan96xx/Kan/parserpass0.c, Revision 1.4
1.4 ! ohara 1: /* $OpenXM: OpenXM/src/kan96xx/Kan/parserpass0.c,v 1.3 2001/05/04 01:06: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:
50: static 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:
90: static 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:
176: static 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;
185: return;
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: }
207: }
208:
209:
210:
211: char *str2strPass0(string,ringp)
1.3 takayama 212: char *string;
213: struct ring *ringp;
1.1 maekawa 214: {
215:
216: struct tokens ptoken; /* previous token */
217: struct tokens ctoken; /* current token */
218: struct tokens nulltoken;
219: char *result;
220: int i;
221: char work[100];
222: int inputTranslation = 1;
223: int n;
224:
225: n = ringp->n;
226:
227: nulltoken.token = (char *)NULL;
228: nulltoken.kind = -1;
229: putstr("",INIT);
230: String0 = (char *)sGC_malloc((strlen(string)+2)*sizeof(char));
231: strcpy(String0,string);
232: getoken_pass0(INIT);
233:
234: ptoken = nulltoken;
235: for (;;) {
236: ctoken = getoken_pass0(GET);
237:
238: /* ** ----> ^ */
239: if (ctoken.kind == '*' && ptoken.kind == '*') {
240: ptoken.kind = '^';
241: ptoken.token = (char *)sGC_malloc(sizeof(char)*5);
242: strcpy(ptoken.token,"^");
243: ctoken = getoken_pass0(GET);
244: }
245:
246: if (ctoken.token == (char *)NULL) {
247: fprintf(stderr,"\n Unexpected eof in str2strPass0() (parserpass0.c). ; is expected. \n");
248: return((char *)NULL);
249: }
250: /* output the ptoken.token */
251: if (inputTranslation && (ptoken.kind == ID)) {
252: /* Do the input translation for identifier*/
253: for (i=0; i<n; i++) {
1.3 takayama 254: if (strcmp(ptoken.token,ringp->x[i]) == 0) {
255: sprintf(work,"x%d",i);
256: putstr(work,PUT);
257: i = -1;
258: break; /* break for */
259: }
260: if (strcmp(ptoken.token,ringp->D[i]) == 0) {
261: sprintf(work,"d%d",i);
262: putstr(work,PUT);
263: i = -1;
264: break; /* break for */
265: }
1.1 maekawa 266: }
267: if (strlen(ptoken.token)>0 && (ptoken.token)[0] == '@') {
1.3 takayama 268: putstr(ptoken.token,PUT);
269: i = -1;
1.1 maekawa 270: }
271: if (i != -1) { /* very dirty code */
1.3 takayama 272: fprintf(stderr,"\n Undefined identifier %s. \n",ptoken.token);
273: putstr(ptoken.token,PUT);
1.1 maekawa 274: }
275: /* end of input translations */
276: }else {
277: if (ptoken.token != (char *)NULL) {
1.3 takayama 278: putstr(ptoken.token,PUT);
1.1 maekawa 279: }
280: }
281: /* if ctoken.token is ";" then end */
282: if (strcmp(ctoken.token,";") == 0) {
283: putstr(ctoken.token,PUT);
284: if (ptoken.token != (char *)NULL) sGC_free(ptoken.token);
285: if (ctoken.token != (char *)NULL) sGC_free(ctoken.token);
286: result = (char *)sGC_malloc((strlen(Buf)+2)*sizeof(char));
287: strcpy(result,Buf);
288: return(result);
289: }
290:
291: if (((ptoken.kind == ID) || (ptoken.kind == KAZU) || (ptoken.kind == ')'))
1.3 takayama 292: &&
293: ((ctoken.kind == ID) || (ctoken.kind == KAZU) || (ctoken.kind == '(')))
1.1 maekawa 294: {
1.3 takayama 295: putstr(" * ",PUT);
1.1 maekawa 296: }
297: if (ptoken.token != (char *)NULL) sGC_free(ptoken.token);
298: ptoken = ctoken;
299: }
300: }
301:
302:
303:
304: /*
1.3 takayama 305: main() {
306: char str[200];
307: struct ring r;
308: static char *x[]={"x","y","z"};
309: static char *D[]={"Dx","Dy","Dz"};
310: r.n = 3;
311: r.x = x; r.D = D;
312: gets(str);
313: printf("%s\n",str2strPass0(str,&r));
314: }
1.1 maekawa 315: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>