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

Ric Klaren klaren at cs.utwente.nl
Fri Apr 30 09:23:18 PDT 2004


Hi,

On Fri, Apr 30, 2004 at 10:51:43PM +0700, Andrey R. Urazov wrote:
> The current implementation of the guessing mode in C++ guarantees that
> all the user defined actions be ignored. 

Except init actions..

> In my opinion the same
> requirement should be spread onto the return values --- return values
> should never be assigned in guessing mode. Otherwise, undesired side
> effects are possible which may be caused by overloaded assignment
> operators.

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)).

> Now, for example, I have a situation described by the following
> simplistic example:
> 
> rule1
> {
>     std::string s;
> }
> :
>     s = rule2
> ;
> 
> rule2 returns [char* result]
> {
>     static char buf[BUFF_SIZE];
> }
> :
>     x
>     {
>         strcpy(buf, y);
>         result = buf;
>     }
> ;
> 
> When in guessing mode `buf' is not initialized and may contain any
> trash but the zero value thus causing assignment `s = rule2' to crash.

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;
} : ... ;

Note you can also do this (will not work in this example but just to show
the feature):

rule2 returns [char* result = 0]
{
    static char buf[BUFF_SIZE];
	 buf[0] = '\0';
	 result = buf;
} :  ... ;

Cheers,

Ric
-- 
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893755  ----
-----+++++*****************************************************+++++++++-------
  Quidquid latine dictum sit, altum viditur.
                 (Whatever is said in Latin sounds profound.)



 
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