forked from ceramisch/automatesfinis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tp4automates.py
executable file
·76 lines (55 loc) · 1.58 KB
/
tp4automates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
"""
Read a regular expression and returns:
* YES if word is recognized
* NO if word is rejected"""
from typing import Set, List
from automaton import Automaton, EPSILON, State, error, warn, RegExpReader
import sys
import pdb # for debugging
##################
def is_deterministic(a:Automaton)->bool:
# Copy-paste or import from previous TPs
return False
##################
def recognizes(a:Automaton, word:str)->bool:
# Copy-paste or import from previous TPs
return False
##################
def determinise(a:Automaton):
# Copy-paste or import from previous TPs
pass
##################
def kleene(a1:Automaton)->Automaton:
# Copy-paste or import from previous TPs
return a1
##################
def concat(a1:Automaton, a2:Automaton)->Automaton:
# Copy-paste or import from previous TPs
return a1
##################
def union(a1:Automaton, a2:Automaton)->Automaton:
# Copy-paste or import from previous TPs
return a1
##################
def regexp_to_automaton(re:str)->Automaton:
"""
Moore's algorithm: regular expression `re` -> non-deterministic automaton
"""
postfix = RegExpReader(regexp).to_postfix()
stack:List[Automaton] = []
#TODO implement!
return stack[0]
##################
if __name__ == "__main__" :
if len(sys.argv) != 3:
usagestring = "Usage: {} <regular-expression> <word-to-recognize>"
error(usagestring.format(sys.argv[0]))
regexp = sys.argv[1]
word = sys.argv[2]
a = regexp_to_automaton(regexp)
determinise(a)
if recognizes(a, word):
print("YES")
else:
print("NO")