conflict.c 15 KB

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