[antlr-interest] How to implement an IF
Terence Parr
parrt at cs.usfca.edu
Mon Dec 4 12:30:31 PST 2006
On Dec 4, 2006, at 11:25 AM, <Joerg.Werner at qimonda.com>
<Joerg.Werner at qimonda.com> wrote:
> Hi all,
>
> for antlr3 the stat(x) will not work, because you can not pass the
> sub-tree to a rule any more (which the rule then processes).
> In antlr3 the current tree parsing position is stored in the
> TreeNodeStream class instance, which is an instance variable of
> your TreeParser class. So you can not easily tell a sub-rule what
> part of the tree to process. You need instead to forward/rewind the
> TreeNodeStream to the correct position, before calling the rule of
> interest.
Yes, I noticed this myself the other day and am wondering what to do
about it.
> For writing interpreters with antlr3 I see only two solutions:
> -Create a new instance of the TreeParser to handle the sub-tree
> (incurs a lot of overhead)
> -Sub-class (Common)TreeNodeStream to easily jump to certain
> positions in the TreeNodeStream (similar to the mark() and rewind()
> methods in CommonTreeNodeStream, but these are stack based and will
> thus not work in your case).
>
> Or does one of the other experts knows a better solution to this
> problem?
Ok, I just figured out. we need a new method on the
CommonTreeNodeStream that pushes the current location a stack and
jumps to a new index in the stream. The only trick then is to map
nodes of interest to the correct index. In reality, what happens is
that the recognition of the functions in your language will create a
hash table entry that maps the function name to the index in the stream:
funcDef
@init {int i = input.index();}
: 'void' ID '(' ')' -> ^(FUNCDEF ID)
{functions.put($ID.text, i);}
;
something like that. If you get that to work, let us know.
I should also note that I don't think that I have finished
backtracking for the tree node stream. Comments on the market and
rewind methods should indicate that.
Ter
More information about the antlr-interest
mailing list