Gavin Lambert wrote:
At 14:23 5/02/2009, Jim Idle wrote:
>@after is not supported in the C target. Use this:
>
>statement
>@init { /* Empty */ }
>: ( x | Y | x | g)
>    {
>        sse.afterStatement();
>    }
>;

Isn't that semantically different?  I thought the point of @after is that it's like "finally" code -- it gets executed regardless of success/failure/explosions.  Whereas an inline block at the end of the rule would only get executed on a successful match.

(Although parser rules should usually not fail anyway, so it may be a moot point.)

You are correct that it isn't exactly semantically equivalent, but I have never seen a case where people wanted to do anything different. The exception clause would generally be more useful. In a realt @after clause, you would have to make sure you checked any references for NULL before trying to do anything anyway. So, at least for the more usual case, it makes more sense to have the action that you want to happen at the rule end, when it is successful, in an action at the end. Then, if there is something special you need to do upon failure, you want an exception clause like the Java target. I think most people are reading @after and just see "when this rule finishes successfully".

I still plan on working through all the possible combinations before 3.1.2 is released. The difficulty is not adding an @after section of course, but that using a static template, can I make sure that all possible code paths, given all possible rule element combinations, including backtracking, @after, exceptions and so on, thread their way correctly through the generated C code and are semantically equivalent to the Java code. The answer may well be yes, but I want consider performance and complexity, as if there is a semantically equivalent way of expressing this in the grammar, then it might make more sense to just instruct people in the documentation.

I have this stuff as a reminder at:

http://antlr.org/jira/browse/ANTLR-359

and

http://antlr.org/jira/browse/ANTLR-273

By the way, I remembered what the deal was with checking NULLs for rule returns. Basically, it was decided that trying to reference things that are not initialized is really a grammar error, and even more so in C than other targets, a lot of the NULL guards were useless because having checked for NULL, they return NULL anyway. This was worse with scopes because accessing an empty scope was just happily returning NULL rather than yielding a null pointer exception or stack empty exception etc. This meant that people were writing incorrect code without realizing it, which was particularly poignant when I discovered the the C parser example was guilty of this and its symbol table lookup was returning "Nope, that isn't here" instead of "You should not have got here, there is somethign wrong with the grammar!"

So, in the C target I have removed pretty much all the NULL guards as it is better to get a violation than mask grammar/coding errors. In the case of the return from a rule, the return is in fact a struct, which is declared as such in the calling rule. The struct in the calling rule will therefore never be NULL, and memsetting it to 0 does not solve that issue, though it could have a special field that says if it has been used yet and so on.

That special field would merely result in the NULL guards returning a NULL, which would work in a few cases, where the usage detected NULL, but the conclusion is that you should not reference rule returns unless they will be initialized.


Jim

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to