https://github.com/amiralv82/bminor-to-c
A minimal translator from the Bminor teaching language to readable C code.
https://github.com/amiralv82/bminor-to-c
c-language compiler compiler-construction compiler-design parser transpiler
Last synced: 8 months ago
JSON representation
A minimal translator from the Bminor teaching language to readable C code.
- Host: GitHub
- URL: https://github.com/amiralv82/bminor-to-c
- Owner: amiralv82
- Created: 2025-06-27T21:25:37.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-27T22:16:01.000Z (about 1 year ago)
- Last Synced: 2025-06-27T22:33:03.100Z (about 1 year ago)
- Topics: c-language, compiler, compiler-construction, compiler-design, parser, transpiler
- Language: C
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bminor → C Translator
A lightweight **command-line compiler front-end** that converts programs written in the _Bminor_ teaching language into readable, vanilla C.
> **Why?**
> Bminor is a tiny C-like language used in compiler courses (notably at the University of Wisconsin).
> Translating Bminor to C lets you:
> 1. run Bminor programs anywhere a C compiler exists, and
> 2. peek under the hood of basic compilation techniques—scanning, parsing, semantic checks, and code generation—without diving into full-blown LLVM.
---
## Key Features
| Capability | Details |
|------------|---------|
| **Type-aware symbol table** | Tracks `integer`, `boolean`, `character`, and `string` identifiers for smarter translation (e.g., correct `printf` format specifiers). |
| **Pretty C output** | Preserves indentation & comments, rewrites keywords (`True`/`False` → `true`/`false`), and converts `^` into `pow()` calls. |
| **Function support** | Parses Bminor `function` blocks (name, parameters, return type) and emits idiomatic C signatures. |
| **Rich `print` handling** | Builds a single `printf()` call with auto-generated format strings, handling literals & mixed arguments. |
| **Control flow** | Translates `if`, `while`, `return`, array declarations, and assignment statements. |
| **Zero external deps** | Pure C99—compiles with `gcc`, `clang`, or MSVC. |
---
## Build
```bash
# Clone
git clone https://github.com//bminor-to-c.git
cd bminor-to-c
# Compile (GCC example)
gcc -std=c99 -Wall -Wextra -o Parser Parser.c
```
---
Usage
```bash
./Parser
# then compile the generated C
gcc output.c -o program
./program
```
Example
```bminor
// fibonacci.bminor
n: integer = 10;
fib: function integer (x: integer) = {
if (x < 2) {
return x;
};
return fib(x - 1) + fib(x - 2);
};
print "fib(", n, ") = ", fib(n);
```
```bash
./Parser fibonacci.bminor fibonacci.c
gcc fibonacci.c -o fib
./fib
# -> fib(10) = 55
```
Generated snippet (truncated):
```c
#include
#include
#include
#include
#include
int n = 10;
int fib(int x) {
if (x < 2) {
return x;
}
return fib(x - 1) + fib(x - 2);
}
int main() {
printf("fib(%d) = %d\n", n, fib(n));
return 0;
}
```
---
Project Structure
```
.
├── Parser.c # Translator source (this repo)
├── examples/ # Sample Bminor programs
└── README.md
```
---
Current Limitations / TODO:
• No full grammar verification—assumes well-formed Bminor input.
• Arrays are treated as int only.
• Functions returning non-primitive types are not yet supported.
• No static or dynamic semantic checks (e.g., undefined variables) beyond a simple symbol table.
• Power operator supports only integer exponentiation via (int)pow(a,b).
---
Author
Amir-Abbas Alvand
Feel free to connect on GitHub or reach out with feedback!