Does anyone know of (or have) a simple ANTLR 3.4 example main()
function for C target? I'm trying to get started with ANTLR in C or C++, and all examples I see (including this) are out of date, e.g. they use functions that don't exist any more. There don't seem to be any examples with downloaded package itself, and the example on Wiki is out of date.
Asked
Active
Viewed 3,454 times
4
-
the code in the question here http://stackoverflow.com/questions/5441810/antlr3-c-target-parser-return-misses-out-root-element works - bu you will almost certainly find better examples out there – Jimmy Dec 16 '11 at 17:06
-
Ahh, thanks! That should be enough to get me started. – ikh Dec 16 '11 at 17:11
-
@ikh Hello, First of all, upvoted. And I have a question. Are you same user of http://stackoverflow.com/users/2729109/ikh? Or are you Korean? – Jason Heo Oct 22 '14 at 08:16
-
@InoSHeo Nope, sorry, different ikh. Not Korean either. – ikh Oct 22 '14 at 12:27
-
@ikh Sorry for bothering you ;) – Jason Heo Oct 23 '14 at 00:18
1 Answers
6
Untested.
#include "YourLexer.h"
#include "YourParser.h"
int main()
{
uint8_t * bufferData; // Some memory with text in it
uint32_t bufferSize; // Size of said memory
pANTLR3_UINT8 bufferName; // Name of buffer. ANTLR uses this for some default
// error messages
//Creates an input stream. If you want to parse once from multiple sources
// you can switch among these during lexing
pANTLR3_INPUT_STREAM input = antlr3StringStreamNew(
bufferData,
ANTLR3_ENC_8BIT,
bufferSize,
bufferName);
assert(input != NULL);
//Creates the lexer. Doesn't do anything until the parser(or you) tells it to.
pYourLexer lxr = YourLexerNew(input);
assert(lxr != NULL);
//Creates an empty token stream.
pANTLR3_COMMON_TOKEN_STREAM tstream = antlr3CommonTokenStreamSourceNew(
ANTLR3_SIZE_HINT, TOKENSOURCE(lxr));
assert(tstream != NULL);
//Creates a parser.
pYourParser psr = YourParserNew(tstream);
assert(psr != NULL);
//Run the parser rule. This also runs the lexer to create the token stream.
psr->some_parser_rule(psr);
}