reduce.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* Grammar reduction for Bison.
  2. Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3. This file is part of Bison, the GNU Compiler Compiler.
  4. Bison is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. Bison is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Bison; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  16. * Reduce the grammar: Find and eliminate unreachable terminals,
  17. * nonterminals, and productions. David S. Bakin.
  18. */
  19. /*
  20. * Don't eliminate unreachable terminals: They may be used by the user's
  21. * parser.
  22. */
  23. #include <stdio.h>
  24. #include "system.h"
  25. #include "files.h"
  26. #include "gram.h"
  27. #include "machine.h"
  28. #include "new.h"
  29. extern char **tags; /* reader.c */
  30. extern int verboseflag; /* getargs.c */
  31. static int statisticsflag; /* XXXXXXX */
  32. #define TRUE (1)
  33. #define FALSE (0)
  34. typedef int bool;
  35. typedef unsigned *BSet;
  36. typedef short *rule;
  37. /*
  38. * N is set of all nonterminals which are not useless. P is set of all rules
  39. * which have no useless nonterminals in their RHS. V is the set of all
  40. * accessible symbols.
  41. */
  42. static BSet N, P, V, V1;
  43. static int nuseful_productions, nuseless_productions,
  44. nuseful_nonterminals, nuseless_nonterminals;
  45. static void useless_nonterminals();
  46. static void inaccessable_symbols();
  47. static void reduce_grammar_tables();
  48. static void print_results();
  49. static void print_notices();
  50. void dump_grammar();
  51. extern void fatals ();
  52. bool
  53. bits_equal (L, R, n)
  54. BSet L;
  55. BSet R;
  56. int n;
  57. {
  58. int i;
  59. for (i = n - 1; i >= 0; i--)
  60. if (L[i] != R[i])
  61. return FALSE;
  62. return TRUE;
  63. }
  64. int
  65. nbits (i)
  66. unsigned i;
  67. {
  68. int count = 0;
  69. while (i != 0) {
  70. i ^= (i & -i);
  71. ++count;
  72. }
  73. return count;
  74. }
  75. int
  76. bits_size (S, n)
  77. BSet S;
  78. int n;
  79. {
  80. int i, count = 0;
  81. for (i = n - 1; i >= 0; i--)
  82. count += nbits(S[i]);
  83. return count;
  84. }
  85. void
  86. reduce_grammar ()
  87. {
  88. bool reduced;
  89. /* Allocate the global sets used to compute the reduced grammar */
  90. N = NEW2(WORDSIZE(nvars), unsigned);
  91. P = NEW2(WORDSIZE(nrules + 1), unsigned);
  92. V = NEW2(WORDSIZE(nsyms), unsigned);
  93. V1 = NEW2(WORDSIZE(nsyms), unsigned);
  94. useless_nonterminals();
  95. inaccessable_symbols();
  96. reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
  97. if (verboseflag)
  98. print_results();
  99. if (reduced == FALSE)
  100. goto done_reducing;
  101. print_notices();
  102. if (!BITISSET(N, start_symbol - ntokens))
  103. fatals("Start symbol %s does not derive any sentence.",
  104. tags[start_symbol]);
  105. reduce_grammar_tables();
  106. /* if (verboseflag) {
  107. fprintf(foutput, "REDUCED GRAMMAR\n\n");
  108. dump_grammar();
  109. }
  110. */
  111. /**/ statisticsflag = FALSE; /* someday getopts should handle this */
  112. if (statisticsflag == TRUE)
  113. fprintf(stderr,
  114. "reduced %s defines %d terminal%s, %d nonterminal%s\
  115. , and %d production%s.\n", infile,
  116. ntokens, (ntokens == 1 ? "" : "s"),
  117. nvars, (nvars == 1 ? "" : "s"),
  118. nrules, (nrules == 1 ? "" : "s"));
  119. done_reducing:
  120. /* Free the global sets used to compute the reduced grammar */
  121. FREE(N);
  122. FREE(V);
  123. FREE(P);
  124. }
  125. /*
  126. * Another way to do this would be with a set for each production and then do
  127. * subset tests against N, but even for the C grammar the whole reducing
  128. * process takes only 2 seconds on my 8Mhz AT.
  129. */
  130. static bool
  131. useful_production (i, N)
  132. int i;
  133. BSet N;
  134. {
  135. rule r;
  136. short n;
  137. /*
  138. * A production is useful if all of the nonterminals in its RHS
  139. * appear in the set of useful nonterminals.
  140. */
  141. for (r = &ritem[rrhs[i]]; *r > 0; r++)
  142. if (ISVAR(n = *r))
  143. if (!BITISSET(N, n - ntokens))
  144. return FALSE;
  145. return TRUE;
  146. }
  147. /* Remember that rules are 1-origin, symbols are 0-origin. */
  148. static void
  149. useless_nonterminals ()
  150. {
  151. BSet Np, Ns;
  152. int i, n;
  153. /*
  154. * N is set as built. Np is set being built this iteration. P is set
  155. * of all productions which have a RHS all in N.
  156. */
  157. Np = NEW2(WORDSIZE(nvars), unsigned);
  158. /*
  159. * The set being computed is a set of nonterminals which can derive
  160. * the empty string or strings consisting of all terminals. At each
  161. * iteration a nonterminal is added to the set if there is a
  162. * production with that nonterminal as its LHS for which all the
  163. * nonterminals in its RHS are already in the set. Iterate until the
  164. * set being computed remains unchanged. Any nonterminals not in the
  165. * set at that point are useless in that they will never be used in
  166. * deriving a sentence of the language.
  167. *
  168. * This iteration doesn't use any special traversal over the
  169. * productions. A set is kept of all productions for which all the
  170. * nonterminals in the RHS are in useful. Only productions not in
  171. * this set are scanned on each iteration. At the end, this set is
  172. * saved to be used when finding useful productions: only productions
  173. * in this set will appear in the final grammar.
  174. */
  175. n = 0;
  176. while (1)
  177. {
  178. for (i = WORDSIZE(nvars) - 1; i >= 0; i--)
  179. Np[i] = N[i];
  180. for (i = 1; i <= nrules; i++)
  181. {
  182. if (!BITISSET(P, i))
  183. {
  184. if (useful_production(i, N))
  185. {
  186. SETBIT(Np, rlhs[i] - ntokens);
  187. SETBIT(P, i);
  188. }
  189. }
  190. }
  191. if (bits_equal(N, Np, WORDSIZE(nvars)))
  192. break;
  193. Ns = Np;
  194. Np = N;
  195. N = Ns;
  196. }
  197. FREE(N);
  198. N = Np;
  199. }
  200. static void
  201. inaccessable_symbols ()
  202. {
  203. BSet Vp, Vs, Pp;
  204. int i, n;
  205. short t;
  206. rule r;
  207. /*
  208. * Find out which productions are reachable and which symbols are
  209. * used. Starting with an empty set of productions and a set of
  210. * symbols which only has the start symbol in it, iterate over all
  211. * productions until the set of productions remains unchanged for an
  212. * iteration. For each production which has a LHS in the set of
  213. * reachable symbols, add the production to the set of reachable
  214. * productions, and add all of the nonterminals in the RHS of the
  215. * production to the set of reachable symbols.
  216. *
  217. * Consider only the (partially) reduced grammar which has only
  218. * nonterminals in N and productions in P.
  219. *
  220. * The result is the set P of productions in the reduced grammar, and
  221. * the set V of symbols in the reduced grammar.
  222. *
  223. * Although this algorithm also computes the set of terminals which are
  224. * reachable, no terminal will be deleted from the grammar. Some
  225. * terminals might not be in the grammar but might be generated by
  226. * semantic routines, and so the user might want them available with
  227. * specified numbers. (Is this true?) However, the nonreachable
  228. * terminals are printed (if running in verbose mode) so that the user
  229. * can know.
  230. */
  231. Vp = NEW2(WORDSIZE(nsyms), unsigned);
  232. Pp = NEW2(WORDSIZE(nrules + 1), unsigned);
  233. /* If the start symbol isn't useful, then nothing will be useful. */
  234. if (!BITISSET(N, start_symbol - ntokens))
  235. goto end_iteration;
  236. SETBIT(V, start_symbol);
  237. n = 0;
  238. while (1)
  239. {
  240. for (i = WORDSIZE(nsyms) - 1; i >= 0; i--)
  241. Vp[i] = V[i];
  242. for (i = 1; i <= nrules; i++)
  243. {
  244. if (!BITISSET(Pp, i) && BITISSET(P, i) &&
  245. BITISSET(V, rlhs[i]))
  246. {
  247. for (r = &ritem[rrhs[i]]; *r >= 0; r++)
  248. {
  249. if (ISTOKEN(t = *r)
  250. || BITISSET(N, t - ntokens))
  251. {
  252. SETBIT(Vp, t);
  253. }
  254. }
  255. SETBIT(Pp, i);
  256. }
  257. }
  258. if (bits_equal(V, Vp, WORDSIZE(nsyms)))
  259. {
  260. break;
  261. }
  262. Vs = Vp;
  263. Vp = V;
  264. V = Vs;
  265. }
  266. end_iteration:
  267. FREE(V);
  268. V = Vp;
  269. /* Tokens 0, 1, and 2 are internal to Bison. Consider them useful. */
  270. SETBIT(V, 0); /* end-of-input token */
  271. SETBIT(V, 1); /* error token */
  272. SETBIT(V, 2); /* illegal token */
  273. FREE(P);
  274. P = Pp;
  275. nuseful_productions = bits_size(P, WORDSIZE(nrules + 1));
  276. nuseless_productions = nrules - nuseful_productions;
  277. nuseful_nonterminals = 0;
  278. for (i = ntokens; i < nsyms; i++)
  279. if (BITISSET(V, i))
  280. nuseful_nonterminals++;
  281. nuseless_nonterminals = nvars - nuseful_nonterminals;
  282. /* A token that was used in %prec should not be warned about. */
  283. for (i = 1; i < nrules; i++)
  284. if (rprecsym[i] != 0)
  285. SETBIT(V1, rprecsym[i]);
  286. }
  287. static void
  288. reduce_grammar_tables ()
  289. {
  290. /* This is turned off because we would need to change the numbers
  291. in the case statements in the actions file. */
  292. #if 0
  293. /* remove useless productions */
  294. if (nuseless_productions > 0)
  295. {
  296. short np, pn, ni, pi;
  297. np = 0;
  298. ni = 0;
  299. for (pn = 1; pn <= nrules; pn++)
  300. {
  301. if (BITISSET(P, pn))
  302. {
  303. np++;
  304. if (pn != np)
  305. {
  306. rlhs[np] = rlhs[pn];
  307. rline[np] = rline[pn];
  308. rprec[np] = rprec[pn];
  309. rassoc[np] = rassoc[pn];
  310. rrhs[np] = rrhs[pn];
  311. if (rrhs[np] != ni)
  312. {
  313. pi = rrhs[np];
  314. rrhs[np] = ni;
  315. while (ritem[pi] >= 0)
  316. ritem[ni++] = ritem[pi++];
  317. ritem[ni++] = -np;
  318. }
  319. } else {
  320. while (ritem[ni++] >= 0);
  321. }
  322. }
  323. }
  324. ritem[ni] = 0;
  325. nrules -= nuseless_productions;
  326. nitems = ni;
  327. /*
  328. * Is it worth it to reduce the amount of memory for the
  329. * grammar? Probably not.
  330. */
  331. }
  332. #endif /* 0 */
  333. /* Disable useless productions,
  334. since they may contain useless nonterms
  335. that would get mapped below to -1 and confuse everyone. */
  336. if (nuseless_productions > 0)
  337. {
  338. int pn;
  339. for (pn = 1; pn <= nrules; pn++)
  340. {
  341. if (!BITISSET(P, pn))
  342. {
  343. rlhs[pn] = -1;
  344. }
  345. }
  346. }
  347. /* remove useless symbols */
  348. if (nuseless_nonterminals > 0)
  349. {
  350. int i, n;
  351. /* short j; JF unused */
  352. short *nontermmap;
  353. rule r;
  354. /*
  355. * create a map of nonterminal number to new nonterminal
  356. * number. -1 in the map means it was useless and is being
  357. * eliminated.
  358. */
  359. nontermmap = NEW2(nvars, short) - ntokens;
  360. for (i = ntokens; i < nsyms; i++)
  361. nontermmap[i] = -1;
  362. n = ntokens;
  363. for (i = ntokens; i < nsyms; i++)
  364. if (BITISSET(V, i))
  365. nontermmap[i] = n++;
  366. /* Shuffle elements of tables indexed by symbol number. */
  367. for (i = ntokens; i < nsyms; i++)
  368. {
  369. n = nontermmap[i];
  370. if (n >= 0)
  371. {
  372. sassoc[n] = sassoc[i];
  373. sprec[n] = sprec[i];
  374. tags[n] = tags[i];
  375. } else {
  376. free(tags[i]);
  377. }
  378. }
  379. /* Replace all symbol numbers in valid data structures. */
  380. for (i = 1; i <= nrules; i++)
  381. {
  382. /* Ignore the rules disabled above. */
  383. if (rlhs[i] >= 0)
  384. rlhs[i] = nontermmap[rlhs[i]];
  385. if (ISVAR (rprecsym[i]))
  386. /* Can this happen? */
  387. rprecsym[i] = nontermmap[rprecsym[i]];
  388. }
  389. for (r = ritem; *r; r++)
  390. if (ISVAR(*r))
  391. *r = nontermmap[*r];
  392. start_symbol = nontermmap[start_symbol];
  393. nsyms -= nuseless_nonterminals;
  394. nvars -= nuseless_nonterminals;
  395. free(&nontermmap[ntokens]);
  396. }
  397. }
  398. static void
  399. print_results ()
  400. {
  401. int i;
  402. /* short j; JF unused */
  403. rule r;
  404. bool b;
  405. if (nuseless_nonterminals > 0)
  406. {
  407. fprintf(foutput, "Useless nonterminals:\n\n");
  408. for (i = ntokens; i < nsyms; i++)
  409. if (!BITISSET(V, i))
  410. fprintf(foutput, " %s\n", tags[i]);
  411. }
  412. b = FALSE;
  413. for (i = 0; i < ntokens; i++)
  414. {
  415. if (!BITISSET(V, i) && !BITISSET(V1, i))
  416. {
  417. if (!b)
  418. {
  419. fprintf(foutput, "\n\nTerminals which are not used:\n\n");
  420. b = TRUE;
  421. }
  422. fprintf(foutput, " %s\n", tags[i]);
  423. }
  424. }
  425. if (nuseless_productions > 0)
  426. {
  427. fprintf(foutput, "\n\nUseless rules:\n\n");
  428. for (i = 1; i <= nrules; i++)
  429. {
  430. if (!BITISSET(P, i))
  431. {
  432. fprintf(foutput, "#%-4d ", i);
  433. fprintf(foutput, "%s :\t", tags[rlhs[i]]);
  434. for (r = &ritem[rrhs[i]]; *r >= 0; r++)
  435. {
  436. fprintf(foutput, " %s", tags[*r]);
  437. }
  438. fprintf(foutput, ";\n");
  439. }
  440. }
  441. }
  442. if (nuseless_nonterminals > 0 || nuseless_productions > 0 || b)
  443. fprintf(foutput, "\n\n");
  444. }
  445. void
  446. dump_grammar ()
  447. {
  448. int i;
  449. rule r;
  450. fprintf(foutput,
  451. "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
  452. ntokens, nvars, nsyms, nrules, nitems);
  453. fprintf(foutput, "Variables\n---------\n\n");
  454. fprintf(foutput, "Value Sprec Sassoc Tag\n");
  455. for (i = ntokens; i < nsyms; i++)
  456. fprintf(foutput, "%5d %5d %5d %s\n",
  457. i, sprec[i], sassoc[i], tags[i]);
  458. fprintf(foutput, "\n\n");
  459. fprintf(foutput, "Rules\n-----\n\n");
  460. for (i = 1; i <= nrules; i++)
  461. {
  462. fprintf(foutput, "%-5d(%5d%5d)%5d : (@%-5d)",
  463. i, rprec[i], rassoc[i], rlhs[i], rrhs[i]);
  464. for (r = &ritem[rrhs[i]]; *r > 0; r++)
  465. fprintf(foutput, "%5d", *r);
  466. fprintf(foutput, " [%d]\n", -(*r));
  467. }
  468. fprintf(foutput, "\n\n");
  469. fprintf(foutput, "Rules interpreted\n-----------------\n\n");
  470. for (i = 1; i <= nrules; i++)
  471. {
  472. fprintf(foutput, "%-5d %s :", i, tags[rlhs[i]]);
  473. for (r = &ritem[rrhs[i]]; *r > 0; r++)
  474. fprintf(foutput, " %s", tags[*r]);
  475. fprintf(foutput, "\n");
  476. }
  477. fprintf(foutput, "\n\n");
  478. }
  479. static void
  480. print_notices ()
  481. {
  482. extern int fixed_outfiles;
  483. if (fixed_outfiles && nuseless_productions)
  484. fprintf(stderr, "%d rules never reduced\n", nuseless_productions);
  485. fprintf(stderr, "%s contains ", infile);
  486. if (nuseless_nonterminals > 0)
  487. {
  488. fprintf(stderr, "%d useless nonterminal%s",
  489. nuseless_nonterminals,
  490. (nuseless_nonterminals == 1 ? "" : "s"));
  491. }
  492. if (nuseless_nonterminals > 0 && nuseless_productions > 0)
  493. fprintf(stderr, " and ");
  494. if (nuseless_productions > 0)
  495. {
  496. fprintf(stderr, "%d useless rule%s",
  497. nuseless_productions,
  498. (nuseless_productions == 1 ? "" : "s"));
  499. }
  500. fprintf(stderr, ".\n");
  501. fflush(stderr);
  502. }