[antlr-interest] conditional processing
Gerald B. Rosenberg
gbr at newtechlaw.com
Wed Nov 28 09:43:24 PST 2007
Probably a bit of overkill in your given case, but to do it (using
the boolean return from condition):
statement
: simpleStatement
| ^('IF' b=condition { $b.result }? simpleStatement)
;
catch[FailedPredicateException fpe] {System.err.println("Ignore");}
simpleStatement
: ^('ADD' n=NUMBER) { value += toInt(n); }
| ^('SUBTRACT' n=NUMBER) { value -= toInt(n); }
| 'PRINT' { System.out.println(value); }
;
Appropriate where simpleStatement does not/should not be aware of condition.
At 07:29 AM 11/28/2007, Mark Volkmann wrote:
>On 11/26/07, Gerald B. Rosenberg <gbr at newtechlaw.com> wrote:
> >
> > At 07:02 PM 11/26/2007, Mark Volkmann wrote:
> >
> > Part of my AST looks like this.
> >
> > ^('if' condition simpleStatement)
> > Would help to see the full rule and the condition rule. Still, a possible
> > solution is to use a semantic predicate, something like:
> >
> > ^('if' b=condition { $b.equals("true") }? simpleStatement)
>
>I want to try you suggestion. Below is my grammar with your suggestion
>incorporated. When the generated Java code is compiled, I get the
>following error. Do you see anything wrong in my small grammar?
>
>---
>
> [javac]
> /Users/Mark/Documents/Programming/ANTLR/MathScriptAST/gen/com/ociweb/mathscript/MathScriptTree.java:179:
>cannot find symbol
> [javac] symbol : variable $b
> [javac] location: class com.ociweb.mathscript.MathScriptTree
> [javac] if ( !( $b.equals("true") ) ) {
> [javac] ^
>
>---
>
>tree grammar MathScriptTree;
>
>options {
> ASTLabelType = CommonTree;
> tokenVocab = MathScript;
> output = template;
>}
>
>@header {
> package com.ociweb.mathscript;
>}
>
>@members {
> int value;
>
> private static int toInt(CommonTree node) {
> return Integer.parseInt(node.getText());
> }
>}
>
>script: ^(SCRIPT statement*);
>
>statement
> : simpleStatement
> | ^('IF' b=condition { $b.equals("true") }? simpleStatement)
> ;
>
>simpleStatement
> : ^('ADD' n=NUMBER) { value += toInt(n); }
> | ^('SUBTRACT' n=NUMBER) { value -= toInt(n); }
> | 'PRINT' { System.out.println(value); }
> ;
>
>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); }
> ;
>
> >
> >
> >
> >
> > 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.
> >
> > ----
> > Gerald B. Rosenberg, Esq.
> > NewTechLaw
> > 260 Sheridan Ave., Suite 208
> > Palo Alto, CA 94306-2009
> >
> > 650.325.2100 (office) / 650.703.1724 (cell)
> > 650.325.2107 (facsimile)
> >
> > www.newtechlaw.com
> >
> >
> > CONFIDENTIALITY NOTICE: This email message (including any attachments) is
> > being sent by an attorney, is for the sole use of the intended recipient,
> > and may contain confidential and privileged information. Any unauthorized
> > review, use, disclosure or distribution is prohibited. If you are not the
> > intended recipient, please contact the sender immediately by
> reply email and
> > delete all copies of this message and any attachments without retaining a
> > copy.
>
>
>--
>R. Mark Volkmann
>Object Computing, Inc.
----
Gerald B. Rosenberg
Certiv Analytics
www.certiv.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071128/6a070d86/attachment.html
More information about the antlr-interest
mailing list