-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFA.py
150 lines (131 loc) · 4.71 KB
/
FA.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
canBeFirstChar = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'_', '$']
# State finalnya adalah ketika first character dari string adalah huruf alfabet, _, dan $.
def check_var(input):
flag = False
firstChar = input[0]
for char in canBeFirstChar:
if (firstChar == char):
flag = True
return flag
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
# operator = ['+', '-', '*' , '/', '<', '>', '%', '^', '&', '>>>',
# '<<', '>>', '|', '~', '==', '>=', '<=', '===', '!=', '!==',
# '&&', '||', '!', '??', '**']
single_operator = ['+', '-', '*', '/', '<', '>', '%', '^', '&', '>',
'<', '|', '=', '?']
def check_arithmetic_expression(input : str) -> bool:
# Menghapus spasi
input = input.replace(' ', '')
if(input[0] in single_operator):
return False
else:
return q0(input)
def q0(input : str) -> bool:
if (len(input) == 0):
return True
else:
if (input[0] == '!' or input[0] == '~'):
return q3(input)
elif (input[0] in digits):
return q0(input[1:])
elif (input[0] == '.'):
return q1(input[1:])
elif (input[0] in single_operator):
return q2(input)
else:
return False
def q1(input : str) -> bool:
if (len(input) == 0):
return True
else:
if(input[0] in digits):
return q1(input[1:])
elif(input[0] in single_operator):
return q2(input)
else:
return False
def q3(input: str) -> bool:
if(len(input) == 0):
return False
else:
if(input[0] == '~' or input[0] == '!'):
return q3(input[1:])
else:
return q0(input[1:])
# Cek expression
def q2(input : str) -> bool:
if(len(input) == 0):
return False
else:
firstChar = input[0]
if (firstChar == '<'):
if(len(input) > 2):
if(input[1] == '<' or input[1] == '='):
return q0(input[2:])
if (len(input) > 1):
return q0(input[1:])
elif (firstChar == '>'):
if(len(input) > 3):
if(input[1] == '>' and input[2] == '>'):
return q0(input[3:])
if (len(input) > 2):
if(input[1] == '>' or input[1] == '='):
return q0(input[2:])
if (len(input) > 1):
return q0(input[1:])
elif (firstChar == '|'):
if(len(input) > 2):
if(input[1] == '|'):
return q0(input[2:])
if(len(input) > 1):
return q0(input[1:])
elif (firstChar == '?'):
if(len(input) > 2):
if(input[1] == '?'):
return q0(input[2:])
if(len(input) > 1):
return q0(input[1:])
# elif (firstChar == '!'):
# return q2(input[1:])
# elif (firstChar == '~'):
# return q2(input[1:])
elif (firstChar == '*'):
if(len(input) > 2):
if(input[1] == '*'):
return q0(input[2:])
if(len(input) > 1):
return q0(input[1:])
elif (firstChar == '='):
if(len(input) > 3):
if(input[1] == '=' and input[2] == '='):
return q0(input[3:])
if (len(input) > 2):
if(input[1] == '='):
return q0(input[2:])
if (len(input) > 1):
return q0(input[1:])
elif (firstChar == '&'):
if(len(input) > 2):
if(input[1] == '&'):
return q0(input[2:])
if(len(input) > 1):
return q0(input[1:])
elif (firstChar == '/'):
if(len(input) > 2):
if (input[1] == '/'):
return False
if(len(input) > 1):
return q0(input[1:])
elif (firstChar == '+' or firstChar == '-' or firstChar == '^' or firstChar == '%'):
return q0(input[1:])
elif (firstChar in digits or firstChar == '.'):
if(firstChar in digits):
return q0(input[1:])
else:
return q1(input[1:])
else:
return False
# print(check_arithmetic_expression('1 + 1 * 10 ** 2 - 1 % 2 ^ 10 & 1 - .1 > 1 < 2 >= 1 << 2 >> 1 | 1 || 2 / 1.1 + !! 1'))
# print(check_arithmetic_expression('!'))