1 /* Parse C expressions for CCCP.
2 Copyright (C) 1987, 2000 Free Software Foundation.
3 Adapted from expread.y of GDB by Paul Rubin, July 1986.
4 Adapted to ANSI C, Richard Stallman, Jan 1987
5 Dusted off, polished, and adapted for use as traditional
6 preprocessor only, Zack Weinberg, Jul 2000
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Parse a C expression from text in a string */
31 static int yylex PARAMS ((void));
32 static void yyerror PARAMS ((const char *msgid));
34 static int parse_number PARAMS ((int));
35 static int parse_escape PARAMS ((const char **));
37 static int expression_value;
38 static jmp_buf parse_return_error;
42 struct constant {long value; int unsignedp;} integer;
47 %type <integer> exp exp1 start
48 %token <integer> INT CHAR
50 %token <integer> ERROR
71 { expression_value = $1.value; }
74 /* Expressions, including the comma operator. */
80 /* Expressions, not including the comma operator. */
81 exp : '-' exp %prec UNARY
82 { $$.value = - $2.value;
83 $$.unsignedp = $2.unsignedp; }
85 { $$.value = ! $2.value;
90 { $$.value = ~ $2.value;
91 $$.unsignedp = $2.unsignedp; }
96 /* Binary operators in order of decreasing precedence. */
98 { $$.unsignedp = $1.unsignedp || $3.unsignedp;
100 $$.value = (unsigned) $1.value * $3.value;
102 $$.value = $1.value * $3.value; }
106 error ("division by zero in #if");
109 $$.unsignedp = $1.unsignedp || $3.unsignedp;
111 $$.value = (unsigned) $1.value / $3.value;
113 $$.value = $1.value / $3.value; }
117 error ("division by zero in #if");
120 $$.unsignedp = $1.unsignedp || $3.unsignedp;
122 $$.value = (unsigned) $1.value % $3.value;
124 $$.value = $1.value % $3.value; }
126 { $$.value = $1.value + $3.value;
127 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
129 { $$.value = $1.value - $3.value;
130 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
132 { $$.unsignedp = $1.unsignedp;
134 $$.value = (unsigned) $1.value << $3.value;
136 $$.value = $1.value << $3.value; }
138 { $$.unsignedp = $1.unsignedp;
140 $$.value = (unsigned) $1.value >> $3.value;
142 $$.value = $1.value >> $3.value; }
144 { $$.value = ($1.value == $3.value);
147 { $$.value = ($1.value != $3.value);
151 if ($1.unsignedp || $3.unsignedp)
153 (unsigned) $1.value <= (unsigned) $3.value;
155 $$.value = $1.value <= $3.value; }
158 if ($1.unsignedp || $3.unsignedp)
160 (unsigned) $1.value >= (unsigned) $3.value;
162 $$.value = $1.value >= $3.value; }
165 if ($1.unsignedp || $3.unsignedp)
167 (unsigned) $1.value < (unsigned) $3.value;
169 $$.value = $1.value < $3.value; }
172 if ($1.unsignedp || $3.unsignedp)
174 (unsigned) $1.value > (unsigned) $3.value;
176 $$.value = $1.value > $3.value; }
178 { $$.value = $1.value & $3.value;
179 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
181 { $$.value = $1.value ^ $3.value;
182 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
184 { $$.value = $1.value | $3.value;
185 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
187 { $$.value = ($1.value && $3.value);
190 { $$.value = ($1.value || $3.value);
192 | exp '?' exp ':' exp
193 { $$.value = $1.value ? $3.value : $5.value;
194 $$.unsignedp = $3.unsignedp || $5.unsignedp; }
196 { $$ = yylval.integer; }
198 { $$ = yylval.integer; }
205 /* During parsing of a C expression, the pointer to the next character
206 is in this variable. */
208 static const char *lexptr;
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. */
214 /* maybe needs to actually deal with floating point numbers */
220 register const char *p = lexptr;
223 register int base = 10;
224 register int len = olen;
226 for (c = 0; c < len; c++)
228 /* It's a float since it contains a point. */
229 yyerror ("floating point numbers not allowed in #if expressions");
233 yylval.integer.unsignedp = 0;
235 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
246 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
248 if (c >= '0' && c <= '9') {
251 } else if (base == 16 && c >= 'a' && c <= 'f') {
255 /* `l' means long, and `u' means unsigned. */
257 if (c == 'l' || c == 'L')
259 else if (c == 'u' || c == 'U')
260 yylval.integer.unsignedp = 1;
269 /* Don't look for any more digits after the suffixes. */
275 yyerror ("Invalid number in #if expression");
279 /* If too big to be signed, consider it unsigned. */
281 yylval.integer.unsignedp = 1;
284 yylval.integer.value = n;
289 const char *operator;
297 static struct token tokentab2[] = {
309 /* Read one token, getting characters through lexptr. */
315 register int namelen;
316 register const char *tokstart;
317 register struct token *toktab;
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]) {
327 return toktab->token;
345 c = parse_escape (&lexptr);
347 /* Sign-extend the constant if chars are signed on target machine. */
349 if (lookup ((const unsigned char *)"__CHAR_UNSIGNED__",
350 sizeof ("__CHAR_UNSIGNED__")-1, -1)
351 || ((c >> (CHAR_TYPE_SIZE - 1)) & 1) == 0)
352 yylval.integer.value = c & ((1 << CHAR_TYPE_SIZE) - 1);
354 yylval.integer.value = c | ~((1 << CHAR_TYPE_SIZE) - 1);
357 yylval.integer.unsignedp = 0;
360 yyerror ("Invalid character constant in #if");
366 /* some of these chars are invalid in constant expressions;
367 maybe do something about them later */
396 yyerror ("double quoted strings not allowed in #if expressions");
399 if (c >= '0' && c <= '9') {
402 c = tokstart[namelen], is_idchar[c] || c == '.';
405 return parse_number (namelen);
408 if (!is_idstart[c]) {
409 yyerror ("Invalid token in expression");
413 /* It is a name. See how long it is. */
416 is_idchar[(int)(unsigned char)tokstart[namelen]];
425 /* Parse a C escape sequence. STRING_PTR points to a variable
426 containing a pointer to the string to parse. That pointer
427 is updated past the characters we use. The value of the
428 escape sequence is returned.
430 A negative value means the sequence \ newline was seen,
431 which is supposed to be equivalent to nothing at all.
433 If \ is followed by a null character, we return a negative
434 value and leave the string pointer pointing at the null character.
436 If \ is followed by 000, we return 0 and leave the string pointer
437 after the zeros. A value of 0 does not mean end of string. */
440 parse_escape (string_ptr)
441 const char **string_ptr;
443 register int c = *(*string_ptr)++;
455 return TARGET_NEWLINE;
468 c = *(*string_ptr)++;
470 c = parse_escape (string_ptr);
473 return (c & 0200) | (c & 037);
484 register int i = c - '0';
485 register int count = 0;
488 c = *(*string_ptr)++;
489 if (c >= '0' && c <= '7')
490 i = (i << 3) + c - '0';
497 if ((i & ~((1 << CHAR_TYPE_SIZE) - 1)) != 0)
499 i &= (1 << CHAR_TYPE_SIZE) - 1;
500 warning ("octal character constant does not fit in a byte");
509 c = *(*string_ptr)++;
510 if (c >= '0' && c <= '9')
511 i = (i << 4) + c - '0';
512 else if (c >= 'a' && c <= 'f')
513 i = (i << 4) + c - 'a' + 10;
514 else if (c >= 'A' && c <= 'F')
515 i = (i << 4) + c - 'A' + 10;
522 if ((i & ~((1 << BITS_PER_UNIT) - 1)) != 0)
524 i &= (1 << BITS_PER_UNIT) - 1;
525 warning ("hex character constant does not fit in a byte");
539 longjmp (parse_return_error, 1);
542 /* This page contains the entry point to this file. */
544 /* Parse STRING as an expression, and complain if this fails
545 to use up all of the contents of STRING. */
546 /* We do not support C comments. They should be removed before
547 this function is called. */
550 parse_c_expression (string)
555 if (lexptr == 0 || *lexptr == 0) {
556 error ("empty #if expression");
557 return 0; /* don't include the #if group */
560 /* if there is some sort of scanning error, just return 0 and assume
561 the parsing routine has printed an error message somewhere.
562 there is surely a better thing to do than this. */
563 if (setjmp (parse_return_error))
567 return 0; /* actually this is never reached
568 the way things stand. */
570 error ("Junk after end of expression.");
572 return expression_value; /* set by yyparse () */