PitchHut
Log in / Sign up
PyTiny-C
14 views
A small Python-based compiler for learning the Tiny-C language.
Pitch

PyTiny-C is a Python implementation of the Tiny-C compiler, designed as a pedagogical tool for understanding compiler construction. With a focus on simplicity, it allows users to experiment with a streamlined version of C syntax, making it ideal for learning and experimentation.

Description

PyTiny-C is an innovative compiler for a minimalistic programming language, Tiny-C, meticulously rewritten in Python to enhance the learning experience about compiler construction. This version closely mirrors its C predecessor, maintaining a one-to-one correlation wherever feasible.

Tiny-C serves as an excellent educational resource, designed specifically to help learners understand the fundamentals of compiler technology. It features predefined global integer variables ranging from 'a' to 'z', all initialized to zero, while limiting the ability to declare new variables, thus simplifying the learning process.

Key Features:

  • Language Structure: Tiny-C is defined with a straightforward yet rigorous syntax that includes a variety of control flow statements such as if, while, and do-while, alongside basic expressions and operations. The language's grammar is succinctly represented in EBNF:
    <program> ::= <statement>
    <statement> ::= "if" <paren_expr> <statement> |
                    "if" <paren_expr> <statement> "else" <statement> |
                    "while" <paren_expr> <statement> |
                    "do" <statement> "while" <paren_expr> ";" |
                    "{" { <statement> } "}" |
                    <expr> ";" |
                    ";"
    <paren_expr> ::= "(" <expr> ")"
    <expr> ::= <test> | <id> "=" <expr>
    <test> ::= <sum> | <sum> "<" <sum>
    <sum> ::= <term> | <sum> "+" <term> | <sum> "-" <term>
    <term> ::= <id> | <int> | <paren_expr>
    <id> ::= "a" | "b" | "c" | "d" | ... | "z"
    <int> ::= <an_unsigned_decimal_integer>
    

Usage Examples:

Get started by directing Tiny-C scripts to the compiler via standard input:

$ echo "a=b=c=2<3;" | python tinyc.py
 a = 1
 b = 1
 c = 1

$ echo "{ i=1; while (i<100) i=i+i; }" | python tinyc.py
 i = 128

$ echo "{ i=125; j=100; while (i-j) if (i<j) j=j-i; else i=i-j; }" | python tinyc.py
 i = 25
 j = 25

$ echo "{ i=1; do i=i+10; while (i<50); }" | python tinyc.py
 i = 51

$ echo "{ i=1; while ((i=i+10)<50) ; }" | python tinyc.py
 i = 51

$ echo "{ i=7; if (i<5) x=1; if (i<10) y=2; }" | python tinyc.py
 i = 7
 y = 2

The PyTiny-C compiler provides basic error checking, allowing users to focus on the critical aspects of compiler design and functionality. For further insights, refer to the original Tiny-C implementation here.

Explore PyTiny-C today and embark on your journey to mastering compiler development!