Java Doc for lr_parser.java in  » Template-Engine » bte » java_cup » runtime » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Template Engine » bte » java_cup.runtime 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java_cup.runtime.lr_parser

All known Subclasses:   com.Ostermiller.bte.parser,
lr_parser
abstract public class lr_parser (Code)
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:

short[][] production_table()
Provides a reference to the production table (indicating the index of the left hand side non terminal and the length of the right hand side for each production in the grammar).
short[][] action_table()
Provides a reference to the parse action table.
short[][] reduce_table()
Provides a reference to the reduce-goto table.
int start_state()
Indicates the index of the start state.
int start_production()
Indicates the index of the starting production.
int EOF_sym()
Indicates the index of the EOF Symbol.
int error_sym()
Indicates the index of the error Symbol.
Symbol do_action()
Executes a piece of user supplied action code. This always comes at the point of a reduce in the parse, so this code also allocates and fills in the left hand side non terminal Symbol object that is to be pushed onto the stack for the reduce.
void init_actions()
Code to initialize a special object that encapsulates user supplied actions (this object is used by do_action() to actually carry out the actions).
In addition to these routines that must be supplied by the generated subclass there are also a series of routines that may be supplied. These include:
Symbol scan()
Used to get the next input Symbol from the scanner.
Scanner getScanner()
Used to provide a scanner for the default implementation of scan().
int error_sync_size()
This determines how many Symbols past the point of an error must be parsed without error in order to consider a recovery to be valid. This defaults to 3. Values less than 2 are not recommended.
void report_error(String message, Object info)
This method is called to report an error. The default implementation simply prints a message to System.err and where the error occurred. This method is often replaced in order to provide a more sophisticated error reporting mechanism.
void report_fatal_error(String message, Object info)
This method is called when a fatal error that cannot be recovered from is encountered. In the default implementation, it calls report_error() to emit a message, then throws an exception.
void syntax_error(Symbol cur_token)
This method is called as soon as syntax error is detected (but before recovery is attempted). In the default implementation it invokes: report_error("Syntax error", null);
void unrecovered_syntax_error(Symbol cur_token)
This method is called if syntax error recovery fails. In the default implementation it invokes:
report_fatal_error("Couldn't repair and continue parse", null);

See Also:   java_cup.runtime.Symbol
See Also:   java_cup.runtime.Symbol
See Also:   java_cup.runtime.virtual_parse_stack
version:
   last updated: 7/3/96
author:
   Frank Flannery


Field Summary
protected  boolean_done_parsing
     Internal flag to indicate when parser should quit.
final protected static  int_error_sync_size
     The default number of Symbols after an error we much match to consider it recovered from.
protected  short[][]action_tab
     Direct reference to the action table.
protected  Symbolcur_token
     The current lookahead Symbol.
protected  Symbollookahead
     Lookahead Symbols used for attempting error recovery "parse aheads".
protected  intlookahead_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  Stackstack
     The parse stack itself.
protected  inttos
     Indication of the index for top of stack (for use by actions).

Constructor Summary
public  lr_parser()
     Simple constructor.
public  lr_parser(Scanner s)
     Constructor that sets the default scanner.

Method Summary
abstract public  intEOF_sym()
     The index of the end of file terminal Symbol (supplied by generated subclass).
abstract public  short[][]action_table()
     The action table (supplied by generated subclass).
protected  booleanadvance_lookahead()
     Advance to next "parse ahead" input Symbol.
protected  Symbolcur_err_token()
     Return the current lookahead in our error "parse ahead" buffer.
public  voiddebug_message(String mess)
     Write a debugging message to System.err for the debugging version of the parser.
public  Symboldebug_parse()
     Perform a parse with debugging output.
public  voiddebug_reduce(int prod_num, int nt_num, int rhs_size)
     Do debug output for a reduce.
public  voiddebug_shift(Symbol shift_tkn)
     Do debug output for shift.
public  voiddebug_stack()
     Do debug output for stack state.
abstract public  Symboldo_action(int act_num, lr_parser parser, Stack stack, int top)
     Perform a bit of user supplied action code (supplied by generated subclass).
public  voiddone_parsing()
     This method is called to indicate that the parser should quit.
