我正在尝试编写LEPL语法来描述布尔搜索语言。这是我到目前为止所拥有的:
from lepl import *
text = String() | Word()
tail = ~Lookahead('AND') & ~Lookahead('OR') & text
with DroppedSpace():
andClause = (tail & Lookahead('AND') & Drop('AND') & tail)[1:] > tuple
orClause = (tail & Lookahead('OR') & Drop('OR') & tail)[1:] > list
andOrText = (andClause & Lookahead('OR') & Drop('OR') & tail)[1:] > list
orAndText = (orClause & Lookahead('AND') & Drop('AND') & tail)[1:] > tuple
oredAnds = (andClause & Lookahead('OR') & Drop('OR') & andClause)[1:] > list
andedOrs = (orClause & Lookahead('AND') & Drop('AND') & orClause)[1:] > tuple
query = (oredAnds | andedOrs | orAndText | andOrText | orClause | andClause | text)[:]
query.parse('foo AND bar') # Works
query.parse('"bar none" OR foo') # Works
query.parse('foo AND "bar none" OR baz AND floo') # Works
query.parse('a AND b OR c AND d OR e') # Doesn't work
最后一个parse
产生这个:
[[('a', 'b'), ('c', 'd')], 'OR', 'e']
它应该产生这个:
[[('a', 'b'), ('c', 'd'), 'e']]
我该如何解决这个问题以获得我想要的解析?tuple
我认为如果我可以说“whatever AND whatever”变成 a ,并且“whatever OR whatever”变成a ,它会解决这个问题list
。