[antlr-interest] Tree-based interpreters

Joern Gebhardt gebhardt at joern-gebhardt.de
Wed Mar 25 09:58:10 PDT 2009


Hi,

I found in the wiki (
http://www.antlr.org/wiki/display/CS652/Tree-based+interpreters) a page that
describes how an if-statement could be processed based on an AST.

The grammar stated there looks like this:

ifstat
    :   ^('if' c=expr s=. e=.?) // ^('if' expr stat stat?)
        {
        int next = input.index();
        if ( ((Boolean)$c.value).booleanValue() ) {
            input.seek($s.start.streamIndex);
            stat();
        }
        else if ( $e!=null ) {
            input.seek($e.start.streamIndex);
            stat();
        }
        input.seek(next);
        }
    ;


I tried this out but wasn't able to extract the "streamIndex" out of the
statement CommonTree objects "s" and "e" (by the way, this is already stated
as comment on the wiki).
I only found was the "startIndex" and "stopIndex" but I guess that these
attribute refer to the token position in the token stream created by the
lexer.

Is there any possibility to get the index of the CommonTreeNodeStream (and
not the token stream)? Or do you have any other suggestions how I could use
a tree-grammar
to process if-statements?

Thanks in advance,
Joern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090325/f7c920e1/attachment.html 


More information about the antlr-interest mailing list