/* Parse C expressions for CCCP.
Copyright (C) 1987 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 1, 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 */
static jmp_buf parse_return_error;
/* some external tables of character types */
extern unsigned char is_idstart[], is_idchar[];
#define CHAR_TYPE_SIZE BITS_PER_UNIT
struct constant {long value; int unsignedp;} integer;
%type <integer> exp exp1 start
%token <integer> INT CHAR
{ expression_value = $1.value; }
/* Expressions, including the comma operator. */
/* Expressions, not including the comma operator. */
exp : '-' exp %prec UNARY
$$.unsignedp = $2.unsignedp; }
$$.unsignedp = $2.unsignedp; }
/* Binary operators in order of decreasing precedence. */
{ $$.unsignedp = $1.unsignedp || $3.unsignedp;
$$.value = (unsigned) $1.value * $3.value;
$$.value = $1.value * $3.value; }
error ("division by zero in #if");
$$.unsignedp = $1.unsignedp || $3.unsignedp;
$$.value = (unsigned) $1.value / $3.value;
$$.value = $1.value / $3.value; }
error ("division by zero in #if");
$$.unsignedp = $1.unsignedp || $3.unsignedp;
$$.value = (unsigned) $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; }
{ $$.unsignedp = $1.unsignedp;
$$.value = (unsigned) $1.value << $3.value;
$$.value = $1.value << $3.value; }
{ $$.unsignedp = $1.unsignedp;
$$.value = (unsigned) $1.value >> $3.value;
$$.value = $1.value >> $3.value; }
{ $$.value = ($1.value == $3.value);
{ $$.value = ($1.value != $3.value);
if ($1.unsignedp || $3.unsignedp)
$$.value = (unsigned) $1.value <= $3.value;
$$.value = $1.value <= $3.value; }
if ($1.unsignedp || $3.unsignedp)
$$.value = (unsigned) $1.value >= $3.value;
$$.value = $1.value >= $3.value; }
if ($1.unsignedp || $3.unsignedp)
$$.value = (unsigned) $1.value < $3.value;
$$.value = $1.value < $3.value; }
if ($1.unsignedp || $3.unsignedp)
$$.value = (unsigned) $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; }
/* 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;
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))) {
if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
if (c >= '0' && c <= '9') {
} else if (base == 16 && c >= 'a' && c <= 'f') {
/* `l' means long, and `u' means unsigned. */
if (c == 'l' || c == 'L')
else if (c == 'u' || c == 'U')
yylval.integer.unsignedp = 1;
/* Don't look for any more digits after the suffixes. */
yyerror ("Invalid number in #if expression");
/* If too big to be signed, consider it unsigned. */
yylval.integer.unsignedp = 1;
yylval.integer.value = n;
static struct token tokentab2[] = {
/* Read one token, getting characters through lexptr. */
register struct token *toktab;
/* 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]) {
c = parse_escape (&lexptr);
/* Sign-extend the constant if chars are signed on target machine. */
if (lookup ("__CHAR_UNSIGNED__", sizeof ("__CHAR_UNSIGNED__")-1, -1)
|| ((c >> (CHAR_TYPE_SIZE - 1)) & 1) == 0)
yylval.integer.value = c & ((1 << CHAR_TYPE_SIZE) - 1);
yylval.integer.value = c | ~((1 << CHAR_TYPE_SIZE) - 1);
yylval.integer.unsignedp = 0;
yyerror ("Invalid character constant in #if");
/* some of these chars are invalid in constant expressions;
maybe do something about them later */
yyerror ("double quoted strings not allowed in #if expressions");
if (c >= '0' && c <= '9') {
c = tokstart[namelen], is_idchar[c] || c == '.';
return parse_number (namelen);
yyerror ("Invalid token in expression");
/* It is a name. See how long it is. */
for (namelen = 0; is_idchar[tokstart[namelen]]; 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)++;
c = parse_escape (string_ptr);
return (c & 0200) | (c & 037);
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");
if (c >= '0' && c <= '9')
else if (c >= 'a' && c <= 'f')
i = (i << 4) + c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
i = (i << 4) + c - 'A' + 10;
if ((i & ~((1 << BITS_PER_UNIT) - 1)) != 0)
i &= (1 << BITS_PER_UNIT) - 1;
warning ("hex character constant does not fit in a byte");
longjmp (parse_return_error, 1);
/* 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);