[antlr-interest] Rewrite Problem,

Terence Parr parrt at cs.usfca.edu
Mon Jul 31 11:42:52 PDT 2006


On Jul 31, 2006, at 9:20 AM, Craig Main wrote:

>
> Hi Ter,
>
> I am still battling with this.
> I do not understand the rewrite rules.
>
> Given the following simple term, in the standard expression/term/ 
> factor
> arrangement.
>
> expression	: l=term ((op=PLUS|op=MINUS) r=term)*
>
> If I write the expression with the correct tree manipulation  
> operators:
>
> expression	: l=term ((op=PLUS^|op=MINUS^) r=term)*

You want ^^ not ^ ;)

>
> Then I get a complete AST tree when I parse the expression 'a = 90 
> +23-11'
> I can match the tree correctly with:
>
> expression:
>   #(PLUS l=expression r=expression)
> | #(MINUS l=expression r=expression)
>
>
> When I try to do the new stuff (version 3.0)
> I leave out the tree manipulation as that produces an error.
> I use:
>
> expression	: l=term ((op=PLUS|op=MINUS) r=term)*
>
> and then add:
> -> ^($op $expression $r)*
>
> And the tree is just a nil node.

That would need to be inside the loop.

Take a look at mantra.g for example:

postfixExpression
	:   (primary->primary) // set return tree
         (   lp='(' args=expressionList ')'  -> ^(CALL  
$postfixExpression $args)
         |   lb='[' ie=expression ']'        -> ^(INDEX  
$postfixExpression $ie)
         |   dot='.' p=primary               -> ^(FIELDACCESS  
$postfixExpression $p)
         |   c=':' cl=closure[false]         -> ^(APPLY ^(EXPR  
$postfixExpression) $cl)
         )*
         ;

works great!

Ter


More information about the antlr-interest mailing list