-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
192 lines (171 loc) · 7.08 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import pandas as pd
from visual_automata.fa.dfa import VisualDFA
from automata.fa.dfa import DFA
import streamlit as st
from PIL import Image # IMAGE MODULE
# TODO
# Reset input box when clicking reset button
# CONSTANTS
QUESTION = ["First Expression", "Second Expression"]
Q1 = "( b | aa | ab ) ( a | b )´ ( bb | aba | ab )´ ( aaa | bbb ) ( a | b ) ( a | b | ab )´"
Q2 = "( 1 | 0 )´ ( 11 | 00 | 101 | 010 ) ( 1 | 0 | 11 | 00 | 101 )´ ( 11 | 00 ) (11 | 00 | 101 )´ ( 1 | 0 ) ( 1 | 0 | 11 )´"
# Streamlit Start
st.title("The Automata Wizard 🧙🏻♂️")
st.markdown("""
#### The Automata Wizard will help you check String if it is valid on 2 sample Regular Expression while simulating the path the string took in the deterministic finite automaton (DFA)
""")
image = Image.open("logo_wiz.png")
st.sidebar.image(image)
input_box = st.sidebar.selectbox(
"Expression List:", ("Select an Expression", QUESTION[0], QUESTION[1])) # select box
# Page Control
# FIRST EXPRESSION
if input_box == QUESTION[0]:
first_dfa = DFA(
states={'q0', 'q1', 'q2', 'q3', 'q4',
'q5', 'q6', 'q7', 'q8'},
input_symbols={'a', 'b'},
transitions={
'q0': {'a': 'q1', 'b': 'q2'},
'q1': {'a': 'q2', 'b': 'q2'},
'q2': {'a': 'q3', 'b': 'q5'},
'q3': {'a': 'q4', 'b': 'q5'},
'q4': {'a': 'q7', 'b': 'q5'},
'q5': {'a': 'q3', 'b': 'q6'},
'q6': {'a': 'q3', 'b': 'q7'},
'q7': {'a': 'q8', 'b': 'q8'},
'q8': {'a': 'q8', 'b': 'q8'}
},
initial_state='q0',
final_states={'q8'}
)
vis_dfa = VisualDFA(first_dfa) # Create Visual DFA from DFA
st.header("Regular Expression:")
st.subheader(Q1)
# Input Form
with st.form("form"):
st.header("String Checker 🧵")
first_user_input = st.text_input(
label="Enter String To Check If Valid: ⤵")
submit_button = st.form_submit_button(label="Submit")
# Try catch to catch error from switching expression
try:
if not first_user_input:
st.write("Enter a string to simulate DFA")
elif submit_button:
if first_dfa.accepts_input(first_user_input):
with st.form("1st Rectangle"):
st.header(
"deterministic-finite-automaton (DFA) Simulation:")
st.write(vis_dfa.show_diagram(first_user_input))
st.subheader("String is accepted! 🧙🏻♂️💯🎇")
# Table Simulation
df = pd.DataFrame(
vis_dfa.input_check(first_user_input))
listy = []
for x in df.iloc[:, 2]:
if x == "*q8":
# listy.append("Reject")
listy.append("Final_State")
else:
listy.append("Accepted")
df["Accepted/Final_State"] = listy
st.write(df)
st.header("Context-Free Grammar(CFG):")
st.markdown('''
Start symbol: S \n
S → PQRTUV \n
P → b | aa | ab \n
Q → aQ | bQ | ε \n
R → bbR | abaR | abR | ε \n
T → aaa | bbb \n
U → a | b \n
V → aε | bε | abε | ε OR V -> UV | abV | ε \n
''')
# Reset
reset_button = st.form_submit_button(label="Reset")
if reset_button:
first_user_input = ("")
else:
st.header(
"deterministic-finite-automaton (DFA) Simulation:")
st.write(vis_dfa.show_diagram(first_user_input))
st.subheader("String is not accepted! 🧙🏻♂️💢")
except:
st.write("Wrong Input!")
# SECOND EXPRESSION
elif input_box == QUESTION[1]:
dfa = DFA(
states={'q0', 'q1', 'q2', 'q3', 'q4',
'q5', 'q6', 'q7', 'q8'},
input_symbols={'0', '1'},
transitions={
'q0': {'0': 'q1', '1': 'q2'},
'q1': {'0': 'q4', '1': 'q3'},
'q2': {'0': 'q3', '1': 'q4'},
'q3': {'0': 'q4', '1': 'q4'},
'q4': {'0': 'q5', '1': 'q6'},
'q5': {'0': 'q7', '1': 'q6'},
'q6': {'0': 'q5', '1': 'q7'},
'q7': {'0': 'q8', '1': 'q8'},
'q8': {'0': 'q8', '1': 'q8'}
},
initial_state='q0',
final_states={'q8'}
)
vis_dfa = VisualDFA(dfa)
# Start
st.header("Regular Expression:")
st.subheader(Q2)
# User input Box
with st.form("form"):
st.header("String Checker 🧵")
second_user_input = st.text_input(
label="Enter String To Check If Valid: ⤵")
submit_button = st.form_submit_button(label="Submit")
# Check if string is valid
try:
if not second_user_input:
st.write("Enter a string to simulate DFA")
elif submit_button:
if dfa.accepts_input(second_user_input): # If accepted
with st.form("2nd Rectangle"):
st.header(
"deterministic-finite-automaton (DFA) Simulation:")
st.write(vis_dfa.show_diagram(second_user_input))
st.subheader("String is accepted! 🧙🏻♂️💯🎇")
# Table SECTION
df = pd.DataFrame(
vis_dfa.input_check(second_user_input))
listy = []
for x in df.iloc[:, 2]:
if x == "*q8":
listy.append("Final_State")
else:
listy.append("Accepted")
df["Accepted/Final_State"] = listy
st.write(df)
st.header("Context-Free Grammar(CFG):")
st.markdown('''
Start symbol: S \n
S → PQRTUVW \n
P → 1P | 0P | ε \n
Q → 11 | 00 | 101 | 010 \n
R → 1R | 0R | 11R | 00R | 101R | ε \n
T → 11 | 00 \n
U → 11U | 00U | 101U | ε \n
V → 1 | 0 \n
W → 1W | 0W | 11W | ε \n
''')
reset_button = st.form_submit_button(label="Reset")
if reset_button:
second_user_input = ""
else: # IF REJECTED
st.header(
"deterministic-finite-automaton (DFA) Simulation:")
st.write(vis_dfa.show_diagram(second_user_input))
st.subheader("String is not accepted! 🧙🏻♂️💢")
except:
st.write("Wrong Input!")
else:
st.header("⬅ Select an expression to start 🧝🏻♂️")