-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostFixer.py
136 lines (124 loc) · 4.42 KB
/
postFixer.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
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
# Nicholas Prado
# created 11 12 8
# convert & evaluate an infix equation as postfix:
# ( 6 + 2 ) * 5 - 8 / 4 ==>> 6 2 + 5 * 8 4 / -
import stack
def isOperator( opDict, candidate ) :
return candidate in opDict
def higherPrecedence( opDic, focusOp, fromTempOp ) :
focus = opDic[ focusOp ]
stored = opDic[ fromTempOp ]
if "(" == stored :
return False
else :
return focus <= stored
def convertSilently( infix ) :
' no outputs of operations '
operators = { "(": 0, "/": 3, "*": 3, "+": 2, "-": 2, "%" : 4 }
opStack = stack.Stack( )
#infix = infixString.split( ' ' ) # comment for compiller # uncomment for deliberate testing, rename arg
postFix = [ ]
ind = 0
next = 0
opStack.push( "(" )
infix.append( ")" )
char = ""
bound = len( infix )
while ( bound > ind ) :
char = infix[ ind ]
if char.isdigit( ) :
postFix.append( char )
elif char.isalpha( ) :
postFix.append( char )
elif "(" is char :
opStack.push( char )
elif isOperator( operators, char ) :
while higherPrecedence( operators, char, opStack.peek( ) ) : # or maybe just do this once & continue?
postFix.append ( opStack.pop( ) )
opStack.push( char )
elif ")" is char :
while opStack.peek( ) is not "(":
postFix.append( opStack.pop( ) )
else :
print "um, what is %s?" % char
ind += 1
while opStack.notEmpty( ) :
if opStack.peek( ) is not "(" :
postFix.append( opStack.pop( ) )
else :
opStack.pop( )
return postFix
def convertVerbosely( infixString ) :
' blow by blow. also this operates on a string, not a list like silently does '
operators = { "(": 0, "/": 3, "*": 3, "+": 2, "-": 2, "%" : 4 }
opStack = stack.Stack( )
infix = infixString.split( ' ' )
postFix = [ ]
ind = 0
next = 0
opStack.push( "(" )
infix.append( ")" )
char = ""
bound = len( infix )
while ( bound > ind ) :
char = infix[ ind ]
print "char is %s " % char,
if char.isdigit( ) :
print " is digit"
postFix.append( char )
elif char.isalpha( ) :
postFix.append( char )
elif "(" is char :
opStack.push( char )
elif isOperator( operators, char ) :
print " it's an operator"
if opStack.notEmpty( ) :
while opStack.notEmpty( ) and higherPrecedence( operators, char, opStack.peek( ) ) :
#print "\tchar, %s is <= top of stack, %s" % ( char, opStack.peek( ) )
postFix.append ( opStack.pop( ) )
opStack.push( char )
elif ")" is char :
print "time to search for the left parenthesis"
while "(" != opStack.peek( ) :
print "\tfound %s, that's going onto postFix" % opStack.peek( )
postFix.append( opStack.pop( ) )
# discard ( ?
else :
print "um, what is %s?" % char
ind += 1
#print "ind is %d\n" % ind
while opStack.notEmpty( ) :
if opStack.peek( ) is not "(" :
postFix.append ( opStack.pop( ) )
else :
opStack.pop( )
print "postfix : %s" % postFix # I assume only wanting to see the final result
return postFix
def convertToPostFix( infixString, verbose ) :
if verbose :
return convertVerbosely( infixString )
else :
return convertSilently( infixString )
'''
Dijkstra's algorithm. Yeah, I thought theirs was a little underexplained
Read a token.
If the token is a number, then add it to the output queue.
If the token is a function token, then push it onto the stack.
If the token is a function argument separator (e.g., a comma):
Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue. If no left parentheses are encountered, either the separator was misplaced or parentheses were mismatched.
If the token is an operator, o1, then:
while there is an operator token, o2, at the top of the stack, and o1's precedence is less than or equal to that of o2
pop o2 off the stack, onto the output queue;
push o1 onto the stack.
If the token is a left parenthesis, then push it onto the stack.
If the token is a right parenthesis:
Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue.
Pop the left parenthesis from the stack, but not onto the output queue.
If the token at the top of the stack is a function token, pop it onto the output queue.
If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.
When there are no more tokens to read:
While there are still operator tokens in the stack:
If the operator token on the top of the stack is a parenthesis, then there are mismatched parentheses.
Pop the operator onto the output queue.
Exit.
'''