Note that there are some explanatory texts on larger screens.

plurals
  1. POANTLR4: Parser for a Boolean expression
    primarykey
    data
    text
    <p>I am trying to parse a boolean expression of the following type B1=p &amp; A4=p | A6=p &amp;(~A5=c)</p> <p>I want a tree that I can use to evaluate the above expression. So I tried this in Antlr3 with the example in <a href="https://stackoverflow.com/questions/9509048/antlr-parser-for-and-or-logic-how-to-get-expressions-between-logic-operators">Antlr parser for and/or logic - how to get expressions between logic operators?</a></p> <p>It worked in Antlr3. Now I want to do the same thing for Antlr 4. I came up the grammar below and it compiles. But I am having trouble writing the Java code.</p> <h2>Start of Antlr4 grammar</h2> <pre><code>grammar TestAntlr4; options { output = AST; } tokens { AND, OR, NOT } AND : '&amp;'; OR : '|'; NOT : '~'; // parser/production rules start with a lower case letter parse : expression EOF! // omit the EOF token ; expression : or ; or : and (OR^ and)* // make `||` the root ; and : not (AND^ not)* // make `&amp;&amp;` the root ; not : NOT^ atom // make `~` the root | atom ; atom : ID | '('! expression ')'! // omit both `(` and `)` ; // lexer/terminal rules start with an upper case letter ID : ( 'a'..'z' | 'A'..'Z' | '0'..'9' | ' ' | ('+'|'-'|'*'|'/'|'_') | '=' )+ ; </code></pre> <hr> <p>I have written the Java Code (snippet below) for getting a tree for the expression "B1=p &amp; A4=p | A6=p &amp;(~A5=c)". I am expecting &amp; with children B1=p and |. The child | operator will have children A4=p and A6=p &amp;(~A5=c). And so on. Here is that Java code but I am stuck trying to figure out how I will get the tree. I was able to do this in Antlr 3.</p> <h2>Java Code</h2> <pre><code>String src = "B1=p &amp; A4=p | A6=p &amp;(~A5=c)"; CharStream stream = (CharStream)(new ANTLRInputStream(src)); TestAntlr4Lexer lexer = new TestAntlr4Lexer(stream); parser.setBuildParseTree(true); ParserRuleContext tree = parser.parse(); tree.inspect(parser); if ( tree.children.size() &gt; 0) { System.out.println(" **************"); test.getChildren(tree, parser); } </code></pre> <p>The get Children method is below. But this does not seem to extract any tokens.</p> <pre><code>public void getChildren(ParseTree tree, TestAntlr4Parser parser ) { for (int i=0; i&lt;tree.getChildCount(); i++){ System.out.println(" Child i= " + i); System.out.println(" expression = &lt;" + tree.toStringTree(parser) + "&gt;"); if ( tree.getChild(i).getChildCount() != 0 ) { this.getChildren(tree.getChild(i), parser); } } } </code></pre> <p>Could someone help me figure out how to write the parser in Java?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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