Annotation of OpenXM_contrib2/asir2000/parse/lex.c, Revision 1.4
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
! 26: * e-mail at risa-admin@flab.fujitsu.co.jp of the detailed specification
! 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: *
! 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/lex.c,v 1.3 2000/07/25 01:15:07 noro Exp $
! 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 );
400: }
401:
402: static int skipspace() {
403: int c,c1;
404:
405: for ( c = Getc(); ; )
406: switch ( c ) {
407: case ' ': case '\t':
408: c = Getc(); break;
409: case '\n':
410: c = afternl(); break;
411: case '/':
412: if ( (c1 = Getc()) == '*' )
413: c = aftercomment();
414: else {
415: Ungetc(c1); return c;
416: }
417: break;
418: default:
419: return c; break;
420: }
421: }
422:
423: int afternl() {
424: int c,ac,i,quote;
425: char *ptr;
426: char *av[BUFSIZ];
427: static int ilevel = 0;
428: char buf[BUFSIZ];
429:
430: if ( !ilevel )
431: asir_infile->ln++;
432: while ( (c = Getc()) == '#' ) {
433: Gets(buf);
434: for ( quote = 0, ptr = buf; *ptr; ptr++ )
435: if ( *ptr == '"' )
436: quote = quote ? 0 : 1;
437: else if ( quote && (*ptr == ' ') )
438: *ptr = '_';
439: stoarg(buf,&ac,av);
440: if ( ac == 3 )
441: if ( (i = atoi(av[2])) == 1 )
442: ilevel++;
443: else if ( i == 2 )
444: ilevel--;
445: if ( !ilevel )
446: asir_infile->ln = atoi(av[0]);
447: }
448: return c;
449: }
450:
451: int aftercomment() {
452: int c,c1;
453:
454: for ( c = Getc(); ; ) {
455: c1 = Getc();
456: if ( (c == '*') && (c1 == '/') )
457: return Getc();
458: else
459: c = c1;
460: }
461: }
462:
463: int myatoi(s)
464: char *s;
465: {
466: int i,r;
467: for ( i = 0, r = 0; i < DLENGTH; i++ ) r = r * 10 + ( s[i] - '0' );
468: return ( r );
469: }
470:
471: extern int ox_do_copy;
472:
473: void yyerror(s)
474: char *s;
475: {
476: if ( main_parser )
477: if ( ox_do_copy ) {
478: /* push errors to DebugStack */
479: } else {
480: if ( asir_infile->fp == stdin )
481: fprintf(stderr,"%s\n",s);
482: else
483: fprintf(stderr,"\"%s\", near line %d: %s\n",asir_infile->name,asir_infile->ln,s);
484: }
485: else
486: fprintf(stderr,"exprparse : %s\n",s);
487: }
488:
489: int echoback;
490:
491: extern int read_exec_file, do_fep, do_file;
492:
493: int readline_getc();
494: void readline_ungetc();
495: int Egetc();
496: void Eungetc();
497:
498: unsigned char encrypt_char(unsigned char);
499: unsigned char decrypt_char(unsigned char);
500:
501: int Egetc(fp)
502: FILE *fp;
503: {
504: int c;
505:
506: if ( fp ) {
507: c = getc(fp);
508: if ( c == EOF )
509: return c;
510: if ( asir_infile->encoded )
511: c = decrypt_char((unsigned char)c);
512: return c;
1.3 noro 513: } else if ( read_exec_file )
514: return EOF;
515: else {
1.1 noro 516: c = *parse_strp++;
517: if ( !c )
518: return EOF;
519: else
520: return c;
521: }
522: }
523:
524: void Eungetc(c,fp)
525: int c;
526: FILE *fp;
527: {
528: if ( fp ) {
529: if ( asir_infile->encoded )
530: c = (int)encrypt_char((unsigned char)c);
531: ungetc(c,fp);
532: } else
533: *--parse_strp = c;
534: }
535:
536: static int Getc() {
537: int c;
538:
539: if ( main_parser ) {
540: while ( 1 ) {
541: if ((c = Egetc(asir_infile->fp)) == EOF)
542: if ( NEXT(asir_infile) ) {
543: closecurrentinput();
544: c = Getc();
545: break;
546: } else if ( read_exec_file || do_file )
547: asir_terminate(2);
548: else {
549: if ( asir_infile->fp )
550: clearerr(asir_infile->fp);
551: }
552: else
553: break;
554: }
555: if ( echoback )
556: fputc(c,asir_out);
557: } else
558: c = *parse_strp++;
559: return ( c );
560: }
561:
562: static void Ungetc(c) {
563: if ( main_parser ) {
564: Eungetc(c,asir_infile->fp);
565: if ( echoback )
566: fputc('',asir_out);
567: } else
568: *--parse_strp = c;
569: }
570:
571: static void Gets(s)
572: char *s;
573: {
574: int c;
575:
576: while ( (c = Getc()) != '\n' )
577: *s++ = c;
578: *s = 0;
579: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>