[antlr-interest] Syntax for Merging AST Nodes / Rule Text as AST Node

Mihai Danila viridium at gmail.com
Wed Dec 17 12:13:06 PST 2008


I seem to have found a solution to the problem. For the grammar above (which
I paste again for completeness):

grammar Test;
options { output=AST; }
tokens { Start; }
startRule: alphanumeric -> ^(Start alphanumeric);
alphanumeric: (A | D)+;
A: 'A'..'Z' | 'a'..'z';
D: '0'..'9';

Use the bracket notation with a special token type:
grammar Test;
options { output=AST; }
tokens { Start; Constant; }
startRule: alphanumeric -> ^(Start alphanumeric);
alphanumeric: (A | D)+ -> Constant[$alphanumeric.text];
A: 'A'..'Z' | 'a'..'z';
D: '0'..'9';

which instead of adding a lot of children under Start, will add a single
child with the whole alphanumeric text. Let me know if there are other
options that you think might work. Perhaps there's an option that doesn't
require an additional token? (I've tried ->
alphanumeric[$alphanumeric.text]to no avail.)


Mihai


On Wed, Dec 17, 2008 at 2:57 PM, Mihai Danila <viridium at gmail.com> wrote:

>
> Hi,
>
> An AST-producing grammar I've been using generates many nodes that I'd like
> joined into one node. Is there syntax for joining nodes at a certain level
> into one node? Alternatively, is there a way to specify that the entire text
> matched by a rule be put in an AST node?
>
> For example, the rule in the grammar below will produce as many nodes as
> there are characters matched in all the rules that use it, whereas I'd
> prefer the text alone to be used to simplify grammars that consume the AST:
> (well, maybe grammars that use the AST can match the whole subtree and be
> able to obtain the text over an entire subtree, in which case they're good,
> but I'd still like to have a smaller AST for better debugging)
>
> alphanumeric: (A | D)+;
>
> A: 'A'..'Z' | 'a'..'z';
> D: '0'..'9';
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081217/3c7f051d/attachment.html 


More information about the antlr-interest mailing list