|
|
1.1 root 1: /* Parse C expressions for CCCP.
2: Copyright (C) 1987 Free Software Foundation.
3:
1.1.1.6 root 4: This program is free software; you can redistribute it and/or modify it
5: under the terms of the GNU General Public License as published by the
6: Free Software Foundation; either version 1, or (at your option) any
7: later version.
8:
9: This program is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: GNU General Public License for more details.
13:
14: You should have received a copy of the GNU General Public License
15: along with this program; if not, write to the Free Software
16: Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
1.1 root 17:
18: In other words, you are welcome to use, share and improve this program.
19: You are forbidden to forbid anyone else to use, share and improve
20: what you give them. Help stamp out software-hoarding!
21:
22: Adapted from expread.y of GDB by Paul Rubin, July 1986.
23:
24: /* Parse a C expression from text in a string */
25:
26: %{
1.1.1.3 root 27: #include "config.h"
1.1 root 28: #include <setjmp.h>
29: /* #define YYDEBUG 1 */
30:
1.1.1.4 root 31: int yylex ();
32: void yyerror ();
1.1 root 33: int expression_value;
34:
35: static jmp_buf parse_return_error;
36:
37: /* some external tables of character types */
38: extern unsigned char is_idstart[], is_idchar[];
39:
1.1.1.7 root 40: #ifndef CHAR_TYPE_SIZE
41: #define CHAR_TYPE_SIZE BITS_PER_UNIT
42: #endif
1.1 root 43: %}
44:
45: %union {
1.1.1.7 root 46: struct constant {long value; int unsignedp;} integer;
1.1 root 47: int voidval;
48: char *sval;
49: }
50:
1.1.1.7 root 51: %type <integer> exp exp1 start
52: %token <integer> INT CHAR
1.1 root 53: %token <sval> NAME
1.1.1.7 root 54: %token <integer> ERROR
1.1 root 55:
1.1.1.2 root 56: %right '?' ':'
1.1 root 57: %left ','
58: %left OR
59: %left AND
60: %left '|'
61: %left '^'
62: %left '&'
63: %left EQUAL NOTEQUAL
64: %left '<' '>' LEQ GEQ
65: %left LSH RSH
66: %left '+' '-'
67: %left '*' '/' '%'
68: %right UNARY
1.1.1.2 root 69:
70: /* %expect 40 */
1.1 root 71:
72: %%
73:
74: start : exp1
1.1.1.7 root 75: { expression_value = $1.value; }
1.1 root 76: ;
77:
78: /* Expressions, including the comma operator. */
79: exp1 : exp
80: | exp1 ',' exp
81: { $$ = $3; }
82: ;
83:
84: /* Expressions, not including the comma operator. */
85: exp : '-' exp %prec UNARY
1.1.1.7 root 86: { $$.value = - $2.value;
87: $$.unsignedp = $2.unsignedp; }
1.1 root 88: | '!' exp %prec UNARY
1.1.1.7 root 89: { $$.value = ! $2.value;
90: $$.unsignedp = 0; }
1.1.1.8 ! root 91: | '+' exp %prec UNARY
! 92: { $$ = $2; }
1.1 root 93: | '~' exp %prec UNARY
1.1.1.7 root 94: { $$.value = ~ $2.value;
95: $$.unsignedp = $2.unsignedp; }
1.1 root 96: | '(' exp1 ')'
97: { $$ = $2; }
98: ;
99:
100: /* Binary operators in order of decreasing precedence. */
101: exp : exp '*' exp
1.1.1.7 root 102: { $$.unsignedp = $1.unsignedp || $3.unsignedp;
103: if ($$.unsignedp)
104: $$.value = (unsigned) $1.value * $3.value;
105: else
106: $$.value = $1.value * $3.value; }
1.1 root 107: | exp '/' exp
1.1.1.7 root 108: { if ($3.value == 0)
1.1.1.5 root 109: {
110: error ("division by zero in #if");
1.1.1.7 root 111: $3.value = 1;
1.1.1.5 root 112: }
1.1.1.7 root 113: $$.unsignedp = $1.unsignedp || $3.unsignedp;
114: if ($$.unsignedp)
115: $$.value = (unsigned) $1.value / $3.value;
116: else
117: $$.value = $1.value / $3.value; }
1.1 root 118: | exp '%' exp
1.1.1.7 root 119: { if ($3.value == 0)
1.1.1.5 root 120: {
121: error ("division by zero in #if");
1.1.1.7 root 122: $3.value = 1;
1.1.1.5 root 123: }
1.1.1.7 root 124: $$.unsignedp = $1.unsignedp || $3.unsignedp;
125: if ($$.unsignedp)
126: $$.value = (unsigned) $1.value % $3.value;
127: else
128: $$.value = $1.value % $3.value; }
1.1 root 129: | exp '+' exp
1.1.1.7 root 130: { $$.value = $1.value + $3.value;
131: $$.unsignedp = $1.unsignedp || $3.unsignedp; }
1.1 root 132: | exp '-' exp
1.1.1.7 root 133: { $$.value = $1.value - $3.value;
134: $$.unsignedp = $1.unsignedp || $3.unsignedp; }
1.1 root 135: | exp LSH exp
1.1.1.7 root 136: { $$.unsignedp = $1.unsignedp;
137: if ($$.unsignedp)
138: $$.value = (unsigned) $1.value << $3.value;
139: else
140: $$.value = $1.value << $3.value; }
1.1 root 141: | exp RSH exp
1.1.1.7 root 142: { $$.unsignedp = $1.unsignedp;
143: if ($$.unsignedp)
144: $$.value = (unsigned) $1.value >> $3.value;
145: else
146: $$.value = $1.value >> $3.value; }
1.1 root 147: | exp EQUAL exp
1.1.1.7 root 148: { $$.value = ($1.value == $3.value);
149: $$.unsignedp = 0; }
1.1 root 150: | exp NOTEQUAL exp
1.1.1.7 root 151: { $$.value = ($1.value != $3.value);
152: $$.unsignedp = 0; }
1.1 root 153: | exp LEQ exp
1.1.1.7 root 154: { $$.unsignedp = 0;
155: if ($1.unsignedp || $3.unsignedp)
156: $$.value = (unsigned) $1.value <= $3.value;
157: else
158: $$.value = $1.value <= $3.value; }
1.1 root 159: | exp GEQ exp
1.1.1.7 root 160: { $$.unsignedp = 0;
161: if ($1.unsignedp || $3.unsignedp)
162: $$.value = (unsigned) $1.value >= $3.value;
163: else
164: $$.value = $1.value >= $3.value; }
1.1 root 165: | exp '<' exp
1.1.1.7 root 166: { $$.unsignedp = 0;
167: if ($1.unsignedp || $3.unsignedp)
168: $$.value = (unsigned) $1.value < $3.value;
169: else
170: $$.value = $1.value < $3.value; }
1.1 root 171: | exp '>' exp
1.1.1.7 root 172: { $$.unsignedp = 0;
173: if ($1.unsignedp || $3.unsignedp)
174: $$.value = (unsigned) $1.value > $3.value;
175: else
176: $$.value = $1.value > $3.value; }
1.1 root 177: | exp '&' exp
1.1.1.7 root 178: { $$.value = $1.value & $3.value;
179: $$.unsignedp = $1.unsignedp || $3.unsignedp; }
1.1 root 180: | exp '^' exp
1.1.1.7 root 181: { $$.value = $1.value ^ $3.value;
182: $$.unsignedp = $1.unsignedp || $3.unsignedp; }
1.1 root 183: | exp '|' exp
1.1.1.7 root 184: { $$.value = $1.value | $3.value;
185: $$.unsignedp = $1.unsignedp || $3.unsignedp; }
1.1 root 186: | exp AND exp
1.1.1.7 root 187: { $$.value = ($1.value && $3.value);
188: $$.unsignedp = 0; }
1.1 root 189: | exp OR exp
1.1.1.7 root 190: { $$.value = ($1.value || $3.value);
191: $$.unsignedp = 0; }
1.1 root 192: | exp '?' exp ':' exp
1.1.1.7 root 193: { $$.value = $1.value ? $3.value : $5.value;
194: $$.unsignedp = $3.unsignedp || $5.unsignedp; }
1.1 root 195: | INT
1.1.1.7 root 196: { $$ = yylval.integer; }
1.1 root 197: | CHAR
1.1.1.7 root 198: { $$ = yylval.integer; }
1.1 root 199: | NAME
1.1.1.7 root 200: { $$.value = 0;
201: $$.unsignedp = 0; }
1.1 root 202: ;
203: %%
204:
205: /* During parsing of a C expression, the pointer to the next character
206: is in this variable. */
207:
208: static char *lexptr;
209:
210: /* Take care of parsing a number (anything that starts with a digit).
211: Set yylval and return the token type; update lexptr.
212: LEN is the number of characters in it. */
213:
214: /* maybe needs to actually deal with floating point numbers */
215:
1.1.1.4 root 216: int
1.1 root 217: parse_number (olen)
218: int olen;
219: {
220: register char *p = lexptr;
221: register long n = 0;
222: register int c;
223: register int base = 10;
1.1.1.7 root 224: register int len = olen;
1.1 root 225:
226: for (c = 0; c < len; c++)
227: if (p[c] == '.') {
228: /* It's a float since it contains a point. */
229: yyerror ("floating point numbers not allowed in #if expressions");
230: return ERROR;
231: }
1.1.1.7 root 232:
233: yylval.integer.unsignedp = 0;
234:
1.1 root 235: if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
236: p += 2;
237: base = 16;
238: len -= 2;
239: }
240: else if (*p == '0')
241: base = 8;
1.1.1.7 root 242:
243: while (len > 0) {
1.1 root 244: c = *p++;
1.1.1.7 root 245: len--;
246: if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
247:
248: if (c >= '0' && c <= '9') {
249: n *= base;
1.1 root 250: n += c - '0';
1.1.1.7 root 251: } else if (base == 16 && c >= 'a' && c <= 'f') {
252: n *= base;
253: n += c - 'a' + 10;
254: } else {
255: /* `l' means long, and `u' means unsigned. */
256: while (1) {
257: if (c == 'l' || c == 'L')
258: ;
259: else if (c == 'u' || c == 'U')
260: yylval.integer.unsignedp = 1;
261: else
262: break;
263:
264: if (len == 0)
265: break;
266: c = *p++;
267: len--;
1.1 root 268: }
1.1.1.7 root 269: /* Don't look for any more digits after the suffixes. */
270: break;
1.1 root 271: }
272: }
273:
1.1.1.7 root 274: if (len != 0) {
275: yyerror ("Invalid number in #if expression");
276: return ERROR;
277: }
278:
279: /* If too big to be signed, consider it unsigned. */
280: if (n < 0)
281: yylval.integer.unsignedp = 1;
282:
1.1 root 283: lexptr = p;
1.1.1.7 root 284: yylval.integer.value = n;
1.1 root 285: return INT;
286: }
287:
288: struct token {
289: char *operator;
290: int token;
291: };
292:
1.1.1.7 root 293: #ifndef NULL
1.1 root 294: #define NULL 0
1.1.1.7 root 295: #endif
1.1 root 296:
297: static struct token tokentab2[] = {
298: {"&&", AND},
299: {"||", OR},
300: {"<<", LSH},
301: {">>", RSH},
302: {"==", EQUAL},
303: {"!=", NOTEQUAL},
304: {"<=", LEQ},
305: {">=", GEQ},
306: {NULL, ERROR}
307: };
308:
309: /* Read one token, getting characters through lexptr. */
310:
1.1.1.4 root 311: int
1.1 root 312: yylex ()
313: {
314: register int c;
315: register int namelen;
316: register char *tokstart;
317: register struct token *toktab;
318:
319: retry:
320:
321: tokstart = lexptr;
322: c = *tokstart;
323: /* See if it is a special token of length 2. */
324: for (toktab = tokentab2; toktab->operator != NULL; toktab++)
325: if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
326: lexptr += 2;
327: return toktab->token;
328: }
329:
330: switch (c) {
331: case 0:
332: return 0;
333:
334: case ' ':
335: case '\t':
336: case '\n':
337: lexptr++;
338: goto retry;
339:
340: case '\'':
341: lexptr++;
342: c = *lexptr++;
343: if (c == '\\')
344: c = parse_escape (&lexptr);
1.1.1.7 root 345:
346: /* Sign-extend the constant if chars are signed on target machine. */
347: {
348: if (lookup ("__CHAR_UNSIGNED__", sizeof ("__CHAR_UNSIGNED__")-1, -1)
349: || ((c >> (CHAR_TYPE_SIZE - 1)) & 1) == 0)
350: yylval.integer.value = c & ((1 << CHAR_TYPE_SIZE) - 1);
351: else
352: yylval.integer.value = c | ~((1 << CHAR_TYPE_SIZE) - 1);
353: }
354:
355: yylval.integer.unsignedp = 0;
1.1 root 356: c = *lexptr++;
357: if (c != '\'') {
358: yyerror ("Invalid character constant in #if");
359: return ERROR;
360: }
361:
362: return CHAR;
363:
364: /* some of these chars are invalid in constant expressions;
365: maybe do something about them later */
1.1.1.2 root 366: case '/':
1.1 root 367: case '+':
368: case '-':
369: case '*':
370: case '%':
371: case '|':
372: case '&':
373: case '^':
374: case '~':
375: case '!':
376: case '@':
377: case '<':
378: case '>':
379: case '(':
380: case ')':
381: case '[':
382: case ']':
383: case '.':
384: case '?':
385: case ':':
386: case '=':
387: case '{':
388: case '}':
389: case ',':
390: lexptr++;
391: return c;
392:
393: case '"':
394: yyerror ("double quoted strings not allowed in #if expressions");
395: return ERROR;
396: }
397: if (c >= '0' && c <= '9') {
398: /* It's a number */
399: for (namelen = 0;
400: c = tokstart[namelen], is_idchar[c] || c == '.';
401: namelen++)
402: ;
403: return parse_number (namelen);
404: }
405:
406: if (!is_idstart[c]) {
407: yyerror ("Invalid token in expression");
408: return ERROR;
409: }
410:
411: /* It is a name. See how long it is. */
412:
413: for (namelen = 0; is_idchar[tokstart[namelen]]; namelen++)
414: ;
415:
416: lexptr += namelen;
417: return NAME;
418: }
419:
420:
421: /* Parse a C escape sequence. STRING_PTR points to a variable
422: containing a pointer to the string to parse. That pointer
423: is updated past the characters we use. The value of the
424: escape sequence is returned.
425:
426: A negative value means the sequence \ newline was seen,
427: which is supposed to be equivalent to nothing at all.
428:
429: If \ is followed by a null character, we return a negative
430: value and leave the string pointer pointing at the null character.
431:
432: If \ is followed by 000, we return 0 and leave the string pointer
433: after the zeros. A value of 0 does not mean end of string. */
434:
1.1.1.4 root 435: int
1.1 root 436: parse_escape (string_ptr)
437: char **string_ptr;
438: {
439: register int c = *(*string_ptr)++;
440: switch (c)
441: {
442: case 'a':
1.1.1.7 root 443: return TARGET_BELL;
1.1 root 444: case 'b':
1.1.1.7 root 445: return TARGET_BS;
1.1 root 446: case 'e':
447: return 033;
448: case 'f':
1.1.1.7 root 449: return TARGET_FF;
1.1 root 450: case 'n':
1.1.1.7 root 451: return TARGET_NEWLINE;
1.1 root 452: case 'r':
1.1.1.7 root 453: return TARGET_CR;
1.1 root 454: case 't':
1.1.1.7 root 455: return TARGET_TAB;
1.1 root 456: case 'v':
1.1.1.7 root 457: return TARGET_VT;
1.1 root 458: case '\n':
459: return -2;
460: case 0:
461: (*string_ptr)--;
462: return 0;
463: case '^':
464: c = *(*string_ptr)++;
465: if (c == '\\')
466: c = parse_escape (string_ptr);
467: if (c == '?')
468: return 0177;
469: return (c & 0200) | (c & 037);
470:
471: case '0':
472: case '1':
473: case '2':
474: case '3':
475: case '4':
476: case '5':
477: case '6':
478: case '7':
479: {
480: register int i = c - '0';
481: register int count = 0;
482: while (++count < 3)
483: {
1.1.1.7 root 484: c = *(*string_ptr)++;
485: if (c >= '0' && c <= '7')
486: i = (i << 3) + c - '0';
487: else
1.1 root 488: {
1.1.1.7 root 489: (*string_ptr)--;
490: break;
1.1 root 491: }
1.1.1.7 root 492: }
493: if ((i & ~((1 << CHAR_TYPE_SIZE) - 1)) != 0)
494: {
495: i &= (1 << CHAR_TYPE_SIZE) - 1;
496: warning ("octal character constant does not fit in a byte");
497: }
498: return i;
499: }
500: case 'x':
501: {
502: register int i = 0;
503: register int count = 0;
504: for (;;)
505: {
506: c = *(*string_ptr)++;
507: if (c >= '0' && c <= '9')
508: i = (i << 4) + c - '0';
509: else if (c >= 'a' && c <= 'f')
510: i = (i << 4) + c - 'a' + 10;
511: else if (c >= 'A' && c <= 'F')
512: i = (i << 4) + c - 'A' + 10;
1.1 root 513: else
514: {
515: (*string_ptr)--;
516: break;
517: }
518: }
1.1.1.7 root 519: if ((i & ~((1 << BITS_PER_UNIT) - 1)) != 0)
520: {
521: i &= (1 << BITS_PER_UNIT) - 1;
522: warning ("hex character constant does not fit in a byte");
523: }
1.1 root 524: return i;
525: }
526: default:
527: return c;
528: }
529: }
530:
1.1.1.4 root 531: void
1.1 root 532: yyerror (s)
533: char *s;
534: {
535: error (s);
536: longjmp (parse_return_error, 1);
537: }
538:
539: /* This page contains the entry point to this file. */
540:
541: /* Parse STRING as an expression, and complain if this fails
542: to use up all of the contents of STRING. */
1.1.1.2 root 543: /* We do not support C comments. They should be removed before
544: this function is called. */
545:
1.1 root 546: int
547: parse_c_expression (string)
548: char *string;
549: {
550: lexptr = string;
551:
552: if (lexptr == 0 || *lexptr == 0) {
553: error ("empty #if expression");
554: return 0; /* don't include the #if group */
555: }
556:
557: /* if there is some sort of scanning error, just return 0 and assume
558: the parsing routine has printed an error message somewhere.
559: there is surely a better thing to do than this. */
1.1.1.7 root 560: if (setjmp (parse_return_error))
1.1 root 561: return 0;
562:
563: if (yyparse ())
564: return 0; /* actually this is never reached
565: the way things stand. */
566: if (*lexptr)
567: error ("Junk after end of expression.");
568:
1.1.1.7 root 569: return expression_value; /* set by yyparse () */
1.1 root 570: }
571:
572: #ifdef TEST_EXP_READER
573: /* main program, for testing purposes. */
1.1.1.7 root 574: main ()
1.1 root 575: {
1.1.1.7 root 576: int n, c;
1.1 root 577: char buf[1024];
578: extern int yydebug;
579: /*
580: yydebug = 1;
581: */
582: initialize_random_junk ();
583:
584: for (;;) {
1.1.1.7 root 585: printf ("enter expression: ");
1.1 root 586: n = 0;
1.1.1.7 root 587: while ((buf[n] = getchar ()) != '\n' && buf[n] != EOF)
1.1 root 588: n++;
1.1.1.7 root 589: if (buf[n] == EOF)
590: break;
1.1 root 591: buf[n] = '\0';
1.1.1.7 root 592: printf ("parser returned %d\n", parse_c_expression (buf));
1.1 root 593: }
594: }
595:
596: /* table to tell if char can be part of a C identifier. */
1.1.1.7 root 597: unsigned char is_idchar[256];
1.1 root 598: /* table to tell if char can be first char of a c identifier. */
1.1.1.7 root 599: unsigned char is_idstart[256];
600: /* table to tell if c is horizontal space. isspace () thinks that
1.1 root 601: newline is space; this is not a good idea for this program. */
602: char is_hor_space[256];
603:
604: /*
605: * initialize random junk in the hash table and maybe other places
606: */
1.1.1.7 root 607: initialize_random_junk ()
1.1 root 608: {
609: register int i;
610:
611: /*
612: * Set up is_idchar and is_idstart tables. These should be
1.1.1.7 root 613: * faster than saying (is_alpha (c) || c == '_'), etc.
1.1 root 614: * Must do set up these things before calling any routines tthat
615: * refer to them.
616: */
617: for (i = 'a'; i <= 'z'; i++) {
618: ++is_idchar[i - 'a' + 'A'];
619: ++is_idchar[i];
620: ++is_idstart[i - 'a' + 'A'];
621: ++is_idstart[i];
622: }
623: for (i = '0'; i <= '9'; i++)
624: ++is_idchar[i];
625: ++is_idchar['_'];
626: ++is_idstart['_'];
1.1.1.8 ! root 627: #if DOLLARS_IN_IDENTIFIERS
1.1.1.2 root 628: ++is_idchar['$'];
629: ++is_idstart['$'];
630: #endif
1.1 root 631:
632: /* horizontal space table */
633: ++is_hor_space[' '];
634: ++is_hor_space['\t'];
635: }
636:
637: error (msg)
638: {
1.1.1.7 root 639: printf ("error: %s\n", msg);
640: }
641:
642: warning (msg)
643: {
644: printf ("warning: %s\n", msg);
645: }
646:
647: struct hashnode *
648: lookup (name, len, hash)
649: char *name;
650: int len;
651: int hash;
652: {
653: return (DEFAULT_SIGNED_CHAR) ? 0 : ((struct hashnode *) -1);
1.1 root 654: }
655: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.