EM
Source Code
Overview
Architecture
Lexical Analyzer (lex.c)
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)
Code Generator (integrated in parser)
- 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)
Language Features
- Constants and variables
- Procedures with nested lexicographical scoping
if-then-elseandwhile-docontrol 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