forked from IF-LK-2020/py-automaten
Initial commit
This commit is contained in:
40
dea_abaca.py
Normal file
40
dea_abaca.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# Übergangsfunktion
|
||||||
|
# Zustand, Buchstabe => Neuer Zustand
|
||||||
|
def transition( state, char ):
|
||||||
|
new_state = -1
|
||||||
|
|
||||||
|
if state == 0:
|
||||||
|
if char == "a":
|
||||||
|
new_state = 1
|
||||||
|
elif state == 1:
|
||||||
|
if char == "a":
|
||||||
|
new_state = 1
|
||||||
|
elif char in "bc":
|
||||||
|
new_state = 2
|
||||||
|
elif state == 2:
|
||||||
|
if char == "a":
|
||||||
|
new_state = 1
|
||||||
|
|
||||||
|
return new_state
|
||||||
|
|
||||||
|
# Scanner-Funktion für ein Wort
|
||||||
|
# True, wenn das Wort in der Sprache liegt
|
||||||
|
def scan_word(word):
|
||||||
|
# Startzustand q0
|
||||||
|
state = 0
|
||||||
|
|
||||||
|
for char in word: # jeden Buchstaben durchlaufen
|
||||||
|
state = transition(state, char) # Übergangsfunktion ausführen
|
||||||
|
|
||||||
|
# Word wird akzeptiert, wenn einer der Endzustände erreicht wurde
|
||||||
|
return state == 1 # or state == 2
|
||||||
|
|
||||||
|
|
||||||
|
# Programmstart
|
||||||
|
if __name__ == "__main__":
|
||||||
|
word = input("Bitte ein Wort eingeben: ")
|
||||||
|
accepted = scan_word(word)
|
||||||
|
if accepted:
|
||||||
|
print(f"Das Wort '{word}' gehört zur Sprache")
|
||||||
|
else:
|
||||||
|
print(f"Das Wort '{word}' gehört nicht zur Sprache")
|
||||||
31
dea_vorlage.py
Normal file
31
dea_vorlage.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# Übergangsfunktion
|
||||||
|
# Zustand, Buchstabe => Neuer Zustand
|
||||||
|
def transition( state, char ):
|
||||||
|
new_state = -1
|
||||||
|
|
||||||
|
# Übergänge
|
||||||
|
# siehe dea_abaca.py für ein Beispiel
|
||||||
|
|
||||||
|
return new_state
|
||||||
|
|
||||||
|
# Scanner-Funktion für ein Wort
|
||||||
|
# True, wenn das Wort in der Sprache liegt
|
||||||
|
def scan_word(word):
|
||||||
|
# Startzustand q0
|
||||||
|
state = 0
|
||||||
|
|
||||||
|
for char in word: # jeden Buchstaben durchlaufen
|
||||||
|
state = transition(state, char) # Übergangsfunktion ausführen
|
||||||
|
|
||||||
|
# Word wird akzeptiert, wenn einer der Endzustände erreicht wurde
|
||||||
|
return state == 1 # or state == 2
|
||||||
|
|
||||||
|
|
||||||
|
# Programmstart
|
||||||
|
if __name__ == "__main__":
|
||||||
|
word = input("Bitte ein Wort eingeben: ")
|
||||||
|
accepted = scan_word(word)
|
||||||
|
if accepted:
|
||||||
|
print(f"Das Wort '{word}' gehört zur Sprache")
|
||||||
|
else:
|
||||||
|
print(f"Das Wort '{word}' gehört nicht zur Sprache")
|
||||||
42
nea_abacc.py
Normal file
42
nea_abacc.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# Scanner-Funktion für ein Wort
|
||||||
|
# True, wenn das Wort in der Sprache liegt
|
||||||
|
def scan_word(state, word):
|
||||||
|
# Rekursionsabbruch wenn Wort leer (Länge = 0)
|
||||||
|
if len(word) == 0:
|
||||||
|
# Akzeptiert, wenn Automat in Endzustand
|
||||||
|
return state == 1 or state == 3
|
||||||
|
|
||||||
|
char = word[0] # Ersten Buchstaben ist die Eingabe
|
||||||
|
word = word[1:] # Ersten Buchstaben vom Restwort abtrennen
|
||||||
|
|
||||||
|
# Übergangsfunktion
|
||||||
|
if state == 0:
|
||||||
|
if char == "a":
|
||||||
|
return scan_word(1, word)
|
||||||
|
elif state == 1:
|
||||||
|
if char == "a":
|
||||||
|
return scan_word(1, word)
|
||||||
|
elif char in "bc":
|
||||||
|
# Nichtdeterministischer Übergang
|
||||||
|
# Hier findet das Backtracking statt: Hat der erste Aufruf
|
||||||
|
# keinen Erfolg, dann wird der zweite Aufruf gepürft.
|
||||||
|
return scan_word(2, word) or scan_word(3, word)
|
||||||
|
elif state == 2:
|
||||||
|
if char == "a":
|
||||||
|
return scan_word(1, word)
|
||||||
|
elif state == 3:
|
||||||
|
if char == "c":
|
||||||
|
return scan_word(1, word)
|
||||||
|
|
||||||
|
# Kein Übergang möglich, Wort nicht akzeptiert
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# Programmstart
|
||||||
|
if __name__ == "__main__":
|
||||||
|
word = input("Bitte ein Wort eingeben: ")
|
||||||
|
accepted = scan_word(0, word)
|
||||||
|
if accepted:
|
||||||
|
print(f"Das Wort '{word}' gehört zur Sprache")
|
||||||
|
else:
|
||||||
|
print(f"Das Wort '{word}' gehört nicht zur Sprache")
|
||||||
28
nea_vorlage.py
Normal file
28
nea_vorlage.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Scanner-Funktion für ein Wort
|
||||||
|
# True, wenn das Wort in der Sprache liegt
|
||||||
|
def scan_word(state, word):
|
||||||
|
# Rekursionsabbruch wenn Wort leer (Länge = 0)
|
||||||
|
if len(word) == 0:
|
||||||
|
# Akzeptiert, wenn Automat in Endzustand, z.B.:
|
||||||
|
return state == 1 or state == 3
|
||||||
|
|
||||||
|
char = word[0] # Ersten Buchstaben ist die Eingabe
|
||||||
|
word = word[1:] # Ersten Buchstaben vom Restwort abtrennen
|
||||||
|
|
||||||
|
# Übergangsfunktion
|
||||||
|
|
||||||
|
# Übergänge
|
||||||
|
# siehe nea_abacc.py für ein Beispiel
|
||||||
|
|
||||||
|
# Kein Übergang möglich, Wort nicht akzeptiert
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# Programmstart
|
||||||
|
if __name__ == "__main__":
|
||||||
|
word = input("Bitte ein Wort eingeben: ")
|
||||||
|
accepted = scan_word(0, word)
|
||||||
|
if accepted:
|
||||||
|
print(f"Das Wort '{word}' gehört zur Sprache")
|
||||||
|
else:
|
||||||
|
print(f"Das Wort '{word}' gehört nicht zur Sprache")
|
||||||
41
nka_anbn.py
Normal file
41
nka_anbn.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# Scanner-Funktion für ein Wort
|
||||||
|
# True, wenn das Wort in der Sprache liegt
|
||||||
|
def scan_word( state, word, stack ):
|
||||||
|
# obersten Kellerbuchstaben "popen" (wenn vorhanden)
|
||||||
|
stack_char = stack.pop(0) if stack else ""
|
||||||
|
|
||||||
|
# Rekursionsabbruch wenn Wort leer (Länge = 0)
|
||||||
|
if len(word) == 0:
|
||||||
|
# Akzeptiert, wenn Automat in Endzustand
|
||||||
|
# (und ggf. bestimmter Kellerzustand), z.B.:
|
||||||
|
return state == 1 and stack_char == "#"
|
||||||
|
|
||||||
|
char = word[0] # Ersten Buchstaben ist die Eingabe
|
||||||
|
word = word[1:] # Ersten Buchstaben vom Restwort abtrennen
|
||||||
|
|
||||||
|
# Übergangsfunktion
|
||||||
|
if state == 0:
|
||||||
|
if stack_char == '#':
|
||||||
|
if char == 'a':
|
||||||
|
return scan_word(0, word, ['A','#'] + stack)
|
||||||
|
elif stack_char == 'A':
|
||||||
|
if char == 'a':
|
||||||
|
return scan_word(0, word, ['A','A'] + stack)
|
||||||
|
elif char == 'b':
|
||||||
|
return scan_word(1, word, stack)
|
||||||
|
elif state == 1:
|
||||||
|
if stack_char == 'A':
|
||||||
|
if char == 'b':
|
||||||
|
return scan_word(1, word, stack)
|
||||||
|
|
||||||
|
# Kein Übergang möglich, Wort nicht akzeptiert
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Programmstart
|
||||||
|
if __name__ == "__main__":
|
||||||
|
word = input("Bitte ein Wort eingeben: ")
|
||||||
|
accepted = scan_word(0, word, ["#"])
|
||||||
|
if accepted:
|
||||||
|
print(f"Das Wort '{word}' gehört zur Sprache")
|
||||||
|
else:
|
||||||
|
print(f"Das Wort '{word}' gehört nicht zur Sprache")
|
||||||
31
nka_vorlage.py
Normal file
31
nka_vorlage.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# Scanner-Funktion für ein Wort
|
||||||
|
# True, wenn das Wort in der Sprache liegt
|
||||||
|
def scan_word( state, word, stack ):
|
||||||
|
# obersten Kellerbuchstaben "popen" (wenn vorhanden)
|
||||||
|
stack_char = stack.pop(0) if stack else ""
|
||||||
|
|
||||||
|
# Rekursionsabbruch wenn Wort leer (Länge = 0)
|
||||||
|
if len(word) == 0:
|
||||||
|
# Akzeptiert, wenn Automat in Endzustand
|
||||||
|
# (und ggf. bestimmter Kellerzustand), z.B.:
|
||||||
|
return state == 1 and stack_char == "#"
|
||||||
|
|
||||||
|
char = word[0] # Ersten Buchstaben ist die Eingabe
|
||||||
|
word = word[1:] # Ersten Buchstaben vom Restwort abtrennen
|
||||||
|
|
||||||
|
# Übergangsfunktion
|
||||||
|
|
||||||
|
# Übergänge und Rekursionsaufrufe
|
||||||
|
# siehe nka_anbn.py für ein Beispiel
|
||||||
|
|
||||||
|
# Kein Übergang möglich, Wort nicht akzeptiert
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Programmstart
|
||||||
|
if __name__ == "__main__":
|
||||||
|
word = input("Bitte ein Wort eingeben: ")
|
||||||
|
accepted = scan_word(0, word, ["#"])
|
||||||
|
if accepted:
|
||||||
|
print(f"Das Wort '{word}' gehört zur Sprache")
|
||||||
|
else:
|
||||||
|
print(f"Das Wort '{word}' gehört nicht zur Sprache")
|
||||||
Reference in New Issue
Block a user