You don't have a parser rule (parser rules start with a lower case letter), although I'm not sure that last part is necessary when interpreting some test cases in ANTLRWorks.
Anyway, try something like this:
grammar AdifyMapReducePredicate;
parse
: (p=predicate {System.out.println("parsed :: "+$p.text);})+ EOF
;
predicate
: expression
;
expression
: booleanExpression
;
booleanExpression
: atom ((AND | OR) atom)*
;
atom
: ID
| '(' predicate ')'
;
AND
: '&&'
;
OR
: '||'
;
ID
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
SPACE
: (' ' | '\t' | '\r' | '\n') {skip();}
;
With the following test class:
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
public class Main {
public static void main(String[] args) throws Exception {
ANTLRStringStream in = new ANTLRStringStream("(A || B) (C && (D || F || G))");
AdifyMapReducePredicateLexer lexer = new AdifyMapReducePredicateLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
AdifyMapReducePredicateParser parser = new AdifyMapReducePredicateParser(tokens);
parser.parse();
}
}
which after generating a lexer & parser (a), compiling all .java
files (b) and running the test class (c), produces the following output:
parsed :: (A||B)
parsed :: (C&&(D||F||G))
a
java -cp antlr-3.2.jar org.antlr.Tool AdifyMapReducePredicate.g
b
javac -cp antlr-3.2.jar *.java
c (*nix/MacOS)
java -cp .:antlr-3.2.jar Main
c (Windows)
java -cp .;antlr-3.2.jar Main
HTH