[antlr-interest] if statement troubles

Guus Bonnema gbonnema at xs4all.nl
Thu Sep 6 01:58:59 PDT 2012


Hi All,

When interpreting an if-statement for a simple language I found that my
approach for executing the statement block had weird results. When
debugging in Netscape I found that input.index() always returns zero. I
am probably doing something wrong. My intention is to parse the
if-statement first and interprete the statement block according to the
condition.

Could anyone point me in the right direction? What am I doing wrong?

The source copied below is part of a tree grammar that reads the
following tree:

(X (SLIST (VAR int i = (UNAIR - 1)) (if (> 2 0) (SLIST (= i 1)))))

<QUOTE>
if_statement    // options {backtrack=true;}
	@init {int s_index=0; int el_index=0;} 
:               ^(IF b=boolexpr {s_index = input.index();} s=.
                             {el_index = input.index();} el=.?) 
{       int next = input.index(); 
        if ($b.value) {
           input.seek(s_index);
           statement_block();
        } else if ($el!=null) { 
	   input.seek(el_index);
           statement_block();
        }
        input.seek(next);
}
;
</QUOTE>

P.S. I copied this solution from somewhere in Markmail, but must have
missed something.

Kind regards, Guus Bonnema.


More information about the antlr-interest mailing list