cexp.y 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /* Parse C expressions for CCCP.
  2. Copyright (C) 1986 Free Software Foundation.
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously
  27. and appropriately publish on each copy a valid copyright notice
  28. "Copyright (C) 1986 Free Software Foundation"; and include
  29. following the copyright notice a verbatim copy of the above disclaimer
  30. of warranty and of this License.
  31. 2. You may modify your copy or copies of this source file or
  32. any portion of it, and copy and distribute such modifications under
  33. the terms of Paragraph 1 above, provided that you also do the following:
  34. a) cause the modified files to carry prominent notices stating
  35. that you changed the files and the date of any change; and
  36. b) cause the whole of any work that you distribute or publish,
  37. that in whole or in part contains or is a derivative of this
  38. program or any part thereof, to be freely distributed
  39. and licensed to all third parties on terms identical to those
  40. contained in this License Agreement (except that you may choose
  41. to grant more extensive warranty protection to third parties,
  42. at your option).
  43. 3. You may copy and distribute this program or any portion of it in
  44. compiled, executable or object code form under the terms of Paragraphs
  45. 1 and 2 above provided that you do the following:
  46. a) cause each such copy to be accompanied by the
  47. corresponding machine-readable source code, which must
  48. be distributed under the terms of Paragraphs 1 and 2 above; or,
  49. b) cause each such copy to be accompanied by a
  50. written offer, with no time limit, to give any third party
  51. free (except for a nominal shipping charge) a machine readable
  52. copy of the corresponding source code, to be distributed
  53. under the terms of Paragraphs 1 and 2 above; or,
  54. c) in the case of a recipient of this program in compiled, executable
  55. or object code form (without the corresponding source code) you
  56. shall cause copies you distribute to be accompanied by a copy
  57. of the written offer of source code which you received along
  58. with the copy you received.
  59. 4. You may not copy, sublicense, distribute or transfer this program
  60. except as expressly provided under this License Agreement. Any attempt
  61. otherwise to copy, sublicense, distribute or transfer this program is void and
  62. your rights to use the program under this License agreement shall be
  63. automatically terminated. However, parties who have received computer
  64. software programs from you with this License Agreement will not have
  65. their licenses terminated so long as such parties remain in full compliance.
  66. In other words, you are welcome to use, share and improve this program.
  67. You are forbidden to forbid anyone else to use, share and improve
  68. what you give them. Help stamp out software-hoarding!
  69. Adapted from expread.y of GDB by Paul Rubin, July 1986.
  70. /* Parse a C expression from text in a string */
  71. %{
  72. #include <setjmp.h>
  73. /* #define YYDEBUG 1 */
  74. static int yylex ();
  75. static yyerror ();
  76. int expression_value;
  77. static jmp_buf parse_return_error;
  78. /* some external tables of character types */
  79. extern unsigned char is_idstart[], is_idchar[];
  80. %}
  81. %union {
  82. long lval;
  83. int voidval;
  84. char *sval;
  85. }
  86. %type <lval> exp exp1 start
  87. %token <lval> INT CHAR
  88. %token <sval> NAME
  89. %token <lval> ERROR
  90. %left ','
  91. %left OR
  92. %left AND
  93. %left '|'
  94. %left '^'
  95. %left '&'
  96. %left EQUAL NOTEQUAL
  97. %left '<' '>' LEQ GEQ
  98. %left LSH RSH
  99. %left '+' '-'
  100. %left '*' '/' '%'
  101. %right UNARY
  102. %%
  103. start : exp1
  104. { expression_value = $1; }
  105. ;
  106. /* Expressions, including the comma operator. */
  107. exp1 : exp
  108. | exp1 ',' exp
  109. { $$ = $3; }
  110. ;
  111. /* Expressions, not including the comma operator. */
  112. exp : '-' exp %prec UNARY
  113. { $$ = - $2; }
  114. | '!' exp %prec UNARY
  115. { $$ = ! $2; }
  116. | '~' exp %prec UNARY
  117. { $$ = ~ $2; }
  118. | '(' exp1 ')'
  119. { $$ = $2; }
  120. ;
  121. /* Binary operators in order of decreasing precedence. */
  122. exp : exp '*' exp
  123. { $$ = $1 * $3; }
  124. | exp '/' exp
  125. { $$ = $1 / $3; }
  126. | exp '%' exp
  127. { $$ = $1 % $3; }
  128. | exp '+' exp
  129. { $$ = $1 + $3; }
  130. | exp '-' exp
  131. { $$ = $1 - $3; }
  132. | exp LSH exp
  133. { $$ = $1 << $3; }
  134. | exp RSH exp
  135. { $$ = $1 >> $3; }
  136. | exp EQUAL exp
  137. { $$ = ($1 == $3); }
  138. | exp NOTEQUAL exp
  139. { $$ = ($1 != $3); }
  140. | exp LEQ exp
  141. { $$ = ($1 <= $3); }
  142. | exp GEQ exp
  143. { $$ = ($1 >= $3); }
  144. | exp '<' exp
  145. { $$ = ($1 < $3); }
  146. | exp '>' exp
  147. { $$ = ($1 > $3); }
  148. | exp '&' exp
  149. { $$ = ($1 & $3); }
  150. | exp '^' exp
  151. { $$ = ($1 ^ $3); }
  152. | exp '|' exp
  153. { $$ = ($1 | $3); }
  154. | exp AND exp
  155. { $$ = ($1 && $3); }
  156. | exp OR exp
  157. { $$ = ($1 || $3); }
  158. | exp '?' exp ':' exp
  159. { $$ = $1 ? $3 : $5; }
  160. | INT
  161. { $$ = yylval.lval; }
  162. | CHAR
  163. { $$ = yylval.lval; }
  164. | NAME
  165. { $$ = 0; }
  166. ;
  167. %%
  168. /* During parsing of a C expression, the pointer to the next character
  169. is in this variable. */
  170. static char *lexptr;
  171. /* Take care of parsing a number (anything that starts with a digit).
  172. Set yylval and return the token type; update lexptr.
  173. LEN is the number of characters in it. */
  174. /* maybe needs to actually deal with floating point numbers */
  175. static int
  176. parse_number (olen)
  177. int olen;
  178. {
  179. register char *p = lexptr;
  180. register long n = 0;
  181. register int c;
  182. register int base = 10;
  183. register len = olen;
  184. char *err_copy;
  185. extern double atof ();
  186. for (c = 0; c < len; c++)
  187. if (p[c] == '.') {
  188. /* It's a float since it contains a point. */
  189. yyerror ("floating point numbers not allowed in #if expressions");
  190. return ERROR;
  191. /* ****************
  192. yylval.dval = atof (p);
  193. lexptr += len;
  194. return FLOAT;
  195. **************** */
  196. }
  197. if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
  198. p += 2;
  199. base = 16;
  200. len -= 2;
  201. }
  202. else if (*p == '0')
  203. base = 8;
  204. while (len-- > 0) {
  205. c = *p++;
  206. n *= base;
  207. if (c >= '0' && c <= '9')
  208. n += c - '0';
  209. else {
  210. if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
  211. if (base == 16 && c >= 'a' && c <= 'f')
  212. n += c - 'a' + 10;
  213. else if (len == 0 && c == 'l')
  214. ;
  215. else {
  216. yyerror ("Invalid number in #if expression");
  217. return ERROR;
  218. }
  219. }
  220. }
  221. lexptr = p;
  222. yylval.lval = n;
  223. return INT;
  224. }
  225. struct token {
  226. char *operator;
  227. int token;
  228. };
  229. #define NULL 0
  230. static struct token tokentab2[] = {
  231. {"&&", AND},
  232. {"||", OR},
  233. {"<<", LSH},
  234. {">>", RSH},
  235. {"==", EQUAL},
  236. {"!=", NOTEQUAL},
  237. {"<=", LEQ},
  238. {">=", GEQ},
  239. {NULL, ERROR}
  240. };
  241. /* Read one token, getting characters through lexptr. */
  242. static int
  243. yylex ()
  244. {
  245. register int c;
  246. register int namelen;
  247. register char *tokstart;
  248. register struct token *toktab;
  249. retry:
  250. tokstart = lexptr;
  251. c = *tokstart;
  252. /* See if it is a special token of length 2. */
  253. for (toktab = tokentab2; toktab->operator != NULL; toktab++)
  254. if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
  255. lexptr += 2;
  256. return toktab->token;
  257. }
  258. switch (c) {
  259. case 0:
  260. return 0;
  261. case ' ':
  262. case '\t':
  263. case '\n':
  264. lexptr++;
  265. goto retry;
  266. case '\'':
  267. lexptr++;
  268. c = *lexptr++;
  269. if (c == '\\')
  270. c = parse_escape (&lexptr);
  271. yylval.lval = c;
  272. c = *lexptr++;
  273. if (c != '\'') {
  274. yyerror ("Invalid character constant in #if");
  275. return ERROR;
  276. }
  277. return CHAR;
  278. case '/': /* possible comment */
  279. if (*lexptr != '*')
  280. return c;
  281. for (;;) {
  282. while (*lexptr != '\0') {
  283. if (*lexptr++ == '*' && *lexptr == '/') {
  284. lexptr++;
  285. goto retry;
  286. }
  287. }
  288. }
  289. /* some of these chars are invalid in constant expressions;
  290. maybe do something about them later */
  291. case '+':
  292. case '-':
  293. case '*':
  294. case '%':
  295. case '|':
  296. case '&':
  297. case '^':
  298. case '~':
  299. case '!':
  300. case '@':
  301. case '<':
  302. case '>':
  303. case '(':
  304. case ')':
  305. case '[':
  306. case ']':
  307. case '.':
  308. case '?':
  309. case ':':
  310. case '=':
  311. case '{':
  312. case '}':
  313. case ',':
  314. lexptr++;
  315. return c;
  316. case '"':
  317. yyerror ("double quoted strings not allowed in #if expressions");
  318. return ERROR;
  319. }
  320. if (c >= '0' && c <= '9') {
  321. /* It's a number */
  322. for (namelen = 0;
  323. c = tokstart[namelen], is_idchar[c] || c == '.';
  324. namelen++)
  325. ;
  326. return parse_number (namelen);
  327. }
  328. if (!is_idstart[c]) {
  329. yyerror ("Invalid token in expression");
  330. return ERROR;
  331. }
  332. /* It is a name. See how long it is. */
  333. for (namelen = 0; is_idchar[tokstart[namelen]]; namelen++)
  334. ;
  335. lexptr += namelen;
  336. return NAME;
  337. }
  338. /* Parse a C escape sequence. STRING_PTR points to a variable
  339. containing a pointer to the string to parse. That pointer
  340. is updated past the characters we use. The value of the
  341. escape sequence is returned.
  342. A negative value means the sequence \ newline was seen,
  343. which is supposed to be equivalent to nothing at all.
  344. If \ is followed by a null character, we return a negative
  345. value and leave the string pointer pointing at the null character.
  346. If \ is followed by 000, we return 0 and leave the string pointer
  347. after the zeros. A value of 0 does not mean end of string. */
  348. static int
  349. parse_escape (string_ptr)
  350. char **string_ptr;
  351. {
  352. register int c = *(*string_ptr)++;
  353. switch (c)
  354. {
  355. case 'a':
  356. return '\a';
  357. case 'b':
  358. return '\b';
  359. case 'e':
  360. return 033;
  361. case 'f':
  362. return '\f';
  363. case 'n':
  364. return '\n';
  365. case 'r':
  366. return '\r';
  367. case 't':
  368. return '\t';
  369. case 'v':
  370. return '\v';
  371. case '\n':
  372. return -2;
  373. case 0:
  374. (*string_ptr)--;
  375. return 0;
  376. case '^':
  377. c = *(*string_ptr)++;
  378. if (c == '\\')
  379. c = parse_escape (string_ptr);
  380. if (c == '?')
  381. return 0177;
  382. return (c & 0200) | (c & 037);
  383. case '0':
  384. case '1':
  385. case '2':
  386. case '3':
  387. case '4':
  388. case '5':
  389. case '6':
  390. case '7':
  391. {
  392. register int i = c - '0';
  393. register int count = 0;
  394. while (++count < 3)
  395. {
  396. if ((c = *(*string_ptr)++) >= '0' && c <= '7')
  397. {
  398. i *= 8;
  399. i += c - '0';
  400. }
  401. else
  402. {
  403. (*string_ptr)--;
  404. break;
  405. }
  406. }
  407. return i;
  408. }
  409. default:
  410. return c;
  411. }
  412. }
  413. static
  414. yyerror (s)
  415. char *s;
  416. {
  417. error (s);
  418. longjmp (parse_return_error, 1);
  419. }
  420. /* This page contains the entry point to this file. */
  421. /* Parse STRING as an expression, and complain if this fails
  422. to use up all of the contents of STRING. */
  423. int
  424. parse_c_expression (string)
  425. char *string;
  426. {
  427. lexptr = string;
  428. if (lexptr == 0 || *lexptr == 0) {
  429. error ("empty #if expression");
  430. return 0; /* don't include the #if group */
  431. }
  432. /* if there is some sort of scanning error, just return 0 and assume
  433. the parsing routine has printed an error message somewhere.
  434. there is surely a better thing to do than this. */
  435. if (setjmp(parse_return_error))
  436. return 0;
  437. if (yyparse ())
  438. return 0; /* actually this is never reached
  439. the way things stand. */
  440. if (*lexptr)
  441. error ("Junk after end of expression.");
  442. return expression_value; /* set by yyparse() */
  443. }
  444. #ifdef TEST_EXP_READER
  445. /* main program, for testing purposes. */
  446. main()
  447. {
  448. int n;
  449. char buf[1024];
  450. extern int yydebug;
  451. /*
  452. yydebug = 1;
  453. */
  454. initialize_random_junk ();
  455. for (;;) {
  456. printf("enter expression: ");
  457. n = 0;
  458. while ((buf[n] = getchar()) != '\n')
  459. n++;
  460. buf[n] = '\0';
  461. printf("parser returned %d\n", parse_c_expression(buf));
  462. }
  463. }
  464. /* table to tell if char can be part of a C identifier. */
  465. char is_idchar[256];
  466. /* table to tell if char can be first char of a c identifier. */
  467. char is_idstart[256];
  468. /* table to tell if c is horizontal space. isspace() thinks that
  469. newline is space; this is not a good idea for this program. */
  470. char is_hor_space[256];
  471. /*
  472. * initialize random junk in the hash table and maybe other places
  473. */
  474. initialize_random_junk()
  475. {
  476. register int i;
  477. /*
  478. * Set up is_idchar and is_idstart tables. These should be
  479. * faster than saying (is_alpha(c) || c == '_'), etc.
  480. * Must do set up these things before calling any routines tthat
  481. * refer to them.
  482. */
  483. for (i = 'a'; i <= 'z'; i++) {
  484. ++is_idchar[i - 'a' + 'A'];
  485. ++is_idchar[i];
  486. ++is_idstart[i - 'a' + 'A'];
  487. ++is_idstart[i];
  488. }
  489. for (i = '0'; i <= '9'; i++)
  490. ++is_idchar[i];
  491. ++is_idchar['_'];
  492. ++is_idstart['_'];
  493. /* horizontal space table */
  494. ++is_hor_space[' '];
  495. ++is_hor_space['\t'];
  496. }
  497. error (msg)
  498. {
  499. printf("error: %s\n", msg);
  500. }
  501. #endif