[antlr-interest] [antlr3c] Posible bug i C runtime generated code (ANTLR v3.1.3 and libantlr3c 3.1.3)

Adamic Tomislav tomislav.adamic at gmail.com
Mon Aug 3 01:38:25 PDT 2009


Hi

Using two grammars below, ANTLR generates code that is not compilable
with either C or C++ compiler. Specifically, generated file
"ExampleGrammarEval.c" contains expression "MyStruct value = NULL;"
which is not valid C/C++ (because value is not pointer) so the code
doesn't compile. There is a work around which makes
"ExampleGrammarEval.g" bigger and harder to read. Also, this
workaround is not practical to apply for larger struct with more
member variables.

Following are the 2 grammars needed to reproduce the bug, and the
third one that describes workaround.

-------------------------------------cut
here-----------------------------------------
grammar ExampleGrammar;

options {
  output=AST;
  ASTLabelType=pANTLR3_BASE_TREE;
  language=C;
}

tokens {
  PLUS	= '+';
}

prog : expr EOF!;
expr : addition;
addition : atom ( (PLUS)^ atom )*;
atom : NUMBER;
fragment DEC_DIGIT : '0'..'9' ;
NUMBER : DEC_DIGIT+;
WS : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;};
-------------------------------------cut
here-----------------------------------------

-------------------------------------cut
here-----------------------------------------
tree grammar ExampleGrammarEval;

options {
	tokenVocab=ExampleGrammar;
	ASTLabelType=pANTLR3_BASE_TREE;
	language=C;
}

@includes {
typedef struct {
	double d1, d2;
} MyStruct ;
}

prog returns [MyStruct value]
    : expr { $value = $expr.value; };

expr returns [MyStruct value]
    : addition { $value = $addition.value; };

addition returns [MyStruct value]
    : ^(PLUS a=atom b=atom) {
		$value.d1 = $a.value.d1 + $b.value.d1;
		$value.d2 = $a.value.d2 + $b.value.d2;
    }
	| atom {
    	$value = $atom.value;
    };

atom returns [MyStruct value]
// Here is actually some parsing done to convert string into numerical
// value which is omited for simplicity.
    : NUMBER { value.d1 = value.d2 = 0.0 };
-------------------------------------cut
here-----------------------------------------

-------------------------------------cut
here-----------------------------------------
tree grammar ExampleGrammarEval;

options {
	tokenVocab=ExampleGrammar;
	ASTLabelType=pANTLR3_BASE_TREE;
	language=C;
}

@includes {
typedef struct {
	double d1, d2;
} MyStruct ;
}

prog returns [double d1, double d2]
    : expr { $d1 = $expr.d1; $d2 = $expr.d2; };

expr returns [double d1, double d2]
    : addition { $d1 = $addition.d1; $d2 = $addition.d2; };

addition returns [double d1, double d2]
    : ^(PLUS a=atom b=atom) {
		$d1 = $a.d1 + $b.d1;
		$d2 = $a.d2 + $b.d2;
    }
	| atom {
    	$d1 = $a.d1; $d2 = $b.d2;
    };

atom returns [double d1, double d2]
// Here is actually some parsing done to convert string into numerical
// value which is omited for simplicity.
    : NUMBER { value.d1 = value.d2 = 0.0 };
-------------------------------------cut
here-----------------------------------------


More information about the antlr-interest mailing list