|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava_cup.runtime.lr_parser
public abstract class lr_parser
This class implements a skeleton table driven LR parser. In general, LR parsers are a form of bottom up shift-reduce parsers. Shift-reduce parsers act by shifting input onto a parse stack until the Symbols matching the right hand side of a production appear on the top of the stack. Once this occurs, a reduce is performed. This involves removing the Symbols corresponding to the right hand side of the production (the so called "handle") and replacing them with the non-terminal from the left hand side of the production.
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:
Symbol
,
Symbol
,
virtual_parse_stack
Constructor Summary | |
---|---|
lr_parser()
Simple constructor. |
|
lr_parser(Scanner s)
Constructor that sets the default scanner. |
Method Summary | |
---|---|
abstract short[][] |
action_table()
The action table (supplied by generated subclass). |
void |
debug_message(java.lang.String mess)
Write a debugging message to System.err for the debugging version of the parser. |
Symbol |
debug_parse()
Perform a parse with debugging output. |
void |
debug_reduce(int prod_num,
int nt_num,
int rhs_size)
Do debug output for a reduce. |
void |
debug_shift(Symbol shift_tkn)
Do debug output for shift. |
void |
debug_stack()
Do debug output for stack state. |
abstract Symbol |
do_action(int act_num,
lr_parser parser,
java.util.Stack stack,
int top)
Perform a bit of user supplied action code (supplied by generated subclass). |
void |
done_parsing()
This method is called to indicate that the parser should quit. |
void |
dump_stack()
Dump the parse stack for debugging purposes. |
abstract int |
EOF_sym()
The index of the end of file terminal Symbol (supplied by generated subclass). |
abstract int |
error_sym()
The index of the special error Symbol (supplied by generated subclass). |
Scanner |
getScanner()
Simple accessor method to get the default scanner. |
Symbol |
parse()
This method provides the main parsing routine. |
abstract short[][] |
production_table()
Table of production information (supplied by generated subclass). |
abstract short[][] |
reduce_table()
The reduce-goto table (supplied by generated subclass). |
void |
report_error(java.lang.String message,
java.lang.Object info)
Report a non fatal error (or warning). |
void |
report_fatal_error(java.lang.String message,
java.lang.Object info)
Report a fatal error. |
Symbol |
scan()
Get the next Symbol from the input (supplied by generated subclass). |
void |
setScanner(Scanner s)
Simple accessor method to set the default scanner. |
abstract int |
start_production()
The index of the start production (supplied by generated subclass). |
abstract int |
start_state()
The index of the start state (supplied by generated subclass). |
void |
syntax_error(Symbol cur_token)
This method is called when a syntax error has been detected and recovery is about to be invoked. |
void |
unrecovered_syntax_error(Symbol cur_token)
This method is called if it is determined that syntax error recovery has been unsuccessful. |
void |
user_init()
User code for initialization inside the parser. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public lr_parser()
public lr_parser(Scanner s)
Method Detail |
---|
public abstract short[][] production_table()
public abstract short[][] action_table()
get_action(int, int)
public abstract short[][] reduce_table()
get_reduce(int, int)
public abstract int start_state()
public abstract int start_production()
public abstract int EOF_sym()
public abstract int error_sym()
public void done_parsing()
public void setScanner(Scanner s)
public Scanner getScanner()
public abstract Symbol do_action(int act_num, lr_parser parser, java.util.Stack stack, int top) throws java.lang.Exception
act_num
- the internal index of the action to be performed.parser
- the parser object we are acting for.stack
- the parse stack of that object.top
- the index of the top element of the parse stack.
java.lang.Exception
public void user_init() throws java.lang.Exception
java.lang.Exception
public Symbol scan() throws java.lang.Exception
java.lang.Exception
public void report_fatal_error(java.lang.String message, java.lang.Object info) throws java.lang.Exception
message
- an error message.info
- an extra object reserved for use by specialized subclasses.
java.lang.Exception
public void report_error(java.lang.String message, java.lang.Object info)
message
- an error message.info
- an extra object reserved for use by specialized subclasses.public void syntax_error(Symbol cur_token)
cur_token
- the current lookahead Symbol.public void unrecovered_syntax_error(Symbol cur_token) throws java.lang.Exception
cur_token
- the current lookahead Symbol.
java.lang.Exception
public Symbol parse() throws java.lang.Exception
java.lang.Exception
public void debug_message(java.lang.String mess)
mess
- the text of the debugging message.public void dump_stack()
public void debug_reduce(int prod_num, int nt_num, int rhs_size)
prod_num
- the production we are reducing with.nt_num
- the index of the LHS non terminal.rhs_size
- the size of the RHS.public void debug_shift(Symbol shift_tkn)
shift_tkn
- the Symbol being shifted onto the stack.public void debug_stack()
public Symbol debug_parse() throws java.lang.Exception
java.lang.Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |