Compiling an ANTLR 3 grammar in C

天涯浪子 提交于 2019-12-08 05:20:59

问题


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

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

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


回答1:


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

The object file ccn8ZVhk.o is part of the runtime library and is what actually calls main(). It doesn't contain user-servicable parts.




回答2:


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. CajunLuke, can you post the exact command you used to compile when it worked? 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"? I tried compiling using the 64 bit option and I had the same problem.



来源:https://stackoverflow.com/questions/1757357/compiling-an-antlr-3-grammar-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!