Python 类lisp语法的递归解析
我正在尝试分析表单中的行:Python 类lisp语法的递归解析,python,string,parsing,recursion,recursive-descent,Python,String,Parsing,Recursion,Recursive Descent,我正在尝试分析表单中的行: (OP something something (OP something something ) ) ( OP something something ) 其中OP是逻辑门(AND,OR,NOT)的符号,我要计算的是某个东西 我要查找的输出类似于: { 'OPERATOR': [condition1, condition2, .. , conditionN] } tree = dict() cond = list()
(OP something something (OP something something ) ) ( OP something something )
其中OP是逻辑门(AND,OR,NOT)的符号,我要计算的是某个东西
我要查找的输出类似于:
{ 'OPERATOR': [condition1, condition2, .. , conditionN] }
tree = dict()
cond = list()
tree[OP] = cond
for string in conditions:
self.counter += 1
if string.startswith('('):
try:
OP = string[1]
except IndexError:
OP = 'AND'
finally:
if OP == '?':
OP = 'OR'
elif OP == '!':
OP = 'N'
# Recurse
cond.append(self.parse_conditions(conditions[self.counter:], OP))
break
elif not string.endswith(")"):
cond.append(string)
else:
return tree
return tree
其中条件本身可以是dict/list对本身(嵌套条件)。到目前为止,我尝试了以下方法:
{ 'OPERATOR': [condition1, condition2, .. , conditionN] }
tree = dict()
cond = list()
tree[OP] = cond
for string in conditions:
self.counter += 1
if string.startswith('('):
try:
OP = string[1]
except IndexError:
OP = 'AND'
finally:
if OP == '?':
OP = 'OR'
elif OP == '!':
OP = 'N'
# Recurse
cond.append(self.parse_conditions(conditions[self.counter:], OP))
break
elif not string.endswith(")"):
cond.append(string)
else:
return tree
return tree
我也尝试过其他的方法,但我就是不能完全理解递归,所以我想知道我是否可以在这里找到一些指针,我环顾了一下网络,发现了一些关于递归下降解析的东西,但是教程都试图做一些比我需要的更复杂的事情
PS:我意识到我可以用现有的python库来实现这一点,但通过这样做我能学到什么呢?我在这里没有进一步的评论,只是为了学习(在现实生活中,请使用库)。注意,没有错误检查(这是你的家庭作业!) 如果有什么你不明白的,尽管问
# PART 1. The Lexer
symbols = None
def read(input):
global symbols
import re
symbols = re.findall(r'\w+|[()]', input)
def getsym():
global symbols
return symbols[0] if symbols else None
def popsym():
global symbols
return symbols.pop(0)
# PART 2. The Parser
# Built upon the following grammar:
#
# program = expr*
# expr = '(' func args ')'
# func = AND|OR|NOT
# args = arg*
# arg = string|expr
# string = [a..z]
def program():
r = []
while getsym():
r.append(expr())
return r
def expr():
popsym() # (
f = func()
a = args()
popsym() # )
return {f: a}
def func():
return popsym()
def args():
r = []
while getsym() != ')':
r.append(arg())
return r
def arg():
if getsym() == '(':
return expr()
return string()
def string():
return popsym()
# TEST = Lexer + Parser
def parse(input):
read(input)
return program()
print parse('(AND a b (OR c d)) (NOT foo) (AND (OR x y))')
# [{'AND': ['a', 'b', {'OR': ['c', 'd']}]}, {'NOT': ['foo']}, {'AND': [{'OR': ['x', 'y']}]}]
这是你在理解上的实际问题,还是a是如何工作的?如果a后面有空格怎么办(“?不要试图重新发明自行车,从一个漂亮干净的库中学习,比如@JoachimPileborg,我想这是整个递归的事情,我就是做不好。@SK logic我已经处理好了,我没有试图重新发明我试图学习的轮子。你学错了。像你这样的特殊解决方案不会教你任何使用方法ful。你最好马上学习正确的方法,正确的方法是使用组合器。它也更容易理解,更简洁,更灵活。因此,如果你想学习,开始编写你自己的组合器库。谢谢。这正是我所需要的。我仍然需要弄清楚它是如何做到的,但你的代码会有所帮助非常感谢。