예제 #1
0
 /*
  * Returns a parser that for the grammar rule:
  *
  *     minusTerm = '-' term;
  *
  * This parser has an assembler that will pop two
  * numbers from the stack and push their difference.
  */
 protected Parser minusTerm() {
   Sequence s = new Sequence();
   s.add(new Symbol('-').discard());
   s.add(term());
   s.setAssembler(new MinusAssembler());
   return s;
 }
예제 #2
0
 /**
  * Returns a parser that will recognize a Midimath expression.
  *
  * @return a parser that will recognize a Midimath expression
  */
 public Parser expression() {
   if (expression == null) {
     expression = new Sequence();
     expression.add(term());
     expression.add(new Repetition(minusTerm()));
   }
   return expression;
 }
예제 #3
0
  /*
   * Returns a parser that for the grammar rule:
   *
   *    term = '(' expression ')' | Num;
   *
   * This parser adds an assembler to Num, that will
   * replace the top token in the stack with the token's
   * Double value.
   */
  protected Parser term() {

    Sequence s = new Sequence();
    s.add(new Symbol('(').discard());
    s.add(expression());
    s.add(new Symbol(')').discard());

    Alternation a = new Alternation();
    a.add(s);
    a.add(new Num().setAssembler(new NumAssembler()));
    return a;
  }