1 /* Parse C expressions for CCCP.
2 Copyright (C) 1987, 2000, 2001 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 */
30 static int yylex PARAMS
((void));
31 static void yyerror PARAMS
((const char *msgid
)) ATTRIBUTE_NORETURN
;
33 static int parse_number PARAMS
((int));
34 static int parse_escape PARAMS
((const char **));
36 static int expression_value
;
37 static jmp_buf parse_return_error
;
39 /* During parsing of a C expression, the pointer to the next
40 character is in this variable. */
42 static const char *lexptr
;
46 struct constant
{long value
; int unsignedp
;} integer
;
51 %type
<integer
> exp exp1 start
52 %token
<integer
> INT CHAR
54 %token
<integer
> ERROR
75 { expression_value
= $1.value
; }
78 /* Expressions, including the comma operator. */
84 /* Expressions, not including the comma operator. */
85 exp
: '-' exp %prec UNARY
86 { $$.value
= - $2.value
;
87 $$.unsignedp
= $2.unsignedp
; }
89 { $$.value
= ! $2.value
;
94 { $$.value
= ~
$2.value
;
95 $$.unsignedp
= $2.unsignedp
; }
100 /* Binary operators in order of decreasing precedence. */
102 { $$.unsignedp
= $1.unsignedp ||
$3.unsignedp
;
104 $$.value
= (unsigned) $1.value
* $3.value
;
106 $$.value
= $1.value
* $3.value
; }
110 error ("division by zero in #if");
113 $$.unsignedp
= $1.unsignedp ||
$3.unsignedp
;
115 $$.value
= (unsigned) $1.value
/ $3.value
;
117 $$.value
= $1.value
/ $3.value
; }
121 error ("division by zero in #if");
124 $$.unsignedp
= $1.unsignedp ||
$3.unsignedp
;
126 $$.value
= (unsigned) $1.value %
$3.value
;
128 $$.value
= $1.value %
$3.value
; }
130 { $$.value
= $1.value
+ $3.value
;
131 $$.unsignedp
= $1.unsignedp ||
$3.unsignedp
; }
133 { $$.value
= $1.value
- $3.value
;
134 $$.unsignedp
= $1.unsignedp ||
$3.unsignedp
; }
136 { $$.unsignedp
= $1.unsignedp
;
138 $$.value
= (unsigned) $1.value
<< $3.value
;
140 $$.value
= $1.value
<< $3.value
; }
142 { $$.unsignedp
= $1.unsignedp
;
144 $$.value
= (unsigned) $1.value
>> $3.value
;
146 $$.value
= $1.value
>> $3.value
; }
148 { $$.value
= ($1.value
== $3.value
);
151 { $$.value
= ($1.value
!= $3.value
);
155 if
($1.unsignedp ||
$3.unsignedp
)
157 (unsigned) $1.value
<= (unsigned) $3.value
;
159 $$.value
= $1.value
<= $3.value
; }
162 if
($1.unsignedp ||
$3.unsignedp
)
164 (unsigned) $1.value
>= (unsigned) $3.value
;
166 $$.value
= $1.value
>= $3.value
; }
169 if
($1.unsignedp ||
$3.unsignedp
)
171 (unsigned) $1.value
< (unsigned) $3.value
;
173 $$.value
= $1.value
< $3.value
; }
176 if
($1.unsignedp ||
$3.unsignedp
)
178 (unsigned) $1.value
> (unsigned) $3.value
;
180 $$.value
= $1.value
> $3.value
; }
182 { $$.value
= $1.value
& $3.value
;
183 $$.unsignedp
= $1.unsignedp ||
$3.unsignedp
; }
185 { $$.value
= $1.value ^
$3.value
;
186 $$.unsignedp
= $1.unsignedp ||
$3.unsignedp
; }
188 { $$.value
= $1.value |
$3.value
;
189 $$.unsignedp
= $1.unsignedp ||
$3.unsignedp
; }
191 { $$.value
= ($1.value
&& $3.value
);
194 { $$.value
= ($1.value ||
$3.value
);
196 | exp
'?' exp
':' exp
197 { $$.value
= $1.value ?
$3.value
: $5.value
;
198 $$.unsignedp
= $3.unsignedp ||
$5.unsignedp
; }
200 { $$
= yylval.integer
; }
202 { $$
= yylval.integer
; }
207 test_assertion
((unsigned char **) &lexptr
); }
211 /* Take care of parsing a number (anything that starts with a digit).
212 Set yylval and return the token type; update lexptr.
213 LEN is the number of characters in it. */
215 /* maybe needs to actually deal with floating point numbers */
221 register
const char *p
= lexptr
;
224 register
int base
= 10;
225 register
int len
= olen
;
227 for
(c
= 0; c
< len
; c
++)
229 /* It's a float since it contains a point. */
230 yyerror ("floating point numbers not allowed in #if expressions");
234 yylval.integer.unsignedp
= 0;
236 if
(len
>= 3 && (!strncmp
(p
, "0x", 2) ||
!strncmp
(p
, "0X", 2))) {
247 if
(c
>= 'A' && c
<= 'Z') c
+= 'a' - 'A';
249 if
(c
>= '0' && c
<= '9') {
252 } else if
(base
== 16 && c
>= 'a' && c
<= 'f') {
256 /* `l' means long, and `u' means unsigned. */
258 if
(c
== 'l' || c
== 'L')
260 else if
(c
== 'u' || c
== 'U')
261 yylval.integer.unsignedp
= 1;
270 /* Don't look for any more digits after the suffixes. */
276 yyerror ("Invalid number in #if expression");
280 /* If too big to be signed, consider it unsigned. */
282 yylval.integer.unsignedp
= 1;
285 yylval.integer.value
= n
;
290 const char *operator
;
298 static struct token tokentab2
[] = {
310 /* Read one token, getting characters through lexptr. */
316 register
int namelen
;
317 register
const char *tokstart
;
318 register
struct token
*toktab
;
324 /* See if it is a special token of length 2. */
325 for
(toktab
= tokentab2
; toktab
->operator
!= NULL
; toktab
++)
326 if
(c
== *toktab
->operator
&& tokstart
[1] == toktab
->operator
[1]) {
328 return toktab
->token
;
346 c
= parse_escape
(&lexptr
);
348 /* Sign-extend the constant if chars are signed on target machine. */
350 if
(lookup
((const unsigned char *)"__CHAR_UNSIGNED__",
351 sizeof
("__CHAR_UNSIGNED__")-1, -1)
352 ||
((c
>> (CHAR_TYPE_SIZE
- 1)) & 1) == 0)
353 yylval.integer.value
= c
& ((1 << CHAR_TYPE_SIZE
) - 1);
355 yylval.integer.value
= c | ~
((1 << CHAR_TYPE_SIZE
) - 1);
358 yylval.integer.unsignedp
= 0;
361 yyerror ("Invalid character constant in #if");
367 /* some of these chars are invalid in constant expressions;
368 maybe do something about them later */
398 yyerror ("double quoted strings not allowed in #if expressions");
401 if
(c
>= '0' && c
<= '9') {
404 c
= tokstart
[namelen
], is_idchar
(c
) || c
== '.';
407 return parse_number
(namelen
);
410 if
(!is_idstart
(c
)) {
411 yyerror ("Invalid token in expression");
415 /* It is a name. See how long it is. */
418 is_idchar
(tokstart
[namelen
]);
427 /* Parse a C escape sequence. STRING_PTR points to a variable
428 containing a pointer to the string to parse. That pointer
429 is updated past the characters we use. The value of the
430 escape sequence is returned.
432 A negative value means the sequence \ newline was seen,
433 which is supposed to be equivalent to nothing at all.
435 If \ is followed by a null character, we return a negative
436 value and leave the string pointer pointing at the null character.
438 If \ is followed by 000, we return 0 and leave the string pointer
439 after the zeros. A value of 0 does not mean end of string. */
442 parse_escape
(string_ptr
)
443 const char **string_ptr
;
445 register
int c
= *(*string_ptr
)++;
457 return TARGET_NEWLINE
;
470 c
= *(*string_ptr
)++;
472 c
= parse_escape
(string_ptr
);
475 return
(c
& 0200) |
(c
& 037);
486 register
int i
= c
- '0';
487 register
int count
= 0;
490 c
= *(*string_ptr
)++;
491 if
(c
>= '0' && c
<= '7')
492 i
= (i
<< 3) + c
- '0';
499 if
((i
& ~
((1 << CHAR_TYPE_SIZE
) - 1)) != 0)
501 i
&= (1 << CHAR_TYPE_SIZE
) - 1;
502 warning
("octal character constant does not fit in a byte");
511 c
= *(*string_ptr
)++;
512 if
(c
>= '0' && c
<= '9')
513 i
= (i
<< 4) + c
- '0';
514 else if
(c
>= 'a' && c
<= 'f')
515 i
= (i
<< 4) + c
- 'a' + 10;
516 else if
(c
>= 'A' && c
<= 'F')
517 i
= (i
<< 4) + c
- 'A' + 10;
524 if
((i
& ~
((1 << BITS_PER_UNIT
) - 1)) != 0)
526 i
&= (1 << BITS_PER_UNIT
) - 1;
527 warning
("hex character constant does not fit in a byte");
541 longjmp
(parse_return_error
, 1);
544 /* This page contains the entry point to this file. */
546 /* Parse STRING as an expression, and complain if this fails
547 to use up all of the contents of STRING. */
548 /* We do not support C comments. They should be removed before
549 this function is called. */
552 parse_c_expression
(string)
557 if
(lexptr
== 0 ||
*lexptr
== 0) {
558 error ("empty #if expression");
559 return
0; /* don't include the #if group */
562 /* if there is some sort of scanning error, just return 0 and assume
563 the parsing routine has printed an error message somewhere.
564 there is surely a better thing to do than this. */
565 if
(setjmp
(parse_return_error
))
569 return
0; /* actually this is never reached
570 the way things stand. */
572 error ("Junk after end of expression.");
574 return expression_value
; /* set by yyparse () */