Java(TM) Compiler Compiler(TM)

The JavaCC API Routines


This web page is a comprehensive list of all classes, methods, and variables available for use by a JavaCC user. These classes, methods, and variables are typically used from the actions that are embedded in a JavaCC grammar. In the sample code used below, it is assumed that the name of the generated parser is "TheParser".


Non-Terminals in the Input Grammar


For each non-terminal NT in the input grammar file, the following method is generated into the parser class:

Here, returntype and parameters are what were specified in the JavaCC input file in the definition of NT (where NT occurred on the left-hand side).

When this method is called, the input stream is parsed to match this non-terminal. On a successful parse, this method returns normally. On detection of a parse error, an error message is displayed and the method returns by throwing the exception ParseError.

Note that all non-terminals in a JavaCC input grammar have equal status and therefore any non-terminal's method may be called to parse to it.


API for Parser Actions


In addition, the two methods - getToken(int i) and getNextToken() can also be used in actions to traverse the token list.


The Token Manager Interface


Typically, the token manager interface is not to be used. Instead all access must be made through the parser interface. However, in certain situations - such as if you are not building a parser and building only the token manager - the token manager interface is useful. The token manager provides the following routine:

Each call to this method returns the next token in the input stream. This method throws a ParseError exception when there is a lexical error, i.e., it could not find a match for any of the specified tokens from the input stream. The type Token is described later.


Constructors and Other Initialization Routines



The Token Class


The Token class is the type of token objects that are created by the token manager after a successful scanning of the token stream. These token objects are then passed to the parser and is accessible to the actions in a JavaCC grammar usually by grabbing the return value of a token. The methods getToken and getNextToken described below also give access to objects of this type.

Each Token object has the following fields:


Reading Tokens from the Input Stream


There are two methods available for this purpose:


Working with Debugger Tracing


When you generate parsers with the options DEBUG_PARSER or DEBUG_LOOKAHEAD, these parsers produce a trace of their activity which is printed to the user console. You can insert calls to the following methods to control this tracing activity:

For convenience, these methods are available even when you build parsers without the debug options. In this case, these methods are no-ops. Hence you can permanently leave these methods in your code and they automatically kick in when you use the debug options.


Customizing Error Messages


To help the user in customizing error messages generated by the parser and lexer, the user is offered the facilities described in this section. In the case of the parser, these facilities are only available if the option ERROR_REPORTING is true, while in the case of the lexer, these facilities are always available.

The parser contains the following method definition:

To customize error reporting by the parser, the parser class must be subclassed and this method redefined in the subclass. To help with creating your error reporting scheme, the following variables are available:

The lexer contains the following method definition:

To customize error reporting by the lexer, the lexer class must be subclassed and this method redefined in the subclass. To help with creating your error reporting scheme, the following variables are available:


JJTree


JJTree has two APIs: it adds some parser methods; and it requires all node objects to implement the Node interface.

JJTree parser methods

JJTree maintains some state in the parser object itself. It encapsulates all this state with an object that can be referred to via the jjtree field.

The parser state implements an open stack where nodes are held until they can be added to their parent node. The jjtree state object provides methods for you to manipulate the contents of the stack in your actions if the basic JJTree mechanisms are not sufficient.

void reset()
Call this to reinitialize the node stack. All nodes currently on the stack are thrown away. Don't call this from within a node scope, or terrible things will surely happen.
Node rootNode();
Returns the root node of the AST. Since JJTree operates bottom-up, the root node is only defined after the parse has finished.
boolean nodeCreated();
Determines whether the current node was actually closed and pushed. Call this in the final action within a conditional node scope.
int arity();
Returns the number of nodes currently pushed on the node stack in the current node scope.
void pushNode(Node n);
Pushes a node on to the stack.
Node popNode();
Returns the node on the top of the stack, and removes it from the stack.
Node peekNode();
Returns the node currently on the top of the stack.

The Node interface

All AST nodes must implement this interface. It provides basic machinery for constructing the parent and child relationships between nodes.

public void jjtOpen();
This method is called after the node has been made the current node. It indicates that child nodes can now be added to it.
public void jjtClose();
This method is called after all the child nodes have been added.
public void jjtSetParent(Node n);
public Node jjtGetParent();
This pair of methods are used to inform the node of its parent.
public void jjtAddChild(Node n, int i);
This method tells the node to add its argument to the node's list of children.
public Node jjtGetChild(int i);
This method returns a child node. The children are numbered from zero, left to right.
int jjtGetNumChildren();
Return the number of children the node has.

JavaCC Home | SunTest Home | Download | Testimonials | Documentation | FAQ | Support | Contact Us