一个简单的ANTLR 3.4示例,用于C目标运行时

A simple ANTLR 3.4 example for C target runtime

本文关键字:示例 用于 运行时 目标 ANTLR 一个 简单      更新时间:2023-10-16

是否有人知道(或有)一个简单的ANTLR 3.4示例main()函数的C目标?我正在尝试用C或c++开始使用ANTLR,我看到的所有例子(包括这个)都过时了,例如,它们使用的函数不再存在。似乎没有下载包本身的任何示例,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);
}