-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterm_parser.py
More file actions
155 lines (130 loc) · 3.11 KB
/
term_parser.py
File metadata and controls
155 lines (130 loc) · 3.11 KB
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
# parse terms
INFIX_OPERATORS = ["+", "-", "*", "/"]
PREFIX_OPERATORS = ["+", "-"]
OPERATORS = INFIX_OPERATORS
PREFERENCES = { "+": 1, "-": 1, "*": 2, "/": 2}
verbose = False
input = ""
pos = 0
def debug(message):
if not verbose:
return
print(">>> ", message)
def setInput(text):
global input
global pos
input = text
pos = 0
def hasData():
return pos < len(input)
def printPos():
print(input)
print(" " * pos + "^")
def getChar():
if verbose:
printPos()
return input[pos]
def readChar():
global pos
c = getChar()
pos = pos + 1
# print(pos)
return c
def skipWhitespace():
while hasData() and getChar().isspace():
readChar()
def readConstant():
skipWhitespace()
c = ""
while hasData() and getChar().isdigit():
c = c + readChar()
return c
def getPreference(operator):
if len(operator) == 0:
return 0
return PREFERENCES[operator]
def readOperator():
skipWhitespace()
if not hasData():
return ""
c = getChar()
if c in OPERATORS:
readChar()
return c
return ""
def readBracketTerm():
assert "(" == readChar(), "( expected"
r = readMaximalTerm()
debug("bracket result: " + str(r))
skipWhitespace()
assert ")" == readChar(), ") expected"
return r
def readMinimalTerm():
skipWhitespace()
if not hasData():
return []
c = getChar()
r = ""
if c == "(":
r = readBracketTerm()
elif c.isdigit():
r = readConstant()
return r
def readMaximalTerm():
t1 = readMinimalTerm()
op1 = readOperator()
while len(op1) > 0:
t2 = readMinimalTerm()
assert len(t2) > 0
op2 = readOperator()
if len(op2) == 0:
return [op1, t1, t2]
if getPreference(op2) <= getPreference(op1):
t1 = [op1, t1, t2]
op1 = op2
else:
t3 = readMaximalTerm()
assert len(t3) > 0
return [op1, t1, [op2, t2, t3]]
return t1
def createTerm(text):
setInput(text)
t = readMaximalTerm()
print(t)
printTerm(t)
return t
def hasPrefixOperator(term):
return len(term) == 2 and term[0] in PREFIX_OPERATORS
def term2str(term):
r = ""
if type(term) is list:
if hasPrefixOperator(term):
r = term[0]
for t in term[1:]:
r = r + term2str(t)
else:
r = "("
for t in term[1:-1]:
r = r + term2str(t)
r = r + term[0]
r = r + term2str(term[-1])
r = r + ")"
else:
r = str(term)
return r
def printTerm(term):
print(term2str(term))
#printTerm(createTerm("17 * 8"))
# Testing
def test(input, output):
assert term2str(createTerm(input)) == output
test("17 * 8", "(17*8)")
test("((1-2))", "(1-2)")
test("71+2+3", "((71+2)+3)")
test("9 /3 ", "(9/3)")
test("1+2+3", "((1+2)+3)")
test("1-3+(7+9)", "((1-3)+(7+9))")
test("1-3+7+9", "(((1-3)+7)+9)")
test("1+3*7", "(1+(3*7))")
test("1*2+3*4","((1*2)+(3*4))")
createTerm("1+2+3+4")