conflicts.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /* Find and resolve or report lookahead conflicts for bison,
  2. Copyright (C) 1984, 1989, 1992, 2000-2013 Free Software Foundation,
  3. Inc.
  4. This file is part of Bison, the GNU Compiler Compiler.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. #include "system.h"
  17. #include <bitset.h>
  18. #include "LR0.h"
  19. #include "complain.h"
  20. #include "conflicts.h"
  21. #include "files.h"
  22. #include "getargs.h"
  23. #include "gram.h"
  24. #include "lalr.h"
  25. #include "print-xml.h"
  26. #include "reader.h"
  27. #include "state.h"
  28. #include "symtab.h"
  29. /* -1 stands for not specified. */
  30. int expected_sr_conflicts = -1;
  31. int expected_rr_conflicts = -1;
  32. static char *conflicts;
  33. static struct obstack solved_conflicts_obstack;
  34. static struct obstack solved_conflicts_xml_obstack;
  35. static bitset shift_set;
  36. static bitset lookahead_set;
  37. enum conflict_resolution
  38. {
  39. shift_resolution,
  40. reduce_resolution,
  41. left_resolution,
  42. right_resolution,
  43. nonassoc_resolution
  44. };
  45. /*----------------------------------------------------------------.
  46. | Explain how an SR conflict between TOKEN and RULE was resolved: |
  47. | RESOLUTION. |
  48. `----------------------------------------------------------------*/
  49. static inline void
  50. log_resolution (rule *r, symbol_number token,
  51. enum conflict_resolution resolution)
  52. {
  53. if (report_flag & report_solved_conflicts)
  54. {
  55. /* The description of the resolution. */
  56. switch (resolution)
  57. {
  58. case shift_resolution:
  59. case right_resolution:
  60. obstack_printf (&solved_conflicts_obstack,
  61. _(" Conflict between rule %d and token %s"
  62. " resolved as shift"),
  63. r->number,
  64. symbols[token]->tag);
  65. break;
  66. case reduce_resolution:
  67. case left_resolution:
  68. obstack_printf (&solved_conflicts_obstack,
  69. _(" Conflict between rule %d and token %s"
  70. " resolved as reduce"),
  71. r->number,
  72. symbols[token]->tag);
  73. break;
  74. case nonassoc_resolution:
  75. obstack_printf (&solved_conflicts_obstack,
  76. _(" Conflict between rule %d and token %s"
  77. " resolved as an error"),
  78. r->number,
  79. symbols[token]->tag);
  80. break;
  81. }
  82. /* The reason. */
  83. switch (resolution)
  84. {
  85. case shift_resolution:
  86. obstack_printf (&solved_conflicts_obstack,
  87. " (%s < %s)",
  88. r->prec->tag,
  89. symbols[token]->tag);
  90. break;
  91. case reduce_resolution:
  92. obstack_printf (&solved_conflicts_obstack,
  93. " (%s < %s)",
  94. symbols[token]->tag,
  95. r->prec->tag);
  96. break;
  97. case left_resolution:
  98. obstack_printf (&solved_conflicts_obstack,
  99. " (%%left %s)",
  100. symbols[token]->tag);
  101. break;
  102. case right_resolution:
  103. obstack_printf (&solved_conflicts_obstack,
  104. " (%%right %s)",
  105. symbols[token]->tag);
  106. break;
  107. case nonassoc_resolution:
  108. obstack_printf (&solved_conflicts_obstack,
  109. " (%%nonassoc %s)",
  110. symbols[token]->tag);
  111. break;
  112. }
  113. obstack_sgrow (&solved_conflicts_obstack, ".\n");
  114. }
  115. /* XML report */
  116. if (xml_flag)
  117. {
  118. /* The description of the resolution. */
  119. switch (resolution)
  120. {
  121. case shift_resolution:
  122. case right_resolution:
  123. obstack_printf (&solved_conflicts_xml_obstack,
  124. " <resolution rule=\"%d\" symbol=\"%s\""
  125. " type=\"shift\">",
  126. r->number,
  127. xml_escape (symbols[token]->tag));
  128. break;
  129. case reduce_resolution:
  130. case left_resolution:
  131. obstack_printf (&solved_conflicts_xml_obstack,
  132. " <resolution rule=\"%d\" symbol=\"%s\""
  133. " type=\"reduce\">",
  134. r->number,
  135. xml_escape (symbols[token]->tag));
  136. break;
  137. case nonassoc_resolution:
  138. obstack_printf (&solved_conflicts_xml_obstack,
  139. " <resolution rule=\"%d\" symbol=\"%s\""
  140. " type=\"error\">",
  141. r->number,
  142. xml_escape (symbols[token]->tag));
  143. break;
  144. }
  145. /* The reason. */
  146. switch (resolution)
  147. {
  148. case shift_resolution:
  149. obstack_printf (&solved_conflicts_xml_obstack,
  150. "%s &lt; %s",
  151. xml_escape_n (0, r->prec->tag),
  152. xml_escape_n (1, symbols[token]->tag));
  153. break;
  154. case reduce_resolution:
  155. obstack_printf (&solved_conflicts_xml_obstack,
  156. "%s &lt; %s",
  157. xml_escape_n (0, symbols[token]->tag),
  158. xml_escape_n (1, r->prec->tag));
  159. break;
  160. case left_resolution:
  161. obstack_printf (&solved_conflicts_xml_obstack,
  162. "%%left %s",
  163. xml_escape (symbols[token]->tag));
  164. break;
  165. case right_resolution:
  166. obstack_printf (&solved_conflicts_xml_obstack,
  167. "%%right %s",
  168. xml_escape (symbols[token]->tag));
  169. break;
  170. case nonassoc_resolution:
  171. obstack_printf (&solved_conflicts_xml_obstack,
  172. "%%nonassoc %s",
  173. xml_escape (symbols[token]->tag));
  174. break;
  175. }
  176. obstack_sgrow (&solved_conflicts_xml_obstack, "</resolution>\n");
  177. }
  178. }
  179. /*------------------------------------------------------------------.
  180. | Turn off the shift recorded for the specified token in the |
  181. | specified state. Used when we resolve a shift-reduce conflict in |
  182. | favor of the reduction or as an error (%nonassoc). |
  183. `------------------------------------------------------------------*/
  184. static void
  185. flush_shift (state *s, int token)
  186. {
  187. transitions *trans = s->transitions;
  188. int i;
  189. bitset_reset (lookahead_set, token);
  190. for (i = 0; i < trans->num; i++)
  191. if (!TRANSITION_IS_DISABLED (trans, i)
  192. && TRANSITION_SYMBOL (trans, i) == token)
  193. TRANSITION_DISABLE (trans, i);
  194. }
  195. /*--------------------------------------------------------------------.
  196. | Turn off the reduce recorded for the specified token in the |
  197. | specified lookahead set. Used when we resolve a shift-reduce |
  198. | conflict in favor of the shift or as an error (%nonassoc). |
  199. `--------------------------------------------------------------------*/
  200. static void
  201. flush_reduce (bitset lookahead_tokens, int token)
  202. {
  203. bitset_reset (lookahead_tokens, token);
  204. }
  205. /*------------------------------------------------------------------.
  206. | Attempt to resolve shift-reduce conflict for one rule by means of |
  207. | precedence declarations. It has already been checked that the |
  208. | rule has a precedence. A conflict is resolved by modifying the |
  209. | shift or reduce tables so that there is no longer a conflict. |
  210. | |
  211. | RULENO is the number of the lookahead bitset to consider. |
  212. | |
  213. | ERRORS and NERRS can be used to store discovered explicit |
  214. | errors. |
  215. `------------------------------------------------------------------*/
  216. static void
  217. resolve_sr_conflict (state *s, int ruleno, symbol **errors, int *nerrs)
  218. {
  219. symbol_number i;
  220. reductions *reds = s->reductions;
  221. /* Find the rule to reduce by to get precedence of reduction. */
  222. rule *redrule = reds->rules[ruleno];
  223. int redprec = redrule->prec->prec;
  224. bitset lookahead_tokens = reds->lookahead_tokens[ruleno];
  225. for (i = 0; i < ntokens; i++)
  226. if (bitset_test (lookahead_tokens, i)
  227. && bitset_test (lookahead_set, i)
  228. && symbols[i]->prec)
  229. {
  230. /* Shift-reduce conflict occurs for token number i
  231. and it has a precedence.
  232. The precedence of shifting is that of token i. */
  233. if (symbols[i]->prec < redprec)
  234. {
  235. register_precedence (redrule->prec->number, i);
  236. log_resolution (redrule, i, reduce_resolution);
  237. flush_shift (s, i);
  238. }
  239. else if (symbols[i]->prec > redprec)
  240. {
  241. register_precedence (i, redrule->prec->number);
  242. log_resolution (redrule, i, shift_resolution);
  243. flush_reduce (lookahead_tokens, i);
  244. }
  245. else
  246. /* Matching precedence levels.
  247. For non-defined associativity, keep both: unexpected
  248. associativity conflict.
  249. For left associativity, keep only the reduction.
  250. For right associativity, keep only the shift.
  251. For nonassociativity, keep neither. */
  252. switch (symbols[i]->assoc)
  253. {
  254. case undef_assoc:
  255. abort ();
  256. case precedence_assoc:
  257. break;
  258. case right_assoc:
  259. register_assoc (i, redrule->prec->number);
  260. log_resolution (redrule, i, right_resolution);
  261. flush_reduce (lookahead_tokens, i);
  262. break;
  263. case left_assoc:
  264. register_assoc (i, redrule->prec->number);
  265. log_resolution (redrule, i, left_resolution);
  266. flush_shift (s, i);
  267. break;
  268. case non_assoc:
  269. register_assoc (i, redrule->prec->number);
  270. log_resolution (redrule, i, nonassoc_resolution);
  271. flush_shift (s, i);
  272. flush_reduce (lookahead_tokens, i);
  273. /* Record an explicit error for this token. */
  274. errors[(*nerrs)++] = symbols[i];
  275. break;
  276. }
  277. }
  278. }
  279. /*-------------------------------------------------------------------.
  280. | Solve the S/R conflicts of state S using the |
  281. | precedence/associativity, and flag it inconsistent if it still has |
  282. | conflicts. ERRORS can be used as storage to compute the list of |
  283. | lookahead tokens on which S raises a syntax error (%nonassoc). |
  284. `-------------------------------------------------------------------*/
  285. static void
  286. set_conflicts (state *s, symbol **errors)
  287. {
  288. int i;
  289. transitions *trans = s->transitions;
  290. reductions *reds = s->reductions;
  291. int nerrs = 0;
  292. if (s->consistent)
  293. return;
  294. bitset_zero (lookahead_set);
  295. FOR_EACH_SHIFT (trans, i)
  296. bitset_set (lookahead_set, TRANSITION_SYMBOL (trans, i));
  297. /* Loop over all rules which require lookahead in this state. First
  298. check for shift-reduce conflict, and try to resolve using
  299. precedence. */
  300. for (i = 0; i < reds->num; ++i)
  301. if (reds->rules[i]->prec && reds->rules[i]->prec->prec
  302. && !bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set))
  303. resolve_sr_conflict (s, i, errors, &nerrs);
  304. if (nerrs)
  305. {
  306. /* Some tokens have been explicitly made errors. Allocate a
  307. permanent errs structure for this state, to record them. */
  308. state_errs_set (s, nerrs, errors);
  309. }
  310. if (obstack_object_size (&solved_conflicts_obstack))
  311. s->solved_conflicts = obstack_finish0 (&solved_conflicts_obstack);
  312. if (obstack_object_size (&solved_conflicts_xml_obstack))
  313. s->solved_conflicts_xml = obstack_finish0 (&solved_conflicts_xml_obstack);
  314. /* Loop over all rules which require lookahead in this state. Check
  315. for conflicts not resolved above. */
  316. for (i = 0; i < reds->num; ++i)
  317. {
  318. if (!bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set))
  319. conflicts[s->number] = 1;
  320. bitset_or (lookahead_set, lookahead_set, reds->lookahead_tokens[i]);
  321. }
  322. }
  323. /*----------------------------------------------------------------.
  324. | Solve all the S/R conflicts using the precedence/associativity, |
  325. | and flag as inconsistent the states that still have conflicts. |
  326. `----------------------------------------------------------------*/
  327. void
  328. conflicts_solve (void)
  329. {
  330. state_number i;
  331. /* List of lookahead tokens on which we explicitly raise a syntax error. */
  332. symbol **errors = xnmalloc (ntokens + 1, sizeof *errors);
  333. conflicts = xcalloc (nstates, sizeof *conflicts);
  334. shift_set = bitset_create (ntokens, BITSET_FIXED);
  335. lookahead_set = bitset_create (ntokens, BITSET_FIXED);
  336. obstack_init (&solved_conflicts_obstack);
  337. obstack_init (&solved_conflicts_xml_obstack);
  338. for (i = 0; i < nstates; i++)
  339. {
  340. set_conflicts (states[i], errors);
  341. /* For uniformity of the code, make sure all the states have a valid
  342. 'errs' member. */
  343. if (!states[i]->errs)
  344. states[i]->errs = errs_new (0, 0);
  345. }
  346. free (errors);
  347. }
  348. void
  349. conflicts_update_state_numbers (state_number old_to_new[],
  350. state_number nstates_old)
  351. {
  352. state_number i;
  353. for (i = 0; i < nstates_old; ++i)
  354. if (old_to_new[i] != nstates_old)
  355. conflicts[old_to_new[i]] = conflicts[i];
  356. }
  357. /*---------------------------------------------.
  358. | Count the number of shift/reduce conflicts. |
  359. `---------------------------------------------*/
  360. static size_t
  361. count_state_sr_conflicts (state *s)
  362. {
  363. int i;
  364. transitions *trans = s->transitions;
  365. reductions *reds = s->reductions;
  366. if (!trans)
  367. return 0;
  368. bitset_zero (lookahead_set);
  369. bitset_zero (shift_set);
  370. FOR_EACH_SHIFT (trans, i)
  371. bitset_set (shift_set, TRANSITION_SYMBOL (trans, i));
  372. for (i = 0; i < reds->num; ++i)
  373. bitset_or (lookahead_set, lookahead_set, reds->lookahead_tokens[i]);
  374. bitset_and (lookahead_set, lookahead_set, shift_set);
  375. return bitset_count (lookahead_set);
  376. }
  377. /*---------------------------------------------.
  378. | The total number of shift/reduce conflicts. |
  379. `---------------------------------------------*/
  380. static size_t
  381. count_sr_conflicts (void)
  382. {
  383. size_t res = 0;
  384. state_number i;
  385. /* Conflicts by state. */
  386. for (i = 0; i < nstates; i++)
  387. if (conflicts[i])
  388. res += count_state_sr_conflicts (states[i]);
  389. return res;
  390. }
  391. /*----------------------------------------------------------------.
  392. | Count the number of reduce/reduce conflicts. If ONE_PER_TOKEN, |
  393. | count one conflict for each token that has any reduce/reduce |
  394. | conflicts. Otherwise, count one conflict for each pair of |
  395. | conflicting reductions. |
  396. `----------------------------------------------------------------*/
  397. static size_t
  398. count_state_rr_conflicts (state *s, bool one_per_token)
  399. {
  400. int i;
  401. reductions *reds = s->reductions;
  402. size_t res = 0;
  403. for (i = 0; i < ntokens; i++)
  404. {
  405. int count = 0;
  406. int j;
  407. for (j = 0; j < reds->num; ++j)
  408. count += bitset_test (reds->lookahead_tokens[j], i);
  409. if (count >= 2)
  410. res += one_per_token ? 1 : count-1;
  411. }
  412. return res;
  413. }
  414. static size_t
  415. count_rr_conflicts (bool one_per_token)
  416. {
  417. size_t res = 0;
  418. state_number i;
  419. /* Conflicts by state. */
  420. for (i = 0; i < nstates; i++)
  421. if (conflicts[i])
  422. res += count_state_rr_conflicts (states[i], one_per_token);
  423. return res;
  424. }
  425. /*-----------------------------------------------------------.
  426. | Output the detailed description of states with conflicts. |
  427. `-----------------------------------------------------------*/
  428. void
  429. conflicts_output (FILE *out)
  430. {
  431. bool printed_sth = false;
  432. state_number i;
  433. for (i = 0; i < nstates; i++)
  434. {
  435. state *s = states[i];
  436. if (conflicts[i])
  437. {
  438. int src = count_state_sr_conflicts (s);
  439. int rrc = count_state_rr_conflicts (s, true);
  440. fprintf (out, _("State %d "), i);
  441. if (src && rrc)
  442. fprintf (out,
  443. _("conflicts: %d shift/reduce, %d reduce/reduce\n"),
  444. src, rrc);
  445. else if (src)
  446. fprintf (out, _("conflicts: %d shift/reduce\n"), src);
  447. else if (rrc)
  448. fprintf (out, _("conflicts: %d reduce/reduce\n"), rrc);
  449. printed_sth = true;
  450. }
  451. }
  452. if (printed_sth)
  453. fputs ("\n\n", out);
  454. }
  455. /*--------------------------------------------------------.
  456. | Total the number of S/R and R/R conflicts. Unlike the |
  457. | code in conflicts_output, however, count EACH pair of |
  458. | reductions for the same state and lookahead as one |
  459. | conflict. |
  460. `--------------------------------------------------------*/
  461. int
  462. conflicts_total_count (void)
  463. {
  464. return count_sr_conflicts () + count_rr_conflicts (false);
  465. }
  466. /*------------------------------------------.
  467. | Reporting the total number of conflicts. |
  468. `------------------------------------------*/
  469. void
  470. conflicts_print (void)
  471. {
  472. if (! glr_parser && expected_rr_conflicts != -1)
  473. {
  474. complain (NULL, Wother, _("%%expect-rr applies only to GLR parsers"));
  475. expected_rr_conflicts = -1;
  476. }
  477. /* Screams for factoring, but almost useless because of the
  478. different strings to translate. */
  479. {
  480. int total = count_sr_conflicts ();
  481. /* If %expect is not used, but %expect-rr is, then expect 0 sr. */
  482. int expected =
  483. (expected_sr_conflicts == -1 && expected_rr_conflicts != -1)
  484. ? 0
  485. : expected_sr_conflicts;
  486. if (expected != -1)
  487. {
  488. if (expected != total)
  489. complain (NULL, complaint,
  490. _("shift/reduce conflicts: %d found, %d expected"),
  491. total, expected);
  492. }
  493. else if (total)
  494. complain (NULL, Wconflicts_sr,
  495. ngettext ("%d shift/reduce conflict",
  496. "%d shift/reduce conflicts",
  497. total),
  498. total);
  499. }
  500. {
  501. int total = count_rr_conflicts (true);
  502. /* If %expect-rr is not used, but %expect is, then expect 0 rr. */
  503. int expected =
  504. (expected_rr_conflicts == -1 && expected_sr_conflicts != -1)
  505. ? 0
  506. : expected_rr_conflicts;
  507. if (expected != -1)
  508. {
  509. if (expected != total)
  510. complain (NULL, complaint,
  511. _("reduce/reduce conflicts: %d found, %d expected"),
  512. total, expected);
  513. }
  514. else if (total)
  515. complain (NULL, Wconflicts_rr,
  516. ngettext ("%d reduce/reduce conflict",
  517. "%d reduce/reduce conflicts",
  518. total),
  519. total);
  520. }
  521. }
  522. void
  523. conflicts_free (void)
  524. {
  525. free (conflicts);
  526. bitset_free (shift_set);
  527. bitset_free (lookahead_set);
  528. obstack_free (&solved_conflicts_obstack, NULL);
  529. obstack_free (&solved_conflicts_xml_obstack, NULL);
  530. }