-
Notifications
You must be signed in to change notification settings - Fork 1
/
rltools.py
executable file
·132 lines (115 loc) · 4.69 KB
/
rltools.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""rltools.py
The command-line interface for the program.
Gustavo Zambonin & Matheus Ben-Hur de Melo Leite, UFSC, October 2015.
"""
import re
import sys
from algorithms.finite_automaton import FiniteAutomaton
from algorithms.io_manager import load, save, read_source
from algorithms.regular_expression import RegularExpression
from algorithms.regular_grammar import RegularGrammar
from algorithms.tokenizer import Tokenizer
from algorithms.ll_parser import Parser, derive
if __name__ == '__main__':
if len(sys.argv) == 1:
print("Basic usage: man ./rltools")
raise SystemExit
possible_commands = ["--dfa", "--gta", "--atg", "--rta",
"--atr", "--min", "--lex", "--syn"]
if len(set(sys.argv).intersection(possible_commands)) > 1:
print("Only one flag is permitted at a time.")
if sys.argv[1] not in possible_commands:
print("Invalid command.")
if len(sys.argv) > 2:
if "--dfa" in sys.argv:
aut = load(sys.argv[2])
if type(aut) is FiniteAutomaton:
aut.determinize()
outpath = re.sub('.in', r'.out', sys.argv[2])
if '/' in outpath:
savepath = re.sub('/', r'/afd-', outpath)
else:
savepath = 'afd-' + outpath
save(savepath, 'automaton', aut)
print("DFA saved in %s!" % savepath)
else:
print("Input must be an automaton.")
elif "--gta" in sys.argv:
grm = load(sys.argv[2])
if type(grm) is RegularGrammar:
aut = RegularGrammar.grammar_to_automaton(grm)
outpath = re.sub('.in', r'.out', sys.argv[2])
if '/' in outpath:
savepath = re.sub('/', r'/afd-', outpath)
else:
savepath = 'afd-' + outpath
save(savepath, 'automaton', aut)
print("DFA saved in %s!" % savepath)
else:
print("Input must be a grammar.")
elif "--atg" in sys.argv:
aut = load(sys.argv[2])
if type(aut) is FiniteAutomaton:
grm = RegularGrammar.automaton_to_grammar(aut)
outpath = re.sub('.in', r'.out', sys.argv[2])
if '/' in outpath:
savepath = re.sub('/', r'/gr-', outpath)
else:
savepath = 'gr-' + outpath
save(savepath, 'grammar', grm)
print("GR saved in %s!" % savepath)
else:
print("Input must be an automaton.")
elif "--rta" in sys.argv:
reg = sys.argv[2]
if type(reg) is str:
regexp = RegularExpression(reg)
aut = RegularExpression.regexp_to_automaton(regexp)
savepath = "tests/afnd-reg.out"
save(savepath, 'automaton', aut)
print("Automaton saved in %s!" % savepath)
else:
print("Input must be a regular expression.")
elif "--atr" in sys.argv:
aut = load(sys.argv[2])
if type(aut) is FiniteAutomaton:
reg = RegularExpression.automaton_to_regexp(aut)
outpath = re.sub(r'.in', r'.out', sys.argv[2])
if '/' in outpath:
savepath = re.sub('/', r'/re-', outpath)
else:
savepath = 're-' + outpath
save(savepath, 'regexp', reg)
print("RE saved in %s!" % savepath)
else:
print("Input must be an automaton.")
elif "--min" in sys.argv:
aut = load(sys.argv[2])
if type(aut) is FiniteAutomaton:
aut.minimize()
outpath = re.sub('.in', r'.out', sys.argv[2])
if '/' in outpath:
savepath = re.sub('/', r'/min-', outpath)
else:
savepath = 'min-' + outpath
save(savepath, 'automaton', aut)
print("DFA saved in %s!" % savepath)
else:
print("Input must be an automaton.")
elif "--lex" in sys.argv:
lexer = Tokenizer(sys.argv[2])
output = lexer.analyze()
print('\nTokens')
for i in range(0, len(output[0]), 5):
print(output[0][i:i + 5])
print('\nErrors')
for e in output[1]:
print(e)
print()
elif "--syn" in sys.argv:
source = read_source(sys.argv[2])
print(derive(Parser().grammar, source))
else:
print("Input file is missing.")