CONFLICT.C 15 KB

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