I just use {$tok.type = ID; }

 

Jim

 

From: antlr-dev-boun...@antlr.org [mailto:antlr-dev-boun...@antlr.org] On
Behalf Of Sam Harwell
Sent: Wednesday, December 08, 2010 12:21 PM
To: antlr-dev@antlr.org
Subject: [antlr-dev] Does anyone use hetero-ast?

 

I've never used the hetero-AST feature of ANTLR. On the other hand, many
times I've been forced to write a rule as follows:

 

actionScopeName

        :       id

        |       l='lexer'

                -> ID[$l]

        |       p='parser'

                -> ID[$p]

        ;

 

Since the performance of AST operators far exceeds the rewrite syntax, I've
been wishing for a way to write rules like this with the AST operators since
there's no reordering of tokens. It seems like the hetero AST syntax is
ideal for this:

 

actionScopeName

        :       id

        |       'lexer'<ID>

        |       'parser'<ID>

        ;

 

Now this syntax is just shorthand for the following, where "node" is the
default terminal option.

 

actionScopeName

        :       id

        |       'lexer'<node=ID>

        |       'parser'<node=ID>

        ;

 

Benefits of this ability:

 

1.       Clean syntax that associates the rewritten token type with the
target token.

2.       Improved performance when the rewrite syntax was only being used to
change a node type.

3.       Improved information propagation for users who keep writing "-> ID"
instead of "-> ID[$p]".

 

I propose the following changes:

 

1.       Rename the hetero AST terminal option from "node" to "class".
(Rationale: the new name better reflects the option's intent, which is
important when users start specifying "<class=T>" instead of just "<T>".

2.       Add "type" as a legal terminal option.

3.       Make "type" the default terminal option. (Rationale: the default
option should reflect the most recommended syntactical use, and this is a
performance best practice.)

4.       Currently the terminal options are not exposed to the templates.
Replace all template parameter instances of "hetero" with "terminalOptions",
and update the code generator to set the correct value. Usages of "hetero"
in the templates is replaced with "terminalOptions.class".

 

The updated createNodeFromToken template would look as follows (for the
CSharp3 target).

 

>From this:

 

createNodeFromToken(label,hetero) ::= <<

<if(hetero)>

new <hetero>(<label>)

<else>

(<ASTLabelType>)adaptor.Create(<label>)

<endif>

>> 

 

To this:

 

createNodeFromToken(label,terminalOptions) ::= <<

<if(terminalOptions.class)>

new <class>(<if(terminalOptions.type)><terminalOptions.type>,<endif><label>)

<else>

(<ASTLabelType>)adaptor.Create(<if(terminalOptions.type)><terminalOptions.ty
pe>,<endif><label>)

<endif>

>> 

 

Thanks,

Sam

_______________________________________________
antlr-dev mailing list
antlr-dev@antlr.org
http://www.antlr.org/mailman/listinfo/antlr-dev

Reply via email to