To control the decision of whether to shift or reduce at any given point, the parser uses a state machine (the "viable prefix recognition machine" built by the parser generator). The current state of the machine is placed on top of the parse stack (stored as part of a Symbol object representing a terminal or non terminal). The parse action table is consulted (using the current state and the current lookahead Symbol as indexes) to determine whether to shift or to reduce. When the parser shifts, it changes to a new state by pushing a new Symbol (containing a new state) onto the stack. When the parser reduces, it pops the handle (right hand side of a production) off the stack. This leaves the parser in the state it was in before any of those Symbols were matched. Next the reduce-goto table is consulted (using the new state and current lookahead Symbol as indexes) to determine a new state to go to. The parser then shifts to this goto state by pushing the left hand side Symbol of the production (also containing the new state) onto the stack.
This class actually provides four LR parsers. The methods parse() and debug_parse() provide two versions of the main parser (the only difference being that debug_parse() emits debugging trace messages as it parses). In addition to these main parsers, the error recovery mechanism uses two more. One of these is used to simulate "parsing ahead" in the input without carrying out actions (to verify that a potential error recovery has worked), and the other is used to parse through buffered "parse ahead" input in order to execute all actions and re-synchronize the actual parser configuration.
This is an abstract class which is normally filled out by a subclass generated by the JavaCup parser generator. In addition to supplying the actual parse tables, generated code also supplies methods which invoke various pieces of user supplied code, provide access to certain special Symbols (e.g., EOF and error), etc. Specifically, the following abstract methods are normally supplied by generated code:
java_cup.runtime.Symbol , java_cup.runtime.virtual_parse_stack
| Modifier and Type | Field and Description |
|---|---|
| protected boolean | _done_parsing
Internal flag to indicate when parser should quit. |
| protected static final int | _error_sync_size
The default number of Symbols after an error we much match to consider it recovered from. |
| private Scanner | _scanner
This is the scanner object used by the default implementation of scan() to get Symbols. |
| protected short[][] | action_tab
Direct reference to the action table. |
| protected Symbol | cur_token
The current lookahead Symbol. |
| protected Symbol[] | lookahead
Lookahead Symbols used for attempting error recovery "parse aheads". |
| protected int | lookahead_pos
Position in lookahead input buffer used for "parse ahead". |
| protected short[][] | production_tab
Direct reference to the production table. |
| protected short[][] | reduce_tab
Direct reference to the reduce-goto table. |
| protected Stack | stack
The parse stack itself. |
| protected int | tos
Indication of the index for top of stack (for use by actions). |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public abstract short[][] | |
| protected boolean | |
| protected Symbol | |
| public void | debug_message(String
the text of the debugging message. mess)Write a debugging message to System.err for the debugging version of the parser. |
| public Symbol | |
| public void | debug_reduce(int
the production we are reducing with. prod_num, int the index of the LHS non terminal. nt_num, int the size of the RHS. rhs_size)Do debug output for a reduce. |
| public void | |
| public void | |
| public abstract Symbol | do_action(int
the internal index of the action to be performed. act_num, lr_parser the parser object we are acting for. parser, Stack<E> the parse stack of that object. stack, int the index of the top element of the parse stack. top)Perform a bit of user supplied action code (supplied by generated subclass). |
| public void | |
| public void | |
| public abstract int | |
| protected boolean | error_recovery(boolean
should we produce debugging messages as we parse. debug)Attempt to recover from a syntax error. |
| public abstract int | |
| protected int | |
| protected boolean | find_recovery_config(boolean
should we produce debugging messages as we parse. debug)Put the (real) parse stack into error recovery configuration by popping the stack down to a state that can shift on the special error Symbol, then doing the shift. |
| protected final short | get_action(int
the state index of the action being accessed. state, int the Symbol index of the action being accessed. sym)Fetch an action from the action table. |
| protected final short | get_reduce(int
the state index of the entry being accessed. state, int the Symbol index of the entry being accessed. sym)Fetch a state from the reduce-goto table. |
| public Scanner | |
| protected abstract void | |
| public Symbol | |
| protected void | parse_lookahead(boolean
should we produce debugging messages as we parse. debug)Parse forward using stored lookahead Symbols. |
| public abstract short[][] | |
| protected void | |
| public abstract short[][] | |
| public void | report_error(String
an error message. message, Object an extra object reserved for use by specialized subclasses. info)Report a non fatal error (or warning). |
| public void | report_fatal_error(String
an error message. message, Object an extra object reserved for use by specialized subclasses. info)Report a fatal error. |
| protected void | restart_lookahead()
Reset the parse ahead input to one Symbol past where we started error recovery (this consumes one new Symbol from the real input). |
| public Symbol | |
| public void | |
| protected boolean | shift_under_error()
Determine if we can shift under the special error Symbol out of the state currently on the top of the (real) parse stack. |
| public abstract int | |
| public abstract int | |
| public void | syntax_error(Symbol
the current lookahead Symbol. cur_token)This method is called when a syntax error has been detected and recovery is about to be invoked. |
| protected boolean | try_parse_ahead(boolean
should we produce debugging messages as we parse. debug)Do a simulated parse forward (a "parse ahead") from the current stack configuration using stored lookahead input and a virtual parse stack. |
| protected static short[][] | |
| public void | unrecovered_syntax_error(Symbol
the current lookahead Symbol. cur_token)This method is called if it is determined that syntax error recovery has been unsuccessful. |
| public void |
| _done_parsing | back to summary |
|---|---|
| protected boolean _done_parsing Internal flag to indicate when parser should quit. | |
| _error_sync_size | back to summary |
|---|---|
| protected static final int _error_sync_size The default number of Symbols after an error we much match to consider it recovered from. | |
| _scanner | back to summary |
|---|---|
| private Scanner _scanner This is the scanner object used by the default implementation of scan() to get Symbols. To avoid name conflicts with existing code, this field is private. [CSA/davidm] | |
| action_tab | back to summary |
|---|---|
| protected short[][] action_tab Direct reference to the action table. | |
| cur_token | back to summary |
|---|---|
| protected Symbol cur_token The current lookahead Symbol. | |
| lookahead | back to summary |
|---|---|
| protected Symbol[] lookahead Lookahead Symbols used for attempting error recovery "parse aheads". | |
| lookahead_pos | back to summary |
|---|---|
| protected int lookahead_pos Position in lookahead input buffer used for "parse ahead". | |
| production_tab | back to summary |
|---|---|
| protected short[][] production_tab Direct reference to the production table. | |
| reduce_tab | back to summary |
|---|---|
| protected short[][] reduce_tab Direct reference to the reduce-goto table. | |
| stack | back to summary |
|---|---|
| protected Stack<E> stack The parse stack itself. | |
| tos | back to summary |
|---|---|
| protected int tos Indication of the index for top of stack (for use by actions). | |
| lr_parser | back to summary |
|---|---|
| public lr_parser() Simple constructor. | |
| lr_parser | back to summary |
|---|---|
| public lr_parser(Scanner s) Constructor that sets the default scanner. [CSA/davidm] | |
| action_table | back to summary |
|---|---|
| public abstract short[][] action_table() The action table (supplied by generated subclass). This table is indexed by state and terminal number indicating what action is to be taken when the parser is in the given state (i.e., the given state is on top of the stack) and the given terminal is next on the input. States are indexed using the first dimension, however, the entries for a given state are compacted and stored in adjacent index, value pairs which are searched for rather than accessed directly (see get_action()). The actions stored in the table will be either shifts, reduces, or errors. Shifts are encoded as positive values (one greater than the state shifted to). Reduces are encoded as negative values (one less than the production reduced by). Error entries are denoted by zero. | |
| advance_lookahead | back to summary |
|---|---|
| protected boolean advance_lookahead() Advance to next "parse ahead" input Symbol. Return true if we have input to advance to, false otherwise. | |
| cur_err_token | back to summary |
|---|---|
| protected Symbol cur_err_token() Return the current lookahead in our error "parse ahead" buffer. | |
| debug_message | back to summary |
|---|---|
| public void debug_message(String mess) Write a debugging message to System.err for the debugging version of the parser.
| |
| debug_parse | back to summary |
|---|---|
| public Symbol debug_parse() throws Exception Perform a parse with debugging output. This does exactly the same things as parse(), except that it calls debug_shift() and debug_reduce() when shift and reduce moves are taken by the parser and produces various other debugging messages. | |
| debug_reduce | back to summary |
|---|---|
| public void debug_reduce(int prod_num, int nt_num, int rhs_size) Do debug output for a reduce.
| |
| debug_shift | back to summary |
|---|---|
| public void debug_shift(Symbol shift_tkn) Do debug output for shift.
| |
| debug_stack | back to summary |
|---|---|
| public void debug_stack() Do debug output for stack state. [CSA] | |
| do_action | back to summary |
|---|---|
| public abstract Symbol do_action(int act_num, lr_parser parser, Stack<E> stack, int top) throws Exception Perform a bit of user supplied action code (supplied by generated subclass). Actions are indexed by an internal action number assigned at parser generation time. | |
| done_parsing | back to summary |
|---|---|
| public void done_parsing() This method is called to indicate that the parser should quit. This is normally called by an accept action, but can be used to cancel parsing early in other circumstances if desired. | |
| dump_stack | back to summary |
|---|---|
| public void dump_stack() Dump the parse stack for debugging purposes. | |
| EOF_sym | back to summary |
|---|---|
| public abstract int EOF_sym() The index of the end of file terminal Symbol (supplied by generated subclass). | |
| error_recovery | back to summary |
|---|---|
| protected boolean error_recovery(boolean debug) throws Exception Attempt to recover from a syntax error. This returns false if recovery fails, true if it succeeds. Recovery happens in 4 steps. First we pop the parse stack down to a point at which we have a shift out of the top-most state on the error Symbol. This represents the initial error recovery configuration. If no such configuration is found, then we fail. Next a small number of "lookahead" or "parse ahead" Symbols are read into a buffer. The size of this buffer is determined by error_sync_size() and determines how many Symbols beyond the error must be matched to consider the recovery a success. Next, we begin to discard Symbols in attempt to get past the point of error to a point where we can continue parsing. After each Symbol, we attempt to "parse ahead" though the buffered lookahead Symbols. The "parse ahead" process simulates that actual parse, but does not modify the real parser's configuration, nor execute any actions. If we can parse all the stored Symbols without error, then the recovery is considered a success. Once a successful recovery point is determined, we do an actual parse over the stored input -- modifying the real parse configuration and executing all actions. Finally, we return the the normal parser to continue with the overall parse.
| |
| error_sym | back to summary |
|---|---|
| public abstract int error_sym() The index of the special error Symbol (supplied by generated subclass). | |
| error_sync_size | back to summary |
|---|---|
| protected int error_sync_size() The number of Symbols after an error we much match to consider it recovered from. | |
| find_recovery_config | back to summary |
|---|---|
| protected boolean find_recovery_config(boolean debug) Put the (real) parse stack into error recovery configuration by popping the stack down to a state that can shift on the special error Symbol, then doing the shift. If no suitable state exists on the stack we return false
| |
| get_action | back to summary |
|---|---|
| protected final short get_action(int state, int sym) Fetch an action from the action table. The table is broken up into rows, one per state (rows are indexed directly by state number). Within each row, a list of index, value pairs are given (as sequential entries in the table), and the list is terminated by a default entry (denoted with a Symbol index of -1). To find the proper entry in a row we do a linear or binary search (depending on the size of the row).
| |
| get_reduce | back to summary |
|---|---|
| protected final short get_reduce(int state, int sym) Fetch a state from the reduce-goto table. The table is broken up into rows, one per state (rows are indexed directly by state number). Within each row, a list of index, value pairs are given (as sequential entries in the table), and the list is terminated by a default entry (denoted with a Symbol index of -1). To find the proper entry in a row we do a linear search.
| |
| getScanner | back to summary |
|---|---|
| public Scanner getScanner() Simple accessor method to get the default scanner. | |
| init_actions | back to summary |
|---|---|
| protected abstract void init_actions() throws Exception Initialize the action object. This is called before the parser does any parse actions. This is filled in by generated code to create an object that encapsulates all action code. | |
| parse | back to summary |
|---|---|
| public Symbol parse() throws Exception This method provides the main parsing routine. It returns only when done_parsing() has been called (typically because the parser has accepted, or a fatal error has been reported). See the header documentation for the class regarding how shift/reduce parsers operate and how the various tables are used. | |
| parse_lookahead | back to summary |
|---|---|
| protected void parse_lookahead(boolean debug) throws Exception Parse forward using stored lookahead Symbols. In this case we have already verified that parsing will make it through the stored lookahead Symbols and we are now getting back to the point at which we can hand control back to the normal parser. Consequently, this version of the parser performs all actions and modifies the real parse configuration. This returns once we have consumed all the stored input or we accept.
| |
| production_table | back to summary |
|---|---|
| public abstract short[][] production_table() Table of production information (supplied by generated subclass). This table contains one entry per production and is indexed by the negative-encoded values (reduce actions) in the action_table. Each entry has two parts, the index of the non-terminal on the left hand side of the production, and the number of Symbols on the right hand side. | |
| read_lookahead | back to summary |
|---|---|
| protected void read_lookahead() throws Exception Read from input to establish our buffer of "parse ahead" lookahead Symbols. | |
| reduce_table | back to summary |
|---|---|
| public abstract short[][] reduce_table() The reduce-goto table (supplied by generated subclass). This table is indexed by state and non-terminal number and contains state numbers. States are indexed using the first dimension, however, the entries for a given state are compacted and stored in adjacent index, value pairs which are searched for rather than accessed directly (see get_reduce()). When a reduce occurs, the handle (corresponding to the RHS of the matched production) is popped off the stack. The new top of stack indicates a state. This table is then indexed by that state and the LHS of the reducing production to indicate where to "shift" to. | |
| report_error | back to summary |
|---|---|
| public void report_error(String message, Object info) Report a non fatal error (or warning). This method takes a message string and an additional object (to be used by specializations implemented in subclasses). Here in the base class a very simple implementation is provided which simply prints the message to System.err. | |
| report_fatal_error | back to summary |
|---|---|
| public void report_fatal_error(String message, Object info) throws Exception Report a fatal error. This method takes a message string and an additional object (to be used by specializations implemented in subclasses). Here in the base class a very simple implementation is provided which reports the error then throws an exception. | |
| restart_lookahead | back to summary |
|---|---|
| protected void restart_lookahead() throws Exception Reset the parse ahead input to one Symbol past where we started error recovery (this consumes one new Symbol from the real input). | |
| scan | back to summary |
|---|---|
| public Symbol scan() throws Exception Get the next Symbol from the input (supplied by generated subclass). Once end of file has been reached, all subsequent calls to scan should return an EOF Symbol (which is Symbol number 0). By default this method returns getScanner().next_token(); this implementation can be overriden by the generated parser using the code declared in the "scan with" clause. Do not recycle objects; every call to scan() should return a fresh object. | |
| setScanner | back to summary |
|---|---|
| public void setScanner(Scanner s) Simple accessor method to set the default scanner. | |
| shift_under_error | back to summary |
|---|---|
| protected boolean shift_under_error() Determine if we can shift under the special error Symbol out of the state currently on the top of the (real) parse stack. | |
| start_production | back to summary |
|---|---|
| public abstract int start_production() The index of the start production (supplied by generated subclass). | |
| start_state | back to summary |
|---|---|
| public abstract int start_state() The index of the start state (supplied by generated subclass). | |
| syntax_error | back to summary |
|---|---|
| public void syntax_error(Symbol cur_token) This method is called when a syntax error has been detected and recovery is about to be invoked. Here in the base class we just emit a "Syntax error" error message.
| |
| try_parse_ahead | back to summary |
|---|---|
| protected boolean try_parse_ahead(boolean debug) throws Exception Do a simulated parse forward (a "parse ahead") from the current stack configuration using stored lookahead input and a virtual parse stack. Return true if we make it all the way through the stored lookahead input without error. This basically simulates the action of parse() using only our saved "parse ahead" input, and not executing any actions.
| |
| unpackFromStrings | back to summary |
|---|---|
| protected static short[][] unpackFromStrings(String[] sa) Utility function: unpacks parse tables from strings | |
| unrecovered_syntax_error | back to summary |
|---|---|
| public void unrecovered_syntax_error(Symbol cur_token) throws Exception This method is called if it is determined that syntax error recovery has been unsuccessful. Here in the base class we report a fatal error.
| |
| user_init | back to summary |
|---|---|
| public void user_init() throws Exception User code for initialization inside the parser. Typically this initializes the scanner. This is called before the parser requests the first Symbol. Here this is just a placeholder for subclasses that might need this and we perform no action. This method is normally overridden by the generated code using this contents of the "init with" clause as its body. | |