Mapytex/pymath/calculus/str2tokens.py

189 lines
6.1 KiB
Python
Raw Normal View History

2014-11-08 15:43:04 +00:00
#!/usr/bin/env python
# encoding: utf-8
2014-12-22 09:57:18 +00:00
from .generic import Stack, isOperator, isNumber, isPolynom
2016-01-07 16:34:23 +00:00
from .operator import op
2014-11-08 15:43:04 +00:00
2014-11-08 16:40:02 +00:00
def str2tokens(exp):
""" Parse the string into tokens then turn it into postfix form
2016-02-13 03:29:26 +00:00
2014-11-08 16:40:02 +00:00
>>> str2tokens('2+3*4')
[2, 3, 4, '*', '+']
>>> str2tokens('2*3+4')
[2, 3, '*', 4, '+']
2014-12-22 09:57:18 +00:00
>>> str2tokens('2x+4')
2016-01-07 16:34:23 +00:00
[2, < <class 'pymath.calculus.polynom.Polynom'> [0, 1]>, '*', 4, '+']
2014-11-08 16:40:02 +00:00
"""
in_tokens = str2in_tokens(exp)
post_tokens = in2post_fix(in_tokens)
2014-11-08 15:43:04 +00:00
2014-11-08 16:40:02 +00:00
return post_tokens
def str2in_tokens(exp):
2014-11-08 15:43:04 +00:00
""" Parse the expression, ie tranform a string into a list of tokens
/!\ float are not availiable yet!
:param exp: The expression (a string)
:returns: list of token
2014-11-11 08:33:31 +00:00
>>> str2in_tokens('2+3*4')
[2, '+', 3, '*', 4]
>>> str2in_tokens('2*3+4')
[2, '*', 3, '+', 4]
2014-11-08 15:43:04 +00:00
"""
tokens = ['']
for character in exp:
if character.isdigit():
# for "big" numbers (like 2345)
if type(tokens[-1]) == int:
if tokens[-1] > 0:
tokens[-1] = tokens[-1]*10 + int(character)
else:
tokens[-1] = tokens[-1]*10 - int(character)
# Special case for "-" at the begining of an expression or before "("
elif tokens[-1] == "-" and \
str(tokens[-2]) in " (+-*/:":
tokens[-1] = - int(character)
else:
tokens.append(int(character))
elif character in "+-*/:^":
2014-12-02 11:37:23 +00:00
tokens.append(character)
2014-11-08 15:43:04 +00:00
elif character == ")":
tokens.append(character)
2014-11-11 08:33:31 +00:00
elif character == "(":
2014-12-22 09:57:18 +00:00
# If "3(", ")(", "x("
2014-11-08 16:40:02 +00:00
if isNumber(tokens[-1]) \
2014-12-22 09:57:18 +00:00
or tokens[-1] == ")" \
or isPolynom(tokens[-1]):
2014-12-02 11:37:23 +00:00
tokens.append("*")
tokens.append(character)
2014-11-08 15:43:04 +00:00
2014-12-22 09:57:18 +00:00
elif character.isalpha():
# If "3x", ")x", "xy"
if isNumber(tokens[-1]) \
or tokens[-1] == ")" \
or isPolynom(tokens[-1]):
tokens.append("*")
2016-01-07 16:34:23 +00:00
from pymath.calculus.polynom import Polynom
2014-12-22 09:57:18 +00:00
tokens.append(Polynom([0,1], letter = character))
2014-11-08 15:43:04 +00:00
elif character == ".":
raise ValueError("No float number please")
elif character != " ":
raise ValueError("{} is an unvalid character".format(character))
return tokens[1:]
2014-11-08 16:40:02 +00:00
def in2post_fix(infix_tokens):
2014-11-08 15:43:04 +00:00
""" From the infix_tokens list compute the corresponding postfix_tokens list
2016-02-13 03:29:26 +00:00
2014-11-08 15:43:04 +00:00
@param infix_tokens: the infix list of tokens to transform into postfix form.
@return: the corresponding postfix list of tokens.
2014-12-02 11:37:23 +00:00
>>> in2post_fix([op.par, 2, op.add, 5, op.sub, 1, ')', op.div, op.par, 3, op.mul, 4, ')'])
[2, 5, 1, '-', '+', 3, 4, '*', '/']
2014-12-02 11:37:23 +00:00
>>> in2post_fix([op.sub1, op.par, op.sub1, 2, ')'])
2014-11-08 16:40:02 +00:00
[2, '-', '-']
2014-12-02 11:37:23 +00:00
>>> in2post_fix([op.sub1, op.par, op.sub1, 2, op.add, 3, op.mul, 4, ')'])
2014-11-08 16:40:02 +00:00
[2, '-', 3, 4, '*', '+', '-']
2014-11-08 15:43:04 +00:00
"""
2014-11-08 16:40:02 +00:00
# Stack where operator will be stocked
2014-11-08 15:43:04 +00:00
opStack = Stack()
2014-11-08 16:40:02 +00:00
# final postfix list of tokens
postfix_tokens = []
# stack with the nbr of tokens still to compute in postfix_tokens
arity_Stack = Stack()
arity_Stack.push(0)
for (pos_token,token) in enumerate(infix_tokens):
2014-11-08 15:43:04 +00:00
2014-12-02 11:37:23 +00:00
# Pour voir ce qu'il se passe dans cette procédure
2014-11-08 16:40:02 +00:00
#print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
2014-11-11 08:33:31 +00:00
if token == ")":
2014-12-02 11:37:23 +00:00
next_op = opStack.pop()
while next_op != "(":
postfix_tokens.append(next_op)
next_op = opStack.pop()
2014-11-08 16:40:02 +00:00
2016-02-13 03:29:26 +00:00
# Go back to old arity
2014-11-08 16:40:02 +00:00
arity_Stack.pop()
# Raise the arity
arity = arity_Stack.pop()
arity_Stack.push(arity + 1)
2014-12-02 11:37:23 +00:00
elif op.can_be_operator(token):
2014-11-11 08:33:31 +00:00
if token == "(":
2014-12-02 11:37:23 +00:00
opStack.push(op.get_op(token))
2014-11-11 08:33:31 +00:00
# Set next arity counter
arity_Stack.push(0)
else:
2016-02-13 03:29:26 +00:00
arity = arity_Stack.pop()
2014-12-02 11:37:23 +00:00
token_op = op.get_op(token, arity + 1)
2014-11-11 08:33:31 +00:00
# Reset arity to 0 in case there is other operators (the real operation would be "-op.arity + 1")
arity_Stack.push(0)
2014-12-02 11:37:23 +00:00
while (not opStack.isEmpty()) and opStack.peek().priority >= token_op.priority:
next_op = opStack.pop()
postfix_tokens.append(next_op)
opStack.push(token_op)
#print("--", token, " -> ", str(arity + 1))
2014-11-08 15:43:04 +00:00
else:
2014-11-08 16:40:02 +00:00
postfix_tokens.append(token)
arity = arity_Stack.pop()
arity_Stack.push(arity + 1)
2014-11-08 15:43:04 +00:00
2014-11-11 08:33:31 +00:00
## Pour voir ce qu'il se passe dans cette procédure
#print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
2014-11-08 15:43:04 +00:00
while not opStack.isEmpty():
2014-12-02 11:37:23 +00:00
next_op = opStack.pop()
postfix_tokens.append(next_op)
2014-11-08 16:40:02 +00:00
2014-11-11 08:33:31 +00:00
## Pour voir ce qu'il se passe dans cette procédure
#print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
2014-11-08 16:40:02 +00:00
if arity_Stack.peek() != 1:
raise ValueError("Unvalid expression. The arity Stack is ", str(arity_Stack))
2014-11-08 15:43:04 +00:00
2014-11-08 16:40:02 +00:00
return postfix_tokens
2014-11-08 15:43:04 +00:00
2014-11-08 16:40:02 +00:00
if __name__ == '__main__':
#a, s, m, d, p = Operator("+"), Operator("-"), Operator("*"), Operator("/"), Operator("^")
#in_tokens = str2in_tokens("2+3*4")
#print("\t in_tokens :" + str(in_tokens))
#
#print(in2post_fix(in_tokens))
2014-11-08 15:43:04 +00:00
2014-12-22 09:57:18 +00:00
#print(in2post_fix([op.par, 2, op.add, 5, op.sub, 1, ')', op.div, op.par, 3, op.mul, 4, ')']))
#print(in2post_fix([op.sub1, op.par, op.sub1, 2, ')']))
#print(in2post_fix([op.sub1, op.par, op.sub1, 2, op.add, 3, op.mul, 4, ')']))
print(str2tokens('2*3+4'))
print("\n------")
print(str2tokens('2x+4'))
print("\n------")
print(str2tokens('xx+4'))
print("\n------")
print(str2tokens('x(2+1)+4'))
print("\n------")
#import doctest
#doctest.testmod()
2014-11-08 15:43:04 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
2016-02-13 03:29:26 +00:00
# cursor: 16 del