[antlr-interest] How to skip evaluating part of a rule?

Charles Chan chancharles at gmail.com
Fri May 14 09:25:27 PDT 2010


Hi, all. I have a parser optimization question for you guys. In my
grammar (below), I have to evaluate an OR condition and an AND
condition. We all know that "true OR true/false" is always true and
"false AND true/false" is always false. So, technically, if I can skip
evaluating RHS under those conditions.

Is there a way for me to incorporate this kind of optimization in my
grammar? Currently, I am retrieving the values from a Map so that
performance is okay but I plan to retrieve them from DB, so any
optimization to skip the retrieval process would be great.

Any idea?

Thanks!
Charles


grammar Script;

options {
  output=AST;
}

@parser::header {

import java.util.Map;
import java.util.HashMap;
import java.math.BigDecimal;
}

@parser::members {
  public Map<String, Object> values = new HashMap<String, Object>();

  public static void main(String[] args) throws Exception {
    ScriptLexer lexer = new ScriptLexer(new ANTLRFileStream(args[0]));
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    ScriptParser parser = new ScriptParser(tokenStream);
    parser.values.put("A", "TEST1");
    parser.values.put("B", "TEST2");
    parser.values.put("C", "TEST3");
    System.out.println(parser.statement().value);
  }

  protected void mismatch(IntStream input, int ttype, BitSet follow)
throws RecognitionException {
    throw new MismatchedTokenException(ttype, input);
  }

  public Object recoverFromMismatchedSet(IntStream input,
RecognitionException e, BitSet follow) throws RecognitionException {
    throw e;
  }
}

@rulecatch {
  catch (RecognitionException ex) {
    throw ex;
  }
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

statement returns [boolean value]: e=orcondition { $value = $e.value; }
         ;

orcondition returns [boolean value]
    :   e=andcondition  { $value = $e.value; }
        (OR^ e=andcondition
        {
		  $value = $value || $e.value;
        })*
    ;

andcondition returns [boolean value]
    : e=expression { $value = $e.value; }
      (AND^ e=expression
      {
	    $value = $value && $e.value;
      })*
    ;

expression returns [boolean value]
    : ID operator rhs
    {
      Object idValue = (Object)values.get($ID.text);
	  Object rhsValue = $rhs.value;
      $value = idValue.equals(rhsValue);
    }
    | LEFT_PAREN! orcondition RIGHT_PAREN!
	{
	  $value = $orcondition.value;
	}
    ;

operator : GTE | LTE | GT | LT | EQ | NOT_EQ
         ;
rhs returns [Object value]
    : e=INTEGER        { $value = Integer.valueOf($e.text); } |
      e=FLOAT          { $value = new BigDecimal($e.text); } |
	  e=STRING_LITERAL { $value = $e.text.substring(1, $e.text.length() - 1); } |
	  TRUE             { $value = Boolean.TRUE; } |
	  FALSE            { $value = Boolean.FALSE; }
    ;
	
/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/
AND     :       'and'
        ;
OR      :       'or'
        ;
LT      :       '<'
        ;
GT      :       '>'
        ;
LTE     :       '<='
        ;
GTE     :       '>='
        ;
EQ      :       '='
        ;
NOT_EQ  :       '!='
        ;
TRUE    :       'true'
        ;
FALSE   :       'false'
        ;
ID      :       ( 'a' .. 'z' | 'A' .. 'Z' | '.' | '_' )+
        ;
STRING_LITERAL  : '\'' ( ( '\'' '\'' )=>  '\'' '\'' | ~'\'' )* '\''
                ;
LEFT_PAREN      : '('
                ;
RIGHT_PAREN     : ')'
                ;
WS      : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+    { $channel = HIDDEN; }
        ;
SIGN    : '+' | '-'
        ;

INTEGER: '0' | SIGN? '1'..'9' '0'..'9'*;

FLOAT: INTEGER '.' '0'..'9'+;


More information about the antlr-interest mailing list