Annotation of gcc/cexp.y, revision 1.1.1.5

1.1       root        1: /* Parse C expressions for CCCP.
                      2:    Copyright (C) 1987 Free Software Foundation.
                      3: 
                      4:                       NO WARRANTY
                      5: 
                      6:   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
                      7: NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
                      8: WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
                      9: RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
                     10: WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
                     11: BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
                     12: FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
                     13: AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
                     14: DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
                     15: CORRECTION.
                     16: 
                     17:  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
                     18: STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
                     19: WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
                     20: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
                     21: OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
                     22: USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
                     23: DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
                     24: A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
                     25: PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
                     26: DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
                     27: 
                     28:                GENERAL PUBLIC LICENSE TO COPY
                     29: 
                     30:   1. You may copy and distribute verbatim copies of this source file
                     31: as you receive it, in any medium, provided that you conspicuously and
                     32: appropriately publish on each copy a valid copyright notice "Copyright
                     33: (C) 1987 Free Software Foundation"; and include following the
                     34: copyright notice a verbatim copy of the above disclaimer of warranty
                     35: and of this License.  You may charge a distribution fee for the
                     36: physical act of transferring a copy.
                     37: 
                     38:   2. You may modify your copy or copies of this source file or
                     39: any portion of it, and copy and distribute such modifications under
                     40: the terms of Paragraph 1 above, provided that you also do the following:
                     41: 
                     42:     a) cause the modified files to carry prominent notices stating
                     43:     that you changed the files and the date of any change; and
                     44: 
                     45:     b) cause the whole of any work that you distribute or publish,
                     46:     that in whole or in part contains or is a derivative of this
                     47:     program or any part thereof, to be licensed at no charge to all
                     48:     third parties on terms identical to those contained in this
1.1.1.2   root       49:     License Agreement (except that you may choose to grant more extensive
                     50:     warranty protection to some or all third parties, at your option).
1.1       root       51: 
                     52:     c) You may charge a distribution fee for the physical act of
                     53:     transferring a copy, and you may at your option offer warranty
                     54:     protection in exchange for a fee.
                     55: 
1.1.1.2   root       56: Mere aggregation of another unrelated program with this program (or its
                     57: derivative) on a volume of a storage or distribution medium does not bring
                     58: the other program under the scope of these terms.
                     59: 
                     60:   3. You may copy and distribute this program (or a portion or derivative
                     61: of it, under Paragraph 2) in object code or executable form under the terms
                     62: of Paragraphs 1 and 2 above provided that you also do one of the following:
                     63: 
                     64:     a) accompany it with the complete corresponding machine-readable
                     65:     source code, which must be distributed under the terms of
                     66:     Paragraphs 1 and 2 above; or,
                     67: 
                     68:     b) accompany it with a written offer, valid for at least three
                     69:     years, to give any third party free (except for a nominal
                     70:     shipping charge) a complete machine-readable copy of the
                     71:     corresponding source code, to be distributed under the terms of
                     72:     Paragraphs 1 and 2 above; or,
                     73: 
                     74:     c) accompany it with the information you received as to where the
                     75:     corresponding source code may be obtained.  (This alternative is
                     76:     allowed only for noncommercial distribution and only if you
                     77:     received the program in object code or executable form alone.)
                     78: 
                     79: For an executable file, complete source code means all the source code for
                     80: all modules it contains; but, as a special exception, it need not include
                     81: source code for modules which are standard libraries that accompany the
                     82: operating system on which the executable file runs.
1.1       root       83: 
                     84:   4. You may not copy, sublicense, distribute or transfer this program
                     85: except as expressly provided under this License Agreement.  Any attempt
                     86: otherwise to copy, sublicense, distribute or transfer this program is void and
                     87: your rights to use the program under this License agreement shall be
                     88: automatically terminated.  However, parties who have received computer
                     89: software programs from you with this License Agreement will not have
                     90: their licenses terminated so long as such parties remain in full compliance.
                     91: 
                     92:   5. If you wish to incorporate parts of this program into other free
                     93: programs whose distribution conditions are different, write to the Free
1.1.1.2   root       94: Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
1.1       root       95: worked out a simple rule that can be stated here, but we will often permit
                     96: this.  We will be guided by the two goals of preserving the free status of
1.1.1.2   root       97: all derivatives of our free software and of promoting the sharing and reuse of
1.1       root       98: software.
                     99: 
                    100: 
                    101:  In other words, you are welcome to use, share and improve this program.
                    102:  You are forbidden to forbid anyone else to use, share and improve
                    103:  what you give them.   Help stamp out software-hoarding!
                    104: 
                    105:  Adapted from expread.y of GDB by Paul Rubin, July 1986.
                    106: 
                    107: /* Parse a C expression from text in a string  */
                    108:    
                    109: %{
1.1.1.3   root      110: #include "config.h"
1.1       root      111: #include <setjmp.h>
                    112: /* #define YYDEBUG 1 */
                    113: 
1.1.1.4   root      114:   int yylex ();
                    115:   void yyerror ();
1.1       root      116:   int expression_value;
                    117: 
                    118:   static jmp_buf parse_return_error;
                    119: 
                    120:   /* some external tables of character types */
                    121:   extern unsigned char is_idstart[], is_idchar[];
                    122: 
                    123: %}
                    124: 
                    125: %union {
                    126:   long lval;
                    127:   int voidval;
                    128:   char *sval;
                    129: }
                    130: 
                    131: %type <lval> exp exp1 start
                    132: %token <lval> INT CHAR
                    133: %token <sval> NAME
                    134: %token <lval> ERROR
                    135: 
