[antlr-interest] += in a tree grammar
André van der Merwe
AndrevdM at pyrogenesis.co.za
Thu Jul 3 04:44:33 PDT 2008
FWIW I found a simple solution to this problem...
Rather than use += I'm using code blocks in the group itself.
i.e. Instead of this
call
: ^(CALL IDENTIFIER (e+=INT)*)
{
$e ...
};
I'm now doing this
call
: ^(
CALL
IDENTIFIER
(
e { list.Add( e ); }
)*
)
Its not as easy to read but it works.
-----Original Message-----
>From: André van der Merwe
Hi,
I'm trying to build a heterogeneous AST from a tree grammar but I can't get a list of nodes using +=.
Firstly I building the AST in the tree grammar for two reasons
1) Its easier as I'm working with a simplified/normalised tree (i.e. not a parse tree).
2) My parser code can be reused for other purposes, i.e. I just create a new tree walker and leave the parser as is.
What I'm trying to do is create nodes for each parameter in a method call. Here is my (simplified) parser grammar
program
: IDENTIFIER LPAREN parameters* RPAREN -> ^(CALL IDENTIFIER parameters?)
;
parameters
: INT (COMMA INT)* -> INT+
;
And my tree grammar
program
: ^(CALL IDENTIFIER (e+=INT)*)
{
object obj1 = $e;
object obj2 = e;
};
I've tried everything but I can't get this to work.
Using 3.1 beta 1: I can't even parse this "someMethod( 1, 2 )" as I get an RewriteEarlyExitException. The same parser works in 3.0.1
root_0 = (CommonTree)adaptor.GetNilNode();
// 20:21: -> ( INT )+
{
if ( !(stream_INT.HasNext()) ) {
throw new RewriteEarlyExitException();
----
Using 3.0.1 with the tree grammar's output = AST generates the correct code for the '+=' but the rest of the code does not compile as the generated tree is passing CommonTree objects to the adapter's Create() method not ITokens. [I know that 3.0.1 does not support tree to tree grammars so I'm not surprised]
----
Using 3.0.1 with the tree grammar's output = template compiles but then the '+=' is concatenating templates
if (list_e == null)
list_e = new ArrayList();
list_e.Add(e.Template);
----
Any ideas? I don't want to have to move all my code into the parser but it is beginning to look like the only option :( Is there perhaps a later beta available or can I download the latest source somewhere?
Thanks
Andre
More information about the antlr-interest
mailing list