[antlr-interest] ANTLR + C Target Questions

Michael Coupland mcoupland at gmail.com
Fri Apr 24 11:23:04 PDT 2009


Hi, I'm experimenting with using ANTLR to parse a custom file type,
and I have a few questions/comments about ANTLR and the C Target:


1) Consider the following grammar:

       grammar schema;

       options
       {
               language = C;
       }

       root : letter* ;
       letter : A | B ;
       other : C;

       A       :       'a';
       B       :       'b';
       C       :       'c';

If you run it on the input string "abc" in ANTLRWorks it generates a
NoViableAltException (as I would expect), but using the C Runtime to
parse a 'root' it passes successfully. (No errors reported, nothing
printed to stderr.) This seems like a bug at first glance, but I have
a sneaking suspicion that it's tied to the grammar - certainly, if you
were parsing a 'root' as a non-root rule, it shouldn't be an error if
it runs into a 'C'.
The only way I can think of to detect the above situation from C code
is to generates an AST and then examine it to see whether the parser
consumed the whole input stream. I was hoping to test
(retval.stop.type == ANTLR3_TOKEN_EOF), but that doesn't seem to ever
be true. What's the best thing to look at to determine this?
(retval.stop.stop, perhaps?)



2) The C Target generates many structs with members called "free"
which, while not technically a reserved word, isn't an ideal choice
for an indentifier name. There are codebases where free is #defined to
be something else, which can lead to problems in the generated code
that uses 'free' as a normal identifier. I haven't yet looked into
modifying the C target to solve this locally, which doesn't seem like
a huge task, but it would be nice if the default behavior were to use
some other less-overloaded identifier.



3) I can't seem to find documentation on how the C Target's error
handling works. Clearly the documentation at
http://www.antlr.org/wiki/display/ANTLR3/Error+reporting+and+recovery
isn't directly relevant. Where can I find more information about this?
Is there a good way to understand how the C Target emulates the Java
Target's use of exceptions, apart from reading generated code? There
don't seem to be any examples that deal with custom error reporting
using the C Target.



4) I was running into some problems with scope variables, and saw this
thread: http://www.antlr.org/pipermail/antlr-interest/2009-March/033769.html
but the link to http://antlr.org/downloads doesn't seem to work.
http://www.antlr.org/hudson/job/ANTLR_Tool/lastSuccessfulBuild/ seems
like a good place to get the latest development build, but I can't
seem to actually find a download link anywhere?


Thanks!
       Michael Coupland


More information about the antlr-interest mailing list