Interpreter in C, implementing a subset of C instructions
Mini-C is a lightweight C interpreter that executes a subset of the C programming language. It parses and interprets C source code at runtime, making it useful for educational purposes, scripting, and rapid prototyping without the need for compilation.
The interpreter uses a recursive descent parser to analyze expressions and implements a virtual machine-like execution model with support for function calls, local/global variables, and control flow structures.
- Lexical Analyzer: Tokenizes the source code into identifiers, keywords, operators, and literals
- Parser: Builds an abstract representation of the code structure using recursive descent parsing
- Prescan Phase: Identifies all function definitions and global variables before execution
- Interpreter: Executes the parsed code using a stack-based approach for function calls and local variables
- Expression Evaluator: Handles arithmetic, relational, and logical expressions with proper operator precedence
Source Code → Tokenization → Parsing → Prescan → Interpretation → Output
- Global Variables: Stored in a fixed-size array accessible throughout the program
- Local Variables: Managed using a stack that grows/shrinks with function calls
- Function Table: Maps function names to their entry points in the source code
- Call Stack: Tracks nested function calls and maintains return addresses
minic <filename.c>The interpreter will load and execute the specified C source file, starting from the main() function.
int main() {
print("Hello, Mini-C!");
return 0;
}int factorial(int n) {
if(n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int result;
result = factorial(5);
print(result);
return 0;
}int fib(int n) {
int a, b, temp, i;
a = 0;
b = 1;
if(n <= 1) {
return n;
}
for(i = 2; i <= n; i = i + 1) {
temp = a + b;
a = b;
b = temp;
}
return b;
}
int main() {
int i;
for(i = 0; i < 10; i = i + 1) {
print(fib(i));
}
return 0;
}int sum;
int calculate_sum(int count) {
int i;
sum = 0;
for(i = 1; i <= count; i = i + 1) {
sum = sum + i;
}
return sum;
}
int main() {
int result;
result = calculate_sum(10);
print("Sum of 1 to 10: ");
print(result);
return 0;
}int main() {
int i, even, odd;
even = 0;
odd = 0;
for(i = 1; i <= 20; i = i + 1) {
if(i % 2 == 0) {
even = even + 1;
}
else {
odd = odd + 1;
}
}
print("Even numbers: ");
print(even);
print("Odd numbers: ");
print(odd);
return 0;
}print(value)- Output a value to the consoleputs(string)- Output a string followed by newlinegetnum()- Read an integer from keyboardgetche()- Read a character from consoleputch(char)- Output a single character
- Parameterized functions with local variables
- Recursion support
- IF-ELSE statements
- DO-WHILE, WHILE and FOR loops
- Int and char variables
- Global variables
- Int and char constants
- String constants (limited implementation)
- Return statement with or without value
- Limited standard library functions
- Operators: +, -, *, %, <, >, <=, >=, ==, !=, unary - and unary +
- Parentheses support
- Functions returning integers
- C-style comments (/* */)
- Refactor syntax and lexical analyzer
- Additional example programs
- The Switch statement is not implemented
- IF, WHILE, DO and FOR statements must be enclosed in braces
{ } - Arrays are not supported
- Pointers are not supported
- Floating-point numbers are not supported
- Only single-dimensional variable declarations
- Limited string handling capabilities
- Maximum 100 functions
- Maximum 100 global variables
- Maximum 200 local variables per execution context
- Maximum program size: 10,000 characters
- Maximum identifier length: 31 characters
- Maximum 31 nested function calls
The interpreter provides error messages for common syntax errors including:
- Unbalanced parentheses or braces
- Missing expressions or semicolons
- Undefined functions or variables
- Type mismatches
- Excessive local variables or nested calls
Error messages include line numbers to help locate issues in the source code.