[antlr-interest] Composed grammars: small bug with generated files and pbs with errors reporting

Jean-Claude Durand jean-claude.durand at imag.fr
Mon Sep 1 08:55:06 PDT 2008


I use antlr v3.1 with the C language target to compose grammars. I  
send you an archive to show
you the problem:
- first, in the different generated .h files, the variable 'tree' has  
no type in the typedef definition of the
"return" when building ASTs (you should have the type  
'pANTLR3_BASE_TREE'):

typedef struct Composed_Float_reel_return_struct
{
     /** Generic return elements for ANTLR3 rules that are not in  
tree parsers or returning trees
      */
     pANTLR3_COMMON_TOKEN    start;
     pANTLR3_COMMON_TOKEN    stop;
     	tree;

}
     Composed_Float_reel_return;


- second, error reporting does not work very well even with very  
simple grammars as in my example
below. Sometimes it is ok, sometimes not (the same message for 2  
different errors or no message at all):

I compose the 2 grammars:

parser grammar Int;
options
{
	language=C;
	output=AST;
}
ent:	
	INT Id SEMICOL
					-> ^(INT Id)	
	;

and:

parser grammar Float;
options
{
	language=C;	
	output=AST;
}
reel	:	
	FLOAT Id SEMICOL
					-> ^(FLOAT Id)	
	;

in the composed grammar:

parser grammar Composed;
options
{
	language=C;
	tokenVocab=Lex;
	output=AST;
}
import Int, Float;
decls
	:
	e=ent
	r=reel
				-> ^(DECL $e $r)
	;

- example 1: ok

int a ;
float x ;

the result is:
  main:
  main: (DECL (int a) (float x))
  main.

- example 2: ok

int a b;
float x;

the result is:

  main:
err1.ok(1)  : error 9 : Extraneous token, at offset 5
     near [Index: 2 (Start: 5243798-Stop: 5243798) ='b', type<8>  
Line: 1 LinePos:5]
      : Extraneous input - expected SEMICOL ...
  main: (DECL (int a) (float x))
  main.

- example 3: ok

int a ;
float x y;

...

- example 4: NOT ok, I get the same message twice for the first error  
but nothing on the second.

int a b;
float x y;

the result is:

  main:
err.notOk(1)  : error 9 : Extraneous token, at offset 5
     near [Index: 2 (Start: 5243798-Stop: 5243798) ='b', type<8>  
Line: 1 LinePos:5]
      : Extraneous input - expected SEMICOL ...
err.notOk(1)  : error 9 : Extraneous token, at offset 5
     near [Index: 2 (Start: 5243798-Stop: 5243798) ='b', type<8>  
Line: 1 LinePos:5]
      : Extraneous input - expected SEMICOL ...
  main: ast: 249856
  main: (DECL (int a) (float x))
  main.

I use the lexer:

lexer grammar Lex;
options
{
	language=C;
}
tokens
{
	DECL;
}
INT: 'int';
FLOAT: 'float';
SEMICOL: ';';
Id:	'a'..'z'+	;
Blancs: ( ' ' | '\t' | '\n' )+ { $channel=HIDDEN; }	;

With more complicated grammars, I got no error reporting at all.




Have a nice day,
Jean-Claude Durand

LIG, équipe GETALP
385, rue de la Bibliothèque
BP 53
38041 Grenoble cedex 9
France

Jean-Claude.Durand at imag.fr
tél: +33 (0)4 76 51 43 81
fax: +33 (0)4 76 51 44 05



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080901/6e756682/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ComposedGrammars_1.tar.gz
Type: application/x-gzip
Size: 4987 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20080901/6e756682/attachment.gz 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080901/6e756682/attachment-0001.html 


More information about the antlr-interest mailing list