[antlr-interest] Changing root node lable

Martijn Reuvers martijn.reuvers at gmail.com
Wed Apr 6 00:46:16 PDT 2011


Hello,

You could override toString() and do it yourself (rendering it and its
children the way you like) or add an additional method to CommonTree
to do what you want.

Martijn

On Wed, Apr 6, 2011 at 9:39 AM, Wilson Urdaneta <akikax at gmail.com> wrote:
> On Wed, Apr 6, 2011 at 3:
>> Hi everyone,
>>
>> This is my first question on this list and I have been using ANTLR for
>> about two weeks.
>>
>> Lets say I want to parse a simple arithmetic expression. For that I
>> create a simple grammar - as shown in many of the available examples -
>> and I order ANTLR to generate the AST. Now, is there any simple way to
>> change the label of each root node so it reflects the value of their
>> children? That is, given
>>
>> (2+3)*4
>>
>> I would get the following AST
>>
>> *
>>  4
>>  +
>>    2
>>    3
>>
>> and I would like to get instead
>>
>> (2+3)*4
>>  4
>>  (2+3)
>>    2
>>    3
>>
>> Instead of only getting * or + On the root nodes I would like to get a
>> label with the full content or information or the children at current
>> depth.
>>
>> Currently, I am thinking doing this using the commontree class. Is
>> there anyway of specifying this directly at the grammar? Also, I have
>> not succeed in this using rewrite rules....
>>
>> Regards,
>>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list