A complete Pascal compiler that translates Pascal source code to executable x86-64 binaries.
- Full Pascal Grammar Support: Variables, constants, expressions, control structures
- Modular Architecture: Clean separation of compiler phases
- P-code Intermediate Representation: Stack-based intermediate code
- x86-64 Assembly Generation: Direct translation to native assembly
- Automatic Linking: Generates executable binaries
- Error Reporting: Comprehensive lexical, syntactic, and semantic error detection
.
├── src/
│ ├── main.c # Main compiler driver
│ ├── lexer/
│ │ └── lexer.c # Lexical analysis
│ ├── parser/
│ │ └── parser.c # Syntax analysis & parsing
│ ├── semantic/
│ │ └── semantic.c # Semantic analysis
│ ├── codegen/
│ │ └── codegen.c # P-code generation
│ ├── backend/
│ │ ├── x86_translator.c # x86 assembly translation
│ │ └── linker.c # Assembly and linking
│ └── utils/
│ ├── errors.c # Error handling
│ └── globals.c # Global variables and constants
├── include/ # Header files
├── tests/ # Pascal test programs
├── build/ # Build artifacts
└── docs/ # Documentation
- Lexical Analysis - Tokenizes Pascal source code
- Syntax Analysis - Parses tokens into abstract syntax tree
- Semantic Analysis - Type checking and symbol table management
- P-code Generation - Stack-based intermediate code generation
- Assembly Translation - Converts P-code to x86-64 assembly
- Assembly & Linking - Generates executable binary
make # Build compiler
make clean # Clean build files
make test # Run test suite
make install # Install to system (requires sudo)mkdir build && cd build
cmake ..
make./compila program.p # Compile Pascal program
./program # Run generated executable./compila [options] <pascal_file>
Options:
-o <output> Specify output executable name (default: program)
-s Keep assembly file (.s)
-p Keep P-code file (.pcode)
-r Run executable after compilation
-h Show help# Compile and run
./compila -r tests/test
# Compile with custom output name
./compila -o calculator calculator.p
# Keep intermediate files for debugging
./compila -s -p program.p- Program structure with
programkeyword - Variable declarations (
var) - Constant declarations (
const) - Integer arithmetic (+, -, *, /)
- Relational operators (<, >, <=, >=, =, <>)
- Control structures:
if-then-elsewhile-dorepeat-untilforloopscasestatements
- I/O operations (
read,write) - Block structure with
begin-end
program operations;
var num1, num2, result;
begin
read(num1);
read(num2);
result := num1 + num2;
write(result);
result := num1 * num2;
write(result);
end.The tests/ directory contains various Pascal programs for testing:
test- Basic arithmetic operationstest1- Conditional statementstest2- Variable declarationstest3- Control flowtest4- Expression evaluationtest5- Complex operations
Run all tests:
make testThe compiler generates stack-based P-code instructions:
LDA- Load AddressLDI- Load ImmediateLDV- Load ValueSTO- StoreADD, SUB, MUL, DIV- ArithmeticEQL, NEQ, GTR, LSS, GEQ, LEQ- ComparisonsBZE, BRN- BranchingINN, PRN- Input/OutputINT- Initialize stack spaceHLT- Halt
- Uses System V ABI calling convention
- Stack-based evaluation model
- Direct register allocation for operations
- Links with C runtime for I/O functions
- GCC (for linking)
- GNU Assembler (
as) - C standard library
The compiler provides comprehensive error reporting:
- Lexical Errors: Invalid characters, malformed tokens
- Syntax Errors: Missing keywords, incorrect structure
- Semantic Errors: Undefined variables, type mismatches
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new features
- Submit a pull request
This project is open source. See LICENSE file for details.
Pascal Compiler Implementation - Educational compiler project demonstrating complete compilation pipeline from Pascal source to x86-64 executable.