C 目标运行时的简单 ANTLR 3.4 示例 [英] A simple ANTLR 3.4 example for C target runtime

查看:22
本文介绍了C 目标运行时的简单 ANTLR 3.4 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道(或拥有)一个简单的 ANTLR 3.4 示例 main() 用于 C 目标的函数?我正在尝试在 C 或 C++ 中开始使用 ANTLR,以及我看到的所有示例(包括 this) 已过时,例如他们使用不再存在的函数.好像没有下载包本身的例子,而且维基上的例子已经过时了.

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.

推荐答案

未经测试.

#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 目标运行时的简单 ANTLR 3.4 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