conflicts.c 15 KB

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