Python Lex-Yacc报告“检测到符号的无限递归”

时间:2018-07-28 21:42:52

标签: python-3.x ply

使用PLY时我有很多错误。当我尝试将其添加到代码中时发生错误:

import datetime
end_date= datetime.datetime.strptime('2018-03-28', "%Y-%m-%d").date()
start_date= datetime.datetime.strptime('2017-10-25', "%Y-%m-%d").date()
print(start_date)
print(start_date + datetime.timedelta(days=1))
mylist = []
checking_date = start_date
print(checking_date + datetime.timedelta(days=1))
while str(checking_date) < str(end_date):
    if checking_date != start_date:
        mylist.append(checking_date)
    month = str(checking_date).split('-')[1]
    new_date = checking_date + datetime.timedelta(days=20)
    possible_new_month = str(new_date).split('-')[1]
    if possible_new_month == month:
        new_date = new_date + datetime.timedelta(days=20)
    new_year = str(new_date).split('-')[0]
    new_month = str(new_date).split('-')[1]
    checking_date_format = "{0}-{1}-01".format(new_year,new_month)
    checking_date = datetime.datetime.strptime(checking_date_format, "%Y-%m-%d").date()

但是我得到这个错误:

def p_assign(p):
   '''assign : NAME EQUALS expr'''
    p[0] = ('ASSIGN',p[1],p[3])
def p_expr_plus(p):
    '''expr : expr PLUS term'''
    p[0] = ('+',p[1],p[3])
def p_term_mul(p):
    '''term : term TIMES factor'''
    p[0] = ('*',p[1],p[3])
def p_term_factor(p):
    '''term : factor'''
    p[0] = p[1]
def p_factor(p):
    '''factor : NUMBER'''
    p[0] = ('NUM',p[1])

有完整的解析器代码:https://pastebin.com/bPrwDQEK 我无法将解析器代码直接放在此处,因为代码示例也无法正常工作。

1 个答案:

答案 0 :(得分:1)

您为counter = 0 list_of_names = [] for x in range(start_x, len(list1)): for y in range(start_y, len(list2)): for z in range(start_z, len(list3)): for t in range(start_t, len(list4)): name = "A {} B {} C {} D {}".format(x, y, z, t) list_of_names.append(name) counter += 1 print(counter) print(len(list_of_names)) 定义的唯一产品是

expr

由于没有其他产生,expr : expr PLUS term 永远不会产生有限的句子。将此与您的expr作品进行对比:

term

这里,存在非递归的生产,因此递归可以终止。

由于PLY的工作方式,您不需要单独的解析操作函数即可添加term : factor TIMES term term : factor 生成;您可以使用与expr : term(以及其他类似单位产品)相同的功能:

term : factor

def p_unit(p): '''expr : term term : factor ''' p[0] = p[1] 也被标记,因为其仅生产使用assign,而expr仅导致无限递归。希望其他错误和警告是不言自明的。