public  voiddump_stack()
     Dump the parse stack for debugging purposes.
protected  booleanerror_recovery(boolean debug)
     Attempt to recover from a syntax error.
abstract public  interror_sym()
     The index of the special error Symbol (supplied by generated subclass).
protected  interror_sync_size()
     The number of Symbols after an error we much match to consider it recovered from.
protected  booleanfind_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.
public  ScannergetScanner()
     Simple accessor method to get the default scanner.
final protected  shortget_action(int state, int sym)
     Fetch an action from the action table.
final protected  shortget_reduce(int state, int sym)
     Fetch a state from the reduce-goto table.
abstract protected  voidinit_actions()
     Initialize the action object.
public  Symbolparse()
     This method provides the main parsing routine.
protected  voidparse_lookahead(boolean debug)
     Parse forward using stored lookahead Symbols.
abstract public  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.
protected  voidread_lookahead()
     Read from input to establish our buffer of "parse ahead" lookahead Symbols.
abstract public  short[][]reduce_table()
     The reduce-goto table (supplied by generated subclass).
public  voidreport_error(String message, Object info)
     Report a non fatal error (or warning).
public  voidreport_fatal_error(String message, Object info)
     Report a fatal error.
protected  voidrestart_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  Symbolscan()
     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).
public  voidsetScanner(Scanner s)
     Simple accessor method to set the default scanner.
protected  booleanshift_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.
abstract public  intstart_production()
     The index of the start production (supplied by generated subclass).
abstract public  intstart_state()
     The index of the start state (supplied by generated subclass).
public  voidsyntax_error(Symbol cur_token)
     This method is called when a syntax error has been detected and recovery is about to be invoked.
protected  booleantry_parse_ahead(boolean 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[][]unpackFromStrings(String[] sa)
    
public  voidunrecovered_syntax_error(Symbol cur_token)
     This method is called if it is determined that syntax error recovery has been unsuccessful.
public  voiduser_init()
     User code for initialization inside the parser.

Field Detail
_done_parsing
protected boolean _done_parsing(Code)
Internal flag to indicate when parser should quit.



_error_sync_size
final protected static int _error_sync_size(Code)
The default number of Symbols after an error we much match to consider it recovered from.



action_tab
protected short[][] action_tab(Code)
Direct reference to the action table.



cur_token
protected Symbol cur_token(Code)
The current lookahead Symbol.



lookahead
protected Symbol lookahead(Code)
Lookahead Symbols used for attempting error recovery "parse aheads".



lookahead_pos
protected int lookahead_pos(Code)
Position in lookahead input buffer used for "parse ahead".



production_tab
protected short[][] production_tab(Code)
Direct reference to the production table.



reduce_tab
protected short[][] reduce_tab(Code)
Direct reference to the reduce-goto table.



stack
protected Stack stack(Code)
The parse stack itself.



tos
protected int tos(Code)
Indication of the index for top of stack (for use by actions).




Constructor Detail
lr_parser
public lr_parser()(Code)
Simple constructor.



lr_parser
public lr_parser(Scanner s)(Code)
Constructor that sets the default scanner. [CSA/davidm]




Method Detail
EOF_sym
abstract public int EOF_sym()(Code)
The index of the end of file terminal Symbol (supplied by generated subclass).



action_table
abstract public short[][] action_table()(Code)
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.
See Also:   java_cup.runtime.lr_parser.get_action



advance_lookahead
protected boolean advance_lookahead()(Code)
Advance to next "parse ahead" input Symbol. Return true if we have input to advance to, false otherwise.



cur_err_token
protected Symbol cur_err_token()(Code)
Return the current lookahead in our error "parse ahead" buffer.



debug_message
public void debug_message(String mess)(Code)
Write a debugging message to System.err for the debugging version of the parser.
Parameters:
  mess - the text of the debugging message.



debug_parse
public Symbol debug_parse() throws java.lang.Exception(Code)
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
public void debug_reduce(int prod_num, int nt_num, int rhs_size)(Code)
Do debug output for a reduce.
Parameters:
  prod_num - the production we are reducing with.
Parameters:
  nt_num - the index of the LHS non terminal.
Parameters:
  rhs_size - the size of the RHS.



debug_shift
public void debug_shift(Symbol shift_tkn)(Code)
Do debug output for shift.
Parameters:
  shift_tkn - the Symbol being shifted onto the stack.



debug_stack
public void debug_stack()(Code)
Do debug output for stack state. [CSA]



do_action
abstract public Symbol do_action(int act_num, lr_parser parser, Stack stack, int top) throws java.lang.Exception(Code)
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.
Parameters:
  act_num - the internal index of the action to be performed.
Parameters:
  parser - the parser object we are acting for.
Parameters:
  stack - the parse stack of that object.
Parameters:
  top - the index of the top element of the parse stack.



done_parsing
public void done_parsing()(Code)
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
public void dump_stack()(Code)
Dump the parse stack for debugging purposes.



error_recovery
protected boolean error_recovery(boolean debug) throws java.lang.Exception(Code)
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.
Parameters:
  debug - should we produce debugging messages as we parse.



error_sym
abstract public int error_sym()(Code)
The index of the special error Symbol (supplied by generated subclass).



error_sync_size
protected int error_sync_size()(Code)
The number of Symbols after an error we much match to consider it recovered from.



find_recovery_config
protected boolean find_recovery_config(boolean debug)(Code)
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
Parameters:
  debug - should we produce debugging messages as we parse.



getScanner
public Scanner getScanner()(Code)
Simple accessor method to get the default scanner.



get_action
final protected short get_action(int state, int sym)(Code)
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).
Parameters:
  state - the state index of the action being accessed.
