conflicts.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /* Find and resolve or report look-ahead conflicts for bison,
  2. Copyright (C) 1984, 1989, 1992 Free Software Foundation, Inc.
  3. Modified (1992) from bison-1.19 by
  4. Wilfred J. Hansen (wjh+@cmu.edu)
  5. Andrew Consortium, Carnegie Mellon University
  6. This file is part of Bison, the GNU Compiler Compiler.
  7. Bison is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with Bison; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  18. #include <stdio.h>
  19. #include "system.h"
  20. #include "machine.h"
  21. #include "new.h"
  22. #include "files.h"
  23. #include "gram.h"
  24. #include "state.h"
  25. extern char **tags;
  26. extern int tokensetsize;
  27. extern char *consistent;
  28. extern short *accessing_symbol;
  29. extern shifts **shift_table;
  30. extern unsigned *LA;
  31. extern short *LAruleno;
  32. extern short *lookaheads;
  33. extern int verboseflag;
  34. void set_conflicts();
  35. void resolve_sr_conflict();
  36. void flush_shift();
  37. void log_resolution();
  38. void total_conflicts();
  39. void count_sr_conflicts();
  40. void count_rr_conflicts();
  41. char any_conflicts;
  42. char *conflicts;
  43. errs **err_table;
  44. int expected_conflicts;
  45. static unsigned *shiftset;
  46. static unsigned *lookaheadset;
  47. static int src_total;
  48. static int rrc_total;
  49. static int src_count;
  50. static int rrc_count;
  51. void
  52. initialize_conflicts()
  53. {
  54. register int i;
  55. /* register errs *sp; JF unused */
  56. conflicts = NEW2(nstates, char);
  57. shiftset = NEW2(tokensetsize, unsigned);
  58. lookaheadset = NEW2(tokensetsize, unsigned);
  59. err_table = NEW2(nstates, errs *);
  60. any_conflicts = 0;
  61. for (i = 0; i < nstates; i++)
  62. set_conflicts(i);
  63. }
  64. void
  65. set_conflicts(state)
  66. int state;
  67. {
  68. register int i;
  69. register int k;
  70. register shifts *shiftp;
  71. register unsigned *fp2;
  72. register unsigned *fp3;
  73. register unsigned *fp4;
  74. register unsigned *fp1;
  75. register int symbol;
  76. if (consistent[state]) return;
  77. for (i = 0; i < tokensetsize; i++)
  78. lookaheadset[i] = 0;
  79. shiftp = shift_table[state];
  80. if (shiftp)
  81. {
  82. k = shiftp->nshifts;
  83. for (i = 0; i < k; i++)
  84. {
  85. symbol = accessing_symbol[shiftp->shifts[i]];
  86. if (ISVAR(symbol)) break;
  87. SETBIT(lookaheadset, symbol);
  88. }
  89. }
  90. k = lookaheads[state + 1];
  91. fp4 = lookaheadset + tokensetsize;
  92. /* loop over all rules which require lookahead in this state */
  93. /* first check for shift-reduce conflict, and try to resolve using precedence */
  94. for (i = lookaheads[state]; i < k; i++)
  95. if (rprec[LAruleno[i]])
  96. {
  97. fp1 = LA + i * tokensetsize;
  98. fp2 = fp1;
  99. fp3 = lookaheadset;
  100. while (fp3 < fp4)
  101. {
  102. if (*fp2++ & *fp3++)
  103. {
  104. resolve_sr_conflict(state, i);
  105. break;
  106. }
  107. }
  108. }
  109. /* loop over all rules which require lookahead in this state */
  110. /* Check for conflicts not resolved above. */
  111. for (i = lookaheads[state]; i < k; i++)
  112. {
  113. fp1 = LA + i * tokensetsize;
  114. fp2 = fp1;
  115. fp3 = lookaheadset;
  116. while (fp3 < fp4)
  117. {
  118. if (*fp2++ & *fp3++)
  119. {
  120. conflicts[state] = 1;
  121. any_conflicts = 1;
  122. }
  123. }
  124. fp2 = fp1;
  125. fp3 = lookaheadset;
  126. while (fp3 < fp4)
  127. *fp3++ |= *fp2++;
  128. }
  129. }
  130. /* Attempt to resolve shift-reduce conflict for one rule
  131. by means of precedence declarations.
  132. It has already been checked that the rule has a precedence.
  133. A conflict is resolved by modifying the shift or reduce tables
  134. so that there is no longer a conflict. */
  135. void
  136. resolve_sr_conflict(state, lookaheadnum)
  137. int state;
  138. int lookaheadnum;
  139. {
  140. register int i;
  141. register int mask;
  142. register unsigned *fp1;
  143. register unsigned *fp2;
  144. register int redprec;
  145. errs *errp = (errs *) xmalloc (sizeof(errs) + ntokens * sizeof(short));
  146. short *errtokens = errp->errs;
  147. /* find the rule to reduce by to get precedence of reduction */
  148. redprec = rprec[LAruleno[lookaheadnum]];
  149. mask = 1;
  150. fp1 = LA + lookaheadnum * tokensetsize;
  151. fp2 = lookaheadset;
  152. for (i = 0; i < ntokens; i++)
  153. {
  154. if ((mask & *fp2 & *fp1) && sprec[i])
  155. /* Shift-reduce conflict occurs for token number i
  156. and it has a precedence.
  157. The precedence of shifting is that of token i. */
  158. {
  159. if (sprec[i] < redprec)
  160. {
  161. if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  162. *fp2 &= ~mask; /* flush the shift for this token */
  163. flush_shift(state, i);
  164. }
  165. else if (sprec[i] > redprec)
  166. {
  167. if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  168. *fp1 &= ~mask; /* flush the reduce for this token */
  169. }
  170. else
  171. {
  172. /* Matching precedence levels.
  173. For left association, keep only the reduction.
  174. For right association, keep only the shift.
  175. For nonassociation, keep neither. */
  176. switch (sassoc[i])
  177. {
  178. case RIGHT_ASSOC:
  179. if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  180. break;
  181. case LEFT_ASSOC:
  182. if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  183. break;
  184. case NON_ASSOC:
  185. if (verboseflag) log_resolution(state, lookaheadnum, i, "an error");
  186. break;
  187. }
  188. if (sassoc[i] != RIGHT_ASSOC)
  189. {
  190. *fp2 &= ~mask; /* flush the shift for this token */
  191. flush_shift(state, i);
  192. }
  193. if (sassoc[i] != LEFT_ASSOC)
  194. {
  195. *fp1 &= ~mask; /* flush the reduce for this token */
  196. }
  197. if (sassoc[i] == NON_ASSOC)
  198. {
  199. /* Record an explicit error for this token. */
  200. *errtokens++ = i;
  201. }
  202. }
  203. }
  204. mask <<= 1;
  205. if (mask == 0)
  206. {
  207. mask = 1;
  208. fp2++; fp1++;
  209. }
  210. }
  211. errp->nerrs = errtokens - errp->errs;
  212. if (errp->nerrs)
  213. {
  214. /* Some tokens have been explicitly made errors. Allocate
  215. a permanent errs structure for this state, to record them. */
  216. i = (char *) errtokens - (char *) errp;
  217. err_table[state] = (errs *) xmalloc ((unsigned int)i);
  218. bcopy (errp, err_table[state], i);
  219. }
  220. else
  221. err_table[state] = 0;
  222. free(errp);
  223. }
  224. /* turn off the shift recorded for the specified token in the specified state.
  225. Used when we resolve a shift-reduce conflict in favor of the reduction. */
  226. void
  227. flush_shift(state, token)
  228. int state;
  229. int token;
  230. {
  231. register shifts *shiftp;
  232. register int k, i;
  233. /* register unsigned symbol; JF unused */
  234. shiftp = shift_table[state];
  235. if (shiftp)
  236. {
  237. k = shiftp->nshifts;
  238. for (i = 0; i < k; i++)
  239. {
  240. if (shiftp->shifts[i] && token == accessing_symbol[shiftp->shifts[i]])
  241. (shiftp->shifts[i]) = 0;
  242. }
  243. }
  244. }
  245. void
  246. log_resolution(state, LAno, token, resolution)
  247. int state, LAno, token;
  248. char *resolution;
  249. {
  250. fprintf(foutput,
  251. "Conflict in state %d between rule %d and token %s resolved as %s.\n",
  252. state, LAruleno[LAno], tags[token], resolution);
  253. }
  254. void
  255. conflict_log()
  256. {
  257. register int i;
  258. src_total = 0;
  259. rrc_total = 0;
  260. for (i = 0; i < nstates; i++)
  261. {
  262. if (conflicts[i])
  263. {
  264. count_sr_conflicts(i);
  265. count_rr_conflicts(i);
  266. src_total += src_count;
  267. rrc_total += rrc_count;
  268. }
  269. }
  270. total_conflicts();
  271. }
  272. void
  273. verbose_conflict_log()
  274. {
  275. register int i;
  276. src_total = 0;
  277. rrc_total = 0;
  278. for (i = 0; i < nstates; i++)
  279. {
  280. if (conflicts[i])
  281. {
  282. count_sr_conflicts(i);
  283. count_rr_conflicts(i);
  284. src_total += src_count;
  285. rrc_total += rrc_count;
  286. fprintf(foutput, "State %d contains", i);
  287. if (src_count == 1)
  288. fprintf(foutput, " 1 shift/reduce conflict");
  289. else if (src_count > 1)
  290. fprintf(foutput, " %d shift/reduce conflicts", src_count);
  291. if (src_count > 0 && rrc_count > 0)
  292. fprintf(foutput, " and");
  293. if (rrc_count == 1)
  294. fprintf(foutput, " 1 reduce/reduce conflict");
  295. else if (rrc_count > 1)
  296. fprintf(foutput, " %d reduce/reduce conflicts", rrc_count);
  297. putc('.', foutput);
  298. putc('\n', foutput);
  299. }
  300. }
  301. total_conflicts();
  302. }
  303. void
  304. total_conflicts()
  305. {
  306. extern int fixed_outfiles;
  307. if (src_total == expected_conflicts && rrc_total == 0)
  308. return;
  309. if (fixed_outfiles)
  310. {
  311. /* If invoked under the name `yacc', use the output format
  312. specified by POSIX. */
  313. fprintf(stderr, "conflicts: ");
  314. if (src_total > 0)
  315. fprintf(stderr, " %d shift/reduce", src_total);
  316. if (src_total > 0 && rrc_total > 0)
  317. fprintf(stderr, ",");
  318. if (rrc_total > 0)
  319. fprintf(stderr, " %d reduce/reduce", rrc_total);
  320. putc('\n', stderr);
  321. }
  322. else
  323. {
  324. fprintf(stderr, "%s contains", infile);
  325. if (src_total == 1)
  326. fprintf(stderr, " 1 shift/reduce conflict");
  327. else if (src_total > 1)
  328. fprintf(stderr, " %d shift/reduce conflicts", src_total);
  329. if (src_total > 0 && rrc_total > 0)
  330. fprintf(stderr, " and");
  331. if (rrc_total == 1)
  332. fprintf(stderr, " 1 reduce/reduce conflict");
  333. else if (rrc_total > 1)
  334. fprintf(stderr, " %d reduce/reduce conflicts", rrc_total);
  335. putc('.', stderr);
  336. putc('\n', stderr);
  337. }
  338. }
  339. void
  340. count_sr_conflicts(state)
  341. int state;
  342. {
  343. register int i;
  344. register int k;
  345. register int mask;
  346. register shifts *shiftp;
  347. register unsigned *fp1;
  348. register unsigned *fp2;
  349. register unsigned *fp3;
  350. register int symbol;
  351. src_count = 0;
  352. shiftp = shift_table[state];
  353. if (!shiftp) return;
  354. for (i = 0; i < tokensetsize; i++)
  355. {
  356. shiftset[i] = 0;
  357. lookaheadset[i] = 0;
  358. }
  359. k = shiftp->nshifts;
  360. for (i = 0; i < k; i++)
  361. {
  362. if (! shiftp->shifts[i]) continue;
  363. symbol = accessing_symbol[shiftp->shifts[i]];
  364. if (ISVAR(symbol)) break;
  365. SETBIT(shiftset, symbol);
  366. }
  367. k = lookaheads[state + 1];
  368. fp3 = lookaheadset + tokensetsize;
  369. for (i = lookaheads[state]; i < k; i++)
  370. {
  371. fp1 = LA + i * tokensetsize;
  372. fp2 = lookaheadset;
  373. while (fp2 < fp3)
  374. *fp2++ |= *fp1++;
  375. }
  376. fp1 = shiftset;
  377. fp2 = lookaheadset;
  378. while (fp2 < fp3)
  379. *fp2++ &= *fp1++;
  380. mask = 1;
  381. fp2 = lookaheadset;
  382. for (i = 0; i < ntokens; i++)
  383. {
  384. if (mask & *fp2)
  385. src_count++;
  386. mask <<= 1;
  387. if (mask == 0)
  388. {
  389. mask = 1;
  390. fp2++;
  391. }
  392. }
  393. }
  394. void
  395. count_rr_conflicts(state)
  396. int state;
  397. {
  398. register int i;
  399. register int j;
  400. register int count;
  401. register unsigned mask;
  402. register unsigned *baseword;
  403. register unsigned *wordp;
  404. register int m;
  405. register int n;
  406. rrc_count = 0;
  407. m = lookaheads[state];
  408. n = lookaheads[state + 1];
  409. if (n - m < 2) return;
  410. mask = 1;
  411. baseword = LA + m * tokensetsize;
  412. for (i = 0; i < ntokens; i++)
  413. {
  414. wordp = baseword;
  415. count = 0;
  416. for (j = m; j < n; j++)
  417. {
  418. if (mask & *wordp)
  419. count++;
  420. wordp += tokensetsize;
  421. }
  422. if (count >= 2) rrc_count++;
  423. mask <<= 1;
  424. if (mask == 0)
  425. {
  426. mask = 1;
  427. baseword++;
  428. }
  429. }
  430. }
  431. void
  432. print_reductions(state)
  433. int state;
  434. {
  435. register int i;
  436. register int j;
  437. register int k;
  438. register unsigned *fp1;
  439. register unsigned *fp2;
  440. register unsigned *fp3;
  441. register unsigned *fp4;
  442. register int rule;
  443. register int symbol;
  444. register unsigned mask;
  445. register int m;
  446. register int n;
  447. register int default_LA;
  448. register int default_rule;
  449. register int cmax;
  450. register int count;
  451. register shifts *shiftp;
  452. register errs *errp;
  453. int nodefault = 0;
  454. for (i = 0; i < tokensetsize; i++)
  455. shiftset[i] = 0;
  456. shiftp = shift_table[state];
  457. if (shiftp)
  458. {
  459. k = shiftp->nshifts;
  460. for (i = 0; i < k; i++)
  461. {
  462. if (! shiftp->shifts[i]) continue;
  463. symbol = accessing_symbol[shiftp->shifts[i]];
  464. if (ISVAR(symbol)) break;
  465. /* if this state has a shift for the error token,
  466. don't use a default rule. */
  467. if (symbol == error_token_number) nodefault = 1;
  468. SETBIT(shiftset, symbol);
  469. }
  470. }
  471. errp = err_table[state];
  472. if (errp)
  473. {
  474. k = errp->nerrs;
  475. for (i = 0; i < k; i++)
  476. {
  477. if (! errp->errs[i]) continue;
  478. symbol = errp->errs[i];
  479. SETBIT(shiftset, symbol);
  480. }
  481. }
  482. m = lookaheads[state];
  483. n = lookaheads[state + 1];
  484. if (n - m == 1 && ! nodefault)
  485. {
  486. default_rule = LAruleno[m];
  487. fp1 = LA + m * tokensetsize;
  488. fp2 = shiftset;
  489. fp3 = lookaheadset;
  490. fp4 = lookaheadset + tokensetsize;
  491. while (fp3 < fp4)
  492. *fp3++ = *fp1++ & *fp2++;
  493. mask = 1;
  494. fp3 = lookaheadset;
  495. for (i = 0; i < ntokens; i++)
  496. {
  497. if (mask & *fp3)
  498. fprintf(foutput, " %-4s\t[reduce using rule %d (%s)]\n",
  499. tags[i], default_rule, tags[rlhs[default_rule]]);
  500. mask <<= 1;
  501. if (mask == 0)
  502. {
  503. mask = 1;
  504. fp3++;
  505. }
  506. }
  507. fprintf(foutput, " $default\treduce using rule %d (%s)\n\n",
  508. default_rule, tags[rlhs[default_rule]]);
  509. }
  510. else if (n - m >= 1)
  511. {
  512. cmax = 0;
  513. default_LA = -1;
  514. fp4 = lookaheadset + tokensetsize;
  515. if (! nodefault)
  516. for (i = m; i < n; i++)
  517. {
  518. fp1 = LA + i * tokensetsize;
  519. fp2 = shiftset;
  520. fp3 = lookaheadset;
  521. while (fp3 < fp4)
  522. *fp3++ = *fp1++ & ( ~ (*fp2++));
  523. count = 0;
  524. mask = 1;
  525. fp3 = lookaheadset;
  526. for (j = 0; j < ntokens; j++)
  527. {
  528. if (mask & *fp3)
  529. count++;
  530. mask <<= 1;
  531. if (mask == 0)
  532. {
  533. mask = 1;
  534. fp3++;
  535. }
  536. }
  537. if (count > cmax)
  538. {
  539. cmax = count;
  540. default_LA = i;
  541. default_rule = LAruleno[i];
  542. }
  543. fp2 = shiftset;
  544. fp3 = lookaheadset;
  545. while (fp3 < fp4)
  546. *fp2++ |= *fp3++;
  547. }
  548. for (i = 0; i < tokensetsize; i++)
  549. shiftset[i] = 0;
  550. if (shiftp)
  551. {
  552. k = shiftp->nshifts;
  553. for (i = 0; i < k; i++)
  554. {
  555. if (! shiftp->shifts[i]) continue;
  556. symbol = accessing_symbol[shiftp->shifts[i]];
  557. if (ISVAR(symbol)) break;
  558. SETBIT(shiftset, symbol);
  559. }
  560. }
  561. mask = 1;
  562. fp1 = LA + m * tokensetsize;
  563. fp2 = shiftset;
  564. for (i = 0; i < ntokens; i++)
  565. {
  566. int defaulted = 0;
  567. if (mask & *fp2)
  568. count = 1;
  569. else
  570. count = 0;
  571. fp3 = fp1;
  572. for (j = m; j < n; j++)
  573. {
  574. if (mask & *fp3)
  575. {
  576. if (count == 0)
  577. {
  578. if (j != default_LA)
  579. {
  580. rule = LAruleno[j];
  581. fprintf(foutput, " %-4s\treduce using rule %d (%s)\n",
  582. tags[i], rule, tags[rlhs[rule]]);
  583. }
  584. else defaulted = 1;
  585. count++;
  586. }
  587. else
  588. {
  589. if (defaulted)
  590. {
  591. rule = LAruleno[default_LA];
  592. fprintf(foutput, " %-4s\treduce using rule %d (%s)\n",
  593. tags[i], rule, tags[rlhs[rule]]);
  594. defaulted = 0;
  595. }
  596. rule = LAruleno[j];
  597. fprintf(foutput, " %-4s\t[reduce using rule %d (%s)]\n",
  598. tags[i], rule, tags[rlhs[rule]]);
  599. }
  600. }
  601. fp3 += tokensetsize;
  602. }
  603. mask <<= 1;
  604. if (mask == 0)
  605. {
  606. mask = 1;
  607. /* This used to be fp1, but I think fp2 is right
  608. because fp2 is where the words are fetched to test with mask
  609. in this loop. */
  610. fp2++;
  611. }
  612. }
  613. if (default_LA >= 0)
  614. {
  615. fprintf(foutput, " $default\treduce using rule %d (%s)\n",
  616. default_rule, tags[rlhs[default_rule]]);
  617. }
  618. putc('\n', foutput);
  619. }
  620. }
  621. void
  622. finalize_conflicts()
  623. {
  624. FREE(conflicts);
  625. FREE(shiftset);
  626. FREE(lookaheadset);
  627. }