Download presentation
Presentation is loading. Please wait.
1
CMPE 152: Compiler Design ANTLR 4 and C++
Department of Computer Engineering San Jose State University Spring 2018 Instructor: Ron Mak
2
Download and Install ANTLR 4
ANTLR is a Java program. Current version is 4.7.1 In order to run it, you must download its binary antlr-4.7-complete.jar. Run on the command line: Tip: Create a script or an alias: java -jar /your/path/to/antlr-4.7/antlr-4.7-complete.jar -Dlanguage=Cpp my.g4 export ANTLR_JAR="/your/path/to/antlr-4.7-complete.jar" export CLASSPATH="$CLASSPATH:$ANTLR_JAR" alias antlr4="java -jar $ANTLR_JAR –Dlanguage=Cpp"
3
Download and Install the ANTLR 4 Library
ANTLR reads a .g4 grammar file and generates .h and .cpp files for a parser, lexer (scanner), and parse tree routines. In order to compile and link these generated compiler files, you need the ANTLR 4 header (.h) files and the ANTLR 4 library. Go to Scroll down to “C++ target”
4
The ANTLR 4 Library, cont’d
Mac and Linux Install in the standard locations: Header files: /usr/local/include/antlr4-runtime Library files: /usr/local/lib/libantlr4-runtime.* Windows Remember which directory you put the header files in. Unfortunately, the library files you download from the ANTLR site are for Visual Studio C++. Download non-MSVS library files from Remember which directory you put the library files in.
5
Compile, Link, and Run the Generated Compiler Code
To compile and link the ANTLR-generated code, you must use the 2011 dialect of g++ and tell it to use the ANTLR header files and library: If you did not put the library files in the standard location (or if you’re on Windows), you must add the -L option for the library directory before the -l option: For example, to run the executable expr on the input file t.expr: g++ -std=c++0x -I/usr/local/include/antlr4-runtime -lantlr4-runtime -o expr *.cpp -L/your/path/to/lib ./expr t.expr
6
More Headaches for Windows
When you run the the generated compiler in Windows 10, it wants to use the dynamically linked library cygantlr4-runtime-4.7.dll in the lib directory. Therefore, you must first put the lib directory in your execution path. Run the path command to verify it’s there. You may have to restart Windows. If you don't know how to do this, a backup strategy is to copy the .dll file to the same directory as your compiler executable. You still need the -L and -l options to compile and link (previous slide).
7
Download and Install the Eclipse Plug-in
Download and install the latest version “Oxygen” of Eclipse CDT (C/C++ Development Tooling) from Follow the instructions at under “Eclipse Installation”. Use the “Oxygen” version of Eclipse, not “Luna”. Ignore the software version numbers. Install the latest versions of the software.
8
An ANTLR Eclipse Project
Create a new C++ project. Not an ANTLR 4 project, which is for Java. Copy all the .h and .cpp files that you’ve previously generated by running ANTLR on the command line. Include the .g4 file and any .h and .cpp files you’ve written, and any input files.
9
An ANTLR Eclipse Project, cont’d
Open the project’s Property sheet. C/C++ Built Settings C++ Compiler Dialect Language standard: Choose “ISO C++11” C++ Compiler Includes Include paths (-I): /usr/local/include/antlr4-runtime (or wherever you put the ANTLR header files) Linker Libraries Libraries (-l): antlr4-runtime Library search path (-L): The directory where you put the ANTLR 4 libraries if they’re not in the standard place.
10
An ANTLR Eclipse Project, cont’d
Now you can compile and run as you would any other C++ program. Unfortunately, there’s no way to tell the Eclipse plug-in not to generate Java files from the .g4 grammar file but to generate C++ files instead. The plug-in may insist on generating Java files.
11
An ANTLR Eclipse Project, cont’d
To turn off automatic file generation in Eclipse: Open the properties sheet of the project. Click on ANTLR 4 Tool Uncheck ”Enable project specific settings” Click on the link “Configure Workspace Settings ...” Uncheck “Tool is activated” This means that you must run ANTLR on the command line outside of Eclipse if you want to generate new compiler files. Then copy the new files into your Eclipse project.
12
An ANTLR Eclipse Project, cont’d
To generate a syntax diagram in Eclipse: Eclipse Window menu Other ... Syntax Diagram That creates a Syntax Diagram tab at the bottom
13
An ANTLR Eclipse Project, cont’d
To generate a parse tree in Eclipse: Eclipse Window menu Show View Other ... Parse Tree That creates a Parse Tree tab at the bottom Open your .g4 grammar file in Eclipse. Open the Parse Tree tab. Click the name of the starting rule of your grammar. Copy the text of your source program into the left pane of the Parse Tree tab. Make sure the last line ends in a line feed. The parse tree appears in the right pane.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.