Building Parsers with Java

sjm.examples.midimath
Class MidiloopNot

java.lang.Object
  |
  +--sjm.examples.midimath.MidiloopNot

public class MidiloopNot
extends java.lang.Object

This class creates and uses a parser that aims to recognize simple arithmetic expressions, but gets caught in a loop. Allowable expressions include the use of multiplication, addition and parentheses. The grammar for this language is:

	
     expression = term ('+' term)*;
     term       = factor ('*' factor)*;
     factor     = '(' expression ')' | Num;
 
This class implements this grammar as a utility class, and avoids looping while building the subparsers.


Field Summary
protected static Sequence expression
           
 
Constructor Summary
MidiloopNot()
           
 
Method Summary
 Parser expression()
          Returns a parser that will recognize a Midimath expression.
protected  Parser factor()
          Returns a parser that will recognize either numbers, or arithmetic expressions in parentheses.
static void main(java.lang.String[] args)
          Demonstrate that this utility class does not loop.
protected  Parser term()
          Returns a parser that will recognize arithmetic expressions containing just multiplication.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

expression

protected static Sequence expression
Constructor Detail

MidiloopNot

public MidiloopNot()
Method Detail

expression

public Parser expression()
Returns a parser that will recognize a Midimath expression.
Returns:
a parser that will recognize a Midimath expression

factor

protected Parser factor()
Returns a parser that will recognize either numbers, or arithmetic expressions in parentheses.
Returns:
a parser that will recognize either numbers, or arithmetic expressions in parentheses

main

public static void main(java.lang.String[] args)
Demonstrate that this utility class does not loop.

term

protected Parser term()
Returns a parser that will recognize arithmetic expressions containing just multiplication.
Returns:
a parser that will recognize arithmetic expressions containing just multiplication

by Steve Metsker