Building Parsers with Java

sjm.examples.arithmetic
Class ArithmeticParser

java.lang.Object
  |
  +--sjm.examples.arithmetic.ArithmeticParser

public class ArithmeticParser
extends java.lang.Object

This class provides a parser that recognizes arithmetic expressions. This class includes the method value, which is a "façade" that provides an example and makes the parser easy to use. For example,

 
     System.out.println(
         ArithmeticParser.value("(5 + 4) * 3 ^ 2 - 81"));
 
This prints out 0.0.

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

	
     expression    = term (plusTerm | minusTerm)*;
     term          = factor (timesFactor | divideFactor)*;
     plusTerm      = '+' term;
     minusTerm     = '-' term;
     factor        = phrase expFactor | phrase;
     timesFactor   = '*' factor;
     divideFactor  = '/' factor;
     expFactor     = '^' factor;
     phrase        = '(' expression ')' | Num;
 
These rules recognize conventional operator precedence and associativity. 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. In other words, the rules may look simple, but their structure is subtle.


Field Summary
protected  Sequence expression
           
protected  Alternation factor
           
 
Constructor Summary
ArithmeticParser()
           
 
Method Summary
protected  Parser divideFactor()
           
protected  Parser expFactor()
           
 Parser expression()
          Returns a parser that will recognize an arithmetic expression.
protected  Parser factor()
           
protected  Parser minusTerm()
           
protected  Parser phrase()
           
protected  Parser plusTerm()
           
static Parser start()
          Returns a parser that will recognize an arithmetic expression.
protected  Parser term()
           
protected  Parser timesFactor()
           
static double value(java.lang.String s)
          Return the value of an arithmetic 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

factor

protected Alternation factor
Constructor Detail

ArithmeticParser

public ArithmeticParser()
Method Detail

divideFactor

protected Parser divideFactor()

expFactor

protected Parser expFactor()

expression

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

factor

protected Parser factor()

minusTerm

protected Parser minusTerm()

phrase

protected Parser phrase()

plusTerm

protected Parser plusTerm()

start

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

term

protected Parser term()

timesFactor

protected Parser timesFactor()

value

public static double value(java.lang.String s)
                    throws ArithmeticExpressionException
Return the value of an arithmetic expression given in a string. This method is a façade, which provides an example of how to use the parser.
Parameters:
String - the string to evaluate.
Returns:
the value of an arithmetic expression given in a string
Throws:
ArithmeticExpressionException - if this parser does not recognize the given string as a valid expression

by Steve Metsker