[antlr-interest] Tree Construction for empty while loop

Bryan Ewbank ewbank at gmail.com
Thu Nov 3 06:15:21 PST 2005


I think it's working as you desire - it's producing a degenerate tree,
or a root.  In ANTLR, every node is a tree, and parens are used only
if there are children - they do not represent a level of the tree. 
Turned around, left nodes are not printed with parenthesis.

Since "B" is a leaf node when there are no statements, it is correctly
reporting "B" rather than "(B)" - just at it's reporting each
identifier as "name" rather than "(name)".

Your grammar will alway produce a WHILE with two children - the
conditional, and the statement list.  The statement list can have zero
or more children.  ANTLR would describe this tree thus:

   #( WHILE COND #( B (stmt)* ) )

I don't know what <cond> produces, so there is certainly more in the
first child of WHILE.

"##" means "currently active root node".

On 11/2/05, Suman Karumuri <mansuk at gmail.com> wrote:
> Hi all,
>
> I have am constructing a grammar for a while loop using the following grammar.
>
> stmt :       WHILE^ cond stmt_lst EOL!
> stmt_lst : stmt_lst:       BEGIN^ (EOL!)* (stmt)* END!
>                                  { ##.setType(STMT_LIST);##.setText("B"); }
>
>
> This returns a tree of the form (WHILE (<cond>)  (B (stmt1) (stmt2) ... ) ).
> This works for the following while loop:
>
> while( a < b) {
> //some statements
> }
>
> But it fails to construct a tree when there are no statements(i.e. an
> empty while loop).
>
> It only constucts (WHILE (<cond>) B). How can i get the tree as
> (WHILE (<cond>) (B) ). Also what does ## signify?
> TIA.
> -Suman
>


More information about the antlr-interest mailing list