Parameters:
  sym - the Symbol index of the action being accessed.



get_reduce
final protected short get_reduce(int state, int sym)(Code)
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.
Parameters:
  state - the state index of the entry being accessed.
Parameters:
  sym - the Symbol index of the entry being accessed.



init_actions
abstract protected void init_actions() throws java.lang.Exception(Code)
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
public Symbol parse() throws java.lang.Exception(Code)
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
protected void parse_lookahead(boolean debug) throws java.lang.Exception(Code)
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.
Parameters:
  debug - should we produce debugging messages as we parse.



production_table
abstract public short[][] production_table()(Code)
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
protected void read_lookahead() throws java.lang.Exception(Code)
Read from input to establish our buffer of "parse ahead" lookahead Symbols.



reduce_table
abstract public short[][] reduce_table()(Code)
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.
See Also:   java_cup.runtime.lr_parser.get_reduce



report_error
public void report_error(String message, Object info)(Code)
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.
Parameters:
  message - an error message.
Parameters:
  info - an extra object reserved for use by specialized subclasses.



report_fatal_error
public void report_fatal_error(String message, Object info) throws java.lang.Exception(Code)
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.
Parameters:
  message - an error message.
Parameters:
  info - an extra object reserved for use by specialized subclasses.



restart_lookahead
protected void restart_lookahead() throws java.lang.Exception(Code)
Reset the parse ahead input to one Symbol past where we started error recovery (this consumes one new Symbol from the real input).



scan
public Symbol scan() throws java.lang.Exception(Code)
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
public void setScanner(Scanner s)(Code)
Simple accessor method to set the default scanner.



shift_under_error
protected boolean shift_under_error()(Code)
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
abstract public int start_production()(Code)
The index of the start production (supplied by generated subclass).



start_state
abstract public int start_state()(Code)
The index of the start state (supplied by generated subclass).



syntax_error
public void syntax_error(Symbol cur_token)(Code)
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.
Parameters:
  cur_token - the current lookahead Symbol.



try_parse_ahead
protected boolean try_parse_ahead(boolean debug) throws java.lang.Exception(Code)
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.
Parameters:
  debug - should we produce debugging messages as we parse.



unpackFromStrings
protected static short[][] unpackFromStrings(String[] sa)(Code)
Utility function: unpacks parse tables from strings



unrecovered_syntax_error
public void unrecovered_syntax_error(Symbol cur_token) throws java.lang.Exception(Code)
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.
Parameters:
  cur_token - the current lookahead Symbol.



user_init
public void user_init() throws java.lang.Exception(Code)
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.



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.