-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparserGeneral.py
108 lines (94 loc) · 3.21 KB
/
parserGeneral.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import collections
import xml.etree.ElementTree as ET
from anytree import Node, RenderTree, PostOrderIter
def parseInputString(input, grammar, terminalSymbols, nonTerminalSymbols, analyzeTable):
"""
Demo 性质的函数,分析一个输入字符串,比如 "i+i*i"
PARAM <input>: inputString = 'i+i*i'
"""
print('[Start parsing]:', input)
# 初始化
parseStack = ['#']
grammarTop = list(grammar.keys())[0]
parseStack.append(grammarTop)
input = input + '#'
# 开始分析
i = 0
print('{0: >30}'.format(str(parseStack)), input[i])
while True:
if parseStack[-1] in terminalSymbols and input[i] in terminalSymbols:
if parseStack[-1] == '#' and input[i] == '#':
print('[INFO] Parse Success!')
break
elif parseStack[-1] == input[i] and input[i] != '#':
parseStack.pop()
i = i + 1
print('{0: >30}'.format(str(parseStack)), '-')
else:
print('[ERROR] Parse Failed!')
break
elif parseStack[-1] in nonTerminalSymbols:
row = analyzeTable[parseStack[-1]]
if input[i] in row.keys():
parseStack.pop()
for item in reversed(row[input[i]]):
if item != 'ε':
parseStack.append(item)
else:
print('[ERROR] Parse failed.')
break
print('{0: >30}'.format(str(parseStack)), input[i], row[input[i]])
def parseToken(inputToken, grammar, terminalSymbols, nonTerminalSymbols, analyzeTable):
"""
开始了!分析树!
"""
parseStack = ['#']
grammarTop = list(grammar.keys())[0]
parseStack.append(grammarTop)
tree = Node(grammarTop)
i = 0
print(parseStack, inputToken[i][1].text)
while True:
if parseStack[-1] in terminalSymbols and (inputToken[i][1].text in terminalSymbols or inputToken[i][2].text in terminalSymbols):
if parseStack[-1] == '#' and inputToken[i][1].text == '#':
print('[INFO] Parse success!')
break
elif (parseStack[-1] == inputToken[i][1].text or parseStack[-1] == inputToken[i][2].text) and parseStack[-1] != '#':
parseStack.pop()
i = i + 1
print(parseStack, inputToken[i][1].text)
else:
print('[ERROR] Parse failed!')
break
elif parseStack[-1] in nonTerminalSymbols:
row = analyzeTable[parseStack[-1]]
for node in PostOrderIter(tree):
if node.name == parseStack[-1]:
currentRoot = node
break
if inputToken[i][1].text in row.keys():
rule = row[inputToken[i][1].text]
parseStack.pop()
for item in reversed(rule):
if item != 'ε':
parseStack.append(item)
for item in rule:
if item != 'ε':
Node(item, parent=currentRoot)
print(parseStack, inputToken[i][1].text, rule)
elif inputToken[i][2].text in row.keys():
rule = row[inputToken[i][2].text]
parseStack.pop()
for item in reversed(rule):
if item != 'ε':
parseStack.append(item)
for item in rule:
if item != 'ε':
Node(item, parent=currentRoot)
print(parseStack, inputToken[i][1].text, rule)
else:
print('[ERROR] Parse failed!')
break
return tree