REDUCE.C 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. /* remove useless productions */
  291. if (nuseless_productions > 0)
  292. {
  293. short np, pn, ni, pi;
  294. np = 0;
  295. ni = 0;
  296. for (pn = 1; pn <= nrules; pn++)
  297. {
  298. if (BITISSET(P, pn))
  299. {
  300. np++;
  301. if (pn != np)
  302. {
  303. rlhs[np] = rlhs[pn];
  304. rprec[np] = rprec[pn];
  305. rassoc[np] = rassoc[pn];
  306. rrhs[np] = rrhs[pn];
  307. if (rrhs[np] != ni)
  308. {
  309. pi = rrhs[np];
  310. rrhs[np] = ni;
  311. while (ritem[pi] >= 0)
  312. ritem[ni++] = ritem[pi++];
  313. ritem[ni++] = -np;
  314. }
  315. } else {
  316. while (ritem[ni++] >= 0);
  317. }
  318. }
  319. }
  320. ritem[ni] = 0;
  321. nrules -= nuseless_productions;
  322. nitems = ni;
  323. /*
  324. * Is it worth it to reduce the amount of memory for the
  325. * grammar? Probably not.
  326. */
  327. }
  328. /* remove useless symbols */
  329. if (nuseless_nonterminals > 0)
  330. {
  331. int i, n;
  332. /* short j; JF unused */
  333. short *nontermmap;
  334. rule r;
  335. /*
  336. * create a map of nonterminal number to new nonterminal
  337. * number. -1 in the map means it was useless and is being
  338. * eliminated.
  339. */
  340. nontermmap = NEW2(nvars, short) - ntokens;
  341. for (i = ntokens; i < nsyms; i++)
  342. nontermmap[i] = -1;
  343. n = ntokens;
  344. for (i = ntokens; i < nsyms; i++)
  345. if (BITISSET(V, i))
  346. nontermmap[i] = n++;
  347. /* Shuffle elements of tables indexed by symbol number. */
  348. for (i = ntokens; i < nsyms; i++)
  349. {
  350. n = nontermmap[i];
  351. if (n >= 0)
  352. {
  353. sassoc[n] = sassoc[i];
  354. sprec[n] = sprec[i];
  355. tags[n] = tags[i];
  356. } else {
  357. free(tags[i]);
  358. }
  359. }
  360. /* Replace all symbol numbers in valid data structures. */
  361. for (i = 1; i <= nrules; i++)
  362. rlhs[i] = nontermmap[rlhs[i]];
  363. for (r = ritem; *r; r++)
  364. if (ISVAR(*r))
  365. *r = nontermmap[*r];
  366. start_symbol = nontermmap[start_symbol];
  367. nsyms -= nuseless_nonterminals;
  368. nvars -= nuseless_nonterminals;
  369. free(&nontermmap[ntokens]);
  370. }
  371. }
  372. static void
  373. print_results ()
  374. {
  375. int i;
  376. /* short j; JF unused */
  377. rule r;
  378. bool b;
  379. if (nuseless_nonterminals > 0)
  380. {
  381. fprintf(foutput, "Useless nonterminals:\n\n");
  382. for (i = ntokens; i < nsyms; i++)
  383. if (!BITISSET(V, i))
  384. fprintf(foutput, " %s\n", tags[i]);
  385. }
  386. b = FALSE;
  387. for (i = 0; i < ntokens; i++)
  388. {
  389. if (!BITISSET(V, i) && !BITISSET(V1, i))
  390. {
  391. if (!b)
  392. {
  393. fprintf(foutput, "\n\nTerminals which are not used:\n\n");
  394. b = TRUE;
  395. }
  396. fprintf(foutput, " %s\n", tags[i]);
  397. }
  398. }
  399. if (nuseless_productions > 0)
  400. {
  401. fprintf(foutput, "\n\nUseless rules:\n\n");
  402. for (i = 1; i <= nrules; i++)
  403. {
  404. if (!BITISSET(P, i))
  405. {
  406. fprintf(foutput, "#%-4d ", i);
  407. fprintf(foutput, "%s :\t", tags[rlhs[i]]);
  408. for (r = &ritem[rrhs[i]]; *r >= 0; r++)
  409. {
  410. fprintf(foutput, " %s", tags[*r]);
  411. }
  412. fprintf(foutput, ";\n");
  413. }
  414. }
  415. }
  416. if (nuseless_nonterminals > 0 || nuseless_productions > 0 || b)
  417. fprintf(foutput, "\n\n");
  418. }
  419. void
  420. dump_grammar ()
  421. {
  422. int i;
  423. rule r;
  424. fprintf(foutput,
  425. "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
  426. ntokens, nvars, nsyms, nrules, nitems);
  427. fprintf(foutput, "Variables\n---------\n\n");
  428. fprintf(foutput, "Value Sprec Sassoc Tag\n");
  429. for (i = ntokens; i < nsyms; i++)
  430. fprintf(foutput, "%5d %5d %5d %s\n",
  431. i, sprec[i], sassoc[i], tags[i]);
  432. fprintf(foutput, "\n\n");
  433. fprintf(foutput, "Rules\n-----\n\n");
  434. for (i = 1; i <= nrules; i++)
  435. {
  436. fprintf(foutput, "%-5d(%5d%5d)%5d : (@%-5d)",
  437. i, rprec[i], rassoc[i], rlhs[i], rrhs[i]);
  438. for (r = &ritem[rrhs[i]]; *r > 0; r++)
  439. fprintf(foutput, "%5d", *r);
  440. fprintf(foutput, " [%d]\n", -(*r));
  441. }
  442. fprintf(foutput, "\n\n");
  443. fprintf(foutput, "Rules interpreted\n-----------------\n\n");
  444. for (i = 1; i <= nrules; i++)
  445. {
  446. fprintf(foutput, "%-5d %s :", i, tags[rlhs[i]]);
  447. for (r = &ritem[rrhs[i]]; *r > 0; r++)
  448. fprintf(foutput, " %s", tags[*r]);
  449. fprintf(foutput, "\n");
  450. }
  451. fprintf(foutput, "\n\n");
  452. }
  453. static void
  454. print_notices ()
  455. {
  456. extern int fixed_outfiles;
  457. if (fixed_outfiles && nuseless_productions)
  458. fprintf(stderr, "%d rules never reduced\n", nuseless_productions);
  459. fprintf(stderr, "%s contains ", infile);
  460. if (nuseless_nonterminals > 0)
  461. {
  462. fprintf(stderr, "%d useless nonterminal%s",
  463. nuseless_nonterminals,
  464. (nuseless_nonterminals == 1 ? "" : "s"));
  465. }
  466. if (nuseless_nonterminals > 0 && nuseless_productions > 0)
  467. fprintf(stderr, " and ");
  468. if (nuseless_productions > 0)
  469. {
  470. fprintf(stderr, "%d useless rule%s",
  471. nuseless_productions,
  472. (nuseless_productions == 1 ? "" : "s"));
  473. }
  474. fprintf(stderr, ".\n");
  475. fflush(stderr);
  476. }