A compiler for the Pico programming language, implementing the front-end phases of compilation: Scanner, Syntax Analyzer, and Semantic Analyzer.
Course: Compiler Construction 1
Date: October 31, 2025
Development Team:
- Muhammad Rouhan (22k-4577)
- Moiz-ul-Haq (22k-4587)
- Huzefa Tayyab
- Sarim Shah
Pico is a strongly-typed, imperative programming language designed as a minimal, Pascal-like dialect. It enforces a clear separation between variable declarations and executable statements and is case-insensitive.
- INTEGER: 32-bit signed integer
- REAL: 32-bit floating-point number
- BOOLEAN: Logical values (TRUE/FALSE)
A Pico program follows this structure:
PROGRAM program_name;
VAR
variable1 : TYPE;
variable2 : TYPE;
BEGIN
statement1;
statement2;
END.
PROGRAM, VAR, BEGIN, END, PROCEDURE, INTEGER, REAL, BOOLEAN, IF, THEN, ELSE, WHILE, DO, TRUE, FALSE, WRITELN, AND, OR, NOT
- Assignment:
:= - Arithmetic:
+,-,*,/ - Comparison:
=,<>,<,>,<=,>= - Logical:
AND,OR,NOT
- Variable declarations in a single VAR block
- Control flow: IF-THEN-ELSE, WHILE-DO
- Procedures: PROCEDURE name; BEGIN ... END;
- Input/Output: WRITELN(expression)
- Comments:
(* ... *)
makeThis will create the executable at bin/pico_compiler.
mkdir build
cd build
cmake ..
makeThe executable will be in the build directory.
g++ -std=c++17 -Wall -Wextra -O2 -Iinclude src/*.cpp -o pico_compiler./bin/pico_compiler <source_file.pico>Or with make:
make run FILE=examples/test.picoPROGRAM example;
VAR
counter : INTEGER;
flag : BOOLEAN;
value : REAL;
BEGIN
counter := 10;
flag := TRUE;
value := 3.14;
IF counter > 5 THEN
WRITELN(counter);
WHILE counter > 0 DO
counter := counter - 1;
END.- Tokenizes the source code
- Handles case-insensitive input
- Generates a stream of tokens for the parser
- Analyzes the token stream against the Context-Free Grammar (CFG)
- Constructs an Abstract Syntax Tree (AST) representing the program structure
- Traverses the AST to ensure semantic correctness
- Performs type checking
- Validates scope (ensuring variables are declared before use)
.
├── include/
│ ├── token.h # Token definitions
│ ├── ast.h # Abstract Syntax Tree nodes
│ ├── scanner.h # Lexical analyzer
│ ├── parser.h # Syntax analyzer
│ └── semantic_analyzer.h # Semantic analyzer
├── src/
│ ├── main.cpp # Main driver program
│ ├── token.cpp # Token utilities
│ ├── scanner.cpp # Scanner implementation
│ ├── parser.cpp # Parser implementation
│ ├── ast.cpp # AST node implementations
│ └── semantic_analyzer.cpp # Semantic analyzer implementation
├── examples/ # Example Pico programs
├── Makefile # Build configuration
├── CMakeLists.txt # CMake build configuration
└── README.md # This file
The compiler reports errors at three levels:
- Lexical Errors: Invalid tokens or characters
- Syntax Errors: Grammar violations
- Semantic Errors: Type mismatches, undeclared variables, etc.
All errors include line and column numbers for easy debugging.
This project is developed for educational purposes as part of the Compiler Construction course.