Property-based testing utilities for finite automata and context-free grammars.
Project description
pbt4automata
pbt4automata provides tools to construct finite automata and context-free grammars in Python and validate their behavior with property-based tests powered by Hypothesis.
Installation
pip install -e .
Examples
Deterministic Finite Automata
Testing a DFA against a rule
The following example tests a DFA that accepts all strings that contain the substring "010" or "100".
from pbt4automata import DFA
automaton = DFA(
states=["q0", "q1", "q2", "q3", "q4", "q5"],
alphabet="01",
transition_function={
("q0", "0"): "q1",
("q0", "1"): "q4",
("q1", "0"): "q1",
("q1", "1"): "q2",
("q2", "0"): "q3",
("q2", "1"): "q4",
("q3", "0"): "q3",
("q3", "1"): "q3",
("q4", "0"): "q5",
("q4", "1"): "q4",
("q5", "0"): "q3",
("q5", "1"): "q2",
},
start_state="q0",
accept_states=["q3"],
)
result = automaton.test("[01]*(010|100)[01]*")
if result is True:
print("Success!")
else:
print("Counterexample:", result)
You can also pass a function of type Callable[[str], bool] to automaton.test(...) instead of a regex.
Testing two DFAs for equivalence
from pbt4automata import Automaton, DFA
automata1 = DFA(
...
)
automata2 = DFA(
...
)
result = Automaton.test_equivalence(automata1, automata2)
if result is True:
print("Success!")
else:
print("Counterexample:", repr(result))
Context-Free Grammars
Testing a CFG against a rule
The following example tests a CFG in Chomsky Normal Form that generates all non-empty strings of balanced parentheses.
from pbt4automata import CNF
cnf = CNF(
terminals="()",
nonterminals="SLRX",
productions={
"S": ["LX", "SS"],
"L": ["("],
"R": [")"],
"X": ["SR", ")"]
},
start_symbol="S"
)
def check_balance(input_string: str) -> bool:
if input_string == "":
return False
s = 0
for c in input_string:
if c == "(":
s += 1
elif c == ")":
s -= 1
if s < 0:
return False
return s == 0
result = cnf.test(check_balance)
if result is True:
print("Success!")
else:
print("Counterexample:", result)
Development
Install dev dependencies and run tests:
pip install -e ".[dev]"
pytest -v
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pbt4automata-0.1.0.tar.gz.
File metadata
- Download URL: pbt4automata-0.1.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd9e8ae5615b6e671f8aef4ddfc6ef3f793f7b7490c47f9db7ff16ea35024436
|
|
| MD5 |
bc6d79669640ec13bdce9b8538a0072a
|
|
| BLAKE2b-256 |
e8bc1298470c94c82812605c9016aeb1cbd3815bb2d1662797341e460c641f25
|
File details
Details for the file pbt4automata-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pbt4automata-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea90bbdb2e1c79bf3597a15800ee3f0a5da6c2816bfbca60435ae64aa13bd753
|
|
| MD5 |
1159b33f4794eba0c5d2a5a915b8f408
|
|
| BLAKE2b-256 |
731ab830e53c79aa748d94ade2bddfb47d06651fd20759b64f652d521bb6367e
|