Setup Visual Studio Project for Antlr3 C Environment

  1. Build antlr3c lib
    • Visit antlr3c project page and download the latest tarball (It’s 3.4 at the time of writing).
    • Follow Instructions to build the library
  2. Setup your project using antlr3c lib
    • Create a Win32 Console Application project
    • Copy the whole include directory from the antlr3c project you downloaded before to your project folder.(e.g put it under {ProjectRoot}\antlr\include)
    • Copy the antlr3c.lib(in case you build the static library) to your project folder. (e.g put it under {ProjectRoot}\antlr\lib)
    • In visual studio, right click the project, choose Properties. Then go to Configuration Properties->C/C++->General.Set Additional Include Directories to {ProjectRoot}\antlr\include
    • Go to Configuration Properties->Linker->General. Set Additional Library Directories to {ProjectRoot}\antlr\lib
    • Since you need to use java org.antlr.Tool to generate parser from xxx.g grammar files, you need add custom build steps. Visual studio makes use of rule files to define those steps. And fortunately, the antlr3c project you downloaded in the first step includes the rule files dealing with xxx.g. There are four files under its vsrulefiles folder. Those four files are for building lexer, parser, combined lexer parser, tree parser respectively. Add those rule files to the project by right clicking project. Then choose Custom Build Rules…->Find Existing… to add those files.
    • Add xxx.g to the project. Be careful, you need to specify the right rule for xxx.g. e.g. If you apply tree parser rules for lexer grammar file, visual studio would regenerate the lexer files every time even the grammar file itself is unchanged. Also, it seems like Visual studio doesn’t carry over this setting the next time you open your project. So everytime you reopen the project, you need to configure this setting again. A bit annoying.
    • Add generated lexer/parser files to the project. Right click implementation files(.c files). Then go to Configuration Propeties->C/C++->Precompiled Headers, Set Create/Use Precompiled Header to Not Using Precompiled Headers
  3. Add code to drive the parser
    • Go to How to build Generated C code section in this Link. You can use the code snippet there. However, there are two things need to mention:
      • The code snippet hasn’t been updated for antlrC 3.4 yet. In 3.4, antlr38BitFileStreamNew(fName) has been removed, you need to use antlr3FileStreamNew(fName, ANTLR3_ENC_8Bit).
      • That code snippet is based on Lang.g and LangDumpDecl.g grammar files. So the generated structure types are something like pLangLexer, pLangDumpDecl. You are likely to have the different grammar files. Thus you need to change those types accordingly

That’s it. The treeparser project included in the official example is also a good place to get started. Also I create a Sample Project on github. Please type the following on your command line:

Subscribe to 天舟的云游格

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe