简体   繁体   English

在C中编译ANTLR 3语法

[英]Compiling an ANTLR 3 grammar in C

I've been trying to learn ANTLR and get it working with C output code using this tutorial (also referenced in this question). 我一直在尝试学习ANTLR,并使用教程(也在问题中引用)使用C输出代码。 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). 我成功地获得了ANTLR来生成词法分析器和解析器作为C源代码,但我无法在Mac OS X Snow Leopard(i686-apple-darwin10-gcc-4.2.1)上使用gcc进行编译。 Below is the result when I try to compile the "SimpleCalcLexer.c". 下面是我尝试编译“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

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: 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

So several questions: 所以有几个问题:
1) What am I doing wrong? 1)我做错了什么? I'm pretty sure the libraries are being found, as there are other antlr functions and definitions being found in the code. 我很确定找到了库,因为代码中还有其他的antlr函数和定义。 Am I calling gcc incorrectly? 我是否错误地调用了gcc? (I've never compiled anything this complex on the commandline before.) (我以前从未在命令行上编译任何这么复杂的东西。)
2) What is ccn8ZVhk.o ? 2)什么是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 ). 我可以告诉它它是一个目标代码文件,但我在我的系统上找不到它( locatemdfind )。

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

That command line will compile the lexer and the parser and then link the result with the ANTLR library (the "-lantlr3c" part). 该命令行将编译词法分析器和解析器,然后将结果与ANTLR库(“-lantlr3c”部分)链接。

The object file ccn8ZVhk.o is part of the runtime library and is what actually calls main(). 目标文件ccn8ZVhk.o是运行时库的一部分,实际上是调用main()。 It doesn't contain user-servicable parts. 它不包含用户可维修的部件。

If you compile multiple times you'll see that the object code filenames change each time, so I'm guessing they are temporary object files used before the final target is compiled and linked. 如果你多次编译,你会发现目标代码文件名每次都会改变,所以我猜它们是在编译和链接最终目标之前使用的临时目标文件。 I am running into the same problem and I tried specifying the architecture as 386 and 686. I am trying to compile the output of this Python3 grammar file . 遇到了同样的问题,我尝试将架构指定为386和686.我正在尝试编译此Python3语法文件的输出。 CajunLuke, can you post the exact command you used to compile when it worked? CajunLuke,你可以发布你用来编译的确切命令吗? Here's a sample of what I have done: 以下是我所做的一个示例:

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

Also, did you compile the ANTLR3C runtime differently from the usual "configure;make;make install"? 另外,您是否编译了ANTLR3C运行时与通常的“configure; make; make install”不同? I tried compiling using the 64 bit option and I had the same problem. 我尝试使用64位选项进行编译,我遇到了同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM