CONFLICT.C 16 KB

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