This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
109 lines (88 loc) · 2.83 KB
/
parser.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
import ply.yacc as yacc
from tokens import tokens
from queryGenerator import queryGenerator
from preProcessor import preProcessor
def p_start(p):
'''start : friendsWithCond
| friendsWithoutCond
| followersWithCond
| followersWithoutCond'''
p[0] = ('start',) + tuple(p[1:])
def p_friendsWithCond(p):
'''friendsWithCond : FRIENDS OF friendsWithCond WHO cond ENDENTITY
| FRIENDS OF friendsWithoutCond WHO cond ENDENTITY
| FRIENDS OF followersWithCond WHO cond ENDENTITY
| FRIENDS OF followersWithoutCond WHO cond ENDENTITY
| myFriends WHO cond ENDENTITY'''
p[0] = ('friendsWithCond',) + tuple(p[1:-1])
def p_friendsWithoutCond(p):
'''friendsWithoutCond : FRIENDS OF friendsWithoutCond ENDENTITY
| FRIENDS OF followersWithoutCond ENDENTITY
| myFriends ENDENTITY'''
p[0] = ('friendsWithoutCond' ,) + tuple(p[1:-1])
def p_followersWithCond(p):
'''followersWithCond : FOLLOWERS OF followersWithCond WHO cond ENDENTITY
| FOLLOWERS OF followersWithoutCond WHO cond ENDENTITY
| FOLLOWERS OF friendsWithCond WHO cond ENDENTITY
| FOLLOWERS OF friendsWithoutCond WHO cond ENDENTITY
| myFollowers WHO cond ENDENTITY'''
p[0] = ('followersWithCond',) + tuple(p[1:-1])
def p_followersWithoutCond(p):
'''followersWithoutCond : FOLLOWERS OF followersWithoutCond ENDENTITY
| FOLLOWERS OF friendsWithoutCond ENDENTITY
| myFollowers ENDENTITY'''
p[0] = ('followersWithoutCond' ,) + tuple(p[1:-1])
def p_cond(p):
'''cond : specificCond AND cond
| specificCond '''
p[0] = ('cond',) + tuple(p[1:])
def p_specificCond(p):
'''specificCond : placeCond
| followCond
| whoFollow'''
p[0] = ('specificCond',) + tuple(p[1:])
def p_placeCond(p):
'''placeCond : LIVING_IN_PLACE'''
p[0] = ('placeCond',) + tuple(p[1:])
def p_followCond(p):
'''followCond : HAVING_FOLLOWERS_GREATER_THAN_NUM
| HAVING_FOLLOWERS_LESS_THAN_NUM'''
p[0] = ('followCond',) + tuple(p[1:])
def p_whoFollow(p):
'''whoFollow : FOLLOW followersWithoutCond
| FOLLOW friendsWithoutCond'''
p[0] = ('whoFollow',) + tuple(p[1:])
def p_myFriends(p):
'''myFriends : MY FRIENDS'''
p[0] = ('myFriends',) + tuple(p[1:])
def p_myFollowers(p):
'''myFollowers : MY FOLLOWERS'''
p[0] = ('myFollowers',) + tuple(p[1:])
# Error rule for syntax errors
def p_error(p):
raise Exception("Syntax error at '%s'" % repr(p)) #p.value)
def parseQuery(s, username):
# return "hey it works"
parser = yacc.yacc(debug=0)
prep = preProcessor()
s = prep.mapConditions(s)
result = parser.parse(s)
gen = queryGenerator()
query = gen.start(result, username)
return query
def getStr(tup):
if tup[0]:
s = str(tup[0]).upper() + "===> "
else:
s = ''
l = []
for t in tup[1:]:
if isinstance(t, tuple):
l.append(t)
s += str(t[0]).upper()
else:
s += str(t)
s+= " "
print s
for x in l:
getStr(x)