[antlr-interest] On the guessing mode in C++

Andrey R. Urazov a_urazov at mail.ru
Fri Apr 30 10:23:27 PDT 2004


On Fri, Apr 30, 2004 at 06:23:18PM +0200, Ric Klaren wrote:
> It depends a bit where they get assigned. If you mean assignments to
> return values inside the rule they should be returned from, those
> don't happen (unless they're done in an init action) From the calling
> rule the assignment is done. Due to the way antlr generates the return
> value it's more or less an issue of initialization in the rule. Just
> like plain old C garbage in garbage out. I'd rather not clutter the
> codegen with if's for this (also I could think of things where you'd
> want the return value (leaves a bit more room for cheating anltr with
> initactions)).
I see your point. I cannot speak absolutely competently since I have not
been using ANTLR extensively and therefore cannot say what is the right
way to use it. But in my opinion allowing assignments in the calling
rules in guessing mode is rather dangereous. First of all, because this
does not have any clear semantics in general: what is the sense of
assignment of a value whose computation was not triggered (except init
actions)? Secondly, running assignment operators may have side effects
which are inacceptable in guessing mode. User might want assignment
operator of his own object to do anything, for instance, it might
perform some registration of an object in a special object-directory and
increment some counter. Surely, running this registration operation
several times won't be desired.

In my humble opinion it's better if such semantically dubious situations
are prohibited. At least I would like a program I use to run in the safe
mode by default. And for users who do know what they are doing there
might be a special loose mode.

This is just my humble point of view. And again, I have not been using
ANTLR for long, so maybe I cannot see that the mode I suggest is too
restrictive.

> Try this it's safer:
> 
> rule2 returns [char* result]
> {
> 	// initialize in this init action so return value is ok.
>     static char buf[BUFF_SIZE];
> 	 buf[0] = '\0';
> 	 result = buf;
> } : ... ;
Thanks! It helps.


Best regards,
  Andrey Urazov



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list