maquinas

maquinas — formal languages and automata in Python

maquinas is a Python library to build, run and visualize computing machines and grammars, made for teaching Formal Language Theory. Write a machine as a readable table, check which strings it accepts, follow its run step by step, and draw or animate it:

from maquinas.io import load_fa

# Binary numbers divisible by 3: the state is the remainder
div3 = load_fa("""
         | 0  | 1  |
-> q0 ]  | q0 | q1 |
   q1    | q2 | q0 |
   q2    | q1 | q2 |
""")

"110" in div3                         # True
"111" in div3                         # False
div3.save_image("div3.svg")           # the graph of the automaton
div3.save_animation("110", "div3.gif")  # the run on 110, step by step

Every kind of machine works the same way, from finite automata to Turing machines:

Language class

Machines and grammars

Regular

DFA, NDFA, NDFA-ε, regular expressions, regular grammars, conversions between them and minimization

Context free

Push down automata, context free grammars (Earley and CYK parsers)

Context sensitive

Linear bounded automata

Recursively enumerable

Turing machines, push down automata with two or more stacks

It also loads JFLAP files, comes with the machines of the JFLAP book, has color themes and a command line tool, and works in Jupyter notebooks.