CompilateurC is a C language compiler implemented from scratch for educational and experimental purposes. This project demonstrates core compiler concepts such as lexing, parsing, semantic analysis, IR (Intermediate Representation) generation, and code emission.
- Lexical analysis (lexer/tokenizer for C source files)
- Syntax analysis (parser)
- Semantic analysis
- Code generation (target architecture, e.g., x86, or simple VM bytecode)
- Error checking and diagnostics
- Modular design for easy extension
CompilateurC/
├── src/ # Source code for the compiler
│ ├── lexer.c # Lexical analyzer implementation
│ ├── parser.c # Parser implementation
│ ├── semantic.c # Semantic analysis module
│ ├── codegen.c # Code generation module
│ └── ...
├── include/ # Header files for all modules
├── tests/ # Test cases and sample C files
├── build/ # Build outputs (ignored in version control)
├── Makefile # Build script using make
├── README.md # This file
└── ...
- GCC or Clang (C compiler)
- Make
- (Optional) CMake if you want to use CMake instead
Run the following commands in your terminal:
# Clone the repository
git clone https://github.com/AnyaMeetoo492/CompilateurC.git
cd CompilateurC
# Build using Make
make
# (Optional) To clean build artifacts
make cleanTo compile a C source file:
./bin/compilateurc source_file.cThis will output either an assembly file (e.g., source_file.s) or an executable, depending on your codegen module setup.
Test files are provided in the tests/ directory. To run tests:
make test
# or manually:
./bin/compilateurc tests/sample1.c- Lexical Analysis: Splits the input
.cfile into tokens (keywords, identifiers, literals, symbols). - Parsing: Converts the stream of tokens into an abstract syntax tree (AST) representing program structure.
- Semantic Analysis: Checks for type errors, variable declarations, etc.
- Code Generation: Generates target code (assembly or bytecode).
Each stage is implemented in its own source module (see /src/).
- New language constructs: Update parser and semantic analysis logic in
parser.candsemantic.c. - New backend: Add a new codegen module, e.g.,
codegen_mips.c, and update build scripts. - Tests: Add tests in the
tests/directory.
- If you get compilation errors, ensure your environment meets the prerequisites.
- Use
make cleanto remove old build artifacts before rebuilding. - Check comments in
src/files for implementation details.