[antlr-interest] [C Target] How to skip a whole sub-tree (not just a token)

Claude Moulin claude.moulin at hds.utc.fr
Sat Nov 14 12:25:22 PST 2009


Here is the solution I use in Java. The principle is also valid for
loops.

I suppose the CommonTreeNodeStream methods () I use have equivalent
functions in C

push, pop, and mark

 

if_statement 

@init{

  int mark_before_true = 0;

  int mark_before_false = 0;

}     :

 ^(IF e = expbool {mark_before_true = input.mark();} .
({mark_before_false = input.mark();} else_list = .)? ) 

        {

         if ($e.b) {

             push(mark_before_true);

             list_instructions();

             pop();

         }

         else 

         if (else_liste != null) {

             push(mark_before_false);

             list_instructions();

             pop();

         }

        }   

      ;

expbool is a rule returning a Boolean in its b field.

list_instructions is a rule parsing a tree containing instructions in a
list.

input.mark()  gives the index of the following node and in this case it
is the index I want to store.

And in the @members section I add two methods in order to simplify the
code:

  public void push(int index) {

     ((CommonTreeNodeStream)input).push(index);

  }

  public void pop() {

     ((CommonTreeNodeStream)input).pop();

  }

 

I hope it is usefull.

Claude

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091114/67038df5/attachment.html 


More information about the antlr-interest mailing list