Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make a TreeParser in ANTLR3?
    text
    copied!<p>I'm attemping to learn language parsing for fun...</p> <p>I've created a ANTLR grammar which I believe will match a simple language I am hoping to implement. It will have the following syntax:</p> <pre><code>&lt;FunctionName&gt; ( &lt;OptionalArguments&gt;+) { &lt;OptionalChildFunctions&gt;+ } </code></pre> <p>Actual Example:</p> <pre><code>ForEach(in:[1,2,3,4,5] as:"nextNumber") { Print(message:{nextNumber}) } </code></pre> <p>I believe I have the grammar working correctly to match this construct, and now I am attemping to build an Abstract Syntax Tree for the language.</p> <p>Firstly, I must admit I'm not exactly sure HOW this tree should look. Secondly, I'm at a complete loss how to do this in my Antlr grammar...I've been trying without much success for hours.</p> <p>This is the current idea I'm going with for the tree:</p> <pre><code> FunctionName / \ Attributes \ / \ / \ ID /\ ChildFunctions / \ ID etc / \ Attribute AttributeValue Type </code></pre> <p>This is my current Antlr grammar file:</p> <pre><code>grammar Test; options {output=AST;ASTLabelType=CommonTree;} program : function ; function : ID (OPEN_BRACKET (attribute (COMMA? attribute)*)? CLOSE_BRACKET)? (OPEN_BRACE function* CLOSE_BRACE)?; 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; NUMBER : ('+' | '-')? (INTEGER | FLOAT) ; STRING : '"' ( ESC_SEQ | ~('\\'|'"') )* '"' ; BOOLEAN : 'true' | 'TRUE' | 'false' | 'FALSE' ; ID : (LETTER|'_') (LETTER | INTEGER |'_')* ; COMMENT : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;} | '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;} ; WHITESPACE : (' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;} ; COLON : ':' ; COMMA : ',' ; PERIOD : '.' ; OPEN_BRACKET : '(' ; CLOSE_BRACKET : ')' ; OPEN_BRACE : '{' ; CLOSE_BRACE : '}' ; OPEN_BOX : '[' ; CLOSE_BOX : ']' ; fragment LETTER : 'a'..'z' | 'A'..'Z' ; fragment INTEGER : '0'..'9'+ ; fragment FLOAT : INTEGER+ '.' INTEGER* ; fragment ESC_SEQ : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') ; </code></pre> <p>ANY help / advice would be great. I've tried reading dozens of tutorials and nothing about the AST generation seems to stick :(</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