/* Parse C expressions for CCCP.
Copyright (C) 1987, 1992 Free Software Foundation.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding!
Adapted from expread.y of GDB by Paul Rubin, July 1986. */
/* Parse a C expression from text in a string */
typedef unsigned char U_CHAR;
/* This is used for communicating lists of keywords with cccp.c. */
/* Define a generic NULL if one hasn't already been defined. */
#if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
#define GENERIC_PTR void *
#define GENERIC_PTR char *
#define NULL_PTR ((GENERIC_PTR)0)
static jmp_buf parse_return_error;
/* Nonzero means count most punctuation as part of a name. */
static int keyword_parsing = 0;
/* some external tables of character types */
extern unsigned char is_idstart[], is_idchar[], is_hor_space[];
/* Flag for -pedantic. */
/* Flag for -traditional. */
#define CHAR_TYPE_SIZE BITS_PER_UNIT
#define INT_TYPE_SIZE BITS_PER_WORD
#define LONG_TYPE_SIZE BITS_PER_WORD
#define WCHAR_TYPE_SIZE INT_TYPE_SIZE
/* Yield nonzero if adding two numbers with A's and B's signs can yield a
number with SUM's sign, where A, B, and SUM are all C integers. */
#define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
static void integer_overflow ();
static long left_shift ();
static long right_shift ();
struct constant {long value; int unsignedp;} integer;
struct name {U_CHAR *address; int length;} name;
struct arglist *keywords;
%type <integer> exp exp1 start
%type <keywords> keywords
%token <integer> INT CHAR
{ expression_value = $1.value; }
/* Expressions, including the comma operator. */
pedwarn ("comma operator in operand of `#if'");
/* Expressions, not including the comma operator. */
exp : '-' exp %prec UNARY
if (($$.value & $2.value) < 0 && ! $2.unsignedp)
$$.unsignedp = $2.unsignedp; }
$$.unsignedp = $2.unsignedp; }
{ $$.value = check_assertion ($2.address, $2.length,
{ $$.value = check_assertion ($2.address, $2.length,
/* Binary operators in order of decreasing precedence. */
{ $$.unsignedp = $1.unsignedp || $3.unsignedp;
$$.value = (unsigned long) $1.value * $3.value;
$$.value = $1.value * $3.value;
&& ($$.value / $1.value != $3.value
|| ($$.value & $1.value & $3.value) < 0))
error ("division by zero in #if");
$$.unsignedp = $1.unsignedp || $3.unsignedp;
$$.value = (unsigned long) $1.value / $3.value;
$$.value = $1.value / $3.value;
if (($$.value & $1.value & $3.value) < 0)
error ("division by zero in #if");
$$.unsignedp = $1.unsignedp || $3.unsignedp;
$$.value = (unsigned long) $1.value % $3.value;
$$.value = $1.value % $3.value; }
{ $$.value = $1.value + $3.value;
$$.unsignedp = $1.unsignedp || $3.unsignedp;
&& ! possible_sum_sign ($1.value, $3.value,
{ $$.value = $1.value - $3.value;
$$.unsignedp = $1.unsignedp || $3.unsignedp;
&& ! possible_sum_sign ($$.value, $3.value,
{ $$.unsignedp = $1.unsignedp;
if ($3.value < 0 && ! $3.unsignedp)
$$.value = right_shift (&$1, -$3.value);
$$.value = left_shift (&$1, $3.value); }
{ $$.unsignedp = $1.unsignedp;
if ($3.value < 0 && ! $3.unsignedp)
$$.value = left_shift (&$1, -$3.value);
$$.value = right_shift (&$1, $3.value); }
{ $$.value = ($1.value == $3.value);
{ $$.value = ($1.value != $3.value);
if ($1.unsignedp || $3.unsignedp)
$$.value = (unsigned long) $1.value <= $3.value;
$$.value = $1.value <= $3.value; }
if ($1.unsignedp || $3.unsignedp)
$$.value = (unsigned long) $1.value >= $3.value;
$$.value = $1.value >= $3.value; }
if ($1.unsignedp || $3.unsignedp)
$$.value = (unsigned long) $1.value < $3.value;
$$.value = $1.value < $3.value; }
if ($1.unsignedp || $3.unsignedp)
$$.value = (unsigned long) $1.value > $3.value;
$$.value = $1.value > $3.value; }
{ $$.value = $1.value & $3.value;
$$.unsignedp = $1.unsignedp || $3.unsignedp; }
{ $$.value = $1.value ^ $3.value;
$$.unsignedp = $1.unsignedp || $3.unsignedp; }
{ $$.value = $1.value | $3.value;
$$.unsignedp = $1.unsignedp || $3.unsignedp; }
{ $$.value = ($1.value && $3.value);
{ $$.value = ($1.value || $3.value);
{ $$.value = $1.value ? $3.value : $5.value;
$$.unsignedp = $3.unsignedp || $5.unsignedp; }
| '(' keywords ')' keywords
$$ = (struct arglist *) xmalloc (sizeof (struct arglist));
$$->name = (U_CHAR *) "(";
while (temp != 0 && temp->next != 0)
temp->next = (struct arglist *) xmalloc (sizeof (struct arglist));
temp->next->name = (U_CHAR *) ")";
temp->next->length = 1; }
{ $$ = (struct arglist *) xmalloc (sizeof (struct arglist));
/* During parsing of a C expression, the pointer to the next character
/* Take care of parsing a number (anything that starts with a digit).
Set yylval and return the token type; update lexptr.
LEN is the number of characters in it. */
/* maybe needs to actually deal with floating point numbers */
register char *p = lexptr;
register unsigned long n = 0, nd, ULONG_MAX_over_base;
register int overflow = 0;
register int digit, largest_digit = 0;
for (c = 0; c < len; c++)
/* It's a float since it contains a point. */
yyerror ("floating point numbers not allowed in #if expressions");
yylval.integer.unsignedp = 0;
if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
ULONG_MAX_over_base = (unsigned long) -1 / base;
if (c >= '0' && c <= '9')
else if (base == 16 && c >= 'a' && c <= 'f')
else if (base == 16 && c >= 'A' && c <= 'F')
/* `l' means long, and `u' means unsigned. */
if (c == 'l' || c == 'L')
yyerror ("two `l's in integer constant");
else if (c == 'u' || c == 'U')
if (yylval.integer.unsignedp)
yyerror ("two `u's in integer constant");
yylval.integer.unsignedp = 1;
/* Don't look for any more digits after the suffixes. */
if (largest_digit < digit)
overflow |= ULONG_MAX_over_base < n | nd < n;
yyerror ("Invalid number in #if expression");
if (base <= largest_digit)
warning ("integer constant contains digits beyond the radix");
warning ("integer constant out of range");
/* If too big to be signed, consider it unsigned. */
if ((long) n < 0 && ! yylval.integer.unsignedp)
warning ("integer constant is so large that it is unsigned");
yylval.integer.unsignedp = 1;
yylval.integer.value = n;
static struct token tokentab2[] = {
/* Read one token, getting characters through lexptr. */
register unsigned char *tokstart;
register struct token *toktab;
tokstart = (unsigned char *) lexptr;
/* See if it is a special token of length 2. */
for (toktab = tokentab2; toktab->operator != NULL; toktab++)
if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
if (toktab->token == ERROR)
char *buf = (char *) alloca (40);
sprintf (buf, "`%s' not allowed in operand of `#if'", toktab->operator);
/* Capital L may start a wide-string or wide-character constant. */
char *start_ptr = lexptr - 1;
c = parse_escape (&lexptr);
yylval.name.address = tokstart;
yylval.name.length = lexptr - start_ptr;
/* This code for reading a character constant
handles multicharacter constants and wide characters.
It is mostly copied from c-lex.c. */
unsigned width = CHAR_TYPE_SIZE;
max_chars = LONG_TYPE_SIZE / width;
token_buffer = (char *) alloca (max_chars + 1);
if (c == '\'' || c == EOF)
c = parse_escape (&lexptr);
if (width < HOST_BITS_PER_INT
&& (unsigned) c >= (1 << width))
pedwarn ("escape sequence out of range for character");
/* Merge character into result; ignore excess chars. */
if (num_chars < max_chars + 1)
if (width < HOST_BITS_PER_INT)
result = (result << width) | (c & ((1 << width) - 1));
token_buffer[num_chars - 1] = c;
token_buffer[num_chars] = 0;
error ("malformatted character constant");
error ("empty character constant");
else if (num_chars > max_chars)
error ("character constant too long");
else if (num_chars != 1 && ! traditional)
warning ("multi-character character constant");
/* If char type is signed, sign-extend the constant. */
int num_bits = num_chars * width;
if (lookup ("__CHAR_UNSIGNED__", sizeof ("__CHAR_UNSIGNED__")-1, -1)
|| ((result >> (num_bits - 1)) & 1) == 0)
= result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
= result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
/* Set the initial shift state and convert the next sequence. */
/* In all locales L'\0' is zero and mbtowc will return zero,
|| (num_chars == 1 && token_buffer[0] != '\0'))
(void) mbtowc (NULL_PTR, NULL_PTR, 0);
if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
warning ("Ignoring invalid multibyte character");
yylval.integer.value = result;
/* This is always a signed type. */
yylval.integer.unsignedp = 0;
/* some of these chars are invalid in constant expressions;
maybe do something about them later */
char *start_ptr = lexptr;
c = parse_escape (&lexptr);
yylval.name.address = tokstart;
yylval.name.length = lexptr - start_ptr;
yyerror ("string constants not allowed in #if expressions");
if (c >= '0' && c <= '9' && !keyword_parsing) {
c = tokstart[namelen], is_idchar[c] || c == '.';
return parse_number (namelen);
/* It is a name. See how long it is. */
for (namelen = 0;; namelen++) {
if (is_hor_space[tokstart[namelen]])
if (tokstart[namelen] == '(' || tokstart[namelen] == ')')
if (tokstart[namelen] == '"' || tokstart[namelen] == '\'')
yyerror ("Invalid token in expression");
for (namelen = 0; is_idchar[tokstart[namelen]]; namelen++)
yylval.name.address = tokstart;
yylval.name.length = namelen;
/* Parse a C escape sequence. STRING_PTR points to a variable
containing a pointer to the string to parse. That pointer
is updated past the characters we use. The value of the
escape sequence is returned.
A negative value means the sequence \ newline was seen,
which is supposed to be equivalent to nothing at all.
If \ is followed by a null character, we return a negative
value and leave the string pointer pointing at the null character.
If \ is followed by 000, we return 0 and leave the string pointer
after the zeros. A value of 0 does not mean end of string. */
parse_escape (string_ptr)
register int c = *(*string_ptr)++;
pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
register int i = c - '0';
if (c >= '0' && c <= '7')
if ((i & ~((1 << CHAR_TYPE_SIZE) - 1)) != 0)
i &= (1 << CHAR_TYPE_SIZE) - 1;
warning ("octal character constant does not fit in a byte");
register unsigned i = 0, overflow = 0, digits_found = 0, digit;
if (c >= '0' && c <= '9')
else if (c >= 'a' && c <= 'f')
else if (c >= 'A' && c <= 'F')
overflow |= i ^ (i << 4 >> 4);
yyerror ("\\x used with no following hex digits");
if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
i &= (1 << BITS_PER_UNIT) - 1;
warning ("hex character constant does not fit in a byte");
longjmp (parse_return_error, 1);
pedwarn ("integer overflow in preprocessor expression");
if (b >= HOST_BITS_PER_LONG)
if (! a->unsignedp && a->value != 0)
return (unsigned long) a->value << b;
if (b >= HOST_BITS_PER_LONG)
return a->unsignedp ? 0 : a->value >> (HOST_BITS_PER_LONG - 1);
return (unsigned long) a->value >> b;
/* This page contains the entry point to this file. */
/* Parse STRING as an expression, and complain if this fails
to use up all of the contents of STRING. */
/* We do not support C comments. They should be removed before
this function is called. */
parse_c_expression (string)
if (lexptr == 0 || *lexptr == 0) {
error ("empty #if expression");
return 0; /* don't include the #if group */
/* if there is some sort of scanning error, just return 0 and assume
the parsing routine has printed an error message somewhere.
there is surely a better thing to do than this. */
if (setjmp (parse_return_error))
return 0; /* actually this is never reached
error ("Junk after end of expression.");
return expression_value; /* set by yyparse () */
/* Main program for testing purposes. */
initialize_random_junk ();
printf ("enter expression: ");
while ((buf[n] = getchar ()) != '\n' && buf[n] != EOF)
printf ("parser returned %d\n", parse_c_expression (buf));
/* table to tell if char can be part of a C identifier. */
unsigned char is_idchar[256];
/* table to tell if char can be first char of a c identifier. */
unsigned char is_idstart[256];
/* table to tell if c is horizontal space. isspace () thinks that
newline is space; this is not a good idea for this program. */
* initialize random junk in the hash table and maybe other places
initialize_random_junk ()
* Set up is_idchar and is_idstart tables. These should be
* faster than saying (is_alpha (c) || c == '_'), etc.
* Must do set up these things before calling any routines tthat
for (i = 'a'; i <= 'z'; i++) {
++is_idchar[i - 'a' + 'A'];
++is_idstart[i - 'a' + 'A'];
for (i = '0'; i <= '9'; i++)
#if DOLLARS_IN_IDENTIFIERS
/* horizontal space table */
printf ("error: %s\n", msg);
printf ("warning: %s\n", msg);
return (DEFAULT_SIGNED_CHAR) ? 0 : ((struct hashnode *) -1);