-
Installation
sudo pacman -S base-devel sudo pacman -S flex
-
Usage
flex <filename> clang <filename>.yy.c
-
Install
sudo pacman -S bison
-
Usage
bison <filename>
clang <filename>.tab.c -lm # detect pow -> <math.h>
-
Install via CLI
-
Now we are going to install the latest version of JRE
sudo pacman -S jre-openjdk
-
Now we are going to install the latest version of JDK
sudo pacman -S jdk-openjdk
- Others
sudo apt install openjdk-XX-jdk
Install the latest available version for your distro
-
-
Install Manually
-
curl -O https://download.oracle.com/java/20/latest/jdk-20_linux-x64_bin.deb
-
Install JDK
sudo dpkg -i jdk-20_linux-x64_bin.deb
If you have errors, install the missing dependencies with the comand below and install again
sudo apt install -y <lib-name>
-
OPTIONAL
Create a symbolic linkln -s /home/user/<directory-app> /opt/java
-
OPTIONAL
Create a PathJAVA_HOME=/opt/java CLASSPATH=$JAVA_HOME/lib PATH=$JAVA_HOME/bin:$PATH export JAVA_HOME export CLASSPATH
-
-
Installation
-
Create a virtual enviroment
python -m venv /path/to/new/virtual/environment
-
Install Antlr using pip
python -m pip install antlr4-tools
-
Verify installation
antlr
-
-
Usage
-
Show the parser with a tree in terminal
antlr4-parse Expr.g4 prog -tree
-
Show the parser with tokens in terminal
antlr4-parse Expr.g4 prog -tokens -trace
-
Show the parser with a GUI in a new window
antlr4-parse Expr.g4 prog -gui
-
-
Compilation
-
Using
antlr4
as a command, we generate the next files:~ antlr4 Expr.g4 ~ ls Expr*.java ExprBaseListener.java ExprLexer.java ExprListener.java 1ExprParser.java
We can also generate the C++ code.
~ antlr4 -Dlanguage=Cpp Expr.g4 ~ ls Expr*.cpp Expr*.h ExprBaseListener.cpp ExprLexer.cpp ExprListener.cpp ExprParser.cpp ExprBaseListener.h ExprLexer.h ExprListener.h ExprParser.h
-
Now to compile with two files ExprLexer and ExprParser
-
How to be
setup.sh
#!/usr/bin/sh setup(){ local venvpath="$HOME/path/to/env" source "${venvpath}/bin/activate" export CLASSPATH=.:~/.m2/repository/org/antlr/antlr4/4.13.1/antlr4-4.13.1-complete.jar:$CLASSPATH alias grun='java org.antlr.v4.gui.TestRig' } setup
-
-
Now we compile boths files.
antlr4 -no-listener -visitor -o <folderName> Expr*.g4
-
With
javac
we compile the java codes generated.javac *.java
-
Execute the script
./setup.sh
-
Finally run Antlr with
grun
grun <GrammarName> <RuleStart> -<param>
-
-
How to use with C++
-
Dowload antlr4-runtime
-
Using CLI (Arch Linux)
sudo pacman -S antlr4-runtime
-
Manually (Others / Debian)
-
Download
antlr4-runtime
via Curlcurl https://www.antlr.org/download/antlr4-cpp-runtime-4.13.1-source.zip -o antlr4-runtime.zip
-
Now install the necesary dependencies and libraries
sudo apt install cmake sudo apt install uuid-dev sudo apt install pkg-config
-
Now we compile and get the libraries from antlr4-runtime
mkdir build && mkdir run && cd build cmake .. DESTDIR=../run make install
-
Now copy the ANTLR 4 include files to
/usr/local/include
and the ANTLR 4 libraries to/usr/local/lib
cd ../run/usr/local/include ln -s ~/Apps/antlr4-cpp/run/usr/local/include/antlr4-runtime /usr/local/include cd ../lib sudo cp * /usr/local/lib sudo ldconfig
-
Now
antlr4-runtime
is installed
-
-
-
Use with Clang++ (Working)
-
First create the files using
antlr4
and save them in directorylibs
antlr4 -no-listener -visitor -o libs -Dlanguage=Cpp *.g4
-
And now compile with the next command
clang++ -std=c++17 -I/usr/local/include/antlr4-runtime -Ilibs libs/*.cpp -lantlr4-runtime *cpp
Use
-std=c++17
in case not detect std librariesUse
-I/usr/local/include/antlr4-runtime
ifantlr4-runtime
was installed manuallyUse
-I/usr/include/antlr4-runtime
ifantlr4-runtime
was installed with package managerUse
-I$ANTLR4I -L$ANTLR4L
if update the filesetup.sh
, exporting the paths of antlr4export ANTLR4I=/usr/local/include/antlr4-runtime export ANTLR4L=/usr/local/lib/antlr4-runtime
-
-
Use with Cmake file (Not Working Yet)
-
Create the file
CMakeLists.txt
with the next configurationscmake_minimum_required(VERSION 3.20) project(antlr) add_executable(antlr main.cpp) target_link_libraries(antlr path/to/antlr4-runtime)
-
Configuration for Cmake (create folder
build
)mkdir build cd build mkdir release mkdir debug
-
Now create two subfolders
release
anddebug
mkdir release mkdir debug
-
Now from each of both subfolders, execute the nex command
cmake -DCMAKE_BUILD_TYPE=Debug ../.. # For debug cmake ../.. # For release
-
Now execute the program with
make
-
-
-
How to use LLVM
-
Using CLI in Arch
sudo pacman llvm
-
Using CLI in Debian
sudo apt-get install llvm-15 llvm-15-dev llvm-15-doc llvm-15-linker-tools llvm-15-runtime llvm-15-tools
-
Installing manually
git clone --depth 1 https://github.com/llvm/llvm-project.git
cd llvm-project
cmake -S llvm -B build -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang
cmake --build build -j4
For install like in the libraries of the system
cmake --install build --prefix /usr/local/include
-
Installing a
X
versionbash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
To install a specific version of LLVM:
wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh sudo ./llvm.sh <version number>
To install all apt.llvm.org packages at once:
wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh sudo ./llvm.sh <version number> all # or sudo ./llvm.sh all
-
Generate Code to Dot Language
- Generate Intermediate code with clang.
clang -S -emit-llvm file.c
- Use opt for optimization
opt --dot-cfg file.ll -enable-new-pm=0
- Use dot
dot -Tpng .prefix_sum.dot -o ps.png
- Generate the output with llc (Middle End)
llc file.ll -march=arm -o file.arm
- Generate Intermediate code with clang.
-
Compiling a llvm project
-
For Arch
clang++ -lLLVM <file>.cpp
-
For Debian
clang++ <file>.cpp $(llvm-config-15 --cxxflags) -lLLVM-15
-
With Cmake
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=1
cmake --build build
Execute the binary file in
./build/
build/prog
-
-
Optimizing a llvm project
- For size of the output
clang++ -O0 <filename>.cpp args...
- For eficc of the output
clang++ -O3 <filename>.cpp args...
- For eficc2 of the output
clang++ -Ofast <filename>.cpp args...
- For size of the output
-