2011-12-16 43 views
4

有誰知道(或有)簡單ANTLR 3.4示例main()函數用於C目標嗎?我試着用C或C++開始使用ANTLR,並且我看到的所有例子(包括this)都是過時的,例如,他們使用不再存在的功能。似乎下載的軟件包本身並沒有任何示例,Wiki上的示例已過時。針對C目標運行時的簡單ANTLR 3.4示例

+0

在這裏討論的代碼http://stackoverflow.com/questions/5441810/antlr3-c-target-parser-return-misses-out-root-element作品 - 卜你幾乎肯定會找到更好的那裏的例子 – Jimmy 2011-12-16 17:06:06

+0

啊,謝謝!這應該足以讓我開始。 – ikh 2011-12-16 17:11:38

回答

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

}