Skip to content

gitInfinity/Pico-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pico Compiler

A compiler for the Pico programming language, implementing the front-end phases of compilation: Scanner, Syntax Analyzer, and Semantic Analyzer.

Project Information

Course: Compiler Construction 1
Date: October 31, 2025
Development Team:

  • Muhammad Rouhan (22k-4577)
  • Moiz-ul-Haq (22k-4587)
  • Huzefa Tayyab
  • Sarim Shah

The Pico Language

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.

Data Types

  • INTEGER: 32-bit signed integer
  • REAL: 32-bit floating-point number
  • BOOLEAN: Logical values (TRUE/FALSE)

Program Structure

A Pico program follows this structure:

PROGRAM program_name;
VAR
  variable1 : TYPE;
  variable2 : TYPE;
BEGIN
  statement1;
  statement2;
END.

Keywords

PROGRAM, VAR, BEGIN, END, PROCEDURE, INTEGER, REAL, BOOLEAN, IF, THEN, ELSE, WHILE, DO, TRUE, FALSE, WRITELN, AND, OR, NOT

Operators

  • Assignment: :=
  • Arithmetic: +, -, *, /
  • Comparison: =, <>, <, >, <=, >=
  • Logical: AND, OR, NOT

Features

  • Variable declarations in a single VAR block
  • Control flow: IF-THEN-ELSE, WHILE-DO
  • Procedures: PROCEDURE name; BEGIN ... END;
  • Input/Output: WRITELN(expression)
  • Comments: (* ... *)

Building the Compiler

Using Make

make

This will create the executable at bin/pico_compiler.

Using CMake

mkdir build
cd build
cmake ..
make

The executable will be in the build directory.

Manual Compilation

g++ -std=c++17 -Wall -Wextra -O2 -Iinclude src/*.cpp -o pico_compiler

Usage

./bin/pico_compiler <source_file.pico>

Or with make:

make run FILE=examples/test.pico

Example Pico Program

PROGRAM 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.

Compiler Phases

Phase 1: Scanner (Lexical Analyzer)

  • Tokenizes the source code
  • Handles case-insensitive input
  • Generates a stream of tokens for the parser

Phase 2: Syntax Analyzer (Parser)

  • Analyzes the token stream against the Context-Free Grammar (CFG)
  • Constructs an Abstract Syntax Tree (AST) representing the program structure

Phase 3: Semantic Analyzer

  • Traverses the AST to ensure semantic correctness
  • Performs type checking
  • Validates scope (ensuring variables are declared before use)

Project Structure

.
├── 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

Error Reporting

The compiler reports errors at three levels:

  1. Lexical Errors: Invalid tokens or characters
  2. Syntax Errors: Grammar violations
  3. Semantic Errors: Type mismatches, undeclared variables, etc.

All errors include line and column numbers for easy debugging.

License

This project is developed for educational purposes as part of the Compiler Construction course.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors