Annotation of OpenXM_contrib2/asir2000/parse/lex.c, Revision 1.10
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.10 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/lex.c,v 1.9 2000/12/05 01:51:35 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: #include <sys/types.h>
56: #include <sys/stat.h>
1.10 ! noro 57: #if defined(VISUAL)
! 58: #include "ytab.h"
! 59: #else
1.1 noro 60: #include "y.tab.h"
1.10 ! noro 61: #endif
1.1 noro 62:
63: extern IN asir_infile;
64: extern struct oTKWD kwd[];
65:
66: int afternl();
67: int myatoi();
68: int aftercomment();
69:
70: extern int main_parser;
71: extern char *parse_strp;
72:
73: static int skipspace();
74: static int Getc();
75: static void Ungetc();
76: static void Gets();
77:
1.2 noro 78: #define NBUFSIZ (BUFSIZ*10)
79: #define TBUFSIZ (BUFSIZ)
80:
81: #define REALLOC_NBUF \
82: if ( i >= nbufsize ) {\
83: nbufsize += NBUFSIZ;\
84: if ( nbuf == nbuf0 ) {\
85: nbuf = (char *)MALLOC_ATOMIC(nbufsize);\
86: bcopy(nbuf0,nbuf,nbufsize-NBUFSIZ);\
87: } else\
88: nbuf = REALLOC(nbuf,nbufsize);\
89: }
90:
91: #define REALLOC_TBUF \
92: if ( i >= tbufsize ) {\
93: tbufsize += TBUFSIZ;\
94: if ( tbuf == tbuf0 ) {\
95: tbuf = (char *)MALLOC_ATOMIC(tbufsize);\
96: bcopy(tbuf0,tbuf,tbufsize-TBUFSIZ);\
97: } else\
98: tbuf = REALLOC(tbuf,tbufsize);\
99: }
100:
101: #define READ_ALNUM_NBUF \
102: while ( 1 ) {\
103: c = Getc();\
104: if ( isalnum(c) ) {\
105: REALLOC_NBUF nbuf[i++] = c;\
106: } else\
107: break;\
108: }
109:
110: #define READ_DIGIT_NBUF \
111: while ( 1 ) {\
112: c = Getc();\
113: if ( isdigit(c) ) {\
114: REALLOC_NBUF nbuf[i++] = c;\
115: } else\
116: break;\
117: }
118:
1.1 noro 119: yylex()
120: {
121: #define yylvalp (&yylval)
122: register int c,c1;
123: register int *ptr;
124: char *cptr;
125: int d,i,j;
1.2 noro 126: char nbuf0[NBUFSIZ],tbuf0[TBUFSIZ];
127: char *nbuf, *tbuf;
128: int nbufsize, tbufsize;
1.1 noro 129: N n,n1;
130: Q q;
131: Obj r;
132:
1.2 noro 133: /* initialize buffer pointers */
134: nbuf = nbuf0; tbuf = tbuf0;
135: nbufsize = NBUFSIZ; tbufsize = TBUFSIZ;
136:
1.1 noro 137: switch ( c = skipspace() ) {
138: case EOF :
139: asir_terminate(2); break;
140: case '0' :
141: while ( ( c = Getc() ) == '0' );
142: if ( c == '.' ) {
143: Ungetc(c); c = '0';
144: } else if ( c == 'x' ) {
145: for ( i = 0; i < 8; i++ )
146: nbuf[i] = '0';
1.2 noro 147: READ_ALNUM_NBUF
148: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
1.1 noro 149: hexton(nbuf,&n1);
150: NTOQ(n1,1,q); r = (Obj)q;
151: yylvalp->p = (pointer)r;
152: return ( FORMULA );
153: } else if ( c == 'b' ) {
154: for ( i = 0; i < 32; i++ )
155: nbuf[i] = '0';
1.2 noro 156: READ_ALNUM_NBUF
157: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
1.1 noro 158: binaryton(nbuf,&n1);
159: NTOQ(n1,1,q); r = (Obj)q;
160: yylvalp->p = (pointer)r;
161: return ( FORMULA );
162: } else if ( !isdigit(c) ) {
163: yylvalp->p = 0; Ungetc(c);
164: return ( FORMULA );
165: }
166: break;
167: case '\'' :
168: for ( i = 0; ; i++ ) {
169: c = Getc();
170: if ( c == '\'' )
171: break;
172: if ( c == '\\' )
173: c = Getc();
1.2 noro 174: REALLOC_TBUF tbuf[i] = c;
1.1 noro 175: }
1.2 noro 176: REALLOC_TBUF tbuf[i] = 0;
1.1 noro 177: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
178: yylvalp->p = (pointer)cptr;
179: return LCASE; break;
180: case '"' :
181: i = 0;
182: do {
183: c = Getc();
184: if ( c == '\\' ) {
185: c1 = Getc();
186: if ( c1 == 'n' )
187: c1 = '\n';
1.2 noro 188: REALLOC_NBUF nbuf[i++] = c1;
189: } else {
190: REALLOC_NBUF nbuf[i++] = c;
191: }
1.1 noro 192: } while ( c != '"' );
1.2 noro 193: nbuf[i-1] = 0; /* REALLOC_NBUF is not necessary */
1.1 noro 194: cptr = (char *)MALLOC(strlen(nbuf)+1);
195: strcpy(cptr,nbuf); yylvalp->p = (pointer) cptr;
196: return ( STR ); break;
197: case '>': case '<': case '=': case '!':
198: if ( (c1 = Getc()) == '=' )
199: switch ( c ) {
200: case '>': yylvalp->i = (int)C_GE; break;
201: case '<': yylvalp->i = (int)C_LE; break;
202: case '=': yylvalp->i = (int)C_EQ; break;
203: case '!': yylvalp->i = (int)C_NE; break;
204: default: break;
205: }
206: else if ( (c == '<' && c1 == '<') || (c == '>' && c1 == '>') )
207: return c;
208: else {
209: Ungetc(c1);
210: switch ( c ) {
211: case '>': yylvalp->i = (int)C_GT; break;
212: case '<': yylvalp->i = (int)C_LT; break;
213: default: return c; break;
214: }
215: }
216: return CMP; break;
217: case '+': case '-': case '*': case '/': case '%': case '^':
218: case '|': case '&':
219: switch ( c ) {
220: case '+': yylvalp->p = (pointer)addfs; break;
221: case '-': yylvalp->p = (pointer)subfs; break;
222: case '*': yylvalp->p = (pointer)mulfs; break;
223: case '/': yylvalp->p = (pointer)divfs; break;
224: case '%': yylvalp->p = (pointer)remfs; break;
225: case '^': yylvalp->p = (pointer)pwrfs; break;
226: default: break;
227: }
228: if ( (c1 = Getc()) == c )
229: switch ( c ) {
230: case '+': case '-': return SELF; break;
231: case '|': return OR; break;
232: case '&': return AND; break;
233: default: Ungetc(c1); return c; break;
234: }
235: else if ( c1 == '=' )
236: return BOPASS;
237: else if ( (c == '-') && (c1 == '>') )
238: return POINT;
239: else {
240: Ungetc(c1); return c;
241: }
242: break;
243: default :
244: break;
245: }
246: if ( isdigit(c) ) {
247: for ( i = 0; i < DLENGTH; i++ )
248: nbuf[i] = '0';
1.2 noro 249: REALLOC_NBUF nbuf[i++] = c;
250: READ_DIGIT_NBUF
1.1 noro 251: if ( c == '.' ) {
252: double dbl;
253: Real real;
254: double atof();
255: extern int bigfloat;
256:
1.2 noro 257: REALLOC_NBUF nbuf[i++] = c;
258: READ_DIGIT_NBUF
1.1 noro 259: if ( c == 'e' ) {
1.2 noro 260: REALLOC_NBUF nbuf[i++] = c;
261: c = Getc();
262: if ( (c == '+') || (c == '-') ) {
263: REALLOC_NBUF nbuf[i++] = c;
264: } else
265: Ungetc(c);
266: READ_DIGIT_NBUF
1.1 noro 267: }
1.2 noro 268: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
1.1 noro 269: #if PARI
270: if ( !bigfloat ) {
271: dbl = (double)atof(nbuf+DLENGTH);
272: MKReal(dbl,real); r = (Obj)real;
273: } else
274: strtobf(nbuf,(BF *)&r);
275: #else
276: dbl = (double)atof(nbuf+DLENGTH);
277: MKReal(dbl,real); r = (Obj)real;
278: #endif
279: } else {
280: Ungetc(c);
281: i -= DLENGTH; d = (i%DLENGTH?i/DLENGTH+1:i/DLENGTH);
282: n = NALLOC(d); PL(n) = d;
283: for ( j = 0, ptr = BD(n); j < d; j++ ,i -= DLENGTH )
284: ptr[j] = myatoi(nbuf+i);
285: bnton(DBASE,n,&n1);
286: NTOQ(n1,1,q); r = (Obj)q;
287: /* optobj(&r); */
288: }
289: yylvalp->p = (pointer)r;
290: return ( FORMULA );
291: } else if ( isalpha(c) ) {
1.2 noro 292: i = 0;
293: tbuf[i++] = c;
294: while ( 1 ) {
295: c = Getc();
296: if ( isalpha(c)||isdigit(c)||(c=='_') ) {
297: REALLOC_TBUF tbuf[i++] = c;
298: } else
299: break;
300: }
301: REALLOC_TBUF tbuf[i] = 0; Ungetc(c);
1.1 noro 302: if ( isupper(tbuf[0]) ) {
303: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
304: yylvalp->p = (pointer)cptr;
305: return UCASE;
306: } else {
307: for ( i = 0; kwd[i].name && strcmp(tbuf,kwd[i].name); i++ );
308: if ( kwd[i].name ) {
309: yylvalp->i = asir_infile->ln;
310: return kwd[i].token;
311: } else {
312: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
313: yylvalp->p = (pointer)cptr;
314: return LCASE;
315: }
316: }
317: } else if ( c == '@' ) {
318: if ( isdigit(c = Getc()) ) {
1.2 noro 319: i = 0;
320: nbuf[i++] = c;
321: READ_DIGIT_NBUF
322: Ungetc(c); REALLOC_NBUF nbuf[i] = 0;
323: yylvalp->i = atoi(nbuf);
1.1 noro 324: return ANS;
325: } else if ( c == '@' ) {
326: yylvalp->i = MAX(0,APVS->n-1);
327: return ANS;
328: } else if ( c == '>' || c == '<' || c == '=' || c == '!' ) {
329: if ( (c1 = Getc()) == '=' )
330: switch ( c ) {
331: case '>': yylvalp->i = (int)L_GE; break;
332: case '<': yylvalp->i = (int)L_LE; break;
333: case '=': yylvalp->i = (int)L_EQ; break;
334: case '!': yylvalp->i = (int)L_NE; break;
335: default: break;
336: }
337: else {
338: Ungetc(c1);
339: switch ( c ) {
340: case '>': yylvalp->i = (int)L_GT; break;
341: case '<': yylvalp->i = (int)L_LT; break;
342: case '=': yylvalp->i = (int)L_EQ; break;
343: case '!': yylvalp->i = (int)L_NOT; return FOP_NOT; break;
344: default: break;
345: }
346: }
347: return LOP;
348: } else if ( c == '|' || c == '&' ) {
349: if ( (c1 = Getc()) != c )
350: Ungetc(c1);
351: switch ( c ) {
352: case '|': yylvalp->i = (int)L_OR;
353: return FOP_OR; break;
354: case '&': yylvalp->i = (int)L_AND;
355: return FOP_AND; break;
356: }
357: } else if ( isalpha(c) ) {
1.2 noro 358: i = 0;
359: tbuf[i++] = '@';
360: tbuf[i++] = c;
361: while ( 1 ) {
362: c = Getc();
363: if ( isalpha(c) ) {
364: REALLOC_TBUF tbuf[i++] = c;
365: } else
366: break;
367: }
368: Ungetc(c); REALLOC_TBUF tbuf[i] = 0;
1.1 noro 369: if ( !strcmp(tbuf,"@p") )
370: return GFPNGEN;
371: else if ( !strcmp(tbuf,"@i") ) {
372: extern pointer IU;
373:
374: yylvalp->p = IU;
375: return FORMULA;
376: } else if ( !strcmp(tbuf,"@true") ) {
377: yylvalp->p = F_TRUE;
378: return FORMULA;
379: } else if ( !strcmp(tbuf,"@false") ) {
380: yylvalp->p = F_FALSE;
381: return FORMULA;
382: } else if ( !strcmp(tbuf,"@impl") ) {
383: yylvalp->i = (int)L_IMPL;
384: return FOP_IMPL;
385: } else if ( !strcmp(tbuf,"@repl") ) {
386: yylvalp->i = (int)L_REPL;
387: return FOP_REPL;
388: } else if ( !strcmp(tbuf,"@equiv") ) {
389: yylvalp->i = (int)L_EQUIV;
390: return FOP_EQUIV;
391: } else {
392: cptr = (char *)MALLOC(strlen(tbuf)+1); strcpy(cptr,tbuf);
393: yylvalp->p = (pointer)cptr;
394: return LCASE;
395: }
396: } else {
397: Ungetc(c);
398: return GF2NGEN;
399: }
400: } else
401: return ( c );
1.6 noro 402: }
403:
404: void purge_stdin()
405: {
1.7 noro 406: #if defined(__FreeBSD__)
1.6 noro 407: fpurge(stdin);
408: #elif defined(linux)
409: stdin->_IO_read_end = stdin->_IO_read_base;
410: stdin->_IO_read_ptr = stdin->_IO_read_base;
1.10 ! noro 411: #elif defined(VISUAL_LIB)
! 412: w_purge_stdin();
! 413: #elif defined(sparc) || defined(__alpha) || defined(__SVR4) || defined(mips) || defined(VISUAL)
1.6 noro 414: stdin->_ptr = stdin->_base; stdin->_cnt = 0;
415: #else
416: --->FIXIT
417: #endif
1.1 noro 418: }
419:
420: static int skipspace() {
421: int c,c1;
422:
423: for ( c = Getc(); ; )
424: switch ( c ) {
425: case ' ': case '\t':
426: c = Getc(); break;
427: case '\n':
428: c = afternl(); break;
429: case '/':
430: if ( (c1 = Getc()) == '*' )
431: c = aftercomment();
432: else {
433: Ungetc(c1); return c;
434: }
435: break;
436: default:
437: return c; break;
438: }
439: }
440:
441: int afternl() {
442: int c,ac,i,quote;
443: char *ptr;
444: char *av[BUFSIZ];
445: static int ilevel = 0;
446: char buf[BUFSIZ];
447:
448: if ( !ilevel )
449: asir_infile->ln++;
450: while ( (c = Getc()) == '#' ) {
451: Gets(buf);
452: for ( quote = 0, ptr = buf; *ptr; ptr++ )
453: if ( *ptr == '"' )
454: quote = quote ? 0 : 1;
455: else if ( quote && (*ptr == ' ') )
456: *ptr = '_';
457: stoarg(buf,&ac,av);
458: if ( ac == 3 )
459: if ( (i = atoi(av[2])) == 1 )
460: ilevel++;
461: else if ( i == 2 )
462: ilevel--;
463: if ( !ilevel )
464: asir_infile->ln = atoi(av[0]);
465: }
466: return c;
467: }
468:
469: int aftercomment() {
470: int c,c1;
471:
472: for ( c = Getc(); ; ) {
473: c1 = Getc();
474: if ( (c == '*') && (c1 == '/') )
475: return Getc();
476: else
477: c = c1;
478: }
479: }
480:
481: int myatoi(s)
482: char *s;
483: {
484: int i,r;
485: for ( i = 0, r = 0; i < DLENGTH; i++ ) r = r * 10 + ( s[i] - '0' );
486: return ( r );
487: }
488:
489: extern int ox_do_copy;
490:
491: void yyerror(s)
492: char *s;
493: {
494: if ( main_parser )
495: if ( ox_do_copy ) {
496: /* push errors to DebugStack */
497: } else {
498: if ( asir_infile->fp == stdin )
499: fprintf(stderr,"%s\n",s);
500: else
501: fprintf(stderr,"\"%s\", near line %d: %s\n",asir_infile->name,asir_infile->ln,s);
502: }
503: else
504: fprintf(stderr,"exprparse : %s\n",s);
505: }
506:
507: int echoback;
508:
509: extern int read_exec_file, do_fep, do_file;
510:
511: int readline_getc();
512: void readline_ungetc();
513: int Egetc();
514: void Eungetc();
515:
516: unsigned char encrypt_char(unsigned char);
517: unsigned char decrypt_char(unsigned char);
518:
519: int Egetc(fp)
520: FILE *fp;
521: {
522: int c;
523:
524: if ( fp ) {
525: c = getc(fp);
526: if ( c == EOF )
527: return c;
528: if ( asir_infile->encoded )
529: c = decrypt_char((unsigned char)c);
530: return c;
1.3 noro 531: } else if ( read_exec_file )
532: return EOF;
533: else {
1.1 noro 534: c = *parse_strp++;
535: if ( !c )
536: return EOF;
537: else
538: return c;
539: }
540: }
541:
542: void Eungetc(c,fp)
543: int c;
544: FILE *fp;
545: {
546: if ( fp ) {
547: if ( asir_infile->encoded )
548: c = (int)encrypt_char((unsigned char)c);
549: ungetc(c,fp);
550: } else
551: *--parse_strp = c;
552: }
553:
554: static int Getc() {
555: int c;
556:
557: if ( main_parser ) {
558: while ( 1 ) {
559: if ((c = Egetc(asir_infile->fp)) == EOF)
560: if ( NEXT(asir_infile) ) {
561: closecurrentinput();
1.9 noro 562: /* if the input is the top level, generate error */
563: if ( !NEXT(asir_infile) )
564: error("end-of-file detected during parsing");
565: else
566: c = Getc();
1.1 noro 567: break;
568: } else if ( read_exec_file || do_file )
569: asir_terminate(2);
570: else {
571: if ( asir_infile->fp )
572: clearerr(asir_infile->fp);
573: }
574: else
575: break;
576: }
577: if ( echoback )
578: fputc(c,asir_out);
579: } else
580: c = *parse_strp++;
581: return ( c );
582: }
583:
584: static void Ungetc(c) {
585: if ( main_parser ) {
586: Eungetc(c,asir_infile->fp);
587: if ( echoback )
588: fputc('',asir_out);
589: } else
590: *--parse_strp = c;
591: }
592:
593: static void Gets(s)
594: char *s;
595: {
596: int c;
597:
598: while ( (c = Getc()) != '\n' )
599: *s++ = c;
600: *s = 0;
601: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>