无法理解递归下降解析

时间:2015-08-23 15:54:21

标签: json parsing recursion

我正在努力更好地理解递归下降解析器 - 特别是https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js。       我对下一个功能的目的感到困惑:

    next = function (c) {

// If a c parameter is provided, verify that it matches the current character.

            if (c && c !== ch) {
                error("Expected '" + c + "' instead of '" + ch + "'");
            }

// Get the next character. When there are no more characters,
// return the empty string.

            ch = text.charAt(at);
            at += 1;
            return ch;
        },

有人可以帮助我理解这个吗?据我所知(我可能错了)它会检查参数(c)是否与字符串中的下一个字符不同?如果是这样,那有什么意义呢? 任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

您只报告了下一个功能的一部分。这是完整的身体:

def lseq(f_in,f_out, l): #value and length    
    with open(f_in) as f, open(f_out, "w") as out: 
         c = Counter(v[i:i+l] for i in range(len(v)-l+1)) #dictionary
         mx = c.most_common(1)[0][1]
         maxes = ",".join([sub for sub, count in c.items() if count == mx])
         out.write(maxes+"\n")
         print("Done! The most frequent sequence is: {}".format(maxes))

注释在注释中:首先检查作为参数传递的字符(如果有的话)是否存在于字符串中。在此之后,无论如何,获取输入字符串的下一个字符。

相关问题