Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As already mentioned by others, your expression has to end with a <code>EOF</code>, but a nested expression cannot end with an <code>EOF</code>, of course.</p> <p>Remove the <code>EOF</code> from <code>expression</code>, and create a proper "entry point" for your parser that ends with the <code>EOF</code>.</p> <h2>file: T.g</h2> <pre><code>grammar T; options { output=AST; } parse : expression EOF! ; expression : '('! ('&amp;' | '||' | '!')^ (atom | expression)* ')'! ; atom : '('! ITEM '='^ ITEM ')'! ; ITEM : ALPHANUMERIC+ ; fragment ALPHANUMERIC : ('a'..'z' | 'A'..'Z' | '0'..'9') ; WHITESPACE : (' ' | '\t' | '\r' | '\n') { skip(); } ; </code></pre> <h2>file: Main.java</h2> <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 { String source = "(||(attr=hello2)(!(attr2=12)))"; TLexer lexer = new TLexer(new ANTLRStringStream(source)); TParser parser = new TParser(new CommonTokenStream(lexer)); CommonTree tree = (CommonTree)parser.parse().getTree(); DOTTreeGenerator gen = new DOTTreeGenerator(); StringTemplate st = gen.toDOT(tree); System.out.println(st); } } </code></pre> <p>To run the demo, do:</p> <h2>*nix/MacOS:</h2> <pre>java -cp antlr-3.3.jar org.antlr.Tool T.g javac -cp antlr-3.3.jar *.java java -cp .:antlr-3.3.jar Main</pre> <h2>Windows:</h2> <pre>java -cp antlr-3.3.jar org.antlr.Tool T.g javac -cp antlr-3.3.jar *.java java -cp .;antlr-3.3.jar Main</pre> <p>which produces the DOT code representing the following AST:</p> <p><img src="https://i.stack.imgur.com/Pl8hx.png" alt="enter image description here"></p> <p><em>image created using <a href="http://graphviz-dev.appspot.com/" rel="nofollow noreferrer">graphviz-dev.appspot.com</a></em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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