在C中编译ANTLR 3语法
我一直在尝试学习ANTLR,并使用本教程(也在本问题中引用)使用C输出代码。 我成功地获得了ANTLR来生成词法分析器和解析器作为C源代码,但我无法在Mac OS X Snow Leopard(i686-apple-darwin10-gcc-4.2.1)上使用gcc进行编译。 下面是我尝试编译“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”(也没有定义),但解析器确实定义了它,所以我尝试编译它:
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
)。
您需要将词法分析器和解析器编译为相同的可执行文件; 他们共同努力创建一个单一的计划。 试试这个:
gcc -o lexer SimpleCalcLexer.c SimpleCalcParser.c -lantlr3c
该命令行将编译词法分析器和解析器,然后将结果与ANTLR库(“-lantlr3c”部分)链接。
目标文件ccn8ZVhk.o是运行时库的一部分,实际上是调用main()。 它不包含用户可维修的部件。
如果你多次编译,你会发现目标代码文件名每次都会改变,所以我猜它们是在编译和链接最终目标之前使用的临时目标文件。 我遇到了同样的问题,我尝试将架构指定为386和686.我正在尝试编译此Python3语法文件的输出。 CajunLuke,你可以发布你用来编译的确切命令吗? 以下是我所做的一个示例:
WOPR:plex pokstad$ gcc -arch i686 -o lexer python3Lexer.c python3Parser.c -lantlr3c Undefined symbols: "_main", referenced from: start in crt1.10.6.o "_python3Lexer_syntetizeEmptyString", referenced from: _mLEADING_WS in ccGDusga.o "_python3Lexer_createLexerToken", referenced from: _mCONTINUED_LINE in ccGDusga.o _mLEADING_WS in ccGDusga.o "_python3Lexer_initLexer", referenced from: _python3LexerNewSSD in ccGDusga.o ld: symbol(s) not found collect2: ld returned 1 exit status
另外,您是否编译了ANTLR3C运行时与通常的“configure; make; make install”不同? 我尝试使用64位选项进行编译,我遇到了同样的问题。