在C中编译ANTLR 3语法 [英] Compiling an ANTLR 3 grammar in C
问题描述
我一直在努力学习ANTLR,并使用这个教程(也在这个问题)。我成功地让ANTLR生成词法分析器和解析器作为C源代码,但是我无法让它们在Mac OS X Snow Leopard(i686-apple-darwin10-gcc-4.2.1)上使用gcc进行编译。下面是我尝试编译SimpleCalcLexer.c时的结果。
I've been trying to learn ANTLR and get it working with C output code using this tutorial (also referenced in this question). I successfully got ANTLR to generate the lexer and parser as C source, but I cannot get them to compile using gcc on Mac OS X Snow Leopard (i686-apple-darwin10-gcc-4.2.1). Below is the result when I try to compile the "SimpleCalcLexer.c".
dyn-72-33-132-199:Desktop bf$ gcc -o lexer SimpleCalcLexer.c
Undefined symbols:
"_main", referenced from:
start in crt1.10.6.o
"_antlr3LexerNewStream", referenced from:
_SimpleCalcLexerNewSSD in ccjXa6NU.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
SimpleCalcLexer.c文件没有在任何地方引用main(也没有定义),但解析器确实定义了它,所以我试图编译它:
The SimpleCalcLexer.c file does not reference "main" anywhere (nor is it defined), but the parser does define it, so I tried to compile that:
dyn-72-33-132-199:Desktop bf$ gcc -o parser SimpleCalcParser.c
Undefined symbols:
"_antlr3CommonTokenStreamSourceNew", referenced from:
_main in ccn8ZVhk.o
"_antlr3ParserNewStream", referenced from:
_SimpleCalcParserNewSSD in ccn8ZVhk.o
"_SimpleCalcLexerNew", referenced from:
_main in ccn8ZVhk.o
"_antlr3AsciiFileStreamNew", referenced from:
_main in ccn8ZVhk.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
所以几个问题:
1)我在做什么错了?我很确定这些库已经找到,因为在代码中还有其他antlr函数和定义。我是否错误地调用gcc? (我从来没有在命令行上编译过任何复杂的命令)。
2)什么是 ccn8ZVhk.o
?我可以告诉它它是一个目标代码文件,但我在我的系统上找不到它( locate
和 mdfind
)。
So several questions:
1) What am I doing wrong? I'm pretty sure the libraries are being found, as there are other antlr functions and definitions being found in the code. Am I calling gcc incorrectly? (I've never compiled anything this complex on the commandline before.)
2) What is ccn8ZVhk.o
? I can tell that it is an object code file, but I can't find it on my system (both locate
and mdfind
).
推荐答案
您需要将词法分析器和解析器编译为相同的可执行文件;他们一起工作创建一个单一的程序。试试这个:
You need to compile the lexer and the parser into the same executable; they work together to create a single program. Try this:
gcc -o lexer SimpleCalcLexer.c SimpleCalcParser.c -lantlr3c
该命令行将编译词法分析器和解析器,然后将结果与ANTLR库(-lantlr3c部分)相链接。
That command line will compile the lexer and the parser and then link the result with the ANTLR library (the "-lantlr3c" part).
目标文件ccn8ZVhk.o是运行时库的一部分,实际上是调用main()。它不包含用户可服务的部分。
The object file ccn8ZVhk.o is part of the runtime library and is what actually calls main(). It doesn't contain user-servicable parts.
这篇关于在C中编译ANTLR 3语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!