Theory of Computation
Project description
Computation Toolkit - practical assignment
P1. NFA to DFA Converter
A Python implementation of the subset construction algorithm to convert a Non-Deterministic Finite Automaton (NFA) with ε-transitions to an equivalent Deterministic Finite Automaton (DFA).
Algorithm
- Computes ε-closures for NFA states.
- Generates DFA states, transitions, and accept states.
- Handles ε-transitions during DFA state construction.
Usage
Input Format
Define your NFA as follows:
nfa_states: Set of NFA states (e.g.,{'q0', 'q1', 'q2'}).nfa_alphabet: List of symbols including'ε'(e.g.,['a', 'b', 'ε']).nfa_transitions: Dictionary where keys are tuples(state, symbol), and values are sets of next states. (e.g.,{ ('q0', 'ε'): {'q1'}, ('q1', 'a'): {'q1', 'q2'}, ('q1', 'b'): {'q1'}, ('q2', 'a'): {'q2'}, }).nfa_start: The start state (e.g.,'q0').nfa_accept: Set of accept states (e.g.,{'q2'}).
Function Call
from computation_toolkit import nfa_to_dfa
dfa_states, dfa_alphabet, dfa_transitions, dfa_start, dfa_accept = nfa_to_dfa(
nfa_states, nfa_alphabet, nfa_transitions, nfa_start, nfa_accept
)
P2. Check CFG Ambiguity given a string
A program to determine if a given string has more than one parse tree under a provided context-free grammar (CFG), indicating ambiguity for that string. Uses an shift-reduce bottom up parser to handle common sources of ambiguity.
Algorithm
- Detects ambiguity by checking for multiple valid parse trees.
- Uses BFS-based shift-reduce parsing to build the parse trees.
Usage
Specify your CFG as a dictionary where:
- Keys are non-terminals (e.g.,
"E"). - Values are lists of productions (e.g.,
[["E", "+", "E"], ["a"]]).
Example (ambiguous arithmetic grammar):
from computation_toolkit import has_multiple_parses
grammar = {
"E": [["E", "+", "E"], ["E", "*", "E"], ["a"]]
}
start_symbol = "E"
input_string = "a+a*a"
print(has_multiple_parses(grammar, start_symbol, input_string))
P3. Turing Machine to add two uniary numbers
A Python implementation of a Turing Machine that computes the sum of two unary numbers separated by a + symbol.
Algotithm
- Implements a 3-state Turing Machine (Start, MoveToEnd, EraseLast)
- Handles edge cases (empty numbers, missing separator)
- Includes error checking for invalid inputs
- Unit test coverage for all major cases
Usage
from computation_toolkit import tm_add
input_str = "111+11"
print(tm_add(input_str))
Running the Tests
To run the tests, use the following command:
python -m unittest discover tests -v
Author
Project details
Release history Release notifications | RSS feed
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 computation_toolkit-1.tar.gz.
File metadata
- Download URL: computation_toolkit-1.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc445eba9dff6fc6ddfbecef16dd79beda79a23dec4e732b083b1514b3d26ecc
|
|
| MD5 |
e4cd01cb491853f6badcf0e557d5567b
|
|
| BLAKE2b-256 |
c66c2edc99f3ab3f4d2addfab12e37c8689cfbcfb3a41ac436b3f8f9e45e16fd
|
File details
Details for the file computation_toolkit-1-py3-none-any.whl.
File metadata
- Download URL: computation_toolkit-1-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abc06da268ef7e704947227d24876361c740643e62d0acf9e6b24cb78e42d2c9
|
|
| MD5 |
eb4b1edcca42df6c8f75808a8c879d35
|
|
| BLAKE2b-256 |
99c2d145846dfb5b5a8cc286b4c864292b548c7e8bf856414eb1f10237684188
|