1. package com.sun.java_cup.internal;
  2. import java.util.Hashtable;
  3. import java.util.Enumeration;
  4. import java.util.Stack;
  5. /** This class represents a state in the LALR viable prefix recognition machine.
  6. * A state consists of an LALR item set and a set of transitions to other
  7. * states under terminal and non-terminal symbols. Each state represents
  8. * a potential configuration of the parser. If the item set of a state
  9. * includes an item such as: <pre>
  10. * [A ::= B * C d E , {a,b,c}]
  11. * </pre>
  12. * this indicates that when the parser is in this state it is currently
  13. * looking for an A of the given form, has already seen the B, and would
  14. * expect to see an a, b, or c after this sequence is complete. Note that
  15. * the parser is normally looking for several things at once (represented
  16. * by several items). In our example above, the state would also include
  17. * items such as: <pre>
  18. * [C ::= * X e Z, {d}]
  19. * [X ::= * f, {e}]
  20. * </pre>
  21. * to indicate that it was currently looking for a C followed by a d (which
  22. * would be reduced into a C, matching the first symbol in our production
  23. * above), and the terminal f followed by e.<p>
  24. *
  25. * At runtime, the parser uses a viable prefix recognition machine made up
  26. * of these states to parse. The parser has two operations, shift and reduce.
  27. * In a shift, it consumes one Symbol and makes a transition to a new state.
  28. * This corresponds to "moving the dot past" a terminal in one or more items
  29. * in the state (these new shifted items will then be found in the state at
  30. * the end of the transition). For a reduce operation, the parser is
  31. * signifying that it is recognizing the RHS of some production. To do this
  32. * it first "backs up" by popping a stack of previously saved states. It
  33. * pops off the same number of states as are found in the RHS of the
  34. * production. This leaves the machine in the same state is was in when the
  35. * parser first attempted to find the RHS. From this state it makes a
  36. * transition based on the non-terminal on the LHS of the production. This
  37. * corresponds to placing the parse in a configuration equivalent to having
  38. * replaced all the symbols from the the input corresponding to the RHS with
  39. * the symbol on the LHS.
  40. *
  41. * @see com.sun.java_cup.internal.lalr_item
  42. * @see com.sun.java_cup.internal.lalr_item_set
  43. * @see com.sun.java_cup.internal.lalr_transition
  44. * @version last updated: 7/3/96
  45. * @author Frank Flannery
  46. *
  47. */
  48. public class lalr_state {
  49. /*-----------------------------------------------------------*/
  50. /*--- Constructor(s) ----------------------------------------*/
  51. /*-----------------------------------------------------------*/
  52. /** Constructor for building a state from a set of items.
  53. * @param itms the set of items that makes up this state.
  54. */
  55. public lalr_state(lalr_item_set itms) throws internal_error
  56. {
  57. /* don't allow null or duplicate item sets */
  58. if (itms == null)
  59. throw new internal_error(
  60. "Attempt to construct an LALR state from a null item set");
  61. if (find_state(itms) != null)
  62. throw new internal_error(
  63. "Attempt to construct a duplicate LALR state");
  64. /* assign a unique index */
  65. _index = next_index++;
  66. /* store the items */
  67. _items = itms;
  68. /* add to the global collection, keyed with its item set */
  69. _all.put(_items,this);
  70. }
  71. /*-----------------------------------------------------------*/
  72. /*--- (Access to) Static (Class) Variables ------------------*/
  73. /*-----------------------------------------------------------*/
  74. /** Collection of all states. */
  75. protected static Hashtable _all = new Hashtable();
  76. /** Collection of all states. */
  77. public static Enumeration all() {return _all.elements();}
  78. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  79. /** Indicate total number of states there are. */
  80. public static int number() {return _all.size();}
  81. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  82. /** Hash table to find states by their kernels (i.e, the original,
  83. * unclosed, set of items -- which uniquely define the state). This table
  84. * stores state objects using (a copy of) their kernel item sets as keys.
  85. */
  86. protected static Hashtable _all_kernels = new Hashtable();
  87. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  88. /** Find and return state with a given a kernel item set (or null if not
  89. * found). The kernel item set is the subset of items that were used to
  90. * originally create the state. These items are formed by "shifting the
  91. * dot" within items of other states that have a transition to this one.
  92. * The remaining elements of this state's item set are added during closure.
  93. * @param itms the kernel set of the state we are looking for.
  94. */
  95. public static lalr_state find_state(lalr_item_set itms)
  96. {
  97. if (itms == null)
  98. return null;
  99. else
  100. return (lalr_state)_all.get(itms);
  101. }
  102. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  103. /** Static counter for assigning unique state indexes. */
  104. protected static int next_index = 0;
  105. /*-----------------------------------------------------------*/
  106. /*--- (Access to) Instance Variables ------------------------*/
  107. /*-----------------------------------------------------------*/
  108. /** The item set for this state. */
  109. protected lalr_item_set _items;
  110. /** The item set for this state. */
  111. public lalr_item_set items() {return _items;}
  112. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  113. /** List of transitions out of this state. */
  114. protected lalr_transition _transitions = null;
  115. /** List of transitions out of this state. */
  116. public lalr_transition transitions() {return _transitions;}
  117. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  118. /** Index of this state in the parse tables */
  119. protected int _index;
  120. /** Index of this state in the parse tables */
  121. public int index() {return _index;}
  122. /*-----------------------------------------------------------*/
  123. /*--- Static Methods ----------------------------------------*/
  124. /*-----------------------------------------------------------*/
  125. /** Helper routine for debugging -- produces a dump of the given state
  126. * onto System.out.
  127. */
  128. protected static void dump_state(lalr_state st) throws internal_error
  129. {
  130. lalr_item_set itms;
  131. lalr_item itm;
  132. production_part part;
  133. if (st == null)
  134. {
  135. System.out.println("NULL lalr_state");
  136. return;
  137. }
  138. System.out.println("lalr_state [" + st.index() + "] {");
  139. itms = st.items();
  140. for (Enumeration e = itms.all(); e.hasMoreElements(); )
  141. {
  142. itm = (lalr_item)e.nextElement();
  143. System.out.print(" [");
  144. System.out.print(itm.the_production().lhs().the_symbol().name());
  145. System.out.print(" ::= ");
  146. for (int i = 0; i<itm.the_production().rhs_length(); i++)
  147. {
  148. if (i == itm.dot_pos()) System.out.print("(*) ");
  149. part = itm.the_production().rhs(i);
  150. if (part.is_action())
  151. System.out.print("{action} ");
  152. else
  153. System.out.print(((symbol_part)part).the_symbol().name() + " ");
  154. }
  155. if (itm.dot_at_end()) System.out.print("(*) ");
  156. System.out.println("]");
  157. }
  158. System.out.println("}");
  159. }
  160. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  161. /** Propagate lookahead sets through the constructed viable prefix
  162. * recognizer. When the machine is constructed, each item that results
  163. in the creation of another such that its lookahead is included in the
  164. other's will have a propagate link set up for it. This allows additions
  165. to the lookahead of one item to be included in other items that it
  166. was used to directly or indirectly create.
  167. */
  168. protected static void propagate_all_lookaheads() throws internal_error
  169. {
  170. /* iterate across all states */
  171. for (Enumeration st = all(); st.hasMoreElements(); )
  172. {
  173. /* propagate lookaheads out of that state */
  174. ((lalr_state)st.nextElement()).propagate_lookaheads();
  175. }
  176. }
  177. /*-----------------------------------------------------------*/
  178. /*--- General Methods ---------------------------------------*/
  179. /*-----------------------------------------------------------*/
  180. /** Add a transition out of this state to another.
  181. * @param on_sym the symbol the transition is under.
  182. * @param to_st the state the transition goes to.
  183. */
  184. public void add_transition(symbol on_sym, lalr_state to_st)
  185. throws internal_error
  186. {
  187. lalr_transition trans;
  188. /* create a new transition object and put it in our list */
  189. trans = new lalr_transition(on_sym, to_st, _transitions);
  190. _transitions = trans;
  191. }
  192. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  193. /** Build an LALR viable prefix recognition machine given a start
  194. * production. This method operates by first building a start state
  195. * from the start production (based on a single item with the dot at
  196. * the beginning and EOF as expected lookahead). Then for each state
  197. * it attempts to extend the machine by creating transitions out of
  198. * the state to new or existing states. When considering extension
  199. * from a state we make a transition on each symbol that appears before
  200. * the dot in some item. For example, if we have the items: <pre>
  201. * [A ::= a b * X c, {d,e}]
  202. * [B ::= a b * X d, {a,b}]
  203. * </pre>
  204. * in some state, then we would be making a transition under X to a new
  205. * state. This new state would be formed by a "kernel" of items
  206. * corresponding to moving the dot past the X. In this case: <pre>
  207. * [A ::= a b X * c, {d,e}]
  208. * [B ::= a b X * Y, {a,b}]
  209. * </pre>
  210. * The full state would then be formed by "closing" this kernel set of
  211. * items so that it included items that represented productions of things
  212. * the parser was now looking for. In this case we would items
  213. * corresponding to productions of Y, since various forms of Y are expected
  214. * next when in this state (see lalr_item_set.compute_closure() for details
  215. * on closure). <p>
  216. *
  217. * The process of building the viable prefix recognizer terminates when no
  218. * new states can be added. However, in order to build a smaller number of
  219. * states (i.e., corresponding to LALR rather than canonical LR) the state
  220. * building process does not maintain full loookaheads in all items.
  221. * Consequently, after the machine is built, we go back and propagate
  222. * lookaheads through the constructed machine using a call to
  223. * propagate_all_lookaheads(). This makes use of propagation links
  224. * constructed during the closure and transition process.
  225. *
  226. * @param start_prod the start production of the grammar
  227. * @see com.sun.java_cup.internal.lalr_item_set#compute_closure
  228. * @see com.sun.java_cup.internal.lalr_state#propagate_all_lookaheads
  229. */
  230. public static lalr_state build_machine(production start_prod)
  231. throws internal_error
  232. {
  233. lalr_state start_state;
  234. lalr_item_set start_items;
  235. lalr_item_set new_items;
  236. lalr_item_set linked_items;
  237. lalr_item_set kernel;
  238. Stack work_stack = new Stack();
  239. lalr_state st, new_st;
  240. symbol_set outgoing;
  241. lalr_item itm, new_itm, existing, fix_itm;
  242. symbol sym, sym2;
  243. Enumeration i, s, fix;
  244. /* sanity check */
  245. if (start_prod == null)
  246. throw new internal_error(
  247. "Attempt to build viable prefix recognizer using a null production");
  248. /* build item with dot at front of start production and EOF lookahead */
  249. start_items = new lalr_item_set();
  250. itm = new lalr_item(start_prod);
  251. itm.lookahead().add(terminal.EOF);
  252. start_items.add(itm);
  253. /* create copy the item set to form the kernel */
  254. kernel = new lalr_item_set(start_items);
  255. /* create the closure from that item set */
  256. start_items.compute_closure();
  257. /* build a state out of that item set and put it in our work set */
  258. start_state = new lalr_state(start_items);
  259. work_stack.push(start_state);
  260. /* enter the state using the kernel as the key */
  261. _all_kernels.put(kernel, start_state);
  262. /* continue looking at new states until we have no more work to do */
  263. while (!work_stack.empty())
  264. {
  265. /* remove a state from the work set */
  266. st = (lalr_state)work_stack.pop();
  267. /* gather up all the symbols that appear before dots */
  268. outgoing = new symbol_set();
  269. for (i = st.items().all(); i.hasMoreElements(); )
  270. {
  271. itm = (lalr_item)i.nextElement();
  272. /* add the symbol before the dot (if any) to our collection */
  273. sym = itm.symbol_after_dot();
  274. if (sym != null) outgoing.add(sym);
  275. }
  276. /* now create a transition out for each individual symbol */
  277. for (s = outgoing.all(); s.hasMoreElements(); )
  278. {
  279. sym = (symbol)s.nextElement();
  280. /* will be keeping the set of items with propagate links */
  281. linked_items = new lalr_item_set();
  282. /* gather up shifted versions of all the items that have this
  283. symbol before the dot */
  284. new_items = new lalr_item_set();
  285. for (i = st.items().all(); i.hasMoreElements();)
  286. {
  287. itm = (lalr_item)i.nextElement();
  288. /* if this is the symbol we are working on now, add to set */
  289. sym2 = itm.symbol_after_dot();
  290. if (sym.equals(sym2))
  291. {
  292. /* add to the kernel of the new state */
  293. new_items.add(itm.shift());
  294. /* remember that itm has propagate link to it */
  295. linked_items.add(itm);
  296. }
  297. }
  298. /* use new items as state kernel */
  299. kernel = new lalr_item_set(new_items);
  300. /* have we seen this one already? */
  301. new_st = (lalr_state)_all_kernels.get(kernel);
  302. /* if we haven't, build a new state out of the item set */
  303. if (new_st == null)
  304. {
  305. /* compute closure of the kernel for the full item set */
  306. new_items.compute_closure();
  307. /* build the new state */
  308. new_st = new lalr_state(new_items);
  309. /* add the new state to our work set */
  310. work_stack.push(new_st);
  311. /* put it in our kernel table */
  312. _all_kernels.put(kernel, new_st);
  313. }
  314. /* otherwise relink propagation to items in existing state */
  315. else
  316. {
  317. /* walk through the items that have links to the new state */
  318. for (fix = linked_items.all(); fix.hasMoreElements(); )
  319. {
  320. fix_itm = (lalr_item)fix.nextElement();
  321. /* look at each propagate link out of that item */
  322. for (int l =0; l < fix_itm.propagate_items().size(); l++)
  323. {
  324. /* pull out item linked to in the new state */
  325. new_itm =
  326. (lalr_item)fix_itm.propagate_items().elementAt(l);
  327. /* find corresponding item in the existing state */
  328. existing = new_st.items().find(new_itm);
  329. /* fix up the item so it points to the existing set */
  330. if (existing != null)
  331. fix_itm.propagate_items().setElementAt(existing ,l);
  332. }
  333. }
  334. }
  335. /* add a transition from current state to that state */
  336. st.add_transition(sym, new_st);
  337. }
  338. }
  339. /* all done building states */
  340. /* propagate complete lookahead sets throughout the states */
  341. propagate_all_lookaheads();
  342. return start_state;
  343. }
  344. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  345. /** Propagate lookahead sets out of this state. This recursively
  346. * propagates to all items that have propagation links from some item
  347. * in this state.
  348. */
  349. protected void propagate_lookaheads() throws internal_error
  350. {
  351. /* recursively propagate out from each item in the state */
  352. for (Enumeration itm = items().all(); itm.hasMoreElements(); )
  353. ((lalr_item)itm.nextElement()).propagate_lookaheads(null);
  354. }
  355. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  356. /** Fill in the parse table entries for this state. There are two
  357. * parse tables that encode the viable prefix recognition machine, an
  358. * action table and a reduce-goto table. The rows in each table
  359. * correspond to states of the machine. The columns of the action table
  360. * are indexed by terminal symbols and correspond to either transitions
  361. * out of the state (shift entries) or reductions from the state to some
  362. * previous state saved on the stack (reduce entries). All entries in the
  363. * action table that are not shifts or reduces, represent errors. The
  364. * reduce-goto table is indexed by non terminals and represents transitions
  365. * out of a state on that non-terminal.<p>
  366. * Conflicts occur if more than one action needs to go in one entry of the
  367. * action table (this cannot happen with the reduce-goto table). Conflicts
  368. * are resolved by always shifting for shift/reduce conflicts and choosing
  369. * the lowest numbered production (hence the one that appeared first in
  370. * the specification) in reduce/reduce conflicts. All conflicts are
  371. * reported and if more conflicts are detected than were declared by the
  372. * user, code generation is aborted.
  373. *
  374. * @param act_table the action table to put entries in.
  375. * @param reduce_table the reduce-goto table to put entries in.
  376. */
  377. public void build_table_entries(
  378. parse_action_table act_table,
  379. parse_reduce_table reduce_table)
  380. throws internal_error
  381. {
  382. parse_action_row our_act_row;
  383. parse_reduce_row our_red_row;
  384. lalr_item itm;
  385. parse_action act, other_act;
  386. symbol sym;
  387. terminal_set conflict_set = new terminal_set();
  388. /* pull out our rows from the tables */
  389. our_act_row = act_table.under_state[index()];
  390. our_red_row = reduce_table.under_state[index()];
  391. /* consider each item in our state */
  392. for (Enumeration i = items().all(); i.hasMoreElements(); )
  393. {
  394. itm = (lalr_item)i.nextElement();
  395. /* if its completed (dot at end) then reduce under the lookahead */
  396. if (itm.dot_at_end())
  397. {
  398. act = new reduce_action(itm.the_production());
  399. /* consider each lookahead symbol */
  400. for (int t = 0; t < terminal.number(); t++)
  401. {
  402. /* skip over the ones not in the lookahead */
  403. if (!itm.lookahead().contains(t)) continue;
  404. /* if we don't already have an action put this one in */
  405. if (our_act_row.under_term[t].kind() ==
  406. parse_action.ERROR)
  407. {
  408. our_act_row.under_term[t] = act;
  409. }
  410. else
  411. {
  412. /* we now have at least one conflict */
  413. terminal term = terminal.find(t);
  414. other_act = our_act_row.under_term[t];
  415. /* if the other act was not a shift */
  416. if ((other_act.kind() != parse_action.SHIFT) &&
  417. (other_act.kind() != parse_action.NONASSOC))
  418. {
  419. /* if we have lower index hence priority, replace it*/
  420. if (itm.the_production().index() <
  421. ((reduce_action)other_act).reduce_with().index())
  422. {
  423. /* replace the action */
  424. our_act_row.under_term[t] = act;
  425. }
  426. } else {
  427. /* Check precedences,see if problem is correctable */
  428. if(fix_with_precedence(itm.the_production(),
  429. t, our_act_row, act)) {
  430. term = null;
  431. }
  432. }
  433. if(term!=null) {
  434. conflict_set.add(term);
  435. }
  436. }
  437. }
  438. }
  439. }
  440. /* consider each outgoing transition */
  441. for (lalr_transition trans=transitions(); trans!=null; trans=trans.next())
  442. {
  443. /* if its on an terminal add a shift entry */
  444. sym = trans.on_symbol();
  445. if (!sym.is_non_term())
  446. {
  447. act = new shift_action(trans.to_state());
  448. /* if we don't already have an action put this one in */
  449. if ( our_act_row.under_term[sym.index()].kind() ==
  450. parse_action.ERROR)
  451. {
  452. our_act_row.under_term[sym.index()] = act;
  453. }
  454. else
  455. {
  456. /* we now have at least one conflict */
  457. production p = ((reduce_action)our_act_row.under_term[sym.index()]).reduce_with();
  458. /* shift always wins */
  459. if (!fix_with_precedence(p, sym.index(), our_act_row, act)) {
  460. our_act_row.under_term[sym.index()] = act;
  461. conflict_set.add(terminal.find(sym.index()));
  462. }
  463. }
  464. }
  465. else
  466. {
  467. /* for non terminals add an entry to the reduce-goto table */
  468. our_red_row.under_non_term[sym.index()] = trans.to_state();
  469. }
  470. }
  471. /* if we end up with conflict(s), report them */
  472. if (!conflict_set.empty())
  473. report_conflicts(conflict_set);
  474. }
  475. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  476. /** Procedure that attempts to fix a shift/reduce error by using
  477. * precedences. --frankf 6/26/96
  478. *
  479. * if a production (also called rule) or the lookahead terminal
  480. * has a precedence, then the table can be fixed. if the rule
  481. * has greater precedence than the terminal, a reduce by that rule
  482. * in inserted in the table. If the terminal has a higher precedence,
  483. * it is shifted. if they have equal precedence, then the associativity
  484. * of the precedence is used to determine what to put in the table:
  485. * if the precedence is left associative, the action is to reduce.
  486. * if the precedence is right associative, the action is to shift.
  487. * if the precedence is non associative, then it is a syntax error.
  488. *
  489. * @param p the production
  490. * @param term_index the index of the lokahead terminal
  491. * @param parse_action_row a row of the action table
  492. * @param act the rule in conflict with the table entry
  493. */
  494. protected boolean fix_with_precedence(
  495. production p,
  496. int term_index,
  497. parse_action_row table_row,
  498. parse_action act)
  499. throws internal_error {
  500. terminal term = terminal.find(term_index);
  501. /* if the production has a precedence number, it can be fixed */
  502. if (p.precedence_num() > assoc.no_prec) {
  503. /* if production precedes terminal, put reduce in table */
  504. if (p.precedence_num() > term.precedence_num()) {
  505. table_row.under_term[term_index] =
  506. insert_reduce(table_row.under_term[term_index],act);
  507. return true;
  508. }
  509. /* if terminal precedes rule, put shift in table */
  510. else if (p.precedence_num() < term.precedence_num()) {
  511. table_row.under_term[term_index] =
  512. insert_shift(table_row.under_term[term_index],act);
  513. return true;
  514. }
  515. else { /* they are == precedence */
  516. /* equal precedences have equal sides, so only need to
  517. look at one: if it is right, put shift in table */
  518. if (term.precedence_side() == assoc.right) {
  519. table_row.under_term[term_index] =
  520. insert_shift(table_row.under_term[term_index],act);
  521. return true;
  522. }
  523. /* if it is left, put reduce in table */
  524. else if (term.precedence_side() == assoc.left) {
  525. table_row.under_term[term_index] =
  526. insert_reduce(table_row.under_term[term_index],act);
  527. return true;
  528. }
  529. /* if it is nonassoc, we're not allowed to have two nonassocs
  530. of equal precedence in a row, so put in NONASSOC */
  531. else if (term.precedence_side() == assoc.nonassoc) {
  532. table_row.under_term[term_index] = new nonassoc_action();
  533. return true;
  534. } else {
  535. /* something really went wrong */
  536. throw new internal_error("Unable to resolve conflict correctly");
  537. }
  538. }
  539. }
  540. /* check if terminal has precedence, if so, shift, since
  541. rule does not have precedence */
  542. else if (term.precedence_num() > assoc.no_prec) {
  543. table_row.under_term[term_index] =
  544. insert_shift(table_row.under_term[term_index],act);
  545. return true;
  546. }
  547. /* otherwise, neither the rule nor the terminal has a precedence,
  548. so it can't be fixed. */
  549. return false;
  550. }
  551. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  552. /* given two actions, and an action type, return the
  553. action of that action type. give an error if they are of
  554. the same action, because that should never have tried
  555. to be fixed
  556. */
  557. protected parse_action insert_action(
  558. parse_action a1,
  559. parse_action a2,
  560. int act_type)
  561. throws internal_error
  562. {
  563. if ((a1.kind() == act_type) && (a2.kind() == act_type)) {
  564. throw new internal_error("Conflict resolution of bogus actions");
  565. } else if (a1.kind() == act_type) {
  566. return a1;
  567. } else if (a2.kind() == act_type) {
  568. return a2;
  569. } else {
  570. throw new internal_error("Conflict resolution of bogus actions");
  571. }
  572. }
  573. /* find the shift in the two actions */
  574. protected parse_action insert_shift(
  575. parse_action a1,
  576. parse_action a2)
  577. throws internal_error
  578. {
  579. return insert_action(a1, a2, parse_action.SHIFT);
  580. }
  581. /* find the reduce in the two actions */
  582. protected parse_action insert_reduce(
  583. parse_action a1,
  584. parse_action a2)
  585. throws internal_error
  586. {
  587. return insert_action(a1, a2, parse_action.REDUCE);
  588. }
  589. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  590. /** Produce warning messages for all conflicts found in this state. */
  591. protected void report_conflicts(terminal_set conflict_set)
  592. throws internal_error
  593. {
  594. lalr_item itm, compare;
  595. symbol shift_sym;
  596. boolean after_itm;
  597. /* consider each element */
  598. for (Enumeration itms = items().all(); itms.hasMoreElements(); )
  599. {
  600. itm = (lalr_item)itms.nextElement();
  601. /* clear the S/R conflict set for this item */
  602. /* if it results in a reduce, it could be a conflict */
  603. if (itm.dot_at_end())
  604. {
  605. /* not yet after itm */
  606. after_itm = false;
  607. /* compare this item against all others looking for conflicts */
  608. for (Enumeration comps = items().all(); comps.hasMoreElements(); )
  609. {
  610. compare = (lalr_item)comps.nextElement();
  611. /* if this is the item, next one is after it */
  612. if (itm == compare) after_itm = true;
  613. /* only look at it if its not the same item */
  614. if (itm != compare)
  615. {
  616. /* is it a reduce */
  617. if (compare.dot_at_end())
  618. {
  619. /* only look at reduces after itm */
  620. if (after_itm)
  621. /* does the comparison item conflict? */
  622. if (compare.lookahead().intersects(itm.lookahead()))
  623. /* report a reduce/reduce conflict */
  624. report_reduce_reduce(itm, compare);
  625. }
  626. }
  627. }
  628. /* report S/R conflicts under all the symbols we conflict under */
  629. for (int t = 0; t < terminal.number(); t++)
  630. if (conflict_set.contains(t))
  631. report_shift_reduce(itm,t);
  632. }
  633. }
  634. }
  635. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  636. /** Produce a warning message for one reduce/reduce conflict.
  637. *
  638. * @param itm1 first item in conflict.
  639. * @param itm2 second item in conflict.
  640. */
  641. protected void report_reduce_reduce(lalr_item itm1, lalr_item itm2)
  642. throws internal_error
  643. {
  644. boolean comma_flag = false;
  645. System.err.println("*** Reduce/Reduce conflict found in state #"+index());
  646. System.err.print (" between ");
  647. System.err.println(itm1.to_simple_string());
  648. System.err.print (" and ");
  649. System.err.println(itm2.to_simple_string());
  650. System.err.print(" under symbols: {" );
  651. for (int t = 0; t < terminal.number(); t++)
  652. {
  653. if (itm1.lookahead().contains(t) && itm2.lookahead().contains(t))
  654. {
  655. if (comma_flag) System.err.print(", "); else comma_flag = true;
  656. System.err.print(terminal.find(t).name());
  657. }
  658. }
  659. System.err.println("}");
  660. System.err.print(" Resolved in favor of ");
  661. if (itm1.the_production().index() < itm2.the_production().index())
  662. System.err.println("the first production.\n");
  663. else
  664. System.err.println("the second production.\n");
  665. /* count the conflict */
  666. emit.num_conflicts++;
  667. lexer.warning_count++;
  668. }
  669. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  670. /** Produce a warning message for one shift/reduce conflict.
  671. *
  672. * @param red_itm the item with the reduce.
  673. * @param conflict_sym the index of the symbol conflict occurs under.
  674. */
  675. protected void report_shift_reduce(
  676. lalr_item red_itm,
  677. int conflict_sym)
  678. throws internal_error
  679. {
  680. lalr_item itm;
  681. symbol shift_sym;
  682. /* emit top part of message including the reduce item */
  683. System.err.println("*** Shift/Reduce conflict found in state #"+index());
  684. System.err.print (" between ");
  685. System.err.println(red_itm.to_simple_string());
  686. /* find and report on all items that shift under our conflict symbol */
  687. for (Enumeration itms = items().all(); itms.hasMoreElements(); )
  688. {
  689. itm = (lalr_item)itms.nextElement();
  690. /* only look if its not the same item and not a reduce */
  691. if (itm != red_itm && !itm.dot_at_end())
  692. {
  693. /* is it a shift on our conflicting terminal */
  694. shift_sym = itm.symbol_after_dot();
  695. if (!shift_sym.is_non_term() && shift_sym.index() == conflict_sym)
  696. {
  697. /* yes, report on it */
  698. System.err.println(" and " + itm.to_simple_string());
  699. }
  700. }
  701. }
  702. System.err.println(" under symbol "+ terminal.find(conflict_sym).name());
  703. System.err.println(" Resolved in favor of shifting.\n");
  704. /* count the conflict */
  705. emit.num_conflicts++;
  706. lexer.warning_count++;
  707. }
  708. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  709. /** Equality comparison. */
  710. public boolean equals(lalr_state other)
  711. {
  712. /* we are equal if our item sets are equal */
  713. return other != null && items().equals(other.items());
  714. }
  715. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  716. /** Generic equality comparison. */
  717. public boolean equals(Object other)
  718. {
  719. if (!(other instanceof lalr_state))
  720. return false;
  721. else
  722. return equals((lalr_state)other);
  723. }
  724. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  725. /** Produce a hash code. */
  726. public int hashCode()
  727. {
  728. /* just use the item set hash code */
  729. return items().hashCode();
  730. }
  731. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  732. /** Convert to a string. */
  733. public String toString()
  734. {
  735. String result;
  736. lalr_transition tr;
  737. /* dump the item set */
  738. result = "lalr_state [" + index() + "]: " + _items + "\n";
  739. /* do the transitions */
  740. for (tr = transitions(); tr != null; tr = tr.next())
  741. {
  742. result += tr;
  743. result += "\n";
  744. }
  745. return result;
  746. }
  747. /*-----------------------------------------------------------*/
  748. }