[antlr-interest] C target difficulties

Jim Idle jimi at temporal-wave.com
Thu Dec 18 09:33:54 PST 2008


On Thu, 2008-12-18 at 13:48 +0100, M. C. wrote:

> Hi all,
>  
>  
> I have been working with ANTLR for some weeks now, but I'm having some
> difficulties getting the generated code to work.
>  
> I'm using ANTLRworks 1.2.2 under Fedora linux 8. and installed the
> runtime library "libantlr3c-3.1.1", which works perfectly. 
>  
> I generate code for the C target, and made my own main.c using the
> example on the ANTLR website, this code compiles well without errors
> (and some warnings about not used variables, but that isn't harmfull I
> guess)
>  
> Whats the problem then? Well I've got a question about the section
> just above the main, "int ANTLR3_CDECL" what is it good for? when I
> comment this part out, the source compiles well, but if I don't it
> generates the following error:
>  
> "main.c:8: Warning: two or more data types in declaration specifiers"


You are not using it correctly. The runtime will define this is empty on
UNIX and as the proper cdecl spec for exporting functions from DLLs on
windows. So, apart from the fact that you can't export main() from a DLL
like that, look at your code - it is incorrect you have int in there
twice. Use this macro if you want to export your own functions from DLLs
- it is a convenience for you but a necessity for the runtime functions.



>  
> Then I've got a somewhat more serious problem, my code hangs when
> feeding an input file. I checked with some printf statements where it
> hangs, but it doesn't do anything after the  " psr =
> testParserNew(tstream); " line. The program doesn't shutdown, but also
> doesn't do anything else. 

I suggest that this means there is something wrong with the grammar or
the way your are compiling it etc. 

>  
> And is  psr->program(psr);  the right way to invoke the parser? 


In your code below you are saying that it hangs on the parser
constructor, but here you are suggesting that it is hanging on
invocation of the parser? I assume the latter is correct?


> So that the outputs I made in the .g file (like detecting a number and
> then printf("number found"); actually get printed in the terminal?
>  
> I've been trying things for some time now, but really can't find the
> problem. Does someone else maybe know what i'm doing wrong?


Do you absolutely need to use the C target? I would suggest that you
learn using Java just because it will keep you out of trouble, then come
back to the C target.

>  
> Thanx in advance!!
>  




> int  ANTLR3_CDECL
> int main(int argc, char *argv[])


See that you have 

int ANTLR3_CDECL int main...

?


>    tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT,
> TOKENSOURCE(lxr));
>     if (tstream == NULL)
>     {
>         fprintf(stderr, "Out of memory trying to allocate token stream
> \n");
>         exit(1);
>     }
>     psr     = testParserNew(tstream);  // CParserNew is generated by
> ANTLR3
>  
> // SOFTWARE HANGS HERE........


I don't think you mean here?

>  
>  
>     if (psr == NULL)
>     {
>         fprintf(stderr, "Out of memory trying to allocate parser\n");
>         exit(ANTLR3_ERR_NOMEM);
>     }


I think that you mean here?

>     psr->program(psr);


You probably need to post your grammar so that we can look for errors
within it.

Jim

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081218/f61acd64/attachment.html 


More information about the antlr-interest mailing list