Skip to content

mohamedsalahh/Finite-Automata

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Finite-Automata

Finite state machine simultor for regular expression.

Requirements

pip install Algebraic-Expression-Parser

NFA

Importing

from FiniteAutomata import NFA
nfa = NFA('(1+0)*0')
print(nfa)
< Symbols: {'$', '1', '0'}
  States: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  Transitions Table: {'0': {'1': {'1'}}, '2': {'0': {'3'}}, '4': {'$': {'2', '0'}}, '1': {'$': {'5'}}, '3': {'$': {'5'}}, '6': {'$': {'7', '4'}}, '5':  {'$': {'7', '4'}}, '8': {'0': {'9'}}, '7': {'$': {'8'}}, '9': {}}
  Start State: 6
  Final State: 9 >
print(nfa.check_string('10100'))
(True, {'2', '7', '8', '4', '5', '9', '3', '0'})
print(nfa.check_string('101001'))
(False, {'2', '7', '8', '4', '5', '1', '0'})
nfa.visualize(state_color='#B5B5B5', bgcolor='#0d1017', fontcolor='#B5B5B5', arrow_color='#B5B5B5')

states = nfa.check_string('10100')[1]
nfa.visualize(state_color='#B5B5B5', bgcolor='#0d1017', fontcolor='#B5B5B5', arrow_color='#B5B5B5', subgroup_states=states, subgroup_color='#25282e')

DFA

Importing

from FiniteAutomata import DFA
dfa = DFA(regex='(1+0)*0')
print(dfa)
< Symbols: {'1', '0'}
  States: ['0', '1', '2']
  Transitions Table: {'2': {'0': '1', '1': '0'}, '1': {'0': '1', '1': '0'}, '0': {'0': '1', '1': '0'}}
  Start State: 2
  Final States: {'1'} >
print(dfa.check_string('10100'))
(True, '1')
print(dfa.check_string('101001'))
(False, '0')
dfa.visualize(state_color='#B5B5B5', bgcolor='#0d1017', fontcolor='#B5B5B5', arrow_color='#B5B5B5')

states = set(dfa.check_string('10100')[1])
dfa.visualize(state_color='#B5B5B5', bgcolor='#0d1017', fontcolor='#B5B5B5', arrow_color='#B5B5B5', subgroup_states=states, subgroup_color='#25282e')