C目标运行时的简单ANTLR 3.4示例

时间:2011-12-16 16:36:05

标签: c++ c antlr antlr3

是否有人知道(或拥有)C目标的简单ANTLR 3.4示例main()函数?我试图在C或C ++中开始使用ANTLR,我看到的所有示例(包括this)都已过时,例如他们使用不再存在的功能。下载的软件包本身似乎没有任何示例,Wiki上的示例已过时。

1 个答案:

答案 0 :(得分:6)

未测试。

#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);

}