Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/luisfelipepoma/compilers_theory
Learning about Compilers Theory Course: Building with ANTLR, LLVM, Bison, and Flex.
https://github.com/luisfelipepoma/compilers_theory
antlr arch-linux bison c compiler-construction compilers flex llvm
Last synced: 3 days ago
JSON representation
Learning about Compilers Theory Course: Building with ANTLR, LLVM, Bison, and Flex.
- Host: GitHub
- URL: https://github.com/luisfelipepoma/compilers_theory
- Owner: LuisFelipePoma
- License: cc0-1.0
- Created: 2023-08-20T19:53:22.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-07T00:17:01.000Z (12 months ago)
- Last Synced: 2024-01-07T01:54:32.247Z (12 months ago)
- Topics: antlr, arch-linux, bison, c, compiler-construction, compilers, flex, llvm
- Language: C++
- Homepage:
- Size: 7.93 MB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Compilers
---
## Index
- [Compilers](#compilers)
- [Index](#index)
- [Scaner (Lexical Analizer)](#scaner-lexical-analizer)
- [FLEX](#flex)
- [Parser (Syntax Analizer)](#parser-syntax-analizer)
- [BISON](#bison)
- [ANTLR](#antlr)
- [Install Java](#install-java)
- [Install Antlr](#install-antlr)
## Scaner (Lexical Analizer)
### FLEX1. Installation
```bash
sudo pacman -S base-devel
sudo pacman -S flex
```
2. Usage
```bash
flex
clang .yy.c
```
---
## Parser (Syntax Analizer)
### BISON
1. Install
```bash
sudo pacman -S bison
```3. Usage
```bash
bison
```
```bash
clang .tab.c -lm # detect pow ->
```### ANTLR
#### Install Java- Install via CLI
1. [ArchLinux](https://www.tecmint.com/install-java-on-arch-linux/)
* Now we are going to install the latest version of JRE
```bash
sudo pacman -S jre-openjdk
```* Now we are going to install the latest version of JDK
```bash
sudo pacman -S jdk-openjdk
```
2. Others
```bash
sudo apt install openjdk-XX-jdk
```
>Install the latest available version for your distro- Install Manually
1. [Download from Oracle](https://www.oracle.com/java/technologies/downloads/)
```bash
curl -O https://download.oracle.com/java/20/latest/jdk-20_linux-x64_bin.deb
```
2. Install JDK
```bash
sudo dpkg -i jdk-20_linux-x64_bin.deb
```
> If you have errors, install the missing dependencies with the comand below and install again
```bash
sudo apt install -y
```
3. `OPTIONAL` Create a symbolic link
```bash
ln -s /home/user/ /opt/java
```
4. `OPTIONAL` Create a Path
```bash
JAVA_HOME=/opt/java
CLASSPATH=$JAVA_HOME/lib
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME
export CLASSPATH
```#### Install Antlr
1. Installation
* Create a virtual enviroment
```bash
python -m venv /path/to/new/virtual/environment
```
* Install Antlr using pip
```bash
python -m pip install antlr4-tools
```
* Verify installation
```bash
antlr
```
2. Usage
- Show the parser with a **tree** in terminal
```bash
antlr4-parse Expr.g4 prog -tree
```- Show the parser with **tokens** in terminal
```bash
antlr4-parse Expr.g4 prog -tokens -trace
```
- Show the parser with a **GUI** in a new window
```bash
antlr4-parse Expr.g4 prog -gui
```
3. Compilation
- Using `antlr4` as a command, we generate the next files:
```bash
~ antlr4 Expr.g4
~ ls Expr*.javaExprBaseListener.java ExprLexer.java
ExprListener.java 1ExprParser.java
```
> We can also generate the C++ code.```bash
~ antlr4 -Dlanguage=Cpp Expr.g4
~ ls Expr*.cpp Expr*.hExprBaseListener.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`
```sh
#!/usr/bin/shsetup(){
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.
```bash
antlr4 -no-listener -visitor -o Expr*.g4
```
* With `javac` we compile the java codes generated.
```bash
javac *.java
```
* Execute the script
```bash
./setup.sh
```* Finally run Antlr with `grun`
```bash
grun -
```4. How to use with C++
### Configure Antlr Runtime
- Dowload antlr4-runtime
1. Using CLI (Arch Linux)
```bash
sudo pacman -S antlr4-runtime
```2. Manually (Others / Debian)
- Download `antlr4-runtime` via Curl```bash
curl https://www.antlr.org/download/antlr4-cpp-runtime-4.13.1-source.zip -o antlr4-runtime.zip
```- Now install the necesary dependencies and libraries
```bash
sudo apt install cmake
sudo apt install uuid-dev
sudo apt install pkg-config
```- Now we compile and get the libraries from antlr4-runtime
```bash
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`
```bash
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### How to use
- Use with Clang++ (Working)
1. First create the files using `antlr4` and save them in directory `libs`
```bash
antlr4 -no-listener -visitor -o libs -Dlanguage=Cpp *.g4
```2. And now compile with the next command
```bash
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 libraries> Use `-I/usr/local/include/antlr4-runtime` if `antlr4-runtime` was installed manually
> Use `-I/usr/include/antlr4-runtime` if `antlr4-runtime` was installed with package manager
> Use `-I$ANTLR4I -L$ANTLR4L` if update the file `setup.sh`, exporting the paths of antlr4
```sh
export ANTLR4I=/usr/local/include/antlr4-runtime
export ANTLR4L=/usr/local/lib/antlr4-runtime
```- Use with Cmake file (Not Working Yet)
1. Create the file `CMakeLists.txt` with the next configurations
```txt
cmake_minimum_required(VERSION 3.20)
project(antlr)
add_executable(antlr main.cpp)
target_link_libraries(antlr path/to/antlr4-runtime)
```2. Configuration for Cmake (create folder `build`)
```bash
mkdir build
cd build
mkdir release
mkdir debug
```3. Now create two subfolders `release` and `debug`
```bash
mkdir release
mkdir debug
```4. Now from each of both subfolders, execute the nex command
```bash
cmake -DCMAKE_BUILD_TYPE=Debug ../.. # For debug
cmake ../.. # For release
```5. Now execute the program with `make`
5. How to use LLVM
### Installing- Using CLI in Arch
```bash
sudo pacman llvm
```- Using CLI in Debian
```bash
sudo apt-get install llvm-15 llvm-15-dev llvm-15-doc llvm-15-linker-tools llvm-15-runtime llvm-15-tools
```
- Installing manually
```bash
git clone --depth 1 https://github.com/llvm/llvm-project.git
```
```bash
cd llvm-project
```
```bash
cmake -S llvm -B build -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang
```
```bash
cmake --build build -j4
```
> For install like in the libraries of the system
```bash
cmake --install build --prefix /usr/local/include
```- Installing a `X` version
```bash
bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
```
> To install a specific version of LLVM:
```bash
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh
```
> To install all apt.llvm.org packages at once:```bash
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh all
# or
sudo ./llvm.sh all
```
### Using llvm
- Generate Code to Dot Language
- Generate Intermediate code with clang.
```bash
clang -S -emit-llvm file.c
```
- Use opt for optimization
```bash
opt --dot-cfg file.ll -enable-new-pm=0
```
- Use dot
```bash
dot -Tpng .prefix_sum.dot -o ps.png
```
- Generate the output with llc (Middle End)
```bash
llc file.ll -march=arm -o file.arm
```
- Compiling a llvm project
- For Arch
```bash
clang++ -lLLVM .cpp
```
- For Debian
```bash
clang++ .cpp $(llvm-config-15 --cxxflags) -lLLVM-15
```
- With Cmake
```bash
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=1
```
```bash
cmake --build build
```
> Execute the binary file in `./build/`
```bash
build/prog
```- Optimizing a llvm project
- For size of the output
```bash
clang++ -O0 .cpp args...
```
- For eficc of the output
```bash
clang++ -O3 .cpp args...
```
- For eficc2 of the output
```bash
clang++ -Ofast .cpp args...
```