conflicts.c 14 KB

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