Add regex input to regex_to_nfa.py and regex_to_regex.py

This commit is contained in:
Juhani Krekelä 2019-06-02 21:36:28 +03:00
parent 1409400cc8
commit 3a843fb07f
2 changed files with 24 additions and 11 deletions

View File

@ -1,5 +1,8 @@
from regex import Literal, Concatenation, Alternation, Star, lit, concat, bar, star import sys
from regex import Literal, Concatenation, Alternation, Star, lit
from nfa import NFA, prettyprint from nfa import NFA, prettyprint
from parse_regex import parse, ParseError
def to_nfa(regex): def to_nfa(regex):
def new_state(): def new_state():
@ -94,12 +97,16 @@ def to_nfa(regex):
return NFA(start_state, [end_state], transitions) return NFA(start_state, [end_state], transitions)
def main(): def main():
regex = concat(lit('x'), star(bar(lit('a'), lit('b'))), lit('y')) try:
print(regex) regex = parse(input('regex> '))
nfa = to_nfa(regex) except ParseError as err:
print('%s: Error: %s' % (sys.argv[0], str(err)), file=sys.stderr)
prettyprint(nfa) else:
nfa = to_nfa(regex)
prettyprint(nfa)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,15 +1,21 @@
from regex import lit, concat, bar, star import sys
from regex_to_nfa import to_nfa from regex_to_nfa import to_nfa
from nfa_to_regex import to_regex from nfa_to_regex import to_regex
from parse_regex import parse, ParseError
def main(): def main():
regex = concat(bar(lit('foo'), lit('bar')), bar(lit('baz'), lit('qux'))) try:
print(regex) regex = parse(input('regex> '))
nfa = to_nfa(regex) except ParseError as err:
regex_prime = to_regex(nfa) print('%s: Error: %s' % (sys.argv[0], str(err)), file=sys.stderr)
print(regex_prime) else:
nfa = to_nfa(regex)
regex_prime = to_regex(nfa)
print(regex_prime)
if __name__ == '__main__': if __name__ == '__main__':
main() main()