[antlr-interest] Syntactic Predicate gating and @init

Christian Schladetsch christian.schladetsch at gmail.com
Fri Apr 18 21:45:03 PDT 2008


Hello,

I have an RPN system that I've been using for some time. It's time to write
an infix parser which generates post-fix.

My basic approach, which seems wrong, is to do:

compound_statement
   @init { PushScope(); }
   : '{' statement* '}' { *statement list in rpn form* = PopScope();
Push(*continuation
created from statement list*); }
   ;

statement
    : ...*expression rules that resolve to various leaf_node matches*...
    ;

leaf_node:
    :  ..Q... { Push(Q); } // push new object onto current code stream scope
    ;

The problem is that various rules are explored by the look-ahead, which
pushes scope in the @init clause but does not execute the action which pops
the scope, so the scope stack gets broken.

If i use the @synpredgate { true } hack work-around, the output is wrong
because it injects code when it unwinds.

Clearly I am doing something wrong. I got basic expressions working quite
well, "a=3*(1+b)" --> "b 1 + 3* a =" no problem. This sort of thing is very
natural to do in ANTRLR. However, the problem of scoping is not so easy. I
do not want to have to pass context down through each rule as an argument?!
However my attempts to use a global scope stack have been thwarted by the
backtracking.

What I'd like is to only perform the @init if the rule has actually been
matched, rather than when looking forward.

Thanks for any advice.

Regards,
Christian.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080419/bfa48ae4/attachment-0001.html 


More information about the antlr-interest mailing list