1.1.1.2   root      136: %right '?' ':'
1.1       root      137: %left ','
                    138: %left OR
                    139: %left AND
                    140: %left '|'
                    141: %left '^'
                    142: %left '&'
                    143: %left EQUAL NOTEQUAL
                    144: %left '<' '>' LEQ GEQ
                    145: %left LSH RSH
                    146: %left '+' '-'
                    147: %left '*' '/' '%'
                    148: %right UNARY
1.1.1.2   root      149: 
                    150: /* %expect 40 */
1.1       root      151: 
                    152: %%
                    153: 
                    154: start   :      exp1
                    155:                { expression_value = $1; }
                    156:        ;
                    157: 
                    158: /* Expressions, including the comma operator.  */
                    159: exp1   :       exp
                    160:        |       exp1 ',' exp
                    161:                        { $$ = $3; }
                    162:        ;
                    163: 
                    164: /* Expressions, not including the comma operator.  */
                    165: exp    :       '-' exp    %prec UNARY
                    166:                        { $$ = - $2; }
                    167:        |       '!' exp    %prec UNARY
                    168:                        { $$ = ! $2; }
                    169:        |       '~' exp    %prec UNARY
                    170:                        { $$ = ~ $2; }
                    171:        |       '(' exp1 ')'
                    172:                        { $$ = $2; }
                    173:        ;
                    174: 
                    175: /* Binary operators in order of decreasing precedence.  */
                    176: exp    :       exp '*' exp
                    177:                        { $$ = $1 * $3; }
                    178:        |       exp '/' exp
1.1.1.5 ! root      179:                        { if ($3 == 0)
        !           180:                            {
        !           181:                              error ("division by zero in #if");
        !           182:                              $3 = 1;
        !           183:                            }
        !           184:                          $$ = $1 / $3; }
1.1       root      185:        |       exp '%' exp
1.1.1.5 ! root      186:                        { if ($3 == 0)
        !           187:                            {
        !           188:                              error ("division by zero in #if");
        !           189:                              $3 = 1;
        !           190:                            }
        !           191:                          $$ = $1 % $3; }
1.1       root      192:        |       exp '+' exp
                    193:                        { $$ = $1 + $3; }
                    194:        |       exp '-' exp
                    195:                        { $$ = $1 - $3; }
                    196:        |       exp LSH exp
                    197:                        { $$ = $1 << $3; }
                    198:        |       exp RSH exp
                    199:                        { $$ = $1 >> $3; }
                    200:        |       exp EQUAL exp
                    201:                        { $$ = ($1 == $3); }
                    202:        |       exp NOTEQUAL exp
                    203:                        { $$ = ($1 != $3); }
                    204:        |       exp LEQ exp
                    205:                        { $$ = ($1 <= $3); }
                    206:        |       exp GEQ exp
                    207:                        { $$ = ($1 >= $3); }
                    208:        |       exp '<' exp
                    209:                        { $$ = ($1 < $3); }
                    210:        |       exp '>' exp
                    211:                        { $$ = ($1 > $3); }
                    212:        |       exp '&' exp
                    213:                        { $$ = ($1 & $3); }
                    214:        |       exp '^' exp
                    215:                        { $$ = ($1 ^ $3); }
                    216:        |       exp '|' exp
                    217:                        { $$ = ($1 | $3); }
                    218:        |       exp AND exp
                    219:                        { $$ = ($1 && $3); }
                    220:        |       exp OR exp
                    221:                        { $$ = ($1 || $3); }
                    222:        |       exp '?' exp ':' exp
                    223:                        { $$ = $1 ? $3 : $5; }
                    224:        |       INT
                    225:                        { $$ = yylval.lval; }
                    226:        |       CHAR
                    227:                        { $$ = yylval.lval; }
                    228:        |       NAME
                    229:                        { $$ = 0; }
                    230:        ;
                    231: %%
                    232: 
                    233: /* During parsing of a C expression, the pointer to the next character
                    234:    is in this variable.  */
                    235: 
                    236: static char *lexptr;
                    237: 
                    238: /* Take care of parsing a number (anything that starts with a digit).
                    239:    Set yylval and return the token type; update lexptr.
                    240:    LEN is the number of characters in it.  */
                    241: 
                    242: /* maybe needs to actually deal with floating point numbers */
                    243: 
