https://github.com/dbrgn/ndfa
Nondeterministic and deterministic finite automata
https://github.com/dbrgn/ndfa
Last synced: 10 months ago
JSON representation
Nondeterministic and deterministic finite automata
- Host: GitHub
- URL: https://github.com/dbrgn/ndfa
- Owner: dbrgn
- License: other
- Created: 2012-07-28T14:08:05.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2012-07-28T14:15:14.000Z (almost 14 years ago)
- Last Synced: 2025-09-01T15:52:05.010Z (10 months ago)
- Language: Python
- Size: 105 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
ndfa - nondeterministic and deterministic finite automata
=========================================================
Finit state machine implementations.
`ndfa.py` currently contains only a DFA class. NFA will follow later.
Usage
-----
Here is an example of a DFA that accepts strings with an even number of 'a's.
from ndfa import DFA
states = list('01')
alphabet = list('ab')
transitions = {
('0', 'a'): '1',
('0', 'b'): '0',
('1', 'a'): '0',
('1', 'b'): '1',
}
start = '0'
accepts = list('0')
dfa = DFA(states, alphabet, transitions, start, accepts)
To test some words with this dfa:
>>> dfa.test('')
True
>>> dfa.test('aababa')
True
>>> dfa.test('baaa')
False
>>> dfa.test('foo')
Traceback (most recent call last):
File "", line 1, in
File "ndfa.py", line 24, in test
'Symbol "%s" must be in alphabet.' % symbol
AssertionError: Symbol "f" must be in alphabet.
Output
------
The DFA class uses the python logging module. To see basic output, set log
level to INFO:
>>> import logging
>>> logging.basicConfig(level=logging.INFO, format='%(message)s')
>>> dfa.test('aba')
Testing word "aba"...
Accepted: True
...or to DEBUG for more verbose output:
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG, format='%(message)s')
>>> dfa.test('aba')
Testing word "aba"...
Initial state: 0
Current symbol: a
New state: 1
Current symbol: b
New state: 1
Current symbol: a
New state: 0
Final state: 0
Accepted: True
License
-------
GPLv3, see LICENSE.md