[antlr-interest] Syntactic Predicate gating and @init

Jim Idle jimi at temporal-wave.com
Sat Apr 19 10:39:20 PDT 2008


Rather than use the @init, why not just use an action:

 

Instead of

 

rule

@init

{

   dfdsdfdsfdsf

}

: stuff

;

 

Use:

 

rule

:

                { dfsfsfsfddfdsf }

 

                stuff

;

 

Presuming that you actually need to execute the action before elements are parsed. Think of @init more in terms of declaring local variables in other words. The actions will not be executed when backtracking etc.

 

Does that help you any?

 

However, I think that you consider using the parser just to generate an AST, then do your codegen/runtime from the AST (which is very quick to traverse and you can shape it how you want).

 

Jim

 

 

 

 

From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Christian Schladetsch
Sent: Friday, April 18, 2008 9:45 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Syntactic Predicate gating and @init

 

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.




No virus found in this incoming message.
Checked by AVG.
Version: 7.5.524 / Virus Database: 269.23.1/1385 - Release Date: 4/18/2008 9:30 AM


No virus found in this outgoing message.
Checked by AVG. 
Version: 7.5.524 / Virus Database: 269.23.2/1386 - Release Date: 4/18/2008 5:24 PM
 


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


More information about the antlr-interest mailing list