debug str2tokens
This commit is contained in:
parent
d615214c35
commit
633e6b4e2c
@ -9,13 +9,14 @@ class Operator(str):
|
|||||||
|
|
||||||
"""The operator class, is a string (representation of the operator) with its arity"""
|
"""The operator class, is a string (representation of the operator) with its arity"""
|
||||||
|
|
||||||
PRIORITY = {"^": [0, 5], "/": [0, 4], "*" : [0,3], ":": [0,3], "+": [0,1], "-":[2,1]}
|
PRIORITY = {"^": [0, 5], "/": [0, 4], "*" : [0,3], ":": [0,3], "+": [0,1], "-":[2,1], "(":[0,0]}
|
||||||
OPERATIONS = { \
|
OPERATIONS = { \
|
||||||
"+": ["", ("__add__","__radd__")],\
|
"+": ["", ("__add__","__radd__")],\
|
||||||
"-": ["__neg__", ("__sub__", "__rsub__")], \
|
"-": ["__neg__", ("__sub__", "__rsub__")], \
|
||||||
"*": ["", ("__mul__", "__rmul__")], \
|
"*": ["", ("__mul__", "__rmul__")], \
|
||||||
"/": ["", ("__div__","__rdiv__")], \
|
"/": ["", ("__div__","__rdiv__")], \
|
||||||
"^": ["", ("__pow__", "")] \
|
"^": ["", ("__pow__", "")], \
|
||||||
|
"(": ["",""],\
|
||||||
}
|
}
|
||||||
TXT = { \
|
TXT = { \
|
||||||
"+": ["", "{op1} + {op2}"] ,\
|
"+": ["", "{op1} + {op2}"] ,\
|
||||||
@ -23,6 +24,7 @@ class Operator(str):
|
|||||||
"*": ["", "{op1} * {op2}"] ,\
|
"*": ["", "{op1} * {op2}"] ,\
|
||||||
"/": ["", "{op1} / {op2}"] ,\
|
"/": ["", "{op1} / {op2}"] ,\
|
||||||
"^": ["", "{op1} ^ {op2}"] ,\
|
"^": ["", "{op1} ^ {op2}"] ,\
|
||||||
|
"(": ["",""],\
|
||||||
}
|
}
|
||||||
TEX = { \
|
TEX = { \
|
||||||
"+": ["", "{op1} + {op2}"] ,\
|
"+": ["", "{op1} + {op2}"] ,\
|
||||||
@ -30,6 +32,7 @@ class Operator(str):
|
|||||||
"*": ["", "{op1} \\times {op2}"] ,\
|
"*": ["", "{op1} \\times {op2}"] ,\
|
||||||
"/": ["", "\\frac{{ {op1} }}{{ {op2} }}"] ,\
|
"/": ["", "\\frac{{ {op1} }}{{ {op2} }}"] ,\
|
||||||
"^": ["", "{op1}^{{ {op2} }}"] ,\
|
"^": ["", "{op1}^{{ {op2} }}"] ,\
|
||||||
|
"(": ["",""],\
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,10 +26,10 @@ def str2in_tokens(exp):
|
|||||||
:param exp: The expression (a string)
|
:param exp: The expression (a string)
|
||||||
:returns: list of token
|
:returns: list of token
|
||||||
|
|
||||||
>>> str2tokens('2+3*4')
|
>>> str2in_tokens('2+3*4')
|
||||||
['2', '+', '3', '*', '4']
|
[2, '+', 3, '*', 4]
|
||||||
>>> str2tokens('2*3+4')
|
>>> str2in_tokens('2*3+4')
|
||||||
['2', '*', '3', '+', '4']
|
[2, '*', 3, '+', 4]
|
||||||
"""
|
"""
|
||||||
tokens = ['']
|
tokens = ['']
|
||||||
|
|
||||||
@ -56,13 +56,13 @@ def str2in_tokens(exp):
|
|||||||
elif character == ")":
|
elif character == ")":
|
||||||
tokens.append(character)
|
tokens.append(character)
|
||||||
|
|
||||||
elif character in "(":
|
elif character == "(":
|
||||||
# If "3(", ")("
|
# If "3(", ")("
|
||||||
if isNumber(tokens[-1]) \
|
if isNumber(tokens[-1]) \
|
||||||
or tokens[-1] == ")" :
|
or tokens[-1] == ")" :
|
||||||
#tokens.append(Operator("*"))
|
#tokens.append(Operator("*"))
|
||||||
tokens.append(Operator("*"))
|
tokens.append(Operator("*"))
|
||||||
tokens.append(character)
|
tokens.append(Operator(character))
|
||||||
|
|
||||||
elif character == ".":
|
elif character == ".":
|
||||||
raise ValueError("No float number please")
|
raise ValueError("No float number please")
|
||||||
@ -81,12 +81,14 @@ def in2post_fix(infix_tokens):
|
|||||||
@return: the corresponding postfix list of tokens.
|
@return: the corresponding postfix list of tokens.
|
||||||
|
|
||||||
>>> a, s, m, d, p = Operator("+"), Operator("-"), Operator("*"), Operator("/"), Operator("^")
|
>>> a, s, m, d, p = Operator("+"), Operator("-"), Operator("*"), Operator("/"), Operator("^")
|
||||||
|
>>> s1 = Operator("-", 1)
|
||||||
|
>>> par = Operator("(")
|
||||||
|
|
||||||
>>> in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')'])
|
>>> in2post_fix([par, 2, a, 5, s, 1, ')', d, par, 3, m, 4, ')'])
|
||||||
[2, 5, '+', 1, '-', 3, 4, '*', '/']
|
[2, 5, '+', 1, '-', 3, 4, '*', '/']
|
||||||
>>> in2post_fix(['-', '(', '-', 2, ')'])
|
>>> in2post_fix([s1, par, s1, 2, ')'])
|
||||||
[2, '-', '-']
|
[2, '-', '-']
|
||||||
>>> in2post_fix(['-', '(', '-', 2, '+', 3, '*', 4, ')'])
|
>>> in2post_fix([s1, par, s1, 2, a, 3, m, 4, ')'])
|
||||||
[2, '-', 3, 4, '*', '+', '-']
|
[2, '-', 3, 4, '*', '+', '-']
|
||||||
"""
|
"""
|
||||||
# Stack where operator will be stocked
|
# Stack where operator will be stocked
|
||||||
@ -101,11 +103,7 @@ def in2post_fix(infix_tokens):
|
|||||||
|
|
||||||
## Pour voir ce qu'il se passe dans cette procédure
|
## 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))
|
#print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
|
||||||
if token == "(":
|
if token == ")":
|
||||||
opStack.push(token)
|
|
||||||
# Set next arity counter
|
|
||||||
arity_Stack.push(0)
|
|
||||||
elif token == ")":
|
|
||||||
op = opStack.pop()
|
op = opStack.pop()
|
||||||
while op != "(":
|
while op != "(":
|
||||||
postfix_tokens.append(op)
|
postfix_tokens.append(op)
|
||||||
@ -118,28 +116,36 @@ def in2post_fix(infix_tokens):
|
|||||||
arity_Stack.push(arity + 1)
|
arity_Stack.push(arity + 1)
|
||||||
|
|
||||||
elif isOperator(token):
|
elif isOperator(token):
|
||||||
while (not opStack.isEmpty()) and (opStack.peek().priority >= token.priority):
|
if token == "(":
|
||||||
op = opStack.pop()
|
opStack.push(token)
|
||||||
postfix_tokens.append(op)
|
# Set next arity counter
|
||||||
|
arity_Stack.push(0)
|
||||||
|
else:
|
||||||
|
while (not opStack.isEmpty()) and opStack.peek().priority >= token.priority:
|
||||||
|
op = opStack.pop()
|
||||||
|
postfix_tokens.append(op)
|
||||||
|
|
||||||
arity = arity_Stack.pop()
|
arity = arity_Stack.pop()
|
||||||
|
|
||||||
token.arity = arity + 1
|
token.arity = arity + 1
|
||||||
opStack.push(token)
|
opStack.push(token)
|
||||||
# print("--", token, " -> ", str(arity + 1))
|
# print("--", token, " -> ", str(arity + 1))
|
||||||
# Reset arity to 0 in case there is other operators (the real operation would be "-op.arity + 1")
|
# Reset arity to 0 in case there is other operators (the real operation would be "-op.arity + 1")
|
||||||
arity_Stack.push(0)
|
arity_Stack.push(0)
|
||||||
else:
|
else:
|
||||||
postfix_tokens.append(token)
|
postfix_tokens.append(token)
|
||||||
arity = arity_Stack.pop()
|
arity = arity_Stack.pop()
|
||||||
arity_Stack.push(arity + 1)
|
arity_Stack.push(arity + 1)
|
||||||
|
|
||||||
|
## 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))
|
||||||
|
|
||||||
while not opStack.isEmpty():
|
while not opStack.isEmpty():
|
||||||
op = opStack.pop()
|
op = opStack.pop()
|
||||||
postfix_tokens.append(op)
|
postfix_tokens.append(op)
|
||||||
|
|
||||||
# # Pour voir ce qu'il se passe dans cette procédure
|
## 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))
|
#print(str(postfix_tokens), " | ", str(opStack), " | ", str(infix_tokens[(pos_token+1):]), " | ", str(arity_Stack))
|
||||||
|
|
||||||
if arity_Stack.peek() != 1:
|
if arity_Stack.peek() != 1:
|
||||||
raise ValueError("Unvalid expression. The arity Stack is ", str(arity_Stack))
|
raise ValueError("Unvalid expression. The arity Stack is ", str(arity_Stack))
|
||||||
@ -155,6 +161,13 @@ if __name__ == '__main__':
|
|||||||
#
|
#
|
||||||
#print(in2post_fix(in_tokens))
|
#print(in2post_fix(in_tokens))
|
||||||
|
|
||||||
|
#a, s, m, d, p = Operator("+"), Operator("-"), Operator("*"), Operator("/"), Operator("^")
|
||||||
|
#s1 = Operator("-", 1)
|
||||||
|
#par = Operator("(")
|
||||||
|
#print(in2post_fix([par, 2, a, 5, s, 1, ')', d, par, 3, m, 4, ')']))
|
||||||
|
#print(in2post_fix([s1, par, s1, 2, ')']))
|
||||||
|
#print(in2post_fix([s1, par, s1, 2, a, 3, m, 4, ')']))
|
||||||
|
|
||||||
import doctest
|
import doctest
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user