Annotation of OpenXM_contrib2/asir2000/parse/lex.c, Revision 1.7
1.4 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.5 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.4 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.7 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/lex.c,v 1.6 2000/11/13 01:48:13 noro Exp $
1.4 noro 49: */
1.1 noro 50: #include <ctype.h>
51: #include "ca.h"
52: #include "al.h"
53: #include "base.h"
54: #include "parse.h"
55: #if !defined(THINK_C)
56: #include <sys/types.h>
57: #include <sys/stat.h>
58: #endif
59: #include "y.tab.h"
60:
61: extern IN asir_infile;
62: extern struct oTKWD kwd[];
63:
64: int afternl();
65: int myatoi();
66: int aftercomment();
67:
68: extern int main_parser;
69: extern char *parse_strp;
70:
71: static int skipspace();
72: static int Getc();
73: static void Ungetc();
74: static void Gets();
75:
1.2 noro 76: #define NBUFSIZ (BUFSIZ*10)
77: #define TBUFSIZ (BUFSIZ)
78:
79: #define REALLOC_NBUF \
80: if ( i >= nbufsize ) {\
81: nbufsize += NBUFSIZ;\
82: if ( nbuf == nbuf0 ) {\
83: nbuf = (char *)MALLOC_ATOMIC(nbufsize);\
84: bcopy(nbuf0,nbuf,nbufsize-NBUFSIZ);\
85: } else\
86: nbuf = REALLOC(nbuf,nbufsize);\
87: }
88:
89: #define REALLOC_TBUF \
90: if ( i >= tbufsize ) {\
91: tbufsize += TBUFSIZ;\
92: if ( tbuf == tbuf0 ) {\
93: tbuf = (char *)MALLOC_ATOMIC(tbufsize);\
94: bcopy(tbuf0,tbuf,tbufsize-TBUFSIZ);\
95: } else\
96: tbuf = REALLOC(tbuf,tbufsize);\
97: }
98:
99: #define READ_ALNUM_NBUF \
100: while ( 1 ) {\
101: c = Getc();\
102: if ( isalnum(c) ) {\
103: REALLOC_NBUF nbuf[i++] = c;\
104: } else\
105: break;\
106: }
107:
108: #define READ_DIGIT_NBUF \
109: while ( 1 ) {\
110: c = Getc();\
111: if ( isdigit(c) ) {\
112: REALLOC_NBUF nbuf[i++] = c;\
113: } else\
114: break;\
115: }
116:
1.1 noro 117: yylex()
118: {
119: #define yylvalp (&yylval)
120: register int c,c1;
121: register int *ptr;
122: char *cptr;
123: int d,i,j;
1.2 noro 124: char nbuf0[NBUFSIZ],tbuf0[TBUFSIZ];
125: char *nbuf, *tbuf;
126: int nbufsize, tbufsize;
1.1 noro 127: N n,n1;
128: Q q;
129: Obj r;
130:
1.2 noro 131: /* initialize buffer pointers */
132: nbuf = nbuf0; tbuf = tbuf0;
133: nbufsize = NBUFSIZ; tbufsize = TBUFSIZ;
134:
1.1 noro 135: switch ( c = skipspace() ) {
136: case EOF :
137: asir_terminate(2); break;
138: case '0' :
139: while ( ( c = Getc() ) == '0' );
140: if ( c == '.' ) {
141: Ungetc(c); c = '0';
142: } else if ( c == 'x' ) {
143: for ( i = 0; i < 8; i++ )
144: nbuf[i] = '0';
1.2 noro 145: READ_ALNUM_NBUF
146: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
1.1 noro 147: hexton(nbuf,&n1);
148: NTOQ(n1,1,q); r = (Obj)q;
149: yylvalp->p = (pointer)r;
150: return ( FORMULA );
151: } else if ( c == 'b' ) {
152: for ( i = 0; i < 32; i++ )
153: nbuf[i] = '0';
1.2 noro 154: READ_ALNUM_NBUF
155: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
1.1 noro 156: binaryton(nbuf,&n1);
157: NTOQ(n1,1,q); r = (Obj)q;
158: yylvalp->p = (pointer)r;
159: return ( FORMULA );
160: } else if ( !isdigit(c) ) {
161: yylvalp->p = 0; Ungetc(c);
162: return ( FORMULA );
163: }
164: break;
165: case '\'' :
166: for ( i = 0; ; i++ ) {
167: c = Getc();
168: if ( c == '\'' )
169: break;
170: if ( c == '\\' )
171: c = Getc();
1.2 noro 172: REALLOC_TBUF tbuf[i] = c;
1.1 noro 173: }
1.2 noro 174: REALLOC_TBUF tbuf[i] = 0;
1.1 noro 175: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
176: yylvalp->p = (pointer)cptr;
177: return LCASE; break;
178: case '"' :
179: i = 0;
180: do {
181: c = Getc();
182: if ( c == '\\' ) {
183: c1 = Getc();
184: if ( c1 == 'n' )
185: c1 = '\n';
1.2 noro 186: REALLOC_NBUF nbuf[i++] = c1;
187: } else {
188: REALLOC_NBUF nbuf[i++] = c;
189: }
1.1 noro 190: } while ( c != '"' );
1.2 noro 191: nbuf[i-1] = 0; /* REALLOC_NBUF is not necessary */
1.1 noro 192: cptr = (char *)MALLOC(strlen(nbuf)+1);
193: strcpy(cptr,nbuf); yylvalp->p = (pointer) cptr;
194: return ( STR ); break;
195: case '>': case '<': case '=': case '!':
196: if ( (c1 = Getc()) == '=' )
197: switch ( c ) {
198: case '>': yylvalp->i = (int)C_GE; break;
199: case '<': yylvalp->i = (int)C_LE; break;
200: case '=': yylvalp->i = (int)C_EQ; break;
201: case '!': yylvalp->i = (int)C_NE; break;
202: default: break;
203: }
204: else if ( (c == '<' && c1 == '<') || (c == '>' && c1 == '>') )
205: return c;
206: else {
207: Ungetc(c1);
208: switch ( c ) {
209: case '>': yylvalp->i = (int)C_GT; break;
210: case '<': yylvalp->i = (int)C_LT; break;
211: default: return c; break;
212: }
213: }
214: return CMP; break;
215: case '+': case '-': case '*': case '/': case '%': case '^':
216: case '|': case '&':
217: switch ( c ) {
218: case '+': yylvalp->p = (pointer)addfs; break;
219: case '-': yylvalp->p = (pointer)subfs; break;
220: case '*': yylvalp->p = (pointer)mulfs; break;
221: case '/': yylvalp->p = (pointer)divfs; break;
222: case '%': yylvalp->p = (pointer)remfs; break;
223: case '^': yylvalp->p = (pointer)pwrfs; break;
224: default: break;
225: }
226: if ( (c1 = Getc()) == c )
227: switch ( c ) {
228: case '+': case '-': return SELF; break;
229: case '|': return OR; break;
230: case '&': return AND; break;
231: default: Ungetc(c1); return c; break;
232: }
233: else if ( c1 == '=' )
234: return BOPASS;
235: else if ( (c == '-') && (c1 == '>') )
236: return POINT;
237: else {
238: Ungetc(c1); return c;
239: }
240: break;
241: default :
242: break;
243: }
244: if ( isdigit(c) ) {
245: for ( i = 0; i < DLENGTH; i++ )
246: nbuf[i] = '0';
1.2 noro 247: REALLOC_NBUF nbuf[i++] = c;
248: READ_DIGIT_NBUF
1.1 noro 249: if ( c == '.' ) {
250: double dbl;
251: Real real;
252: double atof();
253: extern int bigfloat;
254:
1.2 noro 255: REALLOC_NBUF nbuf[i++] = c;
256: READ_DIGIT_NBUF
1.1 noro 257: if ( c == 'e' ) {
1.2 noro 258: REALLOC_NBUF nbuf[i++] = c;
259: c = Getc();
260: if ( (c == '+') || (c == '-') ) {
261: REALLOC_NBUF nbuf[i++] = c;
262: } else
263: Ungetc(c);
264: READ_DIGIT_NBUF
1.1 noro 265: }
1.2 noro 266: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
1.1 noro 267: #if PARI
268: if ( !bigfloat ) {
269: dbl = (double)atof(nbuf+DLENGTH);
270: MKReal(dbl,real); r = (Obj)real;
271: } else
272: strtobf(nbuf,(BF *)&r);
273: #else
274: dbl = (double)atof(nbuf+DLENGTH);
275: MKReal(dbl,real); r = (Obj)real;
276: #endif
277: } else {
278: Ungetc(c);
279: i -= DLENGTH; d = (i%DLENGTH?i/DLENGTH+1:i/DLENGTH);
280: n = NALLOC(d); PL(n) = d;
281: for ( j = 0, ptr = BD(n); j < d; j++ ,i -= DLENGTH )
282: ptr[j] = myatoi(nbuf+i);
283: bnton(DBASE,n,&n1);
284: NTOQ(n1,1,q); r = (Obj)q;
285: /* optobj(&r); */
286: }
287: yylvalp->p = (pointer)r;
288: return ( FORMULA );
289: } else if ( isalpha(c) ) {
1.2 noro 290: i = 0;
291: tbuf[i++] = c;
292: while ( 1 ) {
293: c = Getc();
294: if ( isalpha(c)||isdigit(c)||(c=='_') ) {
295: REALLOC_TBUF tbuf[i++] = c;
296: } else
297: break;
298: }
299: REALLOC_TBUF tbuf[i] = 0; Ungetc(c);
1.1 noro 300: if ( isupper(tbuf[0]) ) {
301: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
302: yylvalp->p = (pointer)cptr;
303: return UCASE;
304: } else {
305: for ( i = 0; kwd[i].name && strcmp(tbuf,kwd[i].name); i++ );
306: if ( kwd[i].name ) {
307: yylvalp->i = asir_infile->ln;
308: return kwd[i].token;
309: } else {
310: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
311: yylvalp->p = (pointer)cptr;
312: return LCASE;
313: }
314: }
315: } else if ( c == '@' ) {
316: if ( isdigit(c = Getc()) ) {
1.2 noro 317: i = 0;
318: nbuf[i++] = c;
319: READ_DIGIT_NBUF
320: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
321: yylvalp->i = atoi(nbuf);
1.1 noro 322: return ANS;
323: } else if ( c == '@' ) {
324: yylvalp->i = MAX(0,APVS->n-1);
325: return ANS;
326: } else if ( c == '>' || c == '<' || c == '=' || c == '!' ) {
327: if ( (c1 = Getc()) == '=' )
328: switch ( c ) {
329: case '>': yylvalp->i = (int)L_GE; break;
330: case '<': yylvalp->i = (int)L_LE; break;
331: case '=': yylvalp->i = (int)L_EQ; break;
332: case '!': yylvalp->i = (int)L_NE; break;
333: default: break;
334: }
335: else {
336: Ungetc(c1);
337: switch ( c ) {
338: case '>': yylvalp->i = (int)L_GT; break;
339: case '<': yylvalp->i = (int)L_LT; break;
340: case '=': yylvalp->i = (int)L_EQ; break;
341: case '!': yylvalp->i = (int)L_NOT; return FOP_NOT; break;
342: default: break;
343: }
344: }
345: return LOP;
346: } else if ( c == '|' || c == '&' ) {
347: if ( (c1 = Getc()) != c )
348: Ungetc(c1);
349: switch ( c ) {
350: case '|': yylvalp->i = (int)L_OR;
351: return FOP_OR; break;
352: case '&': yylvalp->i = (int)L_AND;
353: return FOP_AND; break;
354: }
355: } else if ( isalpha(c) ) {
1.2 noro 356: i = 0;
357: tbuf[i++] = '@';
358: tbuf[i++] = c;
359: while ( 1 ) {
360: c = Getc();
361: if ( isalpha(c) ) {
362: REALLOC_TBUF tbuf[i++] = c;
363: } else
364: break;
365: }
366: Ungetc(c); REALLOC_TBUF tbuf[i] = 0;
1.1 noro 367: if ( !strcmp(tbuf,"@p") )
368: return GFPNGEN;
369: else if ( !strcmp(tbuf,"@i") ) {
370: extern pointer IU;
371:
372: yylvalp->p = IU;
373: return FORMULA;
374: } else if ( !strcmp(tbuf,"@true") ) {
375: yylvalp->p = F_TRUE;
376: return FORMULA;
377: } else if ( !strcmp(tbuf,"@false") ) {
378: yylvalp->p = F_FALSE;
379: return FORMULA;
380: } else if ( !strcmp(tbuf,"@impl") ) {
381: yylvalp->i = (int)L_IMPL;
382: return FOP_IMPL;
383: } else if ( !strcmp(tbuf,"@repl") ) {
384: yylvalp->i = (int)L_REPL;
385: return FOP_REPL;
386: } else if ( !strcmp(tbuf,"@equiv") ) {
387: yylvalp->i = (int)L_EQUIV;
388: return FOP_EQUIV;
389: } else {
390: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
391: yylvalp->p = (pointer)cptr;
392: return LCASE;
393: }
394: } else {
395: Ungetc(c);
396: return GF2NGEN;
397: }
398: } else
399: return ( c );
1.6 noro 400: }
401:
402: void purge_stdin()
403: {
1.7 ! noro 404: #if defined(__FreeBSD__)
1.6 noro 405: fpurge(stdin);
406: #elif defined(linux)
407: stdin->_IO_read_end = stdin->_IO_read_base;
408: stdin->_IO_read_ptr = stdin->_IO_read_base;
409: #elif defined(sparc) || defined(__alpha) || defined(__SVR4) || defined(mips)
410: stdin->_ptr = stdin->_base; stdin->_cnt = 0;
411: #elif defined(VISUAL)
412: w_purge_stdin();
413: #else
414: --->FIXIT
415: #endif
1.1 noro 416: }
417:
418: static int skipspace() {
419: int c,c1;
420:
421: for ( c = Getc(); ; )
422: switch ( c ) {
423: case ' ': case '\t':
424: c = Getc(); break;
425: case '\n':
426: c = afternl(); break;
427: case '/':
428: if ( (c1 = Getc()) == '*' )
429: c = aftercomment();
430: else {
431: Ungetc(c1); return c;
432: }
433: break;
434: default:
435: return c; break;
436: }
437: }
438:
439: int afternl() {
440: int c,ac,i,quote;
441: char *ptr;
442: char *av[BUFSIZ];
443: static int ilevel = 0;
444: char buf[BUFSIZ];
445:
446: if ( !ilevel )
447: asir_infile->ln++;
448: while ( (c = Getc()) == '#' ) {
449: Gets(buf);
450: for ( quote = 0, ptr = buf; *ptr; ptr++ )
451: if ( *ptr == '"' )
452: quote = quote ? 0 : 1;
453: else if ( quote && (*ptr == ' ') )
454: *ptr = '_';
455: stoarg(buf,&ac,av);
456: if ( ac == 3 )
457: if ( (i = atoi(av[2])) == 1 )
458: ilevel++;
459: else if ( i == 2 )
460: ilevel--;
461: if ( !ilevel )
462: asir_infile->ln = atoi(av[0]);
463: }
464: return c;
465: }
466:
467: int aftercomment() {
468: int c,c1;
469:
470: for ( c = Getc(); ; ) {
471: c1 = Getc();
472: if ( (c == '*') && (c1 == '/') )
473: return Getc();
474: else
475: c = c1;
476: }
477: }
478:
479: int myatoi(s)
480: char *s;
481: {
482: int i,r;
483: for ( i = 0, r = 0; i < DLENGTH; i++ ) r = r * 10 + ( s[i] - '0' );
484: return ( r );
485: }
486:
487: extern int ox_do_copy;
488:
489: void yyerror(s)
490: char *s;
491: {
492: if ( main_parser )
493: if ( ox_do_copy ) {
494: /* push errors to DebugStack */
495: } else {
496: if ( asir_infile->fp == stdin )
497: fprintf(stderr,"%s\n",s);
498: else
499: fprintf(stderr,"\"%s\", near line %d: %s\n",asir_infile->name,asir_infile->ln,s);
500: }
501: else
502: fprintf(stderr,"exprparse : %s\n",s);
503: }
504:
505: int echoback;
506:
507: extern int read_exec_file, do_fep, do_file;
508:
509: int readline_getc();
510: void readline_ungetc();
511: int Egetc();
512: void Eungetc();
513:
514: unsigned char encrypt_char(unsigned char);
515: unsigned char decrypt_char(unsigned char);
516:
517: int Egetc(fp)
518: FILE *fp;
519: {
520: int c;
521:
522: if ( fp ) {
523: c = getc(fp);
524: if ( c == EOF )
525: return c;
526: if ( asir_infile->encoded )
527: c = decrypt_char((unsigned char)c);
528: return c;
1.3 noro 529: } else if ( read_exec_file )
530: return EOF;
531: else {
1.1 noro 532: c = *parse_strp++;
533: if ( !c )
534: return EOF;
535: else
536: return c;
537: }
538: }
539:
540: void Eungetc(c,fp)
541: int c;
542: FILE *fp;
543: {
544: if ( fp ) {
545: if ( asir_infile->encoded )
546: c = (int)encrypt_char((unsigned char)c);
547: ungetc(c,fp);
548: } else
549: *--parse_strp = c;
550: }
551:
552: static int Getc() {
553: int c;
554:
555: if ( main_parser ) {
556: while ( 1 ) {
557: if ((c = Egetc(asir_infile->fp)) == EOF)
558: if ( NEXT(asir_infile) ) {
559: closecurrentinput();
560: c = Getc();
561: break;
562: } else if ( read_exec_file || do_file )
563: asir_terminate(2);
564: else {
565: if ( asir_infile->fp )
566: clearerr(asir_infile->fp);
567: }
568: else
569: break;
570: }
571: if ( echoback )
572: fputc(c,asir_out);
573: } else
574: c = *parse_strp++;
575: return ( c );
576: }
577:
578: static void Ungetc(c) {
579: if ( main_parser ) {
580: Eungetc(c,asir_infile->fp);
581: if ( echoback )
582: fputc('',asir_out);
583: } else
584: *--parse_strp = c;
585: }
586:
587: static void Gets(s)
588: char *s;
589: {
590: int c;
591:
592: while ( (c = Getc()) != '\n' )
593: *s++ = c;
594: *s = 0;
595: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>