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

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


> I am really puzzled how it worked at least once - probably on non-linux
> platform (or just different compiler) program was faulty but did not
> crashed.

I don't think it ever would have.

> 
> >> a) I wanted to do some lex/yacc-type return values:
> 
> > This isn't anything to do with the C target - you cannot do that in
> lexers.
> 
> I think there was small misunderstanding. Indeed lexer do not build
> symbol tree, but I was doing something different. I wanted something
> like code I've found in examples/Python/calc/Calculator.g:
> 
> evaluate returns [result]: r = expression { result = r; };
> 
> (this is python syntax, though the usage of 'returns' keyword should
> stay the same). The problem is that C target when sees something like
> "r
> = expression" 

No, that isn't your problem, look at the generated code.

> does initialize internal variable with NULL before
> assignment. This is improper even in plain C. 

I am well aware of what can be initialized with what in C :-). Please consider that you may be doing something wrong or slightly unorthodox yourself before blaming the C target ;-)

> Consider this example
> (just reduced and slightly modified version of
> examples/Python/calc/Calculator.g):
> 
> 
> grammar Calculator;
> options {
>   language = C;
> }
> 
> @members
> {
>         struct foo
>         {
>                 int bar;
>         };
> }
> 
> evaluate returns [struct foo result]: r=INTEGER {result.bar = 42;};
> 
> INTEGER: DIGIT+;
> 
> fragment
> DIGIT: '0'..'9';
> 
> Try to compile it. C compiler will say, that you cannot assign NULL to
> foo.

You would get a similar thing on any target when you use something that the templates do not know how to initialize. The problem is that there is no default initializer for struct foo in the C.stg, so it declares:

struct foo result = NULL;

Because NULL is the default initializing value when the type is unknown. Return anything other than a struct and you will be fine.

Now, why are we initializing the return values? I no longer remember, but I think that there is some good reason and it might be that it is the same code that is used for initializing tree return variables. (If you use output=AST then the error actually goes away). I know though that I have looked at this before and there was no easy solution adn in fact no NEED for a solution (see below for your error). Perhaps it is coming time to revisit it and make sure I have documented it at least. 

However, the answer to your problem is that the rule already returns a struct if you have more than one return value, so if you have just one element in your struct, then don't use the struct. If you have more than one element, then don't use the struct, just list it:

returns [int x, int y]

And let the code generator build the struct for you, whence you can use:

x=evaluate
{
  ... $x.x  .... $x.y .....

}

So, I think that we have reduced all your problems to the one grammar error, one user error, one user misunderstanding and zero problems with the C target, so I don't think you need to be concerned about the C runtime :-)

Jim








More information about the antlr-interest mailing list