Skip to content

Learning about Compilers Theory Course: Building with ANTLR, LLVM, Bison, and Flex.

License

Notifications You must be signed in to change notification settings

LuisFelipePoma/Compilers_Theory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Compilers


Index

Scaner (Lexical Analizer)

FLEX

  1. Installation

    sudo pacman -S base-devel
    sudo pacman -S flex
  2. Usage

    flex <filename>
    clang <filename>.yy.c

Parser (Syntax Analizer)

BISON

  1. Install

    sudo pacman -S bison
  2. Usage

    bison <filename>
    clang <filename>.tab.c -lm # detect pow -> <math.h>

ANTLR

Install Java

  • Install via CLI

    1. ArchLinux
    • 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
    1. Others
       sudo apt install openjdk-XX-jdk

      Install the latest available version for your distro

  • Install Manually

    1. Download from Oracle

        curl -O https://download.oracle.com/java/20/latest/jdk-20_linux-x64_bin.deb
    2. 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>
    3. OPTIONAL Create a symbolic link

        ln -s /home/user/<directory-app> /opt/java
    4. OPTIONAL Create a Path

        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

        python -m venv /path/to/new/virtual/environment
    • Install Antlr using pip

        python -m pip install antlr4-tools
    • Verify installation

        antlr
  2. 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
  3. 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>
  4. How to use with C++

    Configure Antlr Runtime

    • Dowload antlr4-runtime

      1. Using CLI (Arch Linux)

        sudo pacman -S antlr4-runtime
      2. Manually (Others / Debian)

        • Download antlr4-runtime via Curl

           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

           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

    How to use

    • Use with Clang++ (Working)

      1. First create the files using antlr4 and save them in directory libs

        antlr4 -no-listener -visitor -o libs -Dlanguage=Cpp *.g4
      2. 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 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

        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

        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)

        mkdir build
        cd build
        mkdir release
        mkdir debug
      3. Now create two subfolders release and debug

        mkdir release
        mkdir debug
      4. Now from each of both subfolders, execute the nex command

        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

       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 version

       bash -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

    Using llvm

    • 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  
    • 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...