Generated Files
The antlr tool jar, run against a grammar file that targets the C language, will generate the following files according to whether your grammar file contains a lexer, parser, combined or treeparser specification. Your grammar file name and the subject of the grammar line in your file are expected to match. Here the generic name G is used:
Suffix | Generated files |
lexer grammar (G.g3l) | GLexer.c GLexer.h |
parser grammar (G.g3p) | GParser.c GParser.h |
grammar G (G.g3pl) | GParser.c GParser.h GLexer.c GLexer.h |
tree grammar G; (G.g3t) | G.c G.h |
The generated .c files reference the .h files using <G.h>, so you must use -I.
on your compiler command line (or include the current directory in your include paths in Visual Studio). Additionally, the generated .h files reference antlr3.h
, so you must use -I/path/to/antlr/include
(E.g. -I /usr/local/include
) to reference the standard ANTLR include files.
In order to reference the library file at compile time (you can/should only reference one) you need to use the -L/path/to/antlr/lib
(E.g. -L /usr/local/lib
) on Unix, or add the path to your "Additional Library Path" in Visual Studio. You also need to specify the library using -L
on Unix (E.g. -L /usr/local/lib -l antlr3c
) or add antlr3c_dll.lib
to your Additional Library Dependencies in Visual Studio.
In case it isn't obvious, the generated files may be used to produce either a library or an executable (.EXE on Windows) file.
If you use the shared version of the libraries, DLL or .so/.so/.a then you must ship the library with your application must run in an environment whereby the library can be found by the runtime linker/loader. This usually involves specifying the directory in which the library lives to an environment variable. On Windows, X:{yourwininstalldir}\system32 will be searched automatically.
Invoking Your Generated Recognizer
In order to run your lexer/parser/tree parser combination, you will need a small function (or main) function that controls the sequence of events, from reading the input file or string, through to invoking the tree parser(s) and retrieving the results. See "Using the ANTLR3C C Target" for more detailed instructions, but if you just want to get going as fast as possible, study the following code example.
#include <treeparser.h>
main (int argc, char *argv[])
{
pLangLexer lxr;
pLangParser psr;
LangParser_decl_return langAST;
pLangDumpDecl treePsr;
if (argc < 2 || argv[1] == NULL)
{
}
else
{
}
input = antlr38BitFileStreamNew(fName);
if ( input == NULL )
{
ANTLR3_FPRINTF(stderr,
"Unable to open file %s due to malloc() failure1\n", (
char *)fName);
}
lxr = LangLexerNew(input);
if ( lxr == NULL )
{
ANTLR3_FPRINTF(stderr,
"Unable to create the lexer due to malloc() failure1\n");
}
if (tstream == NULL)
{
ANTLR3_FPRINTF(stderr,
"Out of memory trying to allocate token stream\n");
}
psr = LangParserNew(tstream);
if (psr == NULL)
{
}
langAST = psr->decl(psr);
if (psr->pParser->rec->errorCount > 0)
{
ANTLR3_FPRINTF(stderr,
"The parser returned %d errors, tree walking aborted.\n", psr->pParser->rec->errorCount);
}
else
{
treePsr = LangDumpDeclNew(nodes);
treePsr->decl(treePsr);
nodes ->
free (nodes); nodes = NULL;
treePsr ->
free (treePsr); treePsr = NULL;
}
psr ->free (psr); psr = NULL;
tstream ->
free (tstream); tstream = NULL;
lxr ->
free (lxr); lxr = NULL;
input ->
close (input); input = NULL;
return 0;
}
ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM antlr3CommonTreeNodeStreamNewTree(pANTLR3_BASE_TREE tree, ANTLR3_UINT32 hint)
Definition: antlr3commontreenodestream.c:116
#define ANTLR3_SIZE_HINT
Definition: antlr3defs.h:53
#define ANTLR3_FPRINTF
Default definition of fprintf, set this to something other than fprintf before including antlr3....
Definition: antlr3defs.h:528
#define ANTLR3_CDECL
Definition: antlr3defs.h:360
uint8_t * pANTLR3_UINT8
Definition: antlr3defs.h:406
#define ANTLR3_ERR_NOMEM
Definition: antlr3errors.h:49
ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM antlr3CommonTokenStreamSourceNew(ANTLR3_UINT32 hint, pANTLR3_TOKEN_SOURCE source)
Definition: antlr3tokenstream.c:235
Common token stream is an implementation of ANTLR_TOKEN_STREAM for the default parsers and recognizer...
Definition: antlr3tokenstream.h:191
void(* free)(struct ANTLR3_COMMON_TOKEN_STREAM_struct *tokenStream)
Function that knows how to free an ANTLR3_COMMON_TOKEN_STREAM.
Definition: antlr3tokenstream.h:295
Definition: antlr3commontreenodestream.h:145
void(* free)(struct ANTLR3_COMMON_TREE_NODE_STREAM_struct *ctns)
Definition: antlr3commontreenodestream.h:299
Master context structure for an ANTLR3 C runtime based input stream.
Definition: antlr3input.h:56
void(* close)(struct ANTLR3_INPUT_STREAM_struct *input)
Pointer to function that closes the input stream.
Definition: antlr3input.h:134