1.1.1.4   root      244: int
1.1       root      245: parse_number (olen)
                    246:      int olen;
                    247: {
                    248:   register char *p = lexptr;
                    249:   register long n = 0;
                    250:   register int c;
                    251:   register int base = 10;
                    252:   register len = olen;
                    253: 
                    254:   extern double atof ();
                    255: 
                    256:   for (c = 0; c < len; c++)
                    257:     if (p[c] == '.') {
                    258:       /* It's a float since it contains a point.  */
                    259:       yyerror ("floating point numbers not allowed in #if expressions");
                    260:       return ERROR;
                    261:       
                    262: /* ****************
                    263:         yylval.dval = atof (p);
                    264:         lexptr += len;
                    265:         return FLOAT;
                    266:                 ****************  */
                    267:     }
                    268:   
                    269:   if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
                    270:     p += 2;
                    271:     base = 16;
                    272:     len -= 2;
                    273:   }
                    274:   else if (*p == '0')
                    275:     base = 8;
                    276:   
                    277:   while (len-- > 0) {
                    278:     c = *p++;
                    279:     n *= base;
                    280:     if (c >= '0' && c <= '9')
                    281:       n += c - '0';
                    282:     else {
                    283:       if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
                    284:       if (base == 16 && c >= 'a' && c <= 'f')
                    285:        n += c - 'a' + 10;
                    286:       else if (len == 0 && c == 'l')
                    287:        ;
                    288:       else {
                    289:        yyerror ("Invalid number in #if expression");
                    290:        return ERROR;
                    291:       }
                    292:     }
                    293:   }
                    294: 
                    295:   lexptr = p;
                    296:   yylval.lval = n;
                    297:   return INT;
                    298: }
                    299: 
                    300: struct token {
                    301:   char *operator;
                    302:   int token;
                    303: };
                    304: 
                    305: #define NULL 0
                    306: 
                    307: static struct token tokentab2[] = {
                    308:   {"&&", AND},
                    309:   {"||", OR},
                    310:   {"<<", LSH},
                    311:   {">>", RSH},
                    312:   {"==", EQUAL},
                    313:   {"!=", NOTEQUAL},
                    314:   {"<=", LEQ},
                    315:   {">=", GEQ},
                    316:   {NULL, ERROR}
                    317: };
                    318: 
                    319: /* Read one token, getting characters through lexptr.  */
                    320: 
1.1.1.4   root      321: int
1.1       root      322: yylex ()
                    323: {
                    324:   register int c;
                    325:   register int namelen;
                    326:   register char *tokstart;
                    327:   register struct token *toktab;
                    328: 
                    329:  retry:
                    330: 
                    331:   tokstart = lexptr;
                    332:   c = *tokstart;
                    333:   /* See if it is a special token of length 2.  */
                    334:   for (toktab = tokentab2; toktab->operator != NULL; toktab++)
                    335:     if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
                    336:       lexptr += 2;
                    337:       return toktab->token;
                    338:     }
                    339: 
                    340:   switch (c) {
                    341:   case 0:
                    342:     return 0;
                    343:     
                    344:   case ' ':
                    345:   case '\t':
                    346:   case '\n':
                    347:     lexptr++;
                    348:     goto retry;
                    349:     
                    350:   case '\'':
                    351:     lexptr++;
                    352:     c = *lexptr++;
                    353:     if (c == '\\')
                    354:       c = parse_escape (&lexptr);
                    355:     yylval.lval = c;
                    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':
                    443:       return '\a';
                    444:     case 'b':
                    445:       return '\b';
                    446:     case 'e':
                    447:       return 033;
                    448:     case 'f':
                    449:       return '\f';
                    450:     case 'n':
                    451:       return '\n';
                    452:     case 'r':
                    453:       return '\r';
                    454:     case 't':
                    455:       return '\t';
                    456:     case 'v':
                    457:       return '\v';
                    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:          {
                    484:            if ((c = *(*string_ptr)++) >= '0' && c <= '7')
                    485:              {
                    486:                i *= 8;
                    487:                i += c - '0';
                    488:              }
                    489:            else
                    490:              {
                    491:                (*string_ptr)--;
                    492:                break;
                    493:              }
                    494:          }
                    495:        return i;
                    496:       }
                    497:     default:
                    498:       return c;
                    499:     }
                    500: }
                    501: 
