Ein DFA grundsätzlich durch ein transition table
definiert ist. Karten Dieser Übergang Tabelle, die jede (gültig) Kombination von aktuellen Stand und aktuelle Eingabe in den entsprechenden Nachfolgestatus Eine solche Tabelle kann als Dictionary von Dictionaries modelliert werden, zB: Das äußere Diktat enthält die Zustände als Schlüssel und Wörterbücher als Werte, diese wiederum haben die gültigen Eingaben als Schlüssel und den Nachfolgerzustand als Wert
EDIT: Ihr gewähltes Beispiel ist nicht ideal, so dass es ein ziemlich großes Alphabet (dh alle möglichen Eingabezeichen) von mindestenshat, die verknüpfte Antwort beschränkte sich auf [01]
aus einem Grund ;-) Nichts desto trotz ist hier, wie ich anfangen würde:
{
# in state '' we have not yet processed/consumed any input
# it is the start state
# the only valid input is a 'w'
'': {'w': 'w'},
# in state 'w' we a have already consumed a 'w'
# the only valid input is another 'w'
'w': {'w': 'ww'},
# in state 'ww' we have previously consumed 'ww'
# the only valid input is still only a 'w'
'ww': {'w': 'www'},
# now the only valid input is a '.'
'www': {'.': 'www.'},
# this is where your example becomes unpractical:
# we have to add a transition for every valid input
# (you could get around this by using a defaultdict and some kind of special signal value, but im not quite sure you are up to that)
'www.': {'a': 'www.*', 'b': 'www.*', ...},
# I used the star in this state name to symbolize multiple (at least one) valid character
# we only leave this state if we encounter a '.'
'www.*': {'.': 'www.*.', 'a': 'www.*', 'b': 'www.*', ...},
# it should be obvious how to continue from here
'www.*.': ...
}
EDIT2: nach Chat-Implementierung.
from collections import defaultdict
dfa = {
'initial': defaultdict(lambda: 'invalid', w='w'),
'w': defaultdict(lambda: 'invalid', w='ww'),
'ww': defaultdict(lambda: 'invalid', w='www'),
'www': defaultdict(lambda: 'invalid', [('.','www.')]),
'www.': defaultdict(lambda: 'www.*', [('.','invalid')]),
'www.*': defaultdict(lambda: 'www.*', [('.','www.*.')]),
'www.*.': defaultdict(lambda: 'www.*', [('c','www.*.c')]),
'www.*.c': defaultdict(lambda: 'www.*', [('o','www.*.co')]),
'www.*.co': defaultdict(lambda: 'www.*', [('m','www.*.com'), ('.','www.*.')]),
'www.*.com': defaultdict(lambda: 'www.*', [('.','www.*.')]),
'invalid': defaultdict(lambda: 'invalid')
}
def accepts(transitions,initial,accepting,s):
state = initial
for c in s:
state = transitions[state][c]
print(c, '->', state)
return state in accepting
print(accepts(dfa,'initial',{'www.*.com', 'www.*.co'},"www.hi.com"))
[Searching Google für "DFA in Python"] (https://encrypted.google.com/search?q=DFA+in+Python) bietet sofort mehrere Beispiele (einschließlich [one on Stack Overflow] (http://stackoverflow.com/questions/35272592/how-are- Endliche Automaten-implementiert-in-code)). Was war los mit ihnen? –
Ich verstehe das Konzept besser mit dem Stack-Überlauf, den du verlinkt hast (nicht sicher, warum ich es vorher nicht finden konnte), aber es auf Charaktere über Ints anzuwenden, verwirrte mich. – witcheR