Hi all,

I tried to skip a rule in an if-then-else construct using gated symantic predicates.

ifElse
scope {
  boolean expResult;
} :
   ^(
      IFTHEN b = expression { $ifElse::expResult = b; }
      (
         {$ifElse::expResult == true}?=> actionSequence
         | . // if expResult == false, no action required but eat the token
      )
     )
   |
   ^(
      IFTHENELSE b = expression { $ifElse::expResult = b; }
      (
         {$ifElse::expResult == true}? actionSequence
. // if expResult == true, call the 'then' action and 'eat' the else action | . actionSequence // if expResult == false, 'eat' the 'then' action and call the else action
      )
     );


leeds to
Eval.g: node from after line 1:21 no viable alternative at input 'DOWN

This works well until the subrule itself is a tree (i.e. nested if-then-else). So it seems, that the '.' does not match the whole subtree?

Thanks for any hint, fridi
tree grammar Eval;

options {
   tokenVocab   = Simple;
   language     = Java;
   ASTLabelType = CommonTree;
}

@header {
  import java.util.Hashtable;
}

@members {
  private Hashtable<String, Integer> vars = new Hashtable<String, Integer>();
  
  public void setVars(Hashtable<String, Integer> vars) {
     this.vars = vars;
  }
  
  public void setVar(String name, int value) {
     vars.put(name, new Integer(value));
  }   
  
  public int getVar(String name) {
     if (vars.containsKey(name)) {
        return vars.get(name).intValue();
     }        
     return -1;
  }
  
  boolean expResult;
}

rule :
   ifElse;

ifElse
scope {
  boolean expResult; 
} :
   ^(
      IFTHEN b = expression { $ifElse::expResult = b; }
      (
         {$ifElse::expResult == true}?=> actionSequence
         | . // if expResult == false, no action required but eat the token
      )
     )
   |
   ^(
      IFTHENELSE b = expression { $ifElse::expResult = b; }
      (
         {$ifElse::expResult == true}? actionSequence
         . // if expResult == true, call the 'then' action and 'eat' the else 
action
         | . actionSequence // if expResult == false, 'eat' the 'then' action 
and call the else action
      )
     );

actionSequence :
   ^(as = ASSIGN id = ID in = INT )
   {
    int value = 0;
    try {
      value = Integer.parseInt($in.text);
    } catch (Exception e) {     
    }
    
    setVar($id.text, value);  
  }
   | ifElse;

expression returns [boolean b] :
   ^(op = OPERAND id = ID in = INT {
    int var = getVar($id.text);
   
    int value = 0;
    try {
      value = Integer.parseInt($in.text);
    } catch (Exception e) {     
    }
    
    $b = var > value;
   
  } );
grammar Simple;

options {
  language = Java;
  output   = AST;
}

tokens {
  IFTHEN; 
  IFTHENELSE;
}

start
  :
  rule
  ;

rule
  :
  IF expression 'then' a1=actionSequence
  (
    'else' a2=actionSequence
      ->
        ^(IFTHENELSE expression $a1 $a2)
    |
      ->
        ^(IFTHEN expression $a1)
  )
  'endif'
  ;

actionSequence
  :
  ID ASSIGN^ INT
  | rule
  ;

expression
  :
  ID OPERAND^ INT
  ;

IF
  :
  'if'
  ;

ASSIGN
  :
  '='
  ;

OPERAND
  :
  '>' // reduced to > for simple example
  ;

ID
  :
  (
    'a'..'z'
    | 'A'..'Z'
    | '_'
  )
  (
    'a'..'z'
    | 'A'..'Z'
    | '0'..'9'
    | '_'
  )*
  ;

INT
  :
  '0'..'9'+
  ;

WS
  :
  (
    ' '
    | '\t'
    | '\r'
    | '\n'
  )
  {$channel=HIDDEN;}
  ;
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address
-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to