Orlando, FL
Projects

PL/0 Compiler

August 1, 2022

Source Code

https://github.com/egallardop13/System-Software

Overview

PL/0 compiler built for COP3402 (Systems Software) at UCF. Four-stage pipeline: lexical analysis, parsing, code generation, and VM execution. Written in pure C with no external dependencies. The compiler reads PL/0 source code from a file, tokenizes it, parses it while emitting VM instructions in a single pass, and executes the result on a custom stack-based virtual machine.

Architecture

Lexical Analyzer (lex.c)

Tokenizes PL/0 source into a stream of tokens. Recognizes 13 reserved keywords (begin, end, if, then, else, while, do, call, const, var, procedure, read, write), identifiers, numbers, and operators. Handles block comments (/* ... */), compound assignment operators (+=, -=, *=, /=, %=), and reports 5 distinct error types for malformed input.

Recursive Descent Parser (parser.c)

Performs syntax analysis using a recursive descent approach that maps directly to the PL/0 grammar. Maintains a symbol table with lexicographical scoping to support nested procedure declarations. Reports 19 distinct error messages for syntax violations, covering everything from missing semicolons to undeclared identifiers.

Code Generator (integrated in parser)

One-pass compilation — code is emitted during parsing rather than in a separate pass. Generates a 9-opcode instruction set:
  • LIT — push literal value
  • OPR — arithmetic and relational operations
  • LOD — load variable from stack
  • STO — store value to stack
  • CAL — call procedure
  • INC — allocate stack space
  • JMP — unconditional jump
  • JPC — conditional jump
  • SYS — system calls (read, write, halt)

Stack-Based VM (vm.c)

Executes the generated instruction set on a virtual machine with 3000 words of unified memory (code segment + stack), three registers (PC, BP, SP), and activation records for procedure calls. Each activation record stores the static link, dynamic link, and return address.

Language Features

  • Constants and variables
  • Procedures with nested lexicographical scoping
  • if-then-else and while-do control flow
  • Read/write I/O via system calls
  • Arithmetic with proper operator precedence
  • Compound assignment operators (+=, -=, *=, /=, %=)

Technologies Used

  • C — entire compiler and VM implementation
  • GCC — compilation toolchain
  • Recursive descent parsing — direct grammar-to-code translation
  • Stack-based VM architecture — activation records, unified memory model