[antlr-interest] [C Target] Why It is here: if (BACKTRACKING>0)

Jim Idle jimi at temporal-wave.com
Wed Jan 23 08:37:59 PST 2008


The code generating string template does not care whether there is any 
backtracking involved and so any actions or generated code that should 
not be executed if the rule is in backtracking mode are gated in this 
way. It resolves to an int and the C compiler will generate code that 
checks such an expression extremely efficiently such that coupled with 
pipelining and so on, I doubt that you could measure the difference in 
performance without it. I suppose that the template could check this 
kind of circumstance and gate the check in the stringtemplate, but it is 
hardly worth it.

If you were absolutely sure that the code never does any backtracking, 
then you could change the definition of the macro such that the C 
compiler would always optimize it away. But then you are playing with 
the fundamentals for pretty much zero gain. In general, dont worry 
about the micro optimizations.

I have not tried this, but in theory it would be fine, so long as there 
are no circumstances in which backtracking would be on:

@parser::postinclude
{
#undef BACKTRACKING
#define BACKTRACKING 0
}

But, as I said, this is unlikely to give you any measurable performance 
gain.

What you should look at is whether you are getting a 4M file because 
there is just a lot of rules, or because you have created a parser that 
needs huge DFA tables and so on to deal with the way it is put together. 
Eliminating predicates that invoke rules is always a performance 
improvement, but you seem to be saying you have no backtracking and no 
predicates, so perhaps the grammar is fine. My TSQL 20065 grammar 
generates about the same amount of code with pretty much no predicates 
and definitely not backtracking, which is really a prototyping tool 
(though with memoizing turned on it can be reasonable if performance is 
not your top priority).

Hope that helps.

Jim

> -----Original Message-----
> From: Ruslan Zasukhin [mailto:sunshine at public.kherson.ua]
> Sent: Wednesday, January 23, 2008 2:01 AM
> To: antlr-interest at antlr.org; Jim Idle
> Subject: [C Target] Why It is here: if (BACKTRACKING>0)
> 
> Hi All,
> 
> I have got first success with generated C source files from our
> Valentina
> SQL grammar.
> 
> And I see these lines:
> 
>                     if (BACKTRACKING>0)
>                     {
>                         FAILEDFLAG = ANTLR3_TRUE;
>                         return retval;
>                     }
> 
> 
> I do not understand why this present here at all, if in our grammar
> backtracking is OFF always.
> 
> Jim ?
> 
> 
> -------
> Generated C - file is 4 Mb
> Wow, ANTLR2 did generate 0.5 MB
> 
> But yes I see that a lots of comments present here now.
> :-)
> 
> Its not a problem, just for info.
> 
> 
> --
> Best regards,
> 
> Ruslan Zasukhin
> VP Engineering and New Technology
> Paradigma Software, Inc
> 
> Valentina - Joining Worlds of Information
> http://www.paradigmasoft.com
> 
> [I feel the need: the need for speed]
> 
> 




More information about the antlr-interest mailing list