|
|
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: %{
110: #include <setjmp.h>
111: /* #define YYDEBUG 1 */
112:
113: static int yylex ();
114: static yyerror ();
115: int expression_value;
116:
117: static jmp_buf parse_return_error;
118:
119: /* some external tables of character types */
120: extern unsigned char is_idstart[], is_idchar[];
121:
122: %}
123:
124: %union {
125: long lval;
126: int voidval;
127: char *sval;
128: }
129:
130: %type <lval> exp exp1 start
131: %token <lval> INT CHAR
132: %token <sval> NAME
133: %token <lval> ERROR
134:
1.1.1.2 ! root 135: %right '?' ':'
1.1 root 136: %left ','
137: %left OR
138: %left AND
139: %left '|'
140: %left '^'
141: %left '&'
142: %left EQUAL NOTEQUAL
143: %left '<' '>' LEQ GEQ
144: %left LSH RSH
145: %left '+' '-'
146: %left '*' '/' '%'
147: %right UNARY
1.1.1.2 ! root 148:
! 149: /* %expect 40 */
1.1 root 150:
151: %%
152:
153: start : exp1
154: { expression_value = $1; }
155: ;
156:
157: /* Expressions, including the comma operator. */
158: exp1 : exp
159: | exp1 ',' exp
160: { $$ = $3; }
161: ;
162:
163: /* Expressions, not including the comma operator. */
164: exp : '-' exp %prec UNARY
165: { $$ = - $2; }
166: | '!' exp %prec UNARY
167: { $$ = ! $2; }
168: | '~' exp %prec UNARY
169: { $$ = ~ $2; }
170: | '(' exp1 ')'
171: { $$ = $2; }
172: ;
173:
174: /* Binary operators in order of decreasing precedence. */
175: exp : exp '*' exp
176: { $$ = $1 * $3; }
177: | exp '/' exp
178: { $$ = $1 / $3; }
179: | exp '%' exp
180: { $$ = $1 % $3; }
181: | exp '+' exp
182: { $$ = $1 + $3; }
183: | exp '-' exp
184: { $$ = $1 - $3; }
185: | exp LSH exp
186: { $$ = $1 << $3; }
187: | exp RSH exp
188: { $$ = $1 >> $3; }
189: | exp EQUAL exp
190: { $$ = ($1 == $3); }
191: | exp NOTEQUAL exp
192: { $$ = ($1 != $3); }
193: | exp LEQ exp
194: { $$ = ($1 <= $3); }
195: | exp GEQ exp
196: { $$ = ($1 >= $3); }
197: | exp '<' exp
198: { $$ = ($1 < $3); }
199: | exp '>' exp
200: { $$ = ($1 > $3); }
201: | exp '&' exp
202: { $$ = ($1 & $3); }
203: | exp '^' exp
204: { $$ = ($1 ^ $3); }
205: | exp '|' exp
206: { $$ = ($1 | $3); }
207: | exp AND exp
208: { $$ = ($1 && $3); }
209: | exp OR exp
210: { $$ = ($1 || $3); }
211: | exp '?' exp ':' exp
212: { $$ = $1 ? $3 : $5; }
213: | INT
214: { $$ = yylval.lval; }
215: | CHAR
216: { $$ = yylval.lval; }
217: | NAME
218: { $$ = 0; }
219: ;
220: %%
221:
222: /* During parsing of a C expression, the pointer to the next character
223: is in this variable. */
224:
225: static char *lexptr;
226:
227: /* Take care of parsing a number (anything that starts with a digit).
228: Set yylval and return the token type; update lexptr.
229: LEN is the number of characters in it. */
230:
231: /* maybe needs to actually deal with floating point numbers */
232:
233: static int
234: parse_number (olen)
235: int olen;
236: {
237: register char *p = lexptr;
238: register long n = 0;
239: register int c;
240: register int base = 10;
241: register len = olen;
242: char *err_copy;
243:
244: extern double atof ();
245:
246: for (c = 0; c < len; c++)
247: if (p[c] == '.') {
248: /* It's a float since it contains a point. */
249: yyerror ("floating point numbers not allowed in #if expressions");
250: return ERROR;
251:
252: /* ****************
253: yylval.dval = atof (p);
254: lexptr += len;
255: return FLOAT;
256: **************** */
257: }
258:
259: if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
260: p += 2;
261: base = 16;
262: len -= 2;
263: }
264: else if (*p == '0')
265: base = 8;
266:
267: while (len-- > 0) {
268: c = *p++;
269: n *= base;
270: if (c >= '0' && c <= '9')
271: n += c - '0';
272: else {
273: if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
274: if (base == 16 && c >= 'a' && c <= 'f')
275: n += c - 'a' + 10;
276: else if (len == 0 && c == 'l')
277: ;
278: else {
279: yyerror ("Invalid number in #if expression");
280: return ERROR;
281: }
282: }
283: }
284:
285: lexptr = p;
286: yylval.lval = n;
287: return INT;
288: }
289:
290: struct token {
291: char *operator;
292: int token;
293: };
294:
295: #define NULL 0
296:
297: static struct token tokentab2[] = {
298: {"&&", AND},
299: {"||", OR},
300: {"<<", LSH},
301: {">>", RSH},
302: {"==", EQUAL},
303: {"!=", NOTEQUAL},
304: {"<=", LEQ},
305: {">=", GEQ},
306: {NULL, ERROR}
307: };
308:
309: /* Read one token, getting characters through lexptr. */
310:
311: static int
312: yylex ()
313: {
314: register int c;
315: register int namelen;
316: register char *tokstart;
317: register struct token *toktab;
318:
319: retry:
320:
321: tokstart = lexptr;
322: c = *tokstart;
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]) {
326: lexptr += 2;
327: return toktab->token;
328: }
329:
330: switch (c) {
331: case 0:
332: return 0;
333:
334: case ' ':
335: case '\t':
336: case '\n':
337: lexptr++;
338: goto retry;
339:
340: case '\'':
341: lexptr++;
342: c = *lexptr++;
343: if (c == '\\')
344: c = parse_escape (&lexptr);
345: yylval.lval = c;
346: c = *lexptr++;
347: if (c != '\'') {
348: yyerror ("Invalid character constant in #if");
349: return ERROR;
350: }
351:
352: return CHAR;
353:
354: /* some of these chars are invalid in constant expressions;
355: maybe do something about them later */
1.1.1.2 ! root 356: case '/':
1.1 root 357: case '+':
358: case '-':
359: case '*':
360: case '%':
361: case '|':
362: case '&':
363: case '^':
364: case '~':
365: case '!':
366: case '@':
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: lexptr++;
381: return c;
382:
383: case '"':
384: yyerror ("double quoted strings not allowed in #if expressions");
385: return ERROR;
386: }
387: if (c >= '0' && c <= '9') {
388: /* It's a number */
389: for (namelen = 0;
390: c = tokstart[namelen], is_idchar[c] || c == '.';
391: namelen++)
392: ;
393: return parse_number (namelen);
394: }
395:
396: if (!is_idstart[c]) {
397: yyerror ("Invalid token in expression");
398: return ERROR;
399: }
400:
401: /* It is a name. See how long it is. */
402:
403: for (namelen = 0; is_idchar[tokstart[namelen]]; namelen++)
404: ;
405:
406: lexptr += namelen;
407: return NAME;
408: }
409:
410:
411: /* Parse a C escape sequence. STRING_PTR points to a variable
412: containing a pointer to the string to parse. That pointer
413: is updated past the characters we use. The value of the
414: escape sequence is returned.
415:
416: A negative value means the sequence \ newline was seen,
417: which is supposed to be equivalent to nothing at all.
418:
419: If \ is followed by a null character, we return a negative
420: value and leave the string pointer pointing at the null character.
421:
422: If \ is followed by 000, we return 0 and leave the string pointer
423: after the zeros. A value of 0 does not mean end of string. */
424:
425: static int
426: parse_escape (string_ptr)
427: char **string_ptr;
428: {
429: register int c = *(*string_ptr)++;
430: switch (c)
431: {
432: case 'a':
433: return '\a';
434: case 'b':
435: return '\b';
436: case 'e':
437: return 033;
438: case 'f':
439: return '\f';
440: case 'n':
441: return '\n';
442: case 'r':
443: return '\r';
444: case 't':
445: return '\t';
446: case 'v':
447: return '\v';
448: case '\n':
449: return -2;
450: case 0:
451: (*string_ptr)--;
452: return 0;
453: case '^':
454: c = *(*string_ptr)++;
455: if (c == '\\')
456: c = parse_escape (string_ptr);
457: if (c == '?')
458: return 0177;
459: return (c & 0200) | (c & 037);
460:
461: case '0':
462: case '1':
463: case '2':
464: case '3':
465: case '4':
466: case '5':
467: case '6':
468: case '7':
469: {
470: register int i = c - '0';
471: register int count = 0;
472: while (++count < 3)
473: {
474: if ((c = *(*string_ptr)++) >= '0' && c <= '7')
475: {
476: i *= 8;
477: i += c - '0';
478: }
479: else
480: {
481: (*string_ptr)--;
482: break;
483: }
484: }
485: return i;
486: }
487: default:
488: return c;
489: }
490: }
491:
492: static
493: yyerror (s)
494: char *s;
495: {
496: error (s);
497: longjmp (parse_return_error, 1);
498: }
499:
500: /* This page contains the entry point to this file. */
501:
502: /* Parse STRING as an expression, and complain if this fails
503: to use up all of the contents of STRING. */
1.1.1.2 ! root 504: /* We do not support C comments. They should be removed before
! 505: this function is called. */
! 506:
1.1 root 507: int
508: parse_c_expression (string)
509: char *string;
510: {
511: lexptr = string;
512:
513: if (lexptr == 0 || *lexptr == 0) {
514: error ("empty #if expression");
515: return 0; /* don't include the #if group */
516: }
517:
518: /* if there is some sort of scanning error, just return 0 and assume
519: the parsing routine has printed an error message somewhere.
520: there is surely a better thing to do than this. */
521: if (setjmp(parse_return_error))
522: return 0;
523:
524: if (yyparse ())
525: return 0; /* actually this is never reached
526: the way things stand. */
527: if (*lexptr)
528: error ("Junk after end of expression.");
529:
530: return expression_value; /* set by yyparse() */
531: }
532:
533: #ifdef TEST_EXP_READER
534: /* main program, for testing purposes. */
535: main()
536: {
537: int n;
538: char buf[1024];
539: extern int yydebug;
540: /*
541: yydebug = 1;
542: */
543: initialize_random_junk ();
544:
545: for (;;) {
546: printf("enter expression: ");
547: n = 0;
548: while ((buf[n] = getchar()) != '\n')
549: n++;
550: buf[n] = '\0';
551: printf("parser returned %d\n", parse_c_expression(buf));
552: }
553: }
554:
555: /* table to tell if char can be part of a C identifier. */
556: char is_idchar[256];
557: /* table to tell if char can be first char of a c identifier. */
558: char is_idstart[256];
559: /* table to tell if c is horizontal space. isspace() thinks that
560: newline is space; this is not a good idea for this program. */
561: char is_hor_space[256];
562:
563: /*
564: * initialize random junk in the hash table and maybe other places
565: */
566: initialize_random_junk()
567: {
568: register int i;
569:
570: /*
571: * Set up is_idchar and is_idstart tables. These should be
572: * faster than saying (is_alpha(c) || c == '_'), etc.
573: * Must do set up these things before calling any routines tthat
574: * refer to them.
575: */
576: for (i = 'a'; i <= 'z'; i++) {
577: ++is_idchar[i - 'a' + 'A'];
578: ++is_idchar[i];
579: ++is_idstart[i - 'a' + 'A'];
580: ++is_idstart[i];
581: }
582: for (i = '0'; i <= '9'; i++)
583: ++is_idchar[i];
584: ++is_idchar['_'];
585: ++is_idstart['_'];
1.1.1.2 ! root 586: #ifdef DOLLARS_IN_IDENTIFIERS
! 587: ++is_idchar['$'];
! 588: ++is_idstart['$'];
! 589: #endif
1.1 root 590:
591: /* horizontal space table */
592: ++is_hor_space[' '];
593: ++is_hor_space['\t'];
594: }
595:
596: error (msg)
597: {
598: printf("error: %s\n", msg);
599: }
600: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.