Building Parsers with Java

sjm.examples.regular
Class RegularParser

java.lang.Object
  |
  +--sjm.examples.regular.RegularParser

public class RegularParser
extends java.lang.Object

This class provides a parser that recognizes regular expressions.

Regular expressions are a "metalanguage", which means they form a language for describing languages. For example, a* is a regular expression that describes a simple language whose elements are strings composed of 0 or more a's. Thus the result of parsing a* is a new parser, namely a parser that will match strings of a's.

This class exists to show how a simple regular expression parser works. It recognizes expressions according to the following rules.

     expression    = term orTerm*;
     term          = factor nextFactor*;
     orTerm        = '|' term;
     factor        = phrase | phraseStar;
     nextFactor    = factor;
     phrase        = letterOrDigit | '(' expression ')';
     phraseStar    = phrase '*';
     letterOrDigit = Letter | Digit;
 
These rules recognize conventional operator precedence. They also avoid the problem of left recursion, and their implementation avoids problems with the infinite loop inherent in the cyclic dependencies of the rules.


Field Summary
protected  Sequence expression
           
 
Constructor Summary
RegularParser()
           
 
Method Summary
 Parser expression()
          Returns a parser that will recognize a regular expression.
protected  Parser factor()
           
protected  Parser letterOrDigit()
           
protected  Parser nextFactor()
           
protected  Parser orTerm()
           
protected  Parser phrase()
           
protected  Parser phraseStar()
           
static Parser start()
          Returns a parser that will recognize a regular expression.
protected  Parser term()
           
static Parser value(java.lang.String s)
          Return a parser that will match a CharacterAssembly, according to the value of a regular expression given in a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

expression

protected Sequence expression
Constructor Detail

RegularParser

public RegularParser()
Method Detail

expression

public Parser expression()
Returns a parser that will recognize a regular expression. (Identical to start()).
Returns:
a parser that will recognize a regular expression

factor

protected Parser factor()

letterOrDigit

protected Parser letterOrDigit()

nextFactor

protected Parser nextFactor()

orTerm

protected Parser orTerm()

phrase

protected Parser phrase()

phraseStar

protected Parser phraseStar()

start

public static Parser start()
Returns a parser that will recognize a regular expression.
Returns:
a parser that will recognize a regular expression

term

protected Parser term()

value

public static Parser value(java.lang.String s)
                    throws RegularExpressionException
Return a parser that will match a CharacterAssembly, according to the value of a regular expression given in a string. For example, given the string a*, this method will return a parser which will match any element of the set {"", "a", "aa", "aaa", ...}.
Parameters:
String - the string to evaluate
Returns:
a parser that will match a CharacterAssembly, according to the value of a regular expression in the given string
Throws:
RegularExpressionException - if this parser does not recognize the given string as a valid expression

by Steve Metsker