代码之家  ›  专栏  ›  技术社区  ›  Cajunluke Martijn Courteaux

用C语言编译Antlr3语法

  •  1
  • Cajunluke Martijn Courteaux  · 技术社区  · 15 年前

    我一直在尝试学习antlr,并让它与c输出代码一起使用 this 教程(另请参见 this 问题)。我成功地让antlr生成lexer和parser作为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函数和定义。我打电话给海湾合作委员会是不是打错了?(我以前从未在命令行上编译过这么复杂的东西。)
    2)什么是 ccn8ZVhk.o ?我可以知道它是一个对象代码文件,但在我的系统中找不到它(两者都是 locate mdfind )

    2 回复  |  直到 13 年前
        1
  •  4
  •   janm    15 年前

    您需要将lexer和解析器编译成同一个可执行文件;它们一起工作来创建一个程序。试试这个:

    gcc -o lexer SimpleCalcLexer.c SimpleCalcParser.c -lantlr3c
    

    该命令行将编译lexer和解析器,然后将结果链接到antlr库(“-lantlr3c”部分)。

    对象文件ccn8zvhk.o是运行库的一部分,它实际上是调用main()的。它不包含用户可维修部件。

        2
  •  0
  •   pokstad    15 年前

    如果您多次编译,您会看到对象代码文件名每次都会更改,所以我猜它们是在编译和链接最终目标之前使用的临时对象文件。我遇到了同样的问题,我尝试将架构指定为386和686。我正试图编译这个的输出 Python3 grammar file .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运行时是否与通常的“配置;生成;生成安装”不同?我尝试使用64位选项进行编译,但遇到了同样的问题。