-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
363 lines (285 loc) · 11.8 KB
/
Copy pathparser.py
File metadata and controls
363 lines (285 loc) · 11.8 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""
Parser Module - Mathematical Expression Parser
This module provides advanced parsing capabilities for mathematical
expressions including tokenization, syntax analysis, and AST generation.
"""
import re
from typing import List, Dict, Any, Optional, Tuple
from enum import Enum
class TokenType(Enum):
"""Enumeration of different token types in mathematical expressions."""
NUMBER = "NUMBER"
VARIABLE = "VARIABLE"
OPERATOR = "OPERATOR"
FUNCTION = "FUNCTION"
LEFT_PAREN = "LEFT_PAREN"
RIGHT_PAREN = "RIGHT_PAREN"
COMMA = "COMMA"
CONSTANT = "CONSTANT"
EOF = "EOF"
class Token:
"""Represents a token in a mathematical expression."""
def __init__(self, token_type: TokenType, value: str, position: int = 0):
self.type = token_type
self.value = value
self.position = position
def __repr__(self):
return f"Token({self.type}, '{self.value}', {self.position})"
class MathParser:
"""Mathematical expression parser with tokenization and syntax analysis."""
def __init__(self):
"""Initialize the parser with operator precedence and function definitions."""
self.operators = {
'+': {'precedence': 1, 'associativity': 'left'},
'-': {'precedence': 1, 'associativity': 'left'},
'*': {'precedence': 2, 'associativity': 'left'},
'/': {'precedence': 2, 'associativity': 'left'},
'^': {'precedence': 3, 'associativity': 'right'},
'**': {'precedence': 3, 'associativity': 'right'},
}
self.functions = {
'sin', 'cos', 'tan', 'asin', 'acos', 'atan',
'sinh', 'cosh', 'tanh', 'ln', 'log', 'exp',
'sqrt', 'abs', 'floor', 'ceil', 'round',
'derivative', 'integral', 'diff', 'int'
}
self.constants = {
'pi', 'e', 'phi', 'gamma', 'inf', 'nan'
}
def tokenize(self, expression: str) -> List[Token]:
"""
Tokenize a mathematical expression into a list of tokens.
Args:
expression (str): Mathematical expression to tokenize
Returns:
List[Token]: List of tokens representing the expression
"""
tokens = []
position = 0
# Clean expression
expression = expression.replace(' ', '')
while position < len(expression):
char = expression[position]
# Numbers (including decimals and scientific notation)
if char.isdigit() or char == '.':
number, new_pos = self._parse_number(expression, position)
tokens.append(Token(TokenType.NUMBER, number, position))
position = new_pos
continue
# Variables and functions/constants
if char.isalpha():
identifier, new_pos = self._parse_identifier(expression, position)
if identifier in self.functions:
tokens.append(Token(TokenType.FUNCTION, identifier, position))
elif identifier in self.constants:
tokens.append(Token(TokenType.CONSTANT, identifier, position))
else:
tokens.append(Token(TokenType.VARIABLE, identifier, position))
position = new_pos
continue
# Operators
if char in '+-*/^':
# Check for ** operator
if char == '*' and position + 1 < len(expression) and expression[position + 1] == '*':
tokens.append(Token(TokenType.OPERATOR, '**', position))
position += 2
else:
tokens.append(Token(TokenType.OPERATOR, char, position))
position += 1
continue
# Parentheses
if char == '(':
tokens.append(Token(TokenType.LEFT_PAREN, char, position))
position += 1
continue
if char == ')':
tokens.append(Token(TokenType.RIGHT_PAREN, char, position))
position += 1
continue
# Comma
if char == ',':
tokens.append(Token(TokenType.COMMA, char, position))
position += 1
continue
# Skip unknown characters or handle special symbols
position += 1
# Add EOF token
tokens.append(Token(TokenType.EOF, '', position))
return tokens
def _parse_number(self, expression: str, start: int) -> Tuple[str, int]:
"""Parse a number from the expression starting at the given position."""
end = start
has_decimal = False
while end < len(expression):
char = expression[end]
if char.isdigit():
end += 1
elif char == '.' and not has_decimal:
has_decimal = True
end += 1
elif char.lower() == 'e' and end + 1 < len(expression):
# Scientific notation
end += 1
if end < len(expression) and expression[end] in '+-':
end += 1
while end < len(expression) and expression[end].isdigit():
end += 1
break
else:
break
return expression[start:end], end
def _parse_identifier(self, expression: str, start: int) -> Tuple[str, int]:
"""Parse an identifier (variable, function, or constant) from the expression."""
end = start
while end < len(expression) and (expression[end].isalnum() or expression[end] == '_'):
end += 1
return expression[start:end], end
def parse_to_postfix(self, tokens: List[Token]) -> List[Token]:
"""
Convert infix tokens to postfix notation using the Shunting Yard algorithm.
Args:
tokens (List[Token]): List of infix tokens
Returns:
List[Token]: List of tokens in postfix notation
"""
output = []
operator_stack = []
for token in tokens:
if token.type == TokenType.EOF:
break
if token.type in [TokenType.NUMBER, TokenType.VARIABLE, TokenType.CONSTANT]:
output.append(token)
elif token.type == TokenType.FUNCTION:
operator_stack.append(token)
elif token.type == TokenType.COMMA:
# Pop operators until left parenthesis
while (operator_stack and
operator_stack[-1].type != TokenType.LEFT_PAREN):
output.append(operator_stack.pop())
elif token.type == TokenType.OPERATOR:
while (operator_stack and
operator_stack[-1].type == TokenType.OPERATOR and
self._has_higher_precedence(operator_stack[-1], token)):
output.append(operator_stack.pop())
operator_stack.append(token)
elif token.type == TokenType.LEFT_PAREN:
operator_stack.append(token)
elif token.type == TokenType.RIGHT_PAREN:
# Pop operators until left parenthesis
while (operator_stack and
operator_stack[-1].type != TokenType.LEFT_PAREN):
output.append(operator_stack.pop())
# Remove left parenthesis
if operator_stack:
operator_stack.pop()
# If there's a function on top, add it to output
if (operator_stack and
operator_stack[-1].type == TokenType.FUNCTION):
output.append(operator_stack.pop())
# Pop remaining operators
while operator_stack:
output.append(operator_stack.pop())
return output
def _has_higher_precedence(self, op1: Token, op2: Token) -> bool:
"""Check if op1 has higher or equal precedence than op2."""
if op1.value not in self.operators or op2.value not in self.operators:
return False
prec1 = self.operators[op1.value]['precedence']
prec2 = self.operators[op2.value]['precedence']
assoc2 = self.operators[op2.value]['associativity']
return prec1 > prec2 or (prec1 == prec2 and assoc2 == 'left')
def validate_syntax(self, tokens: List[Token]) -> Tuple[bool, str]:
"""
Validate the syntax of tokenized expression.
Args:
tokens (List[Token]): List of tokens to validate
Returns:
Tuple[bool, str]: (is_valid, error_message)
"""
if not tokens or tokens[0].type == TokenType.EOF:
return False, "Empty expression"
paren_count = 0
prev_token = None
for token in tokens:
if token.type == TokenType.EOF:
break
# Check parentheses balance
if token.type == TokenType.LEFT_PAREN:
paren_count += 1
elif token.type == TokenType.RIGHT_PAREN:
paren_count -= 1
if paren_count < 0:
return False, f"Unmatched closing parenthesis at position {token.position}"
# Check for consecutive operators
if (prev_token and
prev_token.type == TokenType.OPERATOR and
token.type == TokenType.OPERATOR):
return False, f"Consecutive operators at position {token.position}"
# Check function calls
if (token.type == TokenType.FUNCTION and
prev_token and
prev_token.type not in [TokenType.OPERATOR, TokenType.LEFT_PAREN, TokenType.COMMA]):
return False, f"Invalid function call at position {token.position}"
prev_token = token
# Check final parentheses balance
if paren_count != 0:
return False, "Unmatched parentheses"
# Check if expression ends properly
if prev_token and prev_token.type == TokenType.OPERATOR:
return False, "Expression cannot end with an operator"
return True, ""
def parse_expression(self, expression: str) -> Dict[str, Any]:
"""
Parse a complete mathematical expression.
Args:
expression (str): Expression to parse
Returns:
Dict containing parsing results
"""
try:
# Tokenize
tokens = self.tokenize(expression)
# Validate syntax
is_valid, error = self.validate_syntax(tokens)
if not is_valid:
return {
'success': False,
'error': error,
'tokens': tokens
}
# Convert to postfix
postfix_tokens = self.parse_to_postfix(tokens)
return {
'success': True,
'tokens': tokens,
'postfix': postfix_tokens,
'expression': expression
}
except Exception as e:
return {
'success': False,
'error': str(e),
'expression': expression
}
# Example usage and testing
if __name__ == "__main__":
parser = MathParser()
test_expressions = [
"2 + 3 * 4",
"sin(x) + cos(y)",
"d/dx[x^2 + 3x + 1]",
"integral(2*x + 5)",
"(a + b) * (c - d)",
"sqrt(16) + ln(e)",
]
print("🧪 Testing Mathematical Parser")
print("-" * 40)
for expr in test_expressions:
print(f"\nExpression: {expr}")
result = parser.parse_expression(expr)
if result['success']:
print("✅ Parsing successful")
print(f"Tokens: {len(result['tokens']) - 1}") # -1 for EOF
print(f"Postfix length: {len(result['postfix'])}")
else:
print(f"❌ Error: {result['error']}")