Note that there are some explanatory texts on larger screens.

plurals
  1. POAntrl3 conditional tree rewrites
    primarykey
    data
    text
    <p>Stackoverflow.</p> <p>Continuing on my journey into Antlr (Previous questions may provide additional clues on what I'm trying to achieve! <a href="https://stackoverflow.com/questions/2061166/how-do-i-make-a-treeparser-in-antrl3/2061277#2061277">Q1 - How do I make a tree parser</a> and <a href="https://stackoverflow.com/questions/2113703/antlr-left-recursive-problem/2117334#2117334">Q2 - Solving LL recursion problem</a>) I've hit yet another roadblock I cannot flathom.</p> <p>Basically (I believe) the <code>expression</code> rule in my grammar needs to either create a new root node depending on the number of <code>datatype</code>s it has matched. I have put together an example to try best describe what I mean:</p> <p>Given the following input:</p> <pre><code>ComplexFunction(id="Test" args:[1, 25 + 9 + 8, true, [1,2,3]]) </code></pre> <p>I get this tree:</p> <blockquote> <p><a href="http://img25.imageshack.us/img25/2273/treeka.png" rel="nofollow noreferrer">http://img25.imageshack.us/img25/2273/treeka.png</a></p> </blockquote> <p>For reference - The first element in the "args" array as been correctly parsed. Whereas the 2nd element in the array "args" '25 + 9 + 8' has not. It appears to only match the last 2 parts of the expression (9 + 8). </p> <p>I'm trying to get the 2nd element of the array to be an <code>EXPRESSION</code> node, with the 3 children 25, 9, and 8).</p> <p>I'm honestly stuck and need your help (Again). Thanks for your time :)</p> <p>For reference, here is my grammar:</p> <pre><code>grammar Test; options {output=AST;ASTLabelType=CommonTree;} tokens {FUNCTION; NAME; ATTRIBUTES; ATTRIBUTE; VALUE; CHILDREN; EXPRESSION;} program : function ; function : ID (OPEN_BRACKET (attribute (COMMA? attribute)*)? CLOSE_BRACKET)? (OPEN_BRACE function* CLOSE_BRACE)? SEMICOLON? -&gt; ^(FUNCTION ^(NAME ID) ^(ATTRIBUTES attribute*) ^(CHILDREN function*)) ; attribute : ID (COLON | EQUALS) expression -&gt; ^(ATTRIBUTE ^(NAME ID) ^(VALUE expression)); expression : datatype (PLUS datatype)* -&gt; datatype ^(EXPRESSION datatype+)?; datatype : ID -&gt; ^(STRING["ID"] ID) | NUMBER -&gt; ^(STRING["NUMBER"] NUMBER) | STRING -&gt; ^(STRING["STRING"] STRING) | BOOLEAN -&gt; ^(STRING["BOOLEAN"] BOOLEAN) | array -&gt; ^(STRING["ARRAY"] array) | lookup -&gt; ^(STRING["LOOKUP"] lookup) ; array : OPEN_BOX (expression (COMMA expression)*)? CLOSE_BOX -&gt; expression* ; lookup : OPEN_BRACE (ID (PERIOD ID)*) CLOSE_BRACE -&gt; ID* ; 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 : ':' ; SEMICOLON : ';' ; COMMA : ',' ; PERIOD : '.' ; PLUS : '+' ; EQUALS : '=' ; 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>
    singulars
    1. This table or related slice is empty.
    plurals
    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