With ANTLR2, you could define something like this in grammar definition file:
options
{
language = "CSharp";
namespace = "Extended.Tokens";
}
tokens {
TOKEN<AST=Extended.Tokens.TokenNode>;
}
And then, you could create a class:
public class TokenNode: antlr.BaseAST
{
...
}
Any ideea if something like this can be used (delegate class creation to AST factory instead of me doing the tree replication manually)? It's not working just by simple grammar definition copy from old to new format, and I tried to search their site and samples for somthing similar. Any hints?
EDIT
I'm not trying to create custom tokens, but custom 'node parsers'.
In order to 'execute' a tree you have 2 choices (as far as I understood):
- create a 'tree visitor' and handle values, or
- create a tree parser by 'almost-duplicating' the grammar definition.
In v2 case, I could decorate the tree node to whateveer method I would have liked and then call them after the parser ran by just calling something like 'execute' from root node.
I know little C#, but there shouldn't be much difference with the Java target.
You can create - and let ANTLR use - a custom tree by setting the
ASTLabelType
in theoptions { ... }
section (anXTree
in this case):T.g
You then create a custom class which extends a
CommonTree
:demo/XTree.java
and when you create an instance of your
TParser
, you must create and set a customTreeAdaptor
which creates instances of yourXTree
:demo/Main.java
Running the demo:
will print:
For more info, see: http://www.antlr.org/wiki/display/ANTLR3/Tree+construction