[antlr-interest] conditional processing

Mark Volkmann r.mark.volkmann at gmail.com
Wed Nov 28 12:08:12 PST 2007


On 11/28/07, Gerald B. Rosenberg <gbr at newtechlaw.com> wrote:
>
>  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.

I like your approach, but I can't get it to work. It seems that
something is wrong with the catch of FailedPredicateException. When
the first IF whose condition evaluates to false is encountered, it
prints "Ignore", executes the simpleStatement after the semantic
predicate anyway, and then stops processing, ignoring the rest of the
AST.

>  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
>


-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list