-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
executable file
·197 lines (177 loc) · 6.55 KB
/
test.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
193
194
195
196
197
#!/usr/bin/python
# -*- coding: latin-1 -*-
# Deterministic Finite Automata (DFA) implementation in python
#Author: Earl Austin Zuniga
#Bicol University
import os,sys
import itertools
from itertools import product
from prettytable import PrettyTable
import pygraphviz as pgv
from IPython.display import Image, display
import re
def greedy_scanner(arr,pos):
for i in range(pos,len(arr)):
if(i+1 != len(arr)):
if(arr[i+1] == '*'):
return 1
else:
return 0
else:
break
def concatenate_list_data(list):
result= ''
for element in list:
result += str(element)
return result
def print_DFA_diagram_language(prio,less_prio):
G=pgv.AGraph()
G=pgv.AGraph(strict=False,directed=True)
initial = 0
target = 1
to_append = 'digraph G {size="4,4"; '
prio = prio.split('(')
for expression in prio:
if expression == '':
pass
else:
if '+' in expression:
arr = expression.split('+')
for i in range(0,len(arr)):
arr[i] = map(str,arr[i])
i = 0
indi = 0
while i < len(arr):
j = 0
start = initial
while j < len(arr[i]):
if(greedy_scanner(arr[i],j) == 1 and re.match('^[a-z,A-Z]',arr[i][j])):
to_append = to_append + '%s -> %s [label="%s"];'%(start,start,arr[i][j])
j+=2
elif(greedy_scanner(arr[i],j) == 1 and arr[i][j]== ')'):
print "a"
to_append = to_append + '%s -> %s;'%(start,target)
to_append = to_append + '%s -> %s;'%(start-1,target)
to_append = to_append + '%s -> %s;'%(target,initial)
j+=2
else:
to_append = to_append + '%s -> %s [label="%s"];'%(start,target,arr[i][j])
start = start + 1
target = target+1
j+=1
if(indi > 0):
start = target - 1
start = initial
indi = 1
i+=1
initial = target
target = target + 1
elif ')*' in expression:
arr = map(str,expression)
i = 0
while i < len(arr):
if(greedy_scanner(arr,i) == 1):
to_append = to_append + '%s -> %s [label="%s"];'%(initial,initial,arr[i])
i+=2
else:
if(arr[i] == ')' and arr[i+1] == '*'):
i+=2
print "au"
else:
to_append = to_append + '%s -> %s [label="%s"];'%(initial,target,arr[i])
initial = initial + 1
target = target+1
i+=1
else:
arr = map(str,expression)
i = 0
while i < len(arr):
if(greedy_scanner(arr,i) == 1):
to_append = to_append + '%s -> %s [label="%s"];'%(initial,initial,arr[i])
i+=2
else:
if(arr[i] == ')'):
i+=1
pass
else:
to_append = to_append + '%s -> %s [label="%s"];'%(initial,target,arr[i])
initial = initial + 1
target = target+1
i+=1
less_prio = less_prio.split('(')
for expression in less_prio:
if expression == '':
pass
else:
if '+' in expression:
arr = expression.split('+')
for i in range(0,len(arr)):
arr[i] = map(str,arr[i])
i = 0
indi = 0
while i < len(arr):
j = 0
start = initial
while j < len(arr[i]):
if(greedy_scanner(arr[i],j) == 1 and re.match('^[a-z,A-Z]',arr[i][j])):
to_append = to_append + '%s -> %s [label="%s"];'%(start,start,arr[i][j])
j+=2
else:
to_append = to_append + '%s -> %s [label="%s"];'%(start,target,arr[i][j])
start = start + 1
target = target+1
j+=1
if(indi > 0):
start = target - 1
start = initial
indi = 1
i+=1
else:
arr = map(str,expression)
i = 0
while i < len(arr):
if(greedy_scanner(arr,i) == 1):
to_append = to_append + '%s -> %s [label="%s"];'%(initial,initial,arr[i])
i+=2
else:
to_append = to_append + '%s -> %s [label="%s"];'%(initial,target,arr[i])
initial = initial + 1
target = target+1
i+=1
to_append = to_append + '}'
A=pgv.AGraph(to_append)
A.layout()
A.layout(prog='dot')
A.draw('dfa-language.png')
def process_regex(arr):
arr_map = map(str,arr)
i = 0
priority = list()
less_prio = list()
paren = -1
while i < len(arr_map):
if arr_map[i] == '(':
paren = 1
elif arr_map[i] == ')':
paren = 0
priority.append(')')
if paren == 1:
priority.append(arr_map[i])
i +=1
elif re.match('^[a-z,A-Z,0-9]',arr_map[i]) or arr_map[i] == '+':
if(greedy_scanner(arr_map,i) == 1):
less_prio.append(arr_map[i]+'*')
i+=2
else:
less_prio.append(arr_map[i])
i+=1
elif arr_map[i] == '*' and arr_map[i-1] == ')':
priority.append(arr_map[i])
i+=1
else:
i+=1
priority = concatenate_list_data(priority)
less_prio = concatenate_list_data(less_prio)
print_DFA_diagram_language(priority,less_prio)
regex = "(a+b)* (abcd*) (a+b)* ab"
process_regex(regex)