reduce.c 13 KB

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