Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wiegerw committed Sep 24, 2021
1 parent 2667f61 commit 6c7cf91
Show file tree
Hide file tree
Showing 153 changed files with 14,630 additions and 0 deletions.
129 changes: 129 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# GAMBA tools

The `gambatools` package is a Python 3 package that aims to
support education in theoretical computer science at
[Eindhoven University of Technology](https://www.tue.nl/en/).
It contains a library for
DFAs, NFAs, PDAs, Turing machines, context free grammars and
regular expressions. Moreover, the package provides Jupyter notebooks with
exercises. The library has been developed by Wieger Wesselink,
and it was designed together with Erik de Vink.

# GAMBA
The code was developed as part of the GAMBA project, which stands for:
*Grammars and Automata Made Boffo and Assessible*. The goals of the project are
to support

* practising and assessing formal language techniques
* self-paced learning outside contact hours
* immediate feedback while practising
* automated grading for assessment

(*boffo*: extremely successful, sensational)

# License
The code is distributed under the `GPL-3.0-or-later` license.

# Installation
The package can be installed using
```
pip install gambatools
```
The required python packages can be found in `requirements.txt`.

# Rendering images
For visualization the [graphviz](https://pypi.org/project/graphviz/) python
package is used. **To render the generated DOT source code, you also need to install
Graphviz**. See the [Graphviz website](https://graphviz.org/) for further instructions.
Make sure that the directory containing the `dot` executable is on your systems’ path.

# Documentation
The file `doc/specifications.pdf` contains formal specifications
of the algorithms in the library. Note that the code maps almost one-to-one
to the specifications, so in order to understand the code please consult the
pseudocode specifications.

# Notebooks
The directory `notebooks` contains a number of Jupyter notebooks
with exercises. In `notebooks/with-answers` the correct answers are
already given, while in `notebooks/without-answers` they have been
left out.
The answers to the exercises are checked automatically.
Whenever the user makes a mistake, appropriate feedback is given.

![NFA to DFA](doc/images/nfa2dfa.png)

For specifying a DFA, NFA, etc. a line based textual input format is used,
see the example below. The documentation contains a section that describes
the syntax, while in the `examples` directory a number of examples can be found.
```
input_symbols 0 1
states qA qB qC qD
initial qA
final qC
qA qB 0
qA qD 1
qB qB 0
qB qC 1
qC qB 0
qC qC 1
qD qD 0
qD qD 1
```

# Notebook generation
For convenience there is a mechanism to automatically generate notebooks from
templates.
The notebooks in `notebooks/with-answers` and `notebooks/without-answers`
have been generated using the commands
```
make_notebook.py -o without-answers notebooks.batch
make_notebook.py --with-answers -o with-answers notebooks.batch
```
The templates contain tags of the form `<<tag>>` that are substituted by
the `make_notebook.py` script. This generation is still experimental, and
there is currently no documentation available for this.

# Contact
If you are interested in using the package for education or have questions
or feedback, the authors can be reached by email:
<[email protected]> or <[email protected]>.
Binary file added doc/images/nfa2dfa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/specifications.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions examples/cfg1.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%% language = \lbrace \, a^{\mkern1mu n} \mkern1mu b^{\mkern1mu m} c^{\mkern1mu \ell} \mid n = m \lor m = \ell \lor n = \ell \, \rbrace
%% accepted = _ aaa bb c aabb bc aabc aabbbcc aaaccc
%% rejected = bcc aab aabbbc ccaaabb bbccaa abab cb aaacccc
%% question = Give a CFG that generates the language $@language@$.

S -> XC | AY | Z
X -> aXb | ε
Y -> bYc | ε
Z -> aZc | B
A -> aA | ε
B -> bB | ε
C -> cC | ε
3 changes: 3 additions & 0 deletions examples/cfg2.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
S -> ASA | aB
A -> B | S
B -> b | ε
6 changes: 6 additions & 0 deletions examples/chomsky1.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
T -> AC | AS | SA | DB | a
S -> AS | SA | DB | a | AC
B -> b
A -> AC | AS | SA | DB | a | b
C -> SA
D -> a
11 changes: 11 additions & 0 deletions examples/complement.dfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%% language = \{ \, w \in \{a,b\}^\ast \mid \#_a(w) \geqslant 2 \, \}

input_symbols a b
states q0 q1 q2
initial q0
final q2
q0 q0 b
q0 q1 a
q1 q1 b
q1 q2 a
q2 q2 a b
23 changes: 23 additions & 0 deletions examples/dfa1.dfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%% Sipser Exercise 1.6a
%% 0.(0+1)*.1

%% question = Give a DFA that generates the language $@language@$.
%% language = \{ w \in {0,1}^\ast \mid w \text{ starts with } 0 \text{ and ends with } 1 \}

%% selected_word = 0101

input_symbols 0 1
states qA qB qC qD

initial qA
final qC

qA qB 0
qA qD 1
qB qB 0
qB qC 1
qC qB 0
qC qC 1
qD qD 0
qD qD 1

8 changes: 8 additions & 0 deletions examples/dfa2.dfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%% alphabet = \{a,b\}

initial q0
final q1
q0 q1 a
q1 q1 a b
q0 q2 b
q2 q2 a b
10 changes: 10 additions & 0 deletions examples/intersection1.dfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%% language = \{ \, w \in \{a,b\}^\ast \mid \mbox{$\#_a(w)$ even} \, \}

input_symbols a b
states q0 q1
initial q0
final q0
q0 q0 b
q0 q1 a
q1 q0 a
q1 q1 b
11 changes: 11 additions & 0 deletions examples/intersection2.dfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%% language = \{ \, w \in \{a,b\}^\ast \mid \#_b(w) \geqslant 2 \, \}

input_symbols a b
states q0 q1 q2
initial q0
final q2
q0 q0 a
q0 q1 b
q1 q1 a
q1 q2 b
q2 q2 a b
17 changes: 17 additions & 0 deletions examples/nfa1.nfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%% language = \{ w \in \{0,1\}^* \mid \#_0(w) \geq 2, \#_1(w) \leq 1 \}^*
%% question = Give an NFA that accepts the language $@language@$.

initial s6
final s2 s5 s6
s0 s1 0
s0 s3 1
s1 s2 0
s1 s4 1
s2 s0 ε
s2 s2 0
s2 s5 1
s3 s4 0
s4 s5 0
s5 s0 ε
s5 s5 0
s6 s0 ε
9 changes: 9 additions & 0 deletions examples/nfa2.nfa
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%% question = Give the DFA obtained from the given NFA using the NFA to DFA procedure. Use set notation like `{q0,q1}` (without spaces) for the states in the DFA.

input_symbols a b
initial q0
final q2
q0 q0 a b
q0 q1 ε
q1 q1 a
q1 q2 b
11 changes: 11 additions & 0 deletions examples/pda-simple.pda
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%% language = \{ \, a^{\mkern1mu n} b^{\mkern1mu n} \mid n \geqslant 0 \, \}
%% question = Give a PDA that accepts the language $@language@$.

initial q0
final q3

q0 q1 e,e$
q1 q1 a,eX
q1 q2 e,ee
q2 q2 b,Xe
q2 q3 e,$e
18 changes: 18 additions & 0 deletions examples/pda1.pda
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%% language = \{ w \in \{a,b\}^\ast \mid \#_a(w) = 2\#_b(w) \}
%% question = Give a PDA that generates the language $@language@$.

initial q0
final q6
q0 q1 ε,ε$
q1 q2 ε,$$
q1 q3 ε,$$
q1 q6 ε,$ε
q2 q1 ε,$$
q2 q2 a,nε
q2 q5 b,εn
q3 q1 ε,$$
q3 q3 a,εp
q3 q4 b,pε
q4 q3 ε,pε
q4 q5 ε,$$
q5 q2 ε,εn
19 changes: 19 additions & 0 deletions examples/pda2.pda
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%% language = \{ w \in \{a,b\}^\ast \mid \#_a(w) = 2\#_b(w) \}
%% question = Give a PDA that generates the language $@language@$.

epsilon ε
initial q0
final q6
q0 q1 ε,ε$
q1 q2 ε,$$
q1 q3 ε,$$
q1 q6 ε,$ε
q2 q1 ε,$$
q2 q2 a,nε
q2 q5 b,εn
q3 q1 ε,$$
q3 q3 a,εp
q3 q4 b,pε
q4 q3 ε,pε
q4 q5 ε,$$
q5 q2 ε,εn
Loading

0 comments on commit 6c7cf91

Please sign in to comment.