[antlr-interest] Composed grammars and AST, pb with .h files; and "Bus error" with 'toStringTree'
Jean-Claude Durand
jean-claude.durand at imag.fr
Wed Jul 30 06:55:04 PDT 2008
I get a pb easy to fix when I use composed grammars with AST: the .h
files include
each other (because of forward references) and do not compile. Here
is a very simple example
(I am using Version 3.1b2, and C target language):
~/Soft/Ariane-Y/V3Compilateur/ForAI: ls *.g
Composed.g Id.g Int.g Lex.g
~/Soft/Ariane-Y/V3Compilateur/ForAI: more Lex.g
lexer grammar Lex;
options
{
language=C;
}
tokens
{
LITERAL_IM;
INTEGER_IM;
DECL_IM;
}
LITERAL: 'a'..'z'+ ;
INTEGER: '0'..'9'+ ;
Blancs: ( ' ' | '\t' | '\n' )+ { $channel=HIDDEN; } ;
~/Soft/Ariane-Y/V3Compilateur/ForAI:
~/Soft/Ariane-Y/V3Compilateur/ForAI: more Id.g
parser grammar Id;
options
{
language=C;
output=AST;
}
id :
LITERAL
-> ^(LITERAL_IM LITERAL)
;
~/Soft/Ariane-Y/V3Compilateur/ForAI:
~/Soft/Ariane-Y/V3Compilateur/ForAI: more Int.g
parser grammar Int;
options
{
language=C;
output=AST;
}
ent:
INTEGER
-> INTEGER
;
~/Soft/Ariane-Y/V3Compilateur/ForAI:
~/Soft/Ariane-Y/V3Compilateur/ForAI: more Composed.g
parser grammar Composed;
options
{
language=C;
tokenVocab=Lex;
output=AST;
}
import Id, Int;
decls
:
i=id
e=ent
-> ^(DECL_IM $i $e)
;
~/Soft/Ariane-Y/V3Compilateur/ForAI:
~/Soft/Ariane-Y/V3Compilateur/ForAI: java org.antlr.Tool Lex.g
ANTLR Parser Generator Version 3.1b2 (May 20, 2008) 1989-2008
Generating Lex.c
Generating Lex.h
~/Soft/Ariane-Y/V3Compilateur/ForAI: java org.antlr.Tool Composed.g
ANTLR Parser Generator Version 3.1b2 (May 20, 2008) 1989-2008
Generating Composed.c
Generating Composed.h
Generating Composed_Id.c
Generating Composed_Id.h
Generating Composed_Int.c
Generating Composed_Int.h
~/Soft/Ariane-Y/V3Compilateur/ForAI:
~/Soft/Ariane-Y/V3Compilateur/ForAI: c++ -c Composed.c Composed_Id.c
Composed_Int.c Lex.c main.c -I/usr/include/sys -I/usr/local/pkg/
antlr-2008-06-26.17/runtime/C/include/ -I.
./Composed_Int.h:157: error: expected declaration before '}' token
./Composed.h:106: error: expected identifier before '*' token
./Composed.h:106: error: ISO C++ forbids declaration of
'Composed_Id_id_return' with no type
./Composed.h:106: error: 'Composed_Id_id_return' declared as function
returning a function
./Composed.h:159: error: expected declaration before '}' token
./Composed.h:107: error: expected identifier before '*' token
./Composed.h:107: error: ISO C++ forbids declaration of
'Composed_Int_ent_return' with no type
./Composed.h:107: error: 'Composed_Int_ent_return' declared as
function returning a function
./Composed.h:159: error: expected declaration before '}' token
./Composed_Int.h:157: error: expected declaration before '}' token
~/Soft/Ariane-Y/V3Compilateur/ForAI:
So I modified the .h files corresponding to the component grammars by
replacing
"#include <Composed.h>"
by
"typedef struct Composed_Ctx_struct Composed, * pComposed;"
in both files Composed_id.h and Composed_Int.h, and now, it's OK:
~/Soft/Ariane-Y/V3Compilateur/ForAI: c++ -c Composed.c Composed_Id.c
Composed_Int.c Lex.c main.c -I/usr/include/sys -I/usr/local/pkg/
antlr-2008-06-26.17/runtime/C/include/ -I.
main.c: In function 'int main(int, char**)':
main.c:28: warning: comparison is always false due to limited range
of data type
main.c:48: warning: comparison is always false due to limited range
of data type
I took my main from the examples, and wanted to print the AST: I got a
"Bus error", if I want to print the AST with
"printf(" main: %s\n", ast.tree->toStringTree(ast.tree)->chars);":
~/Soft/Ariane-Y/V3Compilateur/ForAI: c++ Lex.o Composed.o
Composed_Id.o Composed_Int.o main.o -lantlr3c
~/Soft/Ariane-Y/V3Compilateur/ForAI: ./a.out test.in main:
main: ast: 344064
Bus error
~/Soft/Ariane-Y/V3Compilateur/ForAI:
Without the printf, it's OK:
~/Soft/Ariane-Y/V3Compilateur/ForAI: ./a.out test.in
main:
main: ast: 344064
main.
~/Soft/Ariane-Y/V3Compilateur/ForAI:
Here is 'my' main:
~/Soft/Ariane-Y/V3Compilateur/ForAI: cat main.c
#include "stdio.h"
#include "Lex.h"
#include "Composed.h"
int ANTLR3_CDECL
main (int argc, char *argv[])
{
printf(" main:\n");
pANTLR3_UINT8 fName;
pANTLR3_INPUT_STREAM input;
pLex lxr;
pANTLR3_COMMON_TOKEN_STREAM tstream;
pComposed psr;
if (argc < 2 || argv[1] == NULL)
{
fName =(pANTLR3_UINT8)"./input";
}
else
{
fName = (pANTLR3_UINT8)argv[1];
}
input = antlr3AsciiFileStreamNew(fName);
if ( (ANTLR3_UINT64)input < 0 )
{
switch((ANTLR3_UINT64)input)
{
case ANTLR3_ERR_NOMEM:
fprintf(stderr, "Unable to open file %s due
to malloc() failure1\n",
(char *)fName);
exit(1);
break;
default:
fprintf(stderr, "Failed to open file %s -
exit with code %d\n",
(char *)fName,
(ANTLR3_UINT64)input);
exit((int)((ANTLR3_UINT64)input));
break;
}
}
lxr = LexNew(input); // SimpleLexerNew is generated by ANTLR
if ( (ANTLR3_UINT64)lxr < 0 )
{
switch((ANTLR3_UINT64)lxr)
{
case ANTLR3_ERR_NOMEM:
fprintf(stderr, "Unable to create the lexer due to
malloc() failure1\n");
exit(1);
break;
default:
fprintf(stderr, "Failed to create lexer -
exit with code %d\n",
(ANTLR3_UINT64)lxr);
exit((int)((ANTLR3_UINT64)lxr));
break;
}
}
//tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT,
lxr->pLexer->tokSource);
// changé pour ci-dessous trouvé dans un programme plus
récent !
tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT,
TOKENSOURCE(lxr));
if ((ANTLR3_UINT64)tstream == ANTLR3_ERR_NOMEM)
{
fprintf(stderr, "Out of memory trying to allocate
token stream\n");
exit(ANTLR3_ERR_NOMEM);
}
psr = ComposedNew(tstream); // SimpleParserNew is generated
by ANTLR3
// analyse lexicale:
/*{
ANTLR3_INT32 T;
T = 0;
while (T != ANTLR3_TOKEN_EOF)
{
T = tstream->tstream->istream->_LA(tstream-
>tstream->istream, 1);
tstream->tstream->istream->consume(tstream-
>tstream->istream);
printf("%d %s\n", T, (psr->pParser->rec-
>state->tokenNames)[T]);
}
}*/
if ((ANTLR3_UINT64)tstream == ANTLR3_ERR_NOMEM)
{
fprintf(stderr, "Out of memory trying to allocate
parser\n");
exit(ANTLR3_ERR_NOMEM);
}
Composed_decls_return ast;
ast = psr->decls(psr);
printf(" main: ast: %d\n", ast);
//printf(" main: %s\n", ast.tree->toStringTree(ast.tree)-
>chars);
psr->free (psr); psr = NULL;
tstream->free (tstream); tstream = NULL;
lxr->free (lxr); lxr = NULL;
input->close (input); input = NULL;
printf(" main.\n");
return 0;
}
~/Soft/Ariane-Y/V3Compilateur/ForAI:
Here is the zip file of the above example including the modified files:

Thanks a lot,
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/20080730/714d0041/attachment-0002.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Composed.tar.gz
Type: application/x-gzip
Size: 4044 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20080730/714d0041/attachment-0001.gz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080730/714d0041/attachment-0003.html
More information about the antlr-interest
mailing list