Skip to content
/ whilec Public

An interpreter/compiler for a toy language called While. The implementation is in Python.

License

Notifications You must be signed in to change notification settings

leissa/whilec

Repository files navigation

While

Python 3.8 Python 3.9 Python 3.10

Pylint

An interpreter/compiler for a toy language called While. The implementation is in Python.

Usage

usage: while.py [-h] [--eval] [-o output] [--output-c output] [--output-py output] file

Compiler and interpreter for the While languge.

positional arguments:
  file                  input file

options:
  -h, --help            show this help message and exit
  --eval                interpret input program
  -o output, --output output
                        print program again
  --output-c output     compile program to C
  --output-py output    compile program to Python

Use '-' to output to stdout.

Examples

Interpreter

Invoke the interpreter like so:

./while.py test/fib.while --eval

Compile to C

Compile a While program to C, then to an executable, and execute:

./while.py test/fib.while --output-c fib.c
cc fib.c -o fib
./fib

Compile to Python

Compile a While program to Pyhon and then execute:

./while.py test/fib.while --output-py fib.py
pyhton fib.py

Compile to While

Output the source program again:

./while.py test/fib.while -o -

Grammar

P = s 'return' e ';' EOF                (* program *)
  ;

s = ';'                                 (* empty statement *)
  | t ID '=' e ';'                      (* decl statement *)
  |   ID '=' e ';'                      (* assignment statement *)
  | 'if' e '{' s '}'                    (* if statement *)
  | 'if' e '{' s '}' 'else' '{' s '}'   (* if-else statement *)
  | 'while' e '{' s '}'                 (* while statement *)
  | s ... s                             (* statement list *)
  ;

t = 'int'                               (* type *)
  | 'bool'
  ;

e = LIT                                 (* expression *)
  | 'true'
  | 'false'
  | ID
  | '(' e ')'
  | OP1 e
  | e OP2 e
  ;

where

  • LIT = [0-9]+
  • ID = [a-zA-Z][a-zA-Z0-9]*
  • OP1 is one of: +, -, not
  • OP2 is one of: *, +, -, ==, !=, <, <=, >, >=, and, or

Precedence

Ambiguities in the expression productions are resolved according to the operator precedence that is summarized in the following table (strongest binding first):

Operator Description
+e, -e unary plus, unary minus
* multiplication
+, - addition, subtraction
==, !=, <, <=, >, > relational operators
not e Boolean NOT
and Boolean AND
or Boolean OR

All binary operators are left associative.

About

An interpreter/compiler for a toy language called While. The implementation is in Python.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages