Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><em>Jason S wrote:</em></p> <p>Is there a way to do this?</p> </blockquote> <p>Yes.</p> <p>First define your grammar (I took your example of an expression parser with only the <code>+</code> and <code>*</code> and <code>()</code> operators):</p> <pre><code>grammar Exp; // parser rules parse : additionExp ; additionExp : multiplyExp (Add multiplyExp)* ; multiplyExp : atomExp (Mult atomExp)* ; atomExp : Number | LParen additionExp RParen ; // lexer rules Add : '+' ; Mult : '*' ; LParen : '(' ; RParen : ')' ; Number : ('0'..'9')+ ('.' ('0'..'9')+)? ; Spaces : (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;} ; </code></pre> <p>If you want to let ANTLR generate a proper AST from the grammar above, you must put the following at the top of your grammar (under the grammar declaration):</p> <pre><code>options { output=AST; } </code></pre> <p>and you must indicate what the root of each of your parse rules should be. This can be done in two ways:</p> <ol> <li>by using <a href="http://www.antlr.org/wiki/display/~admin/2008/04/11/Rewrite+rules">rewrite rules</a>;</li> <li>or by placing one of the "inline tree-operators" <code>^</code> and <code>!</code> after the tokens: <ul> <li><code>^</code> means: <em>make this token the root</em>;</li> <li><code>!</code> means: <em>exclude this token from the AST</em>.</li> </ul></li> </ol> <p>Now your grammar would look like this:</p> <pre><code>grammar Exp; options { output=AST; } // parser rules parse : additionExp ; additionExp : multiplyExp (Add^ multiplyExp)* ; multiplyExp : atomExp (Mult^ atomExp)* ; atomExp : Number | LParen! additionExp RParen! ; // lexer rules Add : '+' ; Mult : '*' ; LParen : '(' ; RParen : ')' ; Number : ('0'..'9')+ ('.' ('0'..'9')+)? ; Spaces : (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;} ; </code></pre> <p>As you can see, I made the <code>Add</code> and <code>Mult</code> roots, and excluded the parenthesis.</p> <p>Now generate a lexer &amp; parser from the grammar:</p> <pre><code>java -cp antlr-3.2.jar org.antlr.Tool Exp.g </code></pre> <p>create a little test harness:</p> <pre><code>import org.antlr.runtime.*; import org.antlr.runtime.tree.*; import java.util.*; public class Main { private static void preOrder(CommonTree tree, int depth) { for(int i = 0; i &lt; depth; i++) { System.out.print("- "); } System.out.println("&gt; "+tree + " :: " + ExpParser.tokenNames[tree.getType()]); List children = tree.getChildren(); if(children == null) return; for(Object o : children) { preOrder((CommonTree)o, depth+1); } } public static void main(String[] args) throws Exception { ANTLRStringStream in = new ANTLRStringStream("3 * (4 + 7 * 6) * (3 + 7 * (4 + 2))"); ExpLexer lexer = new ExpLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); ExpParser parser = new ExpParser(tokens); CommonTree tree = (CommonTree)parser.parse().getTree(); preOrder(tree, 0); } } </code></pre> <p>compile everything:</p> <pre><code>javac -cp antlr-3.2.jar *.java </code></pre> <p>and run the <code>Main</code> class:</p> <pre><code>// *nix/Mac OS java -cp .:antlr-3.2.jar Main // Windows java -cp .;antlr-3.2.jar Main </code></pre> <p>which produces the following:</p> <pre><code>&gt; * :: Mult - &gt; * :: Mult - - &gt; 3 :: Number - - &gt; + :: Add - - - &gt; 4 :: Number - - - &gt; * :: Mult - - - - &gt; 7 :: Number - - - - &gt; 6 :: Number - &gt; + :: Add - - &gt; 3 :: Number - - &gt; * :: Mult - - - &gt; 7 :: Number - - - &gt; + :: Add - - - - &gt; 4 :: Number - - - - &gt; 2 :: Number </code></pre> <p>As you can see, the <code>parse</code> rule (method) returns a <a href="http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1tree_1_1_common_tree.html"><code>CommonTree</code></a> object you can use to create your own walker/visitor leaving the grammar <em>as is</em>.</p> <p>HTH</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.
 

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