Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>Anyway, try something like this:</p> <pre><code>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 : '&amp;&amp;' ; OR : '||' ; ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ; SPACE : (' ' | '\t' | '\r' | '\n') {skip();} ; </code></pre> <p>With the following test class:</p> <pre><code>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 &amp;&amp; (D || F || G))"); AdifyMapReducePredicateLexer lexer = new AdifyMapReducePredicateLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); AdifyMapReducePredicateParser parser = new AdifyMapReducePredicateParser(tokens); parser.parse(); } } </code></pre> <p>which after generating a lexer &amp; parser (a), compiling all <code>.java</code> files (b) and running the test class (c), produces the following output:</p> <pre> parsed :: (A||B) parsed :: (C&&(D||F||G)) </pre> <h2>a</h2> <pre> java -cp antlr-3.2.jar org.antlr.Tool AdifyMapReducePredicate.g </pre> <h2>b</h2> <pre> javac -cp antlr-3.2.jar *.java </pre> <h2>c (*nix/MacOS)</h2> <pre> java -cp .:antlr-3.2.jar Main </pre> <h2>c (Windows)</h2> <pre> java -cp .;antlr-3.2.jar Main </pre> <p>HTH</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload