net.percederberg.grammatica.parser
Class Parser

java.lang.Object
  extended by net.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.
protected  Analyzer newAnalyzer()
          Creates a new analyzer for this parser.
protected  Production newProduction(ProductionPattern pattern)
          Factory method to create a new production node.
protected  Tokenizer newTokenizer(java.io.Reader input)
          Creates a new tokenizer for 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.
 void reset(java.io.Reader input)
          Resets this parser for usage with another input stream.
 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

newTokenizer

protected Tokenizer newTokenizer(java.io.Reader input)
                          throws ParserCreationException
Creates a new tokenizer for this parser. Can be overridden by a subclass to provide a custom implementation.

Parameters:
input - the input stream to read from
Returns:
the tokenizer created
Throws:
ParserCreationException - if the tokenizer couldn't be initialized correctly
Since:
1.5

newAnalyzer

protected Analyzer newAnalyzer()
Creates a new analyzer for this parser. Can be overridden by a subclass to provide a custom implementation.

Returns:
the analyzer created
Since:
1.5

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

reset

public void reset(java.io.Reader input)
Resets this parser for usage with another input stream. The associated tokenizer and analyzer will also be reset. This method will clear all the internal state and the error log in the parser. It is normally called in order to reuse a parser and tokenizer pair with multiple input streams, thereby avoiding the cost of re-analyzing the grammar structures.

Parameters:
input - the new input stream to read
Since:
1.5
See Also:
Tokenizer.reset(java.io.Reader), Analyzer.reset()

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. It will also call the reset() method, to making sure that only the Tokenizer.reset() method must be explicitly called in order to reuse a parser for multiple input streams. In case of a parse error, the parser will attempt to recover and throw all the errors found in a parser log exception at the end of the parsing.

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(), reset(java.io.Reader), Tokenizer.reset(java.io.Reader)

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

newProduction

protected Production newProduction(ProductionPattern pattern)
Factory method to create a new production node. This method can be overridden to provide other production implementations than the default one.

Parameters:
pattern - the production pattern
Returns:
the new production node
Since:
1.5

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.

Overrides:
toString in class java.lang.Object
Returns:
a detailed string representation of this parser