Once you get the hang of it, you can do it by hand without too much
trouble. For example, in converting a v2 grammar to v3, I encountered
this in the members section:
stringAlias =
#(#[BLOCK], #(#[ALT], #[STRING_LITERAL], #[EOA]), #[EOB]);
Which became this (you can use CommonTree instead of GrammarAST). Note
that this uses the CSharp3 target so the function names are capitalized,
unlike the Java target.
/*
* stringAlias = ^(BLOCK[] ^(ALT[] STRING_LITERAL[] EOA[]) EOB[])
*/
stringAlias = (GrammarAST)adaptor.Create( BLOCK, "BLOCK" );
{
GrammarAST alt = (GrammarAST)adaptor.Create( ALT, "ALT" );
adaptor.AddChild( alt, adaptor.Create( STRING_LITERAL,
"STRING_LITERAL" ) );
adaptor.AddChild( alt, adaptor.Create( EOA, "EOA" ) );
adaptor.AddChild( stringAlias, alt );
}
adaptor.AddChild( stringAlias, adaptor.Create( EOB, "EOB" ) );
In your case, you'd use something like this:
CommonTree tree = (CommonTree)adaptor.Nil();
adaptor.AddChild( tree, adaptor.Create( LITERAL_enum, "enum" ) );
adaptor.AddChild( tree, adaptor.Create( ID, enumName ) );
Sam
From: [email protected]
[mailto:[email protected]] On Behalf Of Lucien Antlr
Sent: Wednesday, March 18, 2009 11:53 AM
To: [email protected]
Subject: [antlr-interest] V3 Rule Action allowing inline tree building?
Hi folks,
I saw back in 2005 there was a bit of discussion around inline tree
building. ( linked here
http://markmail.org/message/7zigjknpakugflje
<http://markmail.org/message/7zigjknpakugflje> ). For practice I am
converting the freely available C v2 ANTLR .g and I came over a rule
that has me stuck...
It keeps the internal symbol-table by adding part of a tree into the
symbol table along with the C symbol (in this case enum).
Normally a v2 rewrite would look like below (from stdcparser.g)
enumerator[String enumName]
: i:ID { symbolTable.add( i.getText(),
#( null,
#[LITERAL_enum, "enum"],
#[ ID,
enumName]
)
);
}
(ASSIGN constExpr)?
;
#( ) under v2 calls ASTFactory.create(), but under v3 it just appears to
pass straight through into the Java target?
This is true even when re-written, as I guessed it should be below.
enumerator[String enumName]
: i=ID { symbolTable.add( $i.text,
^( NULL,
^(LITERAL_enum),
^(ID
$enumName.text)
)
);
}
(ASSIGN constExpr)?
;
The only thing I came up with is "rewriting" the rule, and creating a
dummy rule inside and using $dummy.tree inside the action.
Any thoughts from the more experienced community?
Kind Regards,
Luc
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address