[antlr-interest] Problems with the C version of ANTLR

Jim Idle jimi at temporal-wave.com
Sun Sep 6 07:45:22 PDT 2009


Andreas Volz wrote:
> Hello,
>
> I tried some Java examples for ANTLR and it worked nice. But I need a
> parser for C (for later C++ usage). As easy it was to get a working
> Java example as hard is it to get a C example working.
>   
Well, the examples you are using are written for Java, you need to get 
an example that was written for C to work first, then you will see what 
you need to do to convert it to C.

Download the pre-done examples rather than the donated grammars.
> I've read this doc:
>
> http://www.antlr.org/api/C/index.html
>
> -> Generating Code for the C Target 
>
> I'm a little confused about this table:
>
> Suffix 	Grammar should contain...
> .g3l 	A lexer grammar specification only.
> .g3p 	A parser grammar specification only.
> .g3pl 	A combined lexer and parser specification.
> .g3t 	A tree grammar specification. 
>
> All examples I've seen contains only grammar.g files. Is this simply a
> new handling to split my grammar file?
>   
It is for cases where you need to know what is in the grammar file 
because your build tool isn't able to work it out. For instance GNU make 
can ask antlr what the output files are, others cannot. Hence it accepts 
those suffixes for build rules.
> How ever. I go with my grammar.g file that I've used for the Java
> example.
>
> VCard.g:
> http://codepad.org/hiVS9kte
>
> First question is if it's also possible to insert the main code for C
> in the grammar file like with Java?
>   
Yes, but you have to get everything correct of course and this is not a 
wise move for production code. Better to build a little project.
> Then I installed ANTLR-3.0.1 (Ubuntu) from repository and the C runtime
> from ANTLR-3.0.1 from source. This should fit together.
>   
That is way too old and is why I don't like all these bundled packages - 
nobody ever keeps them up to date and so it is OK for applications and 
so on but not so good for development tools. Download 3.1.3 or 3.1.4 
snapshot from the ANTLR download page.
> Now I took an C example and modified it to compile for my grammar. At
> first I compiled the grammar and got these files:
>
> VCardLexer.c
> VCardLexer.h
> VCardParser.c
> VCardParser.h
>
> I modified them from an example:
>
> VCard.c:
> http://codepad.org/uoMWeBW7
>
> VCard.h:
> http://codepad.org/ADTKm47X
>
> Makefile.C:
> http://codepad.org/YWC4E4ti
>
> This compiles, but I get a "Segmentation fault" as result. I've not yet
> debugged into it, because I like to ensure that I did all steps correct.
>   
It means that you are not invoking it correctly, but is probably because 
you are tying to use 3.0.1.
> You could see that I commented out all the "langAST" and "treePsr"
> variables, because they didn't compile. Who creates the "pLangDumpDecl"
> and "LangParser_decl_return" type? I don't see it generated by the code
> generator. Do I need those for my example?
>   
They are auto generated and are the return types for the rules, but if 
you have Java style declarations, then they are not going to compile as C.
> And the last question is why is it so hard to get the C target working
> comparing to the Java target? :-)
>   
1) Dealing with C is always going to be a little more raw than Java, 
though it is very much faster at runtime;
2) You are starting from the wrong place;
3) You need to read through the whole API doc, where it gives you 
example drivers;
4) You need to use the correct version of the tool;
5) You need to read through the C examples;


Jim


More information about the antlr-interest mailing list