[antlr-interest] Print an AST in original order
Varun Bhargava
varun.bhargava at carsales.com.au
Tue Jul 29 18:25:01 PDT 2008
Hi Kevin,
Thanks for replying. I'm using ANTLR for the first time and an absolute
beginner to Parsing stuff, so my apologies if I ask stupid questions.
Firstly, you are right. The tree does look as you mentioned.
To print the tree I am using the ToStringTree() method of the CommonTree
class.
How do I insert the root between binary children? Here is the snippet
from my grammar file:
where
:
| WHERE^ filter
;
filter
: //(key compare literal | key IS NOT? NULL | keyList IN key |
NOT filter | LPAREN filter RPAREN) (AND^ filter | OR^ filter)*
(compareFilter | notNullFilter | keyListFilter | NOT filter |
LPAREN filter RPAREN) (AND^ filter | OR^ filter)*
;
compareFilter
:
key compare (literal | PARAMETER)
;
notNullFilter
:
key IS NOT? NULL
;
keyListFilter
:
keyList IN key
;
keyList
: key (COMMA keyList)*
;
compare
: EQ | NOTEQ | LT | GT | LTE | GTE
Thanks
Varun
-----Original Message-----
From: Kevin J. Cummings [mailto:cummings at kjchome.homeip.net]
Sent: Wednesday, 30 July 2008 03:13
To: Varun Bhargava
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Print an AST in original order
Varun Bhargava wrote:
> Hi All,
>
> I'm using ANTLR3 for C# target. My problem is to load up a statement
> into a tree, so some substitutions to the tree and then print out the
> tree in original order to re-generate the input statement but with
some
> substitutions.
>
> I cannot seem to print the AST in its original order. For EG;
>
> WHERE
>
> |
>
> AND
>
> / \
>
> = =
>
> / \ / \
>
> A 5 b 10
Doesn't your tree actually look like this:?
WHERE
|
AND
|
= ------=
| |
A - 5 b - 10
I think your problem comes in how you are interpreting your
root/children associations. Insert your root between binary children,
or before a unary subtree (as if there is no LHS). It seems to work for
this example....
> The above tree should be printed as WHERE A=5 AND b = 10
>
> However this is not the case in ANTLR. Has anyone done this before? If
> yes could you please point me in the right direction or give me some
> sample code?
What are you using to do this with?
> Thanks
>
> Varun
--
Kevin J. Cummings
kjchome at rcn.com
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)
More information about the antlr-interest
mailing list