1: #include <stdio.h>
2:
3: int tabflg;
4: int labno = 1;
5: int opno;
6: FILE *curbuf;
7: FILE *obuf;
8: FILE *oobuf;
9: char oname[]="/tmp/cvoptaXXXXXX";
10: char ooname[]="/tmp/cvoptbXXXXXX";
11: char lbuf[BUFSIZ];
12: char *lbufp = lbuf;
13:
14: main(argc, argv)
15: char **argv;
16: {
17: /*
18: A1 -> A
19: A2 B
20: A O
21: B1 C
22: B2 D
23: BE L
24: BF P
25: C1 E
26: C2 F
27: F G
28: H H
29: R I
30: R1 J
31: S K
32: I M
33: M N
34:
35: * +1
36: S +2
37: C +4
38: 1 +8
39:
40: z -> 4
41: c 10
42: a 14
43: e 20
44: n 63
45: * +0100
46: */
47:
48: int c, snlflg, nlflg, t, smode, m, ssmode, peekc, side;
49:
50: smode = nlflg = snlflg = ssmode = 0;
51: if (argc>1)
52: if (freopen(argv[1], "r", stdin) == NULL) {
53: fprintf(stderr, "%s?\n", argv[1]);
54: return(1);
55: }
56: if (argc>2)
57: if (freopen(argv[2], "w", stdout) == NULL) {
58: fprintf(stderr, "%s?\n", argv[2]);
59: return(1);
60: }
61: mktemp(oname);
62: if ((obuf = fopen(oname, "w")) == NULL) {
63: fprintf(stderr, "%s?\n", oname);
64: exit(1);
65: }
66: mktemp(ooname);
67: if ((oobuf = fopen(ooname, "w")) == NULL) {
68: fprintf(stderr, "%s?\n", ooname);
69: exit(1);
70: }
71: printf("#include \"c1.h\"");
72: curbuf = obuf;
73: loop:
74: c = getchar();
75: if (c!='\n' && c!='\t')
76: nlflg = 0;
77: if (ssmode!=0 && c!='%') {
78: ssmode = 0;
79: curbuf = stdout;
80: fprintf(curbuf, "\nstatic char L%d[]=\"", labno++);
81: }
82: switch(c) {
83:
84: case EOF:
85: fprintf(obuf, "\t{0},\n};\n");
86: fclose(obuf);
87: if (freopen(oname, "r", stdin) == NULL) {
88: fprintf(stderr, "%s?\n",oname);
89: exit(1);
90: }
91: while ((c = getchar()) != EOF)
92: putchar(c);
93: unlink(oname);
94: fclose(oobuf);
95: if (freopen(ooname, "r", stdin) == NULL) {
96: fprintf(stderr, "%s?\n",ooname);
97: exit(1);
98: }
99: while ((c = getchar()) != EOF)
100: putchar(c);
101: unlink(ooname);
102: return(0);
103:
104: case 'A':
105: if ((c=getchar())=='1' || c=='2') {
106: put(c+'A'-'1');
107: goto loop;
108: }
109: put('O');
110: ungetc(c, stdin);
111: goto loop;
112:
113: case 'B':
114: switch (getchar()) {
115:
116: case '1':
117: put('C');
118: goto loop;
119:
120: case '2':
121: put('D');
122: goto loop;
123:
124: case 'E':
125: put('L');
126: goto loop;
127:
128: case 'F':
129: put('P');
130: goto loop;
131: }
132: put('?');
133: goto loop;
134:
135: case 'C':
136: put(getchar()+'E'-'1');
137: goto loop;
138:
139: case 'F':
140: put('G');
141: goto subtre;
142:
143: case 'R':
144: if ((c=getchar()) == '1')
145: put('J'); else {
146: put('I');
147: ungetc(c, stdin);
148: }
149: goto loop;
150:
151: case 'H':
152: put('H');
153: goto subtre;
154:
155: case 'I':
156: put('M');
157: goto loop;
158:
159: case 'S':
160: put('K');
161: subtre:
162: snlflg = 1;
163: t = 'A';
164: l1:
165: switch (c=getchar()) {
166:
167: case '*':
168: t++;
169: goto l1;
170:
171: case 'S':
172: t += 2;
173: goto l1;
174:
175: case 'C':
176: t += 4;
177: goto l1;
178:
179: case '1':
180: t += 8;
181: goto l1;
182:
183: case '2':
184: t += 16;
185: goto l1;
186: }
187: ungetc(c, stdin);
188: put(t);
189: goto loop;
190:
191: case '#':
192: if(getchar()=='1')
193: put('#'); else
194: put('"');
195: goto loop;
196:
197: case '%':
198: if (smode)
199: curbuf = obuf;
200: if (ssmode==0) {
201: if ((peekc=getchar())=='[') {
202: printf("\n#define ");
203: while((c=getchar())!=']' && c!=':')
204: putchar(c);
205: printf(" L%d\n",labno);
206: if (c==':') getchar();
207: getchar();
208: curbuf = obuf;
209: goto loop;
210: }
211: ungetc(peekc, stdin);
212: }
213: side=0;
214: loop1:
215: switch (c=getchar()) {
216:
217: case ' ':
218: case '\t':
219: goto loop1;
220: case 'a':
221: m = 16;
222: t = flag();
223: goto pf;
224:
225: case ',':
226: side=1;
227: goto loop1;
228:
229: case 'i':
230: m = 12;
231: t = flag();
232: goto pf;
233: case 'z':
234: m = 4;
235: t = flag();
236: goto pf;
237:
238: case 'r':
239: m = 9;
240: t = flag();
241: goto pf;
242:
243: case '1':
244: m = 5;
245: t = flag();
246: goto pf;
247:
248: case 'c':
249: t = 0;
250: m = 8;
251: goto pf;
252:
253: case 'e':
254: t = flag();
255: m = 20;
256: goto pf;
257:
258: case 'n':
259: t = flag();
260: m = 63;
261: pf:
262: if ((c=getchar())=='*')
263: m += 0100; else
264: ungetc(c, stdin);
265: if (side==0) {
266: if (opno==0) fprintf(curbuf,"\nstruct optab optab[]={\n");
267: fprintf(curbuf,"\t{");
268: }
269: fprintf(curbuf, "%d,%d,", m, t);
270: goto loop1;
271: case '[':
272: printf("\n#define L%d ", labno++);
273: while ((c=getchar())!=']')
274: putchar(c);
275: printf("\n");
276: ssmode = 0;
277: smode = 0;
278: goto loop;
279:
280: case '{':
281: for(;;) {
282: while ((c=getchar())!='%') putc(c,oobuf);
283: if ((c=getchar())=='}') goto loop;
284: else {putc('%',oobuf); putc(c,oobuf);}
285: }
286:
287: case '\n':
288: fprintf(curbuf, "L%d}, /* %d */\n", labno,opno);
289: ++opno;
290: ssmode = 1;
291: nlflg = 1;
292: smode = 1;
293: goto loop;
294:
295: case '/':
296: comment(c); goto loop1;
297:
298: }
299: put(c);
300: goto loop1;
301:
302: case '\t':
303: if (nlflg) {
304: nlflg = 0;
305: goto loop;
306: }
307: if (smode) {
308: tabflg++;
309: goto loop;
310: }
311: put('\t');
312: goto loop;
313:
314: case '\n':
315: lbufp=lbuf;
316: if (!smode) {
317: put('\n');
318: goto loop;
319: }
320: if (nlflg) {
321: nlflg = 0;
322: fprintf(curbuf, "\";");
323: curbuf = obuf;
324: smode = 0;
325: goto loop;
326: }
327: if (!snlflg)
328: fprintf(curbuf, "\\n");
329: snlflg = 0;
330: nlflg = 1;
331: goto loop;
332:
333: case '/':
334: comment(c); goto loop;
335:
336: case 'X':
337: case 'Y':
338: case 'T':
339: snlflg++;
340: break;
341:
342: case ':':
343: fseek(curbuf,(long)(lbuf-lbufp),2);
344: *lbufp='\0';
345: if (opno!=0) {fprintf(curbuf,"\t{0},\n"); ++opno;}
346: printf("\n#define %s &optab[%d]\n",lbuf,opno);
347: fprintf(curbuf,"/* %s */",lbuf);
348: lbufp=lbuf;
349: goto loop;
350:
351: }
352: *lbufp++=c;
353: put(c);
354: goto loop;
355: }
356:
357: flag() {
358: register c, f;
359:
360: f = 0;
361: l1:
362: switch(c=getchar()) {
363:
364: case 'w':
365: f = 1;
366: goto l1;
367:
368: case 'i':
369: f = 2;
370: goto l1;
371:
372: case 'b':
373: if (f==9) /* unsigned word/int seen yet? */
374: f = 10; /* yes - it is unsigned byte */
375: else
376: f = 3; /* no - it is regular (signed) byte */
377: goto l1;
378:
379: case 'f':
380: f = 4;
381: goto l1;
382:
383: case 'd':
384: f = 5;
385: goto l1;
386:
387: case 'u':
388: if (f==3) /* regular (signed) byte seen ? */
389: f = 10; /* yes - unsigned byte now */
390: else if (f == 8) /* regular (signed) long seen? */
391: f = 11; /* yes - it is unsigned long now */
392: else
393: f = 9; /* otherwise we have unsigned word */
394: goto l1;
395:
396: case 's':
397: f = 6;
398: goto l1;
399:
400: case 'l':
401: if (f == 9) /* seen unsigned yet? */
402: f = 11; /* yes - it is unsigned long now */
403: else
404: f = 8; /* no - it is unsigned word now */
405: goto l1;
406:
407: case 'p':
408: f += 16;
409: goto l1;
410: }
411: ungetc(c, stdin);
412: return(f);
413: }
414:
415: put(c)
416: {
417: if (tabflg) {
418: tabflg = 0;
419: fprintf(curbuf, "\\%o", c+0200);
420: } else {
421: if (c=='"') putc('\\',curbuf);
422: putc(c, curbuf);
423: }
424: }
425:
426: (c)
427: register char c;
428: {
429: putc(c,curbuf);
430: if ((c=getchar())=='*') for (;;) {
431: do putc(c,curbuf); while ((c=getchar())!='*');
432: putc(c,curbuf);
433: if ((c=getchar())=='/') {putc(c,curbuf); break;}
434: } else ungetc(c,stdin);
435: }