Compiler Construction · Lex & Yacc · Mar 4, 2022
ExpL: Experimental Language Compiler
A complete multi-phase compiler for an object-oriented procedural language built in C using Lex and Yacc, spanning lexical analysis, AST generation, static type/scope checking, register allocation, and XSM assembly codegen.
Problem Statement
Designing a language compiler requires translating expressive high-level constructs (recursion, user-defined types, dynamic heap allocation, and classes with subtype polymorphism) into deterministic low-level register machine code.
Architecture & Design Decisions
Six-stage compilation pipeline in C: Flex tokenizer -> Bison parser constructing typed ASTs -> Global/Local Symbol Tables & Type Table -> Recursive XSM Assembly Generator -> Two-pass Label Translator producing ABI-compliant binaries.
Overview
ExpL (Experimental Language) is a multi-phase cross-compiler built in C with Lex and Yacc at NIT Calicut. It compiles a statically typed procedural and object-oriented language directly into executable binaries targeting the eXpOS / XSM ABI.
1. Lexical Analysis
Flex Token Stream
2. Syntax & AST
Bison Grammar Rules
3. Semantic Checks
Symbol & Type Tables
4. Codegen & Labels
XSM Assembly → Binary
Spiral Implementation Milestones
- Expression & Control-Flow Compiler: Built the lexer, grammar, AST evaluator, static variable binding, conditionals (
if/else), and loops (while,break,continue). - Subroutines & Activation Records: Designed global and local symbol tables, calling conventions, parameter passing on the runtime stack, and return value registers to enable recursion.
- User-Defined Types & Dynamic Allocation: Added composite struct-like types and heap allocation backed by OS library calls (
Alloc,Free,Initialize). - Classes & Subtype Polymorphism: Extended the type table with class definitions, field/method resolution, and virtual function dispatch tables.
- Two-Pass Label Translation: Separated symbolic jump labels during codegen from logical address resolution in a dedicated second pass, keeping the generator clean and modular.
Engineering Takeaways
Writing a compiler sharpens how you design APIs, DSLs, and static analysis tools. Every compiler pass is essentially a well-typed data transformation pipeline with strict invariants between stages.
Engineering Challenges
- Managing nested symbol tables, function activation records, and stack frame offsets cleanly for recursive subroutines.
- Implementing virtual function tables (VFTs) and type checking for user-defined types and class inheritance.
- Register allocation across deeply nested expressions within a constrained hardware register file.
What I Learned
- How static type systems catch entire classes of bugs before runtime, and how compiler IRs decouple syntax from target hardware.
- Practical mastery of recursive data structures, memory management in C, and incremental spiral development.