Simple x86-like Compiler

A compiler written in C that parses arithmetic expressions from a file, generates bytecode, and prints assembly-like instructions.

Compiler Workflow

Project Overview

This project implements a simple compiler that reads arithmetic expressions from a file, such as first = 3; and total = first * second + first * second;. The compiler lexes the input, generates an abstract syntax tree, and produces bytecode instructions to execute the expressions.

Lexing and Parsing

The lexer identifies tokens including numbers, identifiers, operators, and delimiters. The parser builds a syntax tree from the token stream, supporting assignment statements, arithmetic operations, and sequences. This provides a foundation for translating the expressions into executable bytecode.

Code Generation

The compiler translates the syntax tree into bytecode instructions. Instructions include PUSH, READ, WRITE, ADD, SUB, MUL, DIV, and RET. Variables and numbers are handled appropriately, and sequences of expressions are compiled in order. The generated bytecode can then be interpreted or printed as assembly-like instructions.

Assembly Output

Using the included print_asm functionality, the bytecode can be displayed as readable assembly instructions, showing operations like reading variables, writing results, and performing arithmetic operations in a POP-based stack model.