[antlr-interest] conditional processing

Mark Volkmann r.mark.volkmann at gmail.com
Tue Nov 27 04:50:22 PST 2007


On 11/27/07, Harald Mueller <harald_m_mueller at gmx.de> wrote:
> Hi -
>
> You need a context that keeps knowledge about the world - variable values, but also results of evaluations e.g. for ifs, returns etc. Usually the context is a stack - see any text on fundamentals of progam language semantics. The context is threaded through all your calls (as a simple parameter), and your calls - e.g. "simpleStatement" - can then react to it:
>
> if (boolStack.Top) {
>     ...do evaluation...
> }
>
> Just imagine what data a virtual machine would need to do what you want - exactly these data are what you need in your context.

>From what you described, it sounds like my current solution might be
using the right approach. I don't have to worry about nested
conditionals, so I probably don't need stacks now. Here's what I have.
Note how the rule for simpleStatement just remembers what it saw but
doesn't execute the statement. Also note how the rule that matches
"IF" executes the statement if the condition evaluates to true. Does
this seem like a reasonable way to implement this?

tree grammar MathScriptTree;

options {
  ASTLabelType = CommonTree;
  tokenVocab = MathScript;
  output = template;
}

@members {
  enum Operation { Add, Subtract, Print }
  Operation operation;
  int operand;
  int value;

  private void execute() {
    switch (operation) {
      case Add: value += operand; break;
      case Subtract: value -= operand; break;
      case Print: System.out.println(value); break;
    }
  }

  private static int toInt(CommonTree node) {
    return Integer.parseInt(node.getText());
  }
}

script: ^(SCRIPT statement*);

statement
  : simpleStatement { execute(); }
  | ^('IF' c=condition simpleStatement) { if ($c.result) execute(); }
  ;

simpleStatement
  : ^('ADD' n=NUMBER) { operation = Operation.Add; operand = toInt(n); }
  | ^('SUBTRACT' n=NUMBER) { operation = Operation.Subtract; operand =
toInt(n); }
  | 'PRINT' { operation = Operation.Print; }
  ;

condition returns [boolean result]
  : 'POSITIVE' { $result = value > 0; }
  | 'NEGATIVE' { $result = value < 0; }
  | ^('<' n=NUMBER) { $result = value < toInt(n); }
  | ^('>' n=NUMBER) { $result = value > toInt(n); }
  | ^('=' n=NUMBER) { $result = value == toInt(n); }
  ;

> -------- Original-Nachricht --------
> > Datum: Mon, 26 Nov 2007 21:02:46 -0600
> > Von: "Mark Volkmann" <r.mark.volkmann at gmail.com>
> > An: antlr-interest at antlr.org
> > Betreff: [antlr-interest] conditional processing
>
> > Part of my AST looks like this.
> >
> > ^('if' condition simpleStatement)
> >
> > In my tree grammar, the rule for "condition" evaluates the matching
> > content to true or false. The rule for "simpleStatement" executes the
> > statement, for example, it could be a simple print statement. I only
> > want simpleStatement to be executed if condition evaluates to true. I
> > haven't been able to figure out how to make this work because when
> > this part of my AST is matched, the rules for both condition and
> > simpleStatement are fired.
> >
> > I'd appreciate any hints on how to achieve this.
> >
> > --
> > R. Mark Volkmann
> > Object Computing, Inc.
>
> --
> GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
> Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail
>


-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list