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

标签 c++ c antlr antlr3

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

最佳答案

未经测试。

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

}

关于c++ - C 目标运行时的简单 ANTLR 3.4 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8537214/

相关文章:

c++ - 用 C++ 处理 CSV 文件

antlr - 我可以使用 antlr 来解析部分数据吗?

java - 从 Java 源代码生成 AST 报告而不实际运行它

c - 在哪里声明我的数组,哪种方法更好?

c - Eclipse 索引器不起作用

parsing - 我应该如何构建和遍历 ANTLR3 语法的 AST 输出?

c++ - 在 Visual Studio Code 中指定库路径?

c++ - C++中两个整数的乘法

c++将派生类分配给基类的更好方法

c++ - 是否可以同时抛出两个值?