-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_url_parser.py
146 lines (125 loc) · 6.01 KB
/
simple_url_parser.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
from __future__ import print_function
from transition_table import table
from simple_url_lexer import SimpleUrlLexer
class SimpleUrlParser(object):
def __init__(self, output_lexer):
self.output_lexer = output_lexer
self.non_terminals = [
"url", "httpaddress", "httpaddress1", "httpaddress2", "ftpaddress",
"telnetaddress", "mailtoaddress", "login", "login1", "hostport",
"hostport1", "hostname", "hostname1", "port", "path", "path1", "search",
"search1", "user", "password", "segment", "xalphas", "xalphas1",
"xalpha", "digits", "digits1", "alpha", "digit"
]
self.error_position = []
self.output = {}
self.result = ""
def parse(self):
self.output = {}
self.result = ""
self.error_position = []
run = 0
stack = ["$"]
if self.output_lexer[0].value in table["url"]:
rule = table["url"][self.output_lexer[0].value].split(' ')
for i in rule[::-1]:
stack.append(i)
else:
self.error_position.append(self.output_lexer[0])
return 0
index = 0
while True:
run = run + 1
action = None
last_stack = stack.pop()
# non-terminal
if last_stack in self.non_terminals:
if self.output_lexer[index].value.isalpha():
token_value = "A-Z a-z"
if token_value in table[last_stack]:
rule = table[last_stack][token_value]
if rule == "A | .. | Z | a | .. | z":
stack.append(self.output_lexer[index].value)
action = "Rule alpha -> " + self.output_lexer[index].value
else:
rule = rule.split(' ')
if "epsilon" not in rule:
for i in rule[::-1]:
stack.append(i)
action = "Rule " + last_stack + " -> " + " ".join(rule)
else:
action = "Rule " + token_value + "->" + self.output_lexer[index].value + "not exists"
self.error_position.append(self.output_lexer[index])
index = index + 1
stack.append(last_stack)
elif self.output_lexer[index].value.isdigit():
token_value = "0 .. 9"
if token_value in table[last_stack]:
rule = table[last_stack][token_value]
if rule == "0 | .. | 9":
stack.append(self.output_lexer[index].value)
action = "Rule digit -> " + self.output_lexer[index].value
else:
rule = rule.split(' ')
if "epsilon" not in rule:
for i in rule[::-1]:
stack.append(i)
action = "Rule " + last_stack + " -> " + " ".join(rule)
else:
action = "Rule " + token_value + " -> " + self.output_lexer[index].value + " not exists"
self.error_position.append(self.output_lexer[index])
index = index + 1
stack.append(last_stack)
elif self.output_lexer[index].value in table[last_stack]:
rule = table[last_stack][self.output_lexer[index].value].split(' ')
if "epsilon" not in rule:
for i in rule[::-1]:
stack.append(i)
action = "Rule " + last_stack + " -> " + " ".join(rule)
else:
action = "Rule " + last_stack + " -> " + self.output_lexer[index].value + " not exists"
self.error_position.append(self.output_lexer[index])
index = index + 1
stack.append(last_stack)
# terminal
else:
if self.output_lexer[index].value == last_stack:
if self.output_lexer[index].value != "$":
index = index + 1
else:
if last_stack == "$":
for r in self.output_lexer:
for e in self.error_position:
if e.lexpos == r.lexpos:
r.value = "<mark>" + r.value + "</mark>"
self.result = "".join([r.value for r in self.output_lexer]).replace("$", "")
return 1
else:
self.error_position.append(self.output_lexer[index])
index = index + 1
stack.append(last_stack)
action = "Matched"
else:
action = "Not matched, pushed back to stack"
self.error_position.append(self.output_lexer[index])
index = index + 1
stack.append(last_stack)
self.output[run] = {"input": None, "rules": None, "stack": None}
self.output[run]["input"] = [v.value for v in self.output_lexer[index:]]
self.output[run]["stack"] = stack[:]
self.output[run]["rules"] = action
# for e in self.error_position:
# result[e.]
# for i in self.output_lexer[index:]:
# print(i.value, end='')
# print ("\nstack", stack)
# print(self.error_position)
if __name__ == '__main__':
lexer = SimpleUrlLexer()
lexer.build()
file = open('/home/pukes/Projects/SJ/SJ-Assignment/subor', 'r')
lexer.file_lexical_analysis(file)
parser = SimpleUrlParser(lexer.output)
parser.parse()
# for f in parser.error_position:
# print(f)