{"id":23226775,"url":"https://github.com/luisfelipepoma/compilers_theory","last_synced_at":"2025-08-19T13:33:10.982Z","repository":{"id":189869240,"uuid":"680911674","full_name":"LuisFelipePoma/Compilers_Theory","owner":"LuisFelipePoma","description":"Learning about Compilers Theory Course: Building with ANTLR, LLVM, Bison, and Flex.","archived":false,"fork":false,"pushed_at":"2024-01-07T00:17:01.000Z","size":8310,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-01-07T01:54:32.247Z","etag":null,"topics":["antlr","arch-linux","bison","c","compiler-construction","compilers","flex","llvm"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LuisFelipePoma.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-08-20T19:53:22.000Z","updated_at":"2024-01-07T01:54:33.837Z","dependencies_parsed_at":"2023-09-27T09:52:24.683Z","dependency_job_id":"ac5e5841-803f-4851-885b-149262a51baa","html_url":"https://github.com/LuisFelipePoma/Compilers_Theory","commit_stats":null,"previous_names":["luisfelipepoma/compilers","luisfelipepoma/compilers_theory"],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LuisFelipePoma%2FCompilers_Theory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LuisFelipePoma%2FCompilers_Theory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LuisFelipePoma%2FCompilers_Theory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LuisFelipePoma%2FCompilers_Theory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LuisFelipePoma","download_url":"https://codeload.github.com/LuisFelipePoma/Compilers_Theory/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230356003,"owners_count":18213567,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["antlr","arch-linux","bison","c","compiler-construction","compilers","flex","llvm"],"created_at":"2024-12-19T00:19:24.992Z","updated_at":"2024-12-19T00:19:25.832Z","avatar_url":"https://github.com/LuisFelipePoma.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Compilers\r\n---\r\n## Index\r\n- [Compilers](#compilers)\r\n\t- [Index](#index)\r\n\t- [Scaner (Lexical Analizer)](#scaner-lexical-analizer)\r\n\t\t- [FLEX](#flex)\r\n\t- [Parser (Syntax Analizer)](#parser-syntax-analizer)\r\n\t\t- [BISON](#bison)\r\n\t\t- [ANTLR](#antlr)\r\n\t\t\t- [Install Java](#install-java)\r\n\t\t\t- [Install Antlr](#install-antlr)\r\n## Scaner (Lexical Analizer)\r\n### FLEX\r\n\r\n1. Installation\r\n\t```bash\r\n\tsudo pacman -S base-devel\r\n\tsudo pacman -S flex\r\n\t```\r\n \r\n2. Usage\r\n\t```bash\r\n\tflex \u003cfilename\u003e\r\n\tclang \u003cfilename\u003e.yy.c\r\n\t```\r\n \r\n---\r\n## Parser (Syntax Analizer)\r\n### BISON\r\n1. Install\r\n\t```bash\r\n\tsudo pacman -S bison\r\n\t```\r\n\r\n3. Usage\r\n\t```bash\r\n\tbison \u003cfilename\u003e\r\n\t```\r\n\t```bash\r\n\tclang \u003cfilename\u003e.tab.c -lm # detect pow -\u003e \u003cmath.h\u003e\r\n\t```\r\n\r\n### ANTLR\r\n#### Install Java\r\n\r\n- Install via CLI\r\n\r\n  1. [ArchLinux](https://www.tecmint.com/install-java-on-arch-linux/)\r\n   \r\n\t* Now we are going to install the latest version of JRE\r\n\t\t```bash\r\n\t\tsudo pacman -S jre-openjdk\r\n\t\t```\r\n\r\n\t* Now we are going to install the latest version of JDK\r\n\t\t```bash\r\n\t\tsudo pacman -S jdk-openjdk\r\n\t\t```\r\n   2. Others\r\n\t\t```bash\r\n\t\tsudo apt install openjdk-XX-jdk\r\n\t\t```\r\n\t\t\u003eInstall the latest available version for your distro\r\n\r\n- Install Manually\r\n  \r\n  1. [Download from Oracle](https://www.oracle.com/java/technologies/downloads/)\r\n     \t```bash\r\n\t\tcurl -O https://download.oracle.com/java/20/latest/jdk-20_linux-x64_bin.deb\r\n\t\t```\r\n  2. Install JDK\r\n\t\t```bash\r\n\t\tsudo dpkg -i jdk-20_linux-x64_bin.deb\r\n\t\t```\r\n\t\t\u003e If you have errors, install the missing dependencies with the comand below and install again\r\n\t\t```bash\r\n\t\tsudo apt install -y \u003clib-name\u003e\r\n\t\t```\r\n\t\t\r\n  3. `OPTIONAL` Create a symbolic link \r\n\t\t```bash\r\n\t\tln -s /home/user/\u003cdirectory-app\u003e /opt/java\r\n\t\t```\r\n  \r\n  4. `OPTIONAL` Create a Path\r\n\t  ```bash\r\n\t\tJAVA_HOME=/opt/java\r\n\t\tCLASSPATH=$JAVA_HOME/lib\r\n\t\tPATH=$JAVA_HOME/bin:$PATH\r\n\t\texport JAVA_HOME\r\n\t\texport CLASSPATH\r\n\t  ```\r\n\r\n#### Install Antlr\r\n1. Installation\r\n   * Create a virtual enviroment\r\n\t\t```bash\r\n\t\tpython -m venv /path/to/new/virtual/environment\r\n\t\t```\r\n   \r\n   * Install Antlr using pip\r\n\t\t```bash\r\n\t\tpython -m pip install antlr4-tools\r\n\t\t```\r\n   \r\n   * Verify installation\r\n\t\t```bash\r\n\t\tantlr\r\n\t\t```\r\n   \r\n2. Usage\r\n\t- Show the parser with a **tree** in terminal\r\n\t\t```bash\r\n\t\tantlr4-parse Expr.g4 prog -tree\r\n\t\t```\r\n\r\n\t- Show the parser with **tokens** in terminal\r\n\t\t```bash\r\n\t\tantlr4-parse Expr.g4 prog -tokens -trace\r\n\t\t```\r\n  \r\n\t- Show the parser with a **GUI** in a new window\r\n\t\t```bash\r\n\t\tantlr4-parse Expr.g4 prog -gui\r\n\t\t```\r\n  \r\n3. Compilation\r\n   - Using `antlr4` as a command, we generate the next files:\r\n\t\t```bash\r\n\t\t~ antlr4 Expr.g4 \r\n\t\t~ ls Expr*.java\r\n\r\n\t\tExprBaseListener.java  \t \t\tExprLexer.java    \r\n\t\tExprListener.java        \t\t1ExprParser.java\r\n\t\t```\r\n   \r\n\t\t\u003e We can also generate the C++ code.\r\n\r\n\t\t```bash\r\n\t\t~ antlr4 -Dlanguage=Cpp Expr.g4\r\n\t\t~ ls Expr*.cpp Expr*.h\r\n\r\n\t\tExprBaseListener.cpp  ExprLexer.cpp         ExprListener.cpp      ExprParser.cpp\r\n\t\tExprBaseListener.h    ExprLexer.h           ExprListener.h        ExprParser.h\r\n\t\t```\r\n\r\n   - Now to compile with two files **ExprLexer** and **ExprParser**\r\n\r\n     * How to be `setup.sh`\r\n\r\n\t\t```sh\r\n\t\t#!/usr/bin/sh\r\n\r\n\t\tsetup(){\r\n\t\t\tlocal venvpath=\"$HOME/path/to/env\"\r\n\r\n\t\t\tsource \"${venvpath}/bin/activate\"\r\n\t\t\t\r\n\t\t\texport CLASSPATH=.:~/.m2/repository/org/antlr/antlr4/4.13.1/antlr4-4.13.1-complete.jar:$CLASSPATH\r\n\t\t\talias grun='java org.antlr.v4.gui.TestRig'\r\n\t\t}\r\n\r\n\t\tsetup\r\n\t\t```\r\n\t* Now we compile boths files.\r\n\t\t```bash\r\n\t\tantlr4 -no-listener -visitor -o \u003cfolderName\u003e Expr*.g4\r\n\t\t```\r\n\t* With `javac` we compile the java codes generated.\r\n\t\t```bash\r\n\t\tjavac *.java\r\n\t\t```\r\n\t* Execute the script\r\n\t\t```bash\r\n\t\t./setup.sh\r\n\t\t```\r\n\r\n     * Finally run Antlr with `grun`\r\n\t\t```bash\r\n\t\tgrun \u003cGrammarName\u003e \u003cRuleStart\u003e -\u003cparam\u003e\r\n\t\t```\r\n\r\n4. How to use with C++\r\n\r\n\t### Configure Antlr Runtime\r\n\r\n\t- Dowload antlr4-runtime \r\n  \r\n    \t1. Using CLI (Arch Linux)\r\n   \r\n\t\t\t```bash\r\n\t\t\tsudo pacman -S antlr4-runtime\r\n\t\t\t```\r\n\r\n\t\t2. Manually (Others / Debian)\r\n\t\t\r\n\t\t\t- Download `antlr4-runtime` via Curl\r\n\r\n\t\t\t\t```bash\r\n\t\t\t\tcurl https://www.antlr.org/download/antlr4-cpp-runtime-4.13.1-source.zip -o antlr4-runtime.zip  \r\n\t\t\t\t```\r\n\r\n\t\t\t- Now install the necesary dependencies and libraries\r\n\r\n\t\t\t\t```bash\r\n\t\t\t\tsudo apt install cmake\r\n\t\t\t\tsudo apt install uuid-dev\r\n\t\t\t\tsudo apt install pkg-config \r\n\t\t\t\t```\r\n\r\n\t\t\t- Now we compile and get the libraries from antlr4-runtime\r\n\r\n\t\t\t\t```bash\r\n\t\t\t\tmkdir build \u0026\u0026 mkdir run \u0026\u0026 cd build\r\n\t\t\t\tcmake ..\r\n\t\t\t\tDESTDIR=../run make install\r\n\t\t\t\t```\r\n\r\n\t\t\t- Now copy the ANTLR 4 include files to `/usr/local/include` and the ANTLR 4 libraries to `/usr/local/lib`\r\n\r\n\t\t\t\t```bash\r\n\t\t\t\tcd ../run/usr/local/include\r\n\t\t\t\tln -s ~/Apps/antlr4-cpp/run/usr/local/include/antlr4-runtime /usr/local/include\r\n\t\t\t\tcd ../lib\r\n\t\t\t\tsudo cp * /usr/local/lib\r\n\t\t\t\tsudo ldconfig\r\n\t\t\t\t```\r\n\t\t\t- Now `antlr4-runtime` is installed\r\n\r\n\t### How to use\r\n\t- Use with Clang++ (Working)\r\n\t\t1. First create the files using `antlr4` and save them in directory `libs`\r\n\t\t\t```bash\r\n\t\t\tantlr4 -no-listener -visitor -o libs -Dlanguage=Cpp *.g4\r\n\t\t\t```\r\n\r\n\t\t2. And now compile with the next command\r\n\t\t\t```bash\r\n\t\t\tclang++ -std=c++17 -I/usr/local/include/antlr4-runtime -Ilibs libs/*.cpp -lantlr4-runtime *cpp\r\n\t\t\t```\r\n\t\t\t\u003e Use `-std=c++17` in case not detect std libraries\r\n\r\n\t\t\t\u003e Use `-I/usr/local/include/antlr4-runtime` if `antlr4-runtime` was installed manually\r\n\r\n\t\t\t\u003e Use `-I/usr/include/antlr4-runtime` if `antlr4-runtime` was installed with package manager\r\n\r\n\t\t\t\u003e Use `-I$ANTLR4I -L$ANTLR4L` if update the file `setup.sh`, exporting the paths of antlr4\r\n\t\t\t```sh\r\n\t\t\texport ANTLR4I=/usr/local/include/antlr4-runtime\r\n\t\t\texport ANTLR4L=/usr/local/lib/antlr4-runtime\r\n\t\t\t```\r\n\r\n\t- Use with Cmake file (Not Working Yet)\r\n\r\n        1. Create the file `CMakeLists.txt` with the next configurations\r\n    \t\t```txt\r\n    \t\tcmake_minimum_required(VERSION 3.20)\r\n    \t\tproject(antlr)\r\n    \t\tadd_executable(antlr main.cpp)\r\n    \t\ttarget_link_libraries(antlr path/to/antlr4-runtime)\r\n    \t\t```\r\n\r\n        2. Configuration for Cmake (create folder `build`)\r\n           ```bash\r\n           mkdir build\r\n           cd build\r\n           mkdir release\r\n           mkdir debug\r\n           ```\r\n\r\n        3. Now create two subfolders `release` and `debug`\r\n           ```bash\r\n           mkdir release\r\n           mkdir debug\r\n           ```\r\n\r\n        4. Now from each of both subfolders, execute the nex command\r\n           ```bash\r\n           cmake -DCMAKE_BUILD_TYPE=Debug ../.. # For debug\r\n           cmake ../.. # For release\r\n           ```\r\n\r\n        5. Now execute the program with `make`\r\n5. How to use LLVM\r\n    ### Installing\r\n\r\n\t- Using CLI in Arch\r\n\t\t```bash\r\n\t\tsudo pacman llvm\r\n\t\t```\r\n\r\n\t- Using CLI in Debian\r\n\t\t```bash\r\n\t\tsudo apt-get install llvm-15 llvm-15-dev llvm-15-doc llvm-15-linker-tools llvm-15-runtime llvm-15-tools\r\n\t\t```\r\n\t\r\n\t- Installing manually\r\n\t\t```bash\r\n\t\tgit clone --depth 1 https://github.com/llvm/llvm-project.git\r\n\t\t```\r\n\t\t```bash\r\n\t\tcd llvm-project\r\n\t\t```\r\n\t\t```bash\r\n\t\tcmake -S llvm -B build -G \"Unix Makefiles\" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang\r\n\t\t```\r\n\t\t```bash\r\n\t\tcmake --build build -j4\r\n\t\t```\r\n\t\t\u003e For install like in the libraries of the system\r\n\t\t```bash\r\n\t\tcmake --install build --prefix /usr/local/include\r\n\t\t```\r\n\r\n\t- Installing a `X` version\r\n    \t```bash\r\n\t\tbash -c \"$(wget -O - https://apt.llvm.org/llvm.sh)\"\r\n\t\t```\r\n\t\t\u003e To install a specific version of LLVM:\r\n    \t```bash\r\n\t\twget https://apt.llvm.org/llvm.sh\r\n\t\tchmod +x llvm.sh\r\n\t\tsudo ./llvm.sh \u003cversion number\u003e\r\n\t\t```\r\n\t\r\n\t\t\u003e To install all apt.llvm.org packages at once:\r\n\r\n\t\t```bash\r\n\t\twget https://apt.llvm.org/llvm.sh\r\n\t\tchmod +x llvm.sh\r\n\t\tsudo ./llvm.sh \u003cversion number\u003e all\r\n\t\t# or\r\n\t\tsudo ./llvm.sh all\r\n\t\t```\r\n\t\t\r\n\t### Using llvm\r\n\t\r\n    - Generate Code to Dot Language\r\n\t\t- Generate Intermediate code with clang.\r\n\t\t\t```bash\r\n\t\t\tclang -S -emit-llvm file.c  \r\n\t\t\t```\r\n\t\t- Use opt for optimization\r\n\t\t\t```bash\r\n\t\t\topt --dot-cfg file.ll -enable-new-pm=0\r\n\t\t\t```\r\n\t\t- Use dot\r\n\t\t\t```bash\r\n\t\t\tdot -Tpng .prefix_sum.dot -o ps.png   \r\n\t\t\t```\r\n\t\t- Generate the output with llc (Middle End)\r\n\t\t\t```bash\r\n\t\t\tllc file.ll -march=arm -o file.arm  \r\n\t\t\t```\r\n\t\t\t\r\n\t- Compiling a llvm project\r\n    \t- For Arch\r\n\t\t\t```bash\r\n\t\t\tclang++ -lLLVM \u003cfile\u003e.cpp\r\n\t\t\t```\r\n\t\t\t\r\n    \t- For Debian\r\n\t\t\t```bash\r\n\t\t\tclang++ \u003cfile\u003e.cpp $(llvm-config-15 --cxxflags) -lLLVM-15\r\n\t\t\t```\r\n\t\t- With Cmake\r\n\t\t\t```bash\r\n\t\t\tcmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=1\r\n\t\t\t```\r\n\t\t\t```bash\r\n\t\t\tcmake --build build\r\n\t\t\t```\r\n\t\t\t\u003e Execute the binary file in `./build/`\r\n\t\t\t```bash\r\n\t\t\tbuild/prog\r\n\t\t\t```\r\n\r\n\t- Optimizing a llvm project\r\n    \t- For size of the output\r\n\t\t\t```bash\r\n\t\t\tclang++ -O0 \u003cfilename\u003e.cpp args...\r\n\t\t\t```\r\n    \t- For eficc of the output\r\n\t\t\t```bash\r\n\t\t\tclang++ -O3 \u003cfilename\u003e.cpp args...\r\n\t\t\t```\r\n    \t- For eficc2 of the output\r\n\t\t\t```bash\r\n\t\t\tclang++ -Ofast \u003cfilename\u003e.cpp args...\r\n\t\t\t```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluisfelipepoma%2Fcompilers_theory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluisfelipepoma%2Fcompilers_theory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluisfelipepoma%2Fcompilers_theory/lists"}