A compiler implementation for the IFJ23 programming language, developed as part of the Formal Languages and Compilers course at FIT VUT Brno.
This project implements a complete compiler for the IFJ23 language (a simplified Swift-like language) that translates source code into IFJcode23 (a stack-based intermediate representation). The compiler performs lexical analysis, syntactic analysis, semantic analysis, and code generation.
The compiler follows a traditional multi-phase design:
- Lexical Analysis - Tokenizes the input source code
- Syntactic Analysis - Parses tokens using recursive descent and precedence parsing
- Semantic Analysis - Type checking and semantic validation
- Code Generation - Generates IFJcode23 intermediate code
ifj23/
├── src/ # Source code
│ ├── main.c # Main program entry point
│ ├── lexical/ # Lexical analysis
│ │ ├── scanner.c # Lexical analyzer implementation
│ │ └── token_types.c # Token type definitions
│ ├── syntactic/ # Syntactic analysis
│ │ ├── synt_recursive_parser.c # Recursive descent parser
│ │ ├── synt_precedence_parser.c # Precedence parser for expressions
│ │ ├── synt_recur_rules.c # Recursive parser rules
│ │ ├── synt_prec_rules.c # Precedence parser rules
│ │ ├── synt_prec_stack.c # Precedence parser stack
│ │ ├── expr.c # Expression handling
│ │ └── expr_stack.c # Expression stack operations
│ ├── semantic/ # Semantic analysis
│ │ └── semantic_analysis.c # Semantic analyzer
│ ├── codegen/ # Code generation
│ │ └── generator.c # Code generator
│ └── utils/ # Utility modules
│ ├── dynamic_string.c # Dynamic string implementation
│ ├── substring.c # Substring operations
│ ├── symtable.c # Symbol table
│ ├── symtablestack.c # Symbol table stack
│ ├── function_list.c # Function management
│ ├── istack.c # Integer stack
│ └── global_variables.c # Global variables
├── include/ # Header files
│ ├── scanner.h # Lexical analyzer interface
│ ├── token_types.h # Token type definitions
│ ├── synt_analysis.h # Syntactic analysis interface
│ ├── synt_*.h # Various syntactic analysis headers
│ ├── semantic_analysis.h # Semantic analysis interface
│ ├── generator.h # Code generator interface
│ ├── error_codes.h # Error code definitions
│ ├── macros.h # Common macros
│ └── *.h # Other utility headers
├── tests/ # Unit tests
│ ├── operator_precedence_tests.c
│ ├── synt_prec_stack_test.c
│ └── synt_precedence_parser_test.c
├── tests_code/ # Test Swift programs
│ ├── example1.swift # Basic examples
│ ├── factorial.swift # Factorial implementation
│ ├── builtin.swift # Built-in functions test
│ └── *.swift # Various test cases
├── tests_integration/ # Integration tests
├── docs/ # Documentation
│ ├── dokumentace.pdf # Project documentation
│ ├── LL_table.jpg # LL parsing table
│ └── *.pdf # Additional documentation
├── build/ # Build artifacts (created during compilation)
├── Makefile # Build configuration
└── README.md # This file
- GCC compiler with C11 support
- Make utility
- CUnit library (for running tests)
# Build the main compiler
make
# Build and run tests
make test
# Clean build artifacts
make clean
# Build in debug mode
make debugIf you prefer to build manually:
gcc -std=c11 -Wall -Wextra -pedantic -g \
src/main.c \
src/lexical/*.c \
src/syntactic/*.c \
src/semantic/*.c \
src/codegen/*.c \
src/utils/*.c \
-Iinclude \
-o ifj23_compiler# Compile a Swift program
./ifj23_compiler < input.swift > output.ifjcode23
# Example
./ifj23_compiler < tests_code/factorial.swift > factorial.ifjcode23- Input: IFJ23 source code (Swift-like syntax) via stdin
- Output: IFJcode23 intermediate code via stdout
- Errors: Error messages and codes via stderr
# Run all unit tests
make test
# Run specific test modules
./tests/operator_precedence_tests
./tests/synt_prec_stack_test
./tests/synt_precedence_parser_test# Run integration tests
python3 test_integration.py
# Run specific test cases
./ifj23_compiler < tests_code/factorial.swift
./ifj23_compiler < tests_integration/test_0_builtins.swiftThe IFJ23 compiler supports:
- Data Types: Int, Double, String, Bool, optional types
- Variables: Declaration, assignment, optional binding (
if let) - Functions: Definition, calls, parameters, return values
- Control Structures:
if-else,whileloops - Operators: Arithmetic, comparison, logical, string concatenation
- Built-in Functions:
readString(),readInt(),readDouble(),write(),Int2Double(),Double2Int(),length(),substring() - Comments: Single-line (
//) and multi-line (/* */)
The compiler returns specific exit codes for different error types:
0- Success1- Lexical analysis error2- Syntax analysis error3- Semantic analysis error (undefined function/variable)4- Type compatibility error5- Function parameter error6- Other semantic errors7- Runtime error (division by zero, etc.)99- Internal error
- Michal Balogh (xbalog06) - Team Leader
- Adam Čeněk (xcenek04)
- Tadeas Zobal (xzobal02)
Detailed documentation can be found in the docs/ directory:
dokumentace.pdf- Complete project documentation (Slovak)LL_table.jpg- LL parsing table visualizationlex_and_synt.pdf- Lexical and syntactic analysis details