/my_compiler

A Python project that takes a grammar I've defined, lexes it, parses it into an abstract syntax tree, and does semantic analysis. To come: generate bytecode and feed it to a stack-based virtual machine. Maybe will throw in some optimization for good measure.

Primary LanguagePythonMIT LicenseMIT

I was able to do this project based on my learnings from the Bradfield CS class on compilers, from Ruslan Spivak's blog series and Allison Kaptur's blogpost about the Bytecode project she contributed to.

A toy compiler, written in Python

  • Lexes
  • Parses (generates abstract syntax tree
  • Does semantic analysis
  • Generates scoped symbol tables
  • Generates assembly-like instructions
  • Runs the assembly-like instructions.

Currently does simple arithmetic, prints values, variable and function declarations, some type checking, scope setting.

Dependencies

None but for Python.

Compile a sample text

You can run the below sample script by cloning this repo, cd'ing into it, and then running python compile_sample_text.py.

Sample text


function foo (p, q, n, m : int) {
    p = p + 5;
    q = q * 10;
    n = n - 4;
    m = m / 3;
}

function bar (r, s, t, u : int) {
    r = r + 5;
    s = s * 10;
    t = t - 4;
    u = u / 3;
}

print (3 + 3) * (3 - 3); # 0```