-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_tools.py
More file actions
51 lines (47 loc) · 1.39 KB
/
query_tools.py
File metadata and controls
51 lines (47 loc) · 1.39 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
def precedence(token):
''' returns precedence of logical operators'''
__precedence = { "&":2, "|":1 }
try:
return __precedence[token]
# cartch index out of range exception
except KeyError:
return -1
def get_type_of_token(token):
""" gets type of token
:param token: a token ('(',')','&','|', term)
:returns token_type: type of token ('(':1 , ')':2 , '&':3 , '|':3 , term:4 )
"""
if token == '(':
return 1
if token == ')':
return 2
if token == '&':
return 3
if token == '|':
return 3
return 4
def infix_to_postfix(infix):
""" converts infix to postfix
:param infix: a list of tokens
:returns postfix: a list of tokens
"""
stack = []
postfix = []
for token in infix:
token = token.lower()
token_type = get_type_of_token(token)
if token_type == 1:
stack.append(token)
elif token_type == 2:
while len(stack) > 0 and stack[-1] != '(' :
postfix.append(stack.pop())
stack.pop()
elif token_type == 3:
while len(stack) > 0 and precedence(token) <= precedence(stack[-1]):
postfix.append(stack.pop(-1))
stack.append(token)
else:
postfix.append(token)
while len(stack) > 0:
postfix.append(stack.pop())
return postfix