|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava_cup.lalr_state
This class represents a state in the LALR viable prefix recognition machine. A state consists of an LALR item set and a set of transitions to other states under terminal and non-terminal symbols. Each state represents a potential configuration of the parser. If the item set of a state includes an item such as:
[A ::= B * C d E , {a,b,c}]this indicates that when the parser is in this state it is currently looking for an A of the given form, has already seen the B, and would expect to see an a, b, or c after this sequence is complete. Note that the parser is normally looking for several things at once (represented by several items). In our example above, the state would also include items such as:
[C ::= * X e Z, {d}] [X ::= * f, {e}]to indicate that it was currently looking for a C followed by a d (which would be reduced into a C, matching the first symbol in our production above), and the terminal f followed by e.
At runtime, the parser uses a viable prefix recognition machine made up of these states to parse. The parser has two operations, shift and reduce. In a shift, it consumes one Symbol and makes a transition to a new state. This corresponds to "moving the dot past" a terminal in one or more items in the state (these new shifted items will then be found in the state at the end of the transition). For a reduce operation, the parser is signifying that it is recognizing the RHS of some production. To do this it first "backs up" by popping a stack of previously saved states. It pops off the same number of states as are found in the RHS of the production. This leaves the machine in the same state is was in when the parser first attempted to find the RHS. From this state it makes a transition based on the non-terminal on the LHS of the production. This corresponds to placing the parse in a configuration equivalent to having replaced all the symbols from the the input corresponding to the RHS with the symbol on the LHS.
lalr_item
,
lalr_item_set
,
lalr_transition
Field Summary | |
protected static java.util.Hashtable |
_all
Collection of all states. |
protected static java.util.Hashtable |
_all_kernels
Hash table to find states by their kernels (i.e, the original, unclosed, set of items -- which uniquely define the state). |
protected int |
_index
Index of this state in the parse tables |
protected lalr_item_set |
_items
The item set for this state. |
protected lalr_transition |
_transitions
List of transitions out of this state. |
protected static int |
next_index
Static counter for assigning unique state indexes. |
Constructor Summary | |
lalr_state(lalr_item_set itms)
Constructor for building a state from a set of items. |
Method Summary | |
void |
add_transition(symbol on_sym,
lalr_state to_st)
Add a transition out of this state to another. |
static java.util.Enumeration |
all()
Collection of all states. |
static lalr_state |
build_machine(production start_prod)
Build an LALR viable prefix recognition machine given a start production. |
void |
build_table_entries(parse_action_table act_table,
parse_reduce_table reduce_table)
Fill in the parse table entries for this state. |
protected static void |
dump_state(lalr_state st)
Helper routine for debugging -- produces a dump of the given state onto System.out. |
boolean |
equals(lalr_state other)
Equality comparison. |
boolean |
equals(java.lang.Object other)
Generic equality comparison. |
static lalr_state |
find_state(lalr_item_set itms)
Find and return state with a given a kernel item set (or null if not found). |
protected boolean |
fix_with_precedence(production p,
int term_index,
parse_action_row table_row,
parse_action act)
Procedure that attempts to fix a shift/reduce error by using precedences. |
int |
hashCode()
Produce a hash code. |
int |
index()
Index of this state in the parse tables |
protected parse_action |
insert_action(parse_action a1,
parse_action a2,
int act_type)
|
protected parse_action |
insert_reduce(parse_action a1,
parse_action a2)
|
protected parse_action |
insert_shift(parse_action a1,
parse_action a2)
|
lalr_item_set |
items()
The item set for this state. |
static int |
number()
Indicate total number of states there are. |
protected static void |
propagate_all_lookaheads()
Propagate lookahead sets through the constructed viable prefix recognizer. |
protected void |
propagate_lookaheads()
Propagate lookahead sets out of this state. |
protected void |
report_conflicts(terminal_set conflict_set)
Produce warning messages for all conflicts found in this state. |
protected void |
report_reduce_reduce(lalr_item itm1,
lalr_item itm2)
Produce a warning message for one reduce/reduce conflict. |
protected void |
report_shift_reduce(lalr_item red_itm,
int conflict_sym)
Produce a warning message for one shift/reduce conflict. |
java.lang.String |
toString()
Convert to a string. |
lalr_transition |
transitions()
List of transitions out of this state. |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
protected static java.util.Hashtable _all
protected static java.util.Hashtable _all_kernels
protected static int next_index
protected lalr_item_set _items
protected lalr_transition _transitions
protected int _index
Constructor Detail |
public lalr_state(lalr_item_set itms) throws internal_error
itms
- the set of items that makes up this state.Method Detail |
public static java.util.Enumeration all()
public static int number()
public static lalr_state find_state(lalr_item_set itms)
itms
- the kernel set of the state we are looking for.public lalr_item_set items()
public lalr_transition transitions()
public int index()
protected static void dump_state(lalr_state st) throws internal_error
internal_error
protected static void propagate_all_lookaheads() throws internal_error
internal_error
public void add_transition(symbol on_sym, lalr_state to_st) throws internal_error
on_sym
- the symbol the transition is under.to_st
- the state the transition goes to.
internal_error
public static lalr_state build_machine(production start_prod) throws internal_error
[A ::= a b * X c, {d,e}] [B ::= a b * X d, {a,b}]in some state, then we would be making a transition under X to a new state. This new state would be formed by a "kernel" of items corresponding to moving the dot past the X. In this case:
[A ::= a b X * c, {d,e}] [B ::= a b X * Y, {a,b}]The full state would then be formed by "closing" this kernel set of items so that it included items that represented productions of things the parser was now looking for. In this case we would items corresponding to productions of Y, since various forms of Y are expected next when in this state (see lalr_item_set.compute_closure() for details on closure).
The process of building the viable prefix recognizer terminates when no new states can be added. However, in order to build a smaller number of states (i.e., corresponding to LALR rather than canonical LR) the state building process does not maintain full loookaheads in all items. Consequently, after the machine is built, we go back and propagate lookaheads through the constructed machine using a call to propagate_all_lookaheads(). This makes use of propagation links constructed during the closure and transition process.
start_prod
- the start production of the grammar
internal_error
lalr_item_set.compute_closure()
,
propagate_all_lookaheads()
protected void propagate_lookaheads() throws internal_error
internal_error
public void build_table_entries(parse_action_table act_table, parse_reduce_table reduce_table) throws internal_error
Conflicts occur if more than one action needs to go in one entry of the action table (this cannot happen with the reduce-goto table). Conflicts are resolved by always shifting for shift/reduce conflicts and choosing the lowest numbered production (hence the one that appeared first in the specification) in reduce/reduce conflicts. All conflicts are reported and if more conflicts are detected than were declared by the user, code generation is aborted.
act_table
- the action table to put entries in.reduce_table
- the reduce-goto table to put entries in.
internal_error
protected boolean fix_with_precedence(production p, int term_index, parse_action_row table_row, parse_action act) throws internal_error
p
- the productionterm_index
- the index of the lokahead terminalact
- the rule in conflict with the table entry
internal_error
protected parse_action insert_action(parse_action a1, parse_action a2, int act_type) throws internal_error
internal_error
protected parse_action insert_shift(parse_action a1, parse_action a2) throws internal_error
internal_error
protected parse_action insert_reduce(parse_action a1, parse_action a2) throws internal_error
internal_error
protected void report_conflicts(terminal_set conflict_set) throws internal_error
internal_error
protected void report_reduce_reduce(lalr_item itm1, lalr_item itm2) throws internal_error
itm1
- first item in conflict.itm2
- second item in conflict.
internal_error
protected void report_shift_reduce(lalr_item red_itm, int conflict_sym) throws internal_error
red_itm
- the item with the reduce.conflict_sym
- the index of the symbol conflict occurs under.
internal_error
public boolean equals(lalr_state other)
public boolean equals(java.lang.Object other)
public int hashCode()
public java.lang.String toString()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |