[antlr-interest] Problem with C target output on example C grammar

Jim Idle jimi at temporal-wave.com
Tue Apr 1 13:58:11 PDT 2008



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Kamil Burzynski
> Sent: Tuesday, April 01, 2008 11:22 AM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Problem with C target output on example C
> grammar
> 
> Hello,
> 
> > To follow up on this, I did successfully download the .tgz of the
> > examples from Fisheye by right clicking the tgz link and selecting
> > "save as" from IE 7, but the .zip format seems to be corrupt.
> 
> Till now I was using http://www.antlr.org/download/examples-
> v3.1b1.tar.gz
> Now I had downloaded from fisheye, to the same result. This is no
> surprise, since in both cases external_declaration goes to
> function_definition without going through declaration, and
> function_definition in turn calls declarator and direct_declarator,
> while they require declaration to be on stack...

OK - the good news is that nothing is broken in the C target. The reason you are seeing the error now is that I backed off a change inherited from the Java target whereby scope access was 'protected' from being accessed if it was NULL by returning a 'default' value. This is because I believe that this would mask errors in dangerous ways, that would be difficult to debug. 

It seems that I was correct based on our own example ;-). However, it seems that I did not run that particular example after the C code generation template change. Hence this is a grammar problem rather than a runtime problem. In the prior version of the C.stg file, the scope would have been seen to be NULL, so 0 would have been return for the isTypeDef reference and the code would not crash. However, this is clearly incorrect behavior, as it masks a problem and in a real world grammar would have been very difficult to see just what was going on. Best to crash in testing, where it will be obvious that the stack is empty and you can find out why. 

I suspect, but have not yet looked, that the Java and C# versions of this example are also masking this grammar problem.

To fix for the C target example, you can use either the SCOPE_TOP() or SCOPE_SIZE macros, but in this case I choose SCOPE_TOP as it is faster:

direct_declarator
    :   (   IDENTIFIER
			{
	    		if (SCOPE_TOP(declaration) != NULL && $declaration::isTypedef)


Perhaps a better fix would be move around the scope, but then, this is really an example of using scopes rather than how you really would process typedefs in the C grammar.


As to your shared object issue:


~antlrsrc/code/examples-v3/C/C: gcc -I. -fPIC C*.c -c
-~/antlrsrc/code/examples-v3/C/C: gcc -shared *.o -o libcex.so
scalix-~/antlrsrc/code/examples-v3/C/C: gcc -I. -fPIC main.c -o jc -L. -lcex -lantlr3c
scalix-~/antlrsrc/code/examples-v3/C/C: export LD_LIBRARY_PATH=$(pwd):/usr/local/lib
scalix-~/antlrsrc/code/examples-v3/C/C: ./jc
Including file 'jimtest.h'
define type size_t
etc ...

Now add the scope...
scalix-~/antlrsrc/code/examples-v3/C/C: antlr C.g3pl
ANTLR Parser Generator  Version 3.1 1989-2007
Generating CParser.c
Generating CParser.h
Generating CLexer.c
Generating CLexer.h
scalix-~/antlrsrc/code/examples-v3/C/C: gcc -I. -fPIC C*.c -c                                               
scalix-~/antlrsrc/code/examples-v3/C/C: gcc -shared *.o -o libcex.so                                        
scalix-~/antlrsrc/code/examples-v3/C/C: ./jc
Segmentation fault

As expected, now, relink:

scalix-~/antlrsrc/code/examples-v3/C/C: gcc -I. -fPIC main.c -o jc -L. -lcex -lantlr3c                              
scalix-~/antlrsrc/code/examples-v3/C/C: ./jc
Including file 'jimtest.h'
define type size_t

everything works as expected.


Jim





More information about the antlr-interest mailing list