net.percederberg.grammatica.parser
Class Parser

java.lang.Object
  extended bynet.percederberg.grammatica.parser.Parser
Direct Known Subclasses:
RecursiveDescentParser

public abstract class Parser
extends java.lang.Object

A base parser class. This class provides the standard parser interface, as well as token handling.


Method Summary
 void addPattern(ProductionPattern pattern)
          Adds a new production pattern to the parser.
 Analyzer getAnalyzer()
          Returns the analyzer in use by this parser.
 Tokenizer getTokenizer()
          Returns the tokenizer in use by this parser.
 Node parse()
          Parses the token stream and returns a parse tree.
protected abstract  Node parseStart()
          Parses the token stream and returns a parse tree.
 void prepare()
          Initializes the parser.
 java.lang.String toString()
          Returns a string representation of this parser.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getTokenizer

public Tokenizer getTokenizer()
Returns the tokenizer in use by this parser.

Returns:
the tokenizer in use by this parser
Since:
1.4

getAnalyzer

public Analyzer getAnalyzer()
Returns the analyzer in use by this parser.

Returns:
the analyzer in use by this parser
Since:
1.4

addPattern

public void addPattern(ProductionPattern pattern)
                throws ParserCreationException
Adds a new production pattern to the parser. The first pattern added is assumed to be the starting point in the grammar. The patterns added may be validated to some extent.

Parameters:
pattern - the pattern to add
Throws:
ParserCreationException - if the pattern couldn't be added correctly to the parser

prepare

public void prepare()
             throws ParserCreationException
Initializes the parser. All the added production patterns will be analyzed for ambiguities and errors. This method also initializes internal data structures used during the parsing.

Throws:
ParserCreationException - if the parser couldn't be initialized correctly

parse

public Node parse()
           throws ParserCreationException,
                  ParserLogException
Parses the token stream and returns a parse tree. This method will call prepare() if not previously called. In case of a parse error, the parser will attempt to recover and throw all the errors found in a parser log exception in the end.

Returns:
the parse tree
Throws:
ParserCreationException - if the parser couldn't be initialized correctly
ParserLogException - if the input couldn't be parsed correctly
See Also:
prepare()

parseStart

protected abstract Node parseStart()
                            throws ParseException
Parses the token stream and returns a parse tree.

Returns:
the parse tree
Throws:
ParseException - if the input couldn't be parsed correctly

toString

public java.lang.String toString()
Returns a string representation of this parser. The string will contain all the production definitions and various additional information.

Returns:
a detailed string representation of this parser