-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem5.py
66 lines (60 loc) · 1.99 KB
/
problem5.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
"""
Problem 5 (30): Write a program (C++, C#, Java, Python) that parses a file and for each string it finds
(strings are separated by whitespace) outputs the string and which of the above languages it is a member of.
If the string is not a member of any, then it outputs 'none'. For organizational reasons, make a boolean
function for each of the languages. Please provide appropriate and helpful comments.
Example Run:
File: test.txt
Abcde: Id
aAa9: Id
3422222: Num
;: Semicolon
wHile: Id
while: Id, While
&&: And
8eo: none
&: none
;asdf: none
"""
from p5_dfa_definitions import *
from p5_dfa_testing import *
# reads file, splits into array
def split_readfile():
with open("test.txt") as file:
open_line = file.readline()
lines = open_line.split()
return lines
# logic to print out accepting DFAs in file
def whichDFA():
lines = split_readfile()
print("File: test.txt")
for x in lines:
all_reject = True # variable to print none AND inset commas where needed
print(x + ": ", end='') # prints line name
if id_A.accepts_input(x): # if True, print Id
print("Id", end='')
all_reject = False # flag variable flip
if num_B.accepts_input(x):
if all_reject == False:
print(", ", end='')
print("Num", end='')
all_reject = False
if semicolon_C.accepts_input(x):
if all_reject == False:
print(", ", end='')
print("Semicolon", end='')
all_reject = False
if and_D.accepts_input(x):
if all_reject == False:
print(", ", end='')
print("And", end='')
all_reject = False
if while_E.accepts_input(x):
if all_reject == False:
print(", ", end='')
print("While", end='')
all_reject = False
if all_reject == True:
print("none", end='')
print("")
whichDFA()