[antlr-interest] conditional processing
Harald Mueller
harald_m_mueller at gmx.de
Tue Nov 27 05:22:26 PST 2007
Hi
after a quick scanning of your code, I'd say this should work.
However, I don't like the design that much: global variables are per se a bad decision - passing your knowledge around (in single parameters; or, when that gets too cumbersome, in my beloved "context object") is simply more state-of-the-art.
Still, I confess that this argument only is valid if your language will grow one day - then a better design will scale better ... (wouldn't you (or someone) at some time like (or need) to have nested ifs or somewhat more expressive expressions? - all little languages I saw one day wanted to do such things).
At least, you could safe-guard your global variables so that you can only assign a value once to them; then do an evaluation which clears the variables; etc. (in other words, define a clean state machine for them; and implement it - probably in some small classes). That way, any modification of your grammar or your evaluation logic which invalidates your implicit assumptions about when a variable is written and when it is read will at least fly into your face immediately ...
Regards
Harald M.
-------- Original-Nachricht --------
> Datum: Tue, 27 Nov 2007 06:50:22 -0600
> Von: "Mark Volkmann" <r.mark.volkmann at gmail.com>
> An: antlr-interest at antlr.org
> Betreff: Re: [antlr-interest] conditional processing
> 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.
--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser
More information about the antlr-interest
mailing list