conflicts.c 14 KB

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