1.1.1.4   root      502: void
1.1       root      503: yyerror (s)
                    504:      char *s;
                    505: {
                    506:   error (s);
                    507:   longjmp (parse_return_error, 1);
                    508: }
                    509: 
                    510: /* This page contains the entry point to this file.  */
                    511: 
                    512: /* Parse STRING as an expression, and complain if this fails
                    513:    to use up all of the contents of STRING.  */
1.1.1.2   root      514: /* We do not support C comments.  They should be removed before
                    515:    this function is called.  */
                    516: 
1.1       root      517: int
                    518: parse_c_expression (string)
                    519:      char *string;
                    520: {
                    521:   lexptr = string;
                    522:   
                    523:   if (lexptr == 0 || *lexptr == 0) {
                    524:     error ("empty #if expression");
                    525:     return 0;                  /* don't include the #if group */
                    526:   }
                    527: 
                    528:   /* if there is some sort of scanning error, just return 0 and assume
                    529:      the parsing routine has printed an error message somewhere.
                    530:      there is surely a better thing to do than this.     */
                    531:   if (setjmp(parse_return_error))
                    532:     return 0;
                    533: 
                    534:   if (yyparse ())
                    535:     return 0;                  /* actually this is never reached
                    536:                                   the way things stand. */
                    537:   if (*lexptr)
                    538:     error ("Junk after end of expression.");
                    539: 
                    540:   return expression_value;     /* set by yyparse() */
                    541: }
                    542: 
                    543: #ifdef TEST_EXP_READER
                    544: /* main program, for testing purposes. */
                    545: main()
                    546: {
                    547:   int n;
                    548:   char buf[1024];
                    549:   extern int yydebug;
                    550: /*
                    551:   yydebug = 1;
                    552: */
                    553:   initialize_random_junk ();
                    554: 
                    555:   for (;;) {
                    556:     printf("enter expression: ");
                    557:     n = 0;
                    558:     while ((buf[n] = getchar()) != '\n')
                    559:       n++;
                    560:     buf[n] = '\0';
                    561:     printf("parser returned %d\n", parse_c_expression(buf));
                    562:   }
                    563: }
                    564: 
                    565: /* table to tell if char can be part of a C identifier. */
                    566: char is_idchar[256];
                    567: /* table to tell if char can be first char of a c identifier. */
                    568: char is_idstart[256];
                    569: /* table to tell if c is horizontal space.  isspace() thinks that
                    570:    newline is space; this is not a good idea for this program. */
                    571: char is_hor_space[256];
                    572: 
                    573: /*
                    574:  * initialize random junk in the hash table and maybe other places
                    575:  */
                    576: initialize_random_junk()
                    577: {
                    578:   register int i;
                    579: 
                    580:   /*
                    581:    * Set up is_idchar and is_idstart tables.  These should be
                    582:    * faster than saying (is_alpha(c) || c == '_'), etc.
                    583:    * Must do set up these things before calling any routines tthat
                    584:    * refer to them.
                    585:    */
                    586:   for (i = 'a'; i <= 'z'; i++) {
                    587:     ++is_idchar[i - 'a' + 'A'];
                    588:     ++is_idchar[i];
                    589:     ++is_idstart[i - 'a' + 'A'];
                    590:     ++is_idstart[i];
                    591:   }
                    592:   for (i = '0'; i <= '9'; i++)
                    593:     ++is_idchar[i];
                    594:   ++is_idchar['_'];
                    595:   ++is_idstart['_'];
1.1.1.2   root      596: #ifdef DOLLARS_IN_IDENTIFIERS
                    597:   ++is_idchar['$'];
                    598:   ++is_idstart['$'];
                    599: #endif
1.1       root      600: 
                    601:   /* horizontal space table */
                    602:   ++is_hor_space[' '];
                    603:   ++is_hor_space['\t'];
                    604: }
                    605: 
                    606: error (msg)
                    607: {
                    608:   printf("error: %s\n", msg);
                    609: }
                    610: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.