Compare commits
No commits in common. "46aab39ee6eb8c9fa0b774ce42b3f4bd6e63ab3d" and "2b74819b15ed598c7146714316cc06b7e96cca40" have entirely different histories.
46aab39ee6
...
2b74819b15
10
nfa.py
10
nfa.py
|
@ -1,10 +0,0 @@
|
||||||
from collections import namedtuple
|
|
||||||
|
|
||||||
NFA = namedtuple('NFA', ['start', 'accept', 'transitions'])
|
|
||||||
|
|
||||||
def copy_nfa(nfa):
|
|
||||||
transitions_copy = {}
|
|
||||||
for from_state in nfa.transitions:
|
|
||||||
transitions_copy[from_state] = nfa.transitions[from_state].copy()
|
|
||||||
|
|
||||||
return NFA(nfa.start, nfa.accept, transitions_copy)
|
|
|
@ -1,7 +1,16 @@
|
||||||
import enum
|
import enum
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
from regex import lit, concat, bar, star
|
from regex import lit, concat, bar, star
|
||||||
from nfa import NFA, copy_nfa
|
|
||||||
|
NFA = namedtuple('NFA', ['start', 'accept', 'transitions'])
|
||||||
|
|
||||||
|
def copy_nfa(nfa):
|
||||||
|
transitions_copy = {}
|
||||||
|
for from_state in nfa.transitions:
|
||||||
|
transitions_copy[from_state] = nfa.transitions[from_state].copy()
|
||||||
|
|
||||||
|
return NFA(nfa.start, nfa.accept, transitions_copy)
|
||||||
|
|
||||||
def remove_states(nfa):
|
def remove_states(nfa):
|
||||||
start, accept, transitions = nfa
|
start, accept, transitions = nfa
|
||||||
|
@ -141,19 +150,16 @@ def prettyprint(nfa):
|
||||||
print(process_state(from_state) + '\t' + '\t'.join(t))
|
print(process_state(from_state) + '\t' + '\t'.join(t))
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
nfa = NFA('start', ['0'], {
|
nfa = NFA('start', ['end'], {
|
||||||
'start': {'1': lit('i'), '2': lit('d')},
|
'start': {'0': lit('s')},
|
||||||
'0': {'1': lit('i'), '2': lit('d')},
|
'0': {'0': lit('0'), '1': lit('1'), 'end': lit('e'), 'start': lit('r')},
|
||||||
'1': {'0': lit('d'), '2': lit('i')},
|
'1': {'0': lit('1'), '1': lit('0'), 'start': lit('r')},
|
||||||
'2': {'0': lit('i'), '1': lit('d')}
|
'end': {'end': lit('e'), 'start': lit('n')}
|
||||||
})
|
})
|
||||||
|
|
||||||
prettyprint(nfa)
|
prettyprint(nfa)
|
||||||
|
|
||||||
regex = to_regex(nfa)
|
print(to_regex(nfa))
|
||||||
|
|
||||||
print(repr(regex))
|
|
||||||
print(regex)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
12
regex.py
12
regex.py
|
@ -81,11 +81,7 @@ def concat(*elements):
|
||||||
|
|
||||||
combined = []
|
combined = []
|
||||||
for element in flattened:
|
for element in flattened:
|
||||||
if type(element) == Literal and element.text == '':
|
if len(combined) > 0 and type(combined[-1]) == Literal and type(element) == Literal:
|
||||||
# Drop empty literals
|
|
||||||
continue
|
|
||||||
|
|
||||||
elif len(combined) > 0 and type(combined[-1]) == Literal and type(element) == Literal:
|
|
||||||
# Combine two literals next to each other
|
# Combine two literals next to each other
|
||||||
# into one literal
|
# into one literal
|
||||||
previous = combined.pop()
|
previous = combined.pop()
|
||||||
|
@ -94,11 +90,7 @@ def concat(*elements):
|
||||||
else:
|
else:
|
||||||
combined.append(element)
|
combined.append(element)
|
||||||
|
|
||||||
if len(combined) == 0:
|
if len(combined) == 1:
|
||||||
# Empty regex, represent with empty literal
|
|
||||||
return lit('')
|
|
||||||
|
|
||||||
elif len(combined) == 1:
|
|
||||||
element, = combined
|
element, = combined
|
||||||
return element
|
return element
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue