net.percederberg.grammatica.parser
Class RecursiveDescentParser

java.lang.Object
  extended bynet.percederberg.grammatica.parser.Parser
      extended bynet.percederberg.grammatica.parser.RecursiveDescentParser

public class RecursiveDescentParser
extends Parser

A recursive descent parser. This parser handles LL(n) grammars, selecting the appropriate pattern to parse based on the next few tokens. The parser is more efficient the fewer look-ahead tokens that is has to consider.


Constructor Summary
RecursiveDescentParser(Tokenizer tokenizer)
          Creates a new parser.
RecursiveDescentParser(Tokenizer tokenizer, Analyzer analyzer)
          Creates a new parser.
 
Method Summary
 void addPattern(ProductionPattern pattern)
          Adds a new production pattern to the parser.
protected  Node parseStart()
          Parses the input stream and creates a parse tree.
 void prepare()
          Initializes the parser.
 
Methods inherited from class net.percederberg.grammatica.parser.Parser
getAnalyzer, getTokenizer, parse, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RecursiveDescentParser

public RecursiveDescentParser(Tokenizer tokenizer)
Creates a new parser.

Parameters:
tokenizer - the tokenizer to use

RecursiveDescentParser

public RecursiveDescentParser(Tokenizer tokenizer,
                              Analyzer analyzer)
Creates a new parser.

Parameters:
tokenizer - the tokenizer to use
analyzer - the analyzer callback to use
Method Detail

addPattern

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

Overrides:
addPattern in class Parser
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 the internal data structures used during the parsing.

Overrides:
prepare in class Parser
Throws:
ParserCreationException - if the parser couldn't be initialized correctly

parseStart

protected Node parseStart()
                   throws ParseException
Parses the input stream and creates a parse tree.

Specified by:
parseStart in class Parser
Returns:
the parse tree
Throws:
ParseException - if the input couldn't be parsed correctly