Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Step 1 is to make the tree look like the little graph that you posted. Right now, you don't have any tree construction operators, so you're going to end up with a flat list.</p> <p>See <a href="http://www.antlr.org/wiki/display/ANTLR3/Tree+construction" rel="noreferrer">tree construction</a> on the antlr.org website.</p> <p>You can use <a href="http://www.antlr.org/works/index.html" rel="noreferrer">ANTLRWorks</a> to see what your getting for a parse tree and AST. Start adding tree construction operators and watch how things change.</p> <p><strong>EDIT / Additional Info:</strong></p> <p>Here's a process you can follow to give you a rough idea of how to do it:</p> <ol> <li>Download ANTLRWorks and use it's graphing facilities. You will definitely want to see the parse tree and the AST before and after you make changes. Once you understand how everything works, then you can use any IDE or editor you want.</li> <li><p>There are two basic operators for tree construction - The exclamation mark <code>!</code> which tells the compiler to not place the node within the AST, and the carot <code>^</code>, which tells ANTLR to make something the root node. Start by going through each non-terminal rule and deciding which elements don't need to be in the AST. For example, you don't need commas or parenthesis. Once you have all the information you can populate the a structure (or create your own AST structure) that provides all the information. Commas don't help any more, so add a <code>!</code> to them. For example:</p> <p><code>function: ID (OPEN_BRACKET! (attribute (COMMA!? attribute)*)? CLOSE_BRACKET!)? (OPEN_BRACE! function* CLOSE_BRACE!)?;</code></p></li> <li><p>Take a look at the AST in ANTLRWorks before and after. Compare.</p></li> <li>Now decide which element should be the root node. It looks like you want <code>ID</code> to be the root node, so add a <code>^</code> after <code>ID</code> and compare in ANTLRWorks.</li> </ol> <p>Here's a few changes that bring it closer to what I think you want:</p> <pre><code>program : function ; function : ID^ (OPEN_BRACKET! attributeList? CLOSE_BRACKET!)? (OPEN_BRACE! function* CLOSE_BRACE!)?; attributeList: (attribute (COMMA!? attribute)*); attribute : ID COLON! datatype; datatype : NUMBER | STRING | BOOLEAN | array | lookup ; array : OPEN_BOX! (datatype^ (COMMA! datatype)* )? CLOSE_BOX!; lookup : OPEN_BRACE! (ID (PERIOD! ID)*) CLOSE_BRACE!; </code></pre> <p>With that under your belt, now go look at some of the <a href="http://jnb.ociweb.com/jnb/jnbJun2008.html" rel="noreferrer">tutorials</a>.</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