net.percederberg.grammatica.parser
Class RecursiveDescentParser

java.lang.Object
  |
  +--net.percederberg.grammatica.parser.Parser
        |
        +--net.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.

Version:
1.0
Author:
Per Cederberg,

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.
 Node parse()
          Parses the input stream and creates a parse tree.
 void prepare()
          Initializes the parser.
 
Methods inherited from class net.percederberg.grammatica.parser.Parser
addNode, enterNode, exitNode, getPattern, getPatterns, getStartPattern, nextToken, nextToken, peekToken
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, 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

parse

public Node parse()
           throws ParserCreationException,
                  ParseException
Parses the input stream and creates a parse tree.

Specified by:
parse in class Parser
Returns:
the parse tree
Throws:
ParserCreationException - if the parser couldn't be initialized correctly
ParseException - if the input couldn't be parsed correctly
See Also:
prepare()