当前位置: 首页>>代码示例>>Python>>正文


Python Grammar.add_P方法代码示例

本文整理汇总了Python中grammar.Grammar.add_P方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.add_P方法的具体用法?Python Grammar.add_P怎么用?Python Grammar.add_P使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在grammar.Grammar的用法示例。


在下文中一共展示了Grammar.add_P方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_grammar

# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import add_P [as 别名]
def get_grammar(string):
    """
    function takes in lines of the grammar rules as input
    returns a Grammar instance initialized with as per given grammar rules. 
    """
    G = Grammar()
    string = string.split("\n")
    # This loop reads every line for rules
    for line in string:
        line = line.split(" : ")
        # n_term is non terminal symbol for the rule in particular line
        n_term = line[0]
        # add the non terminal to the set of non-terminals in G
        G.add_V(n_term)
        # line contains list of possble right hand productions for the n_term
        line = line[1].split("|")
        # remove leading-trailing whitespaces
        line = map(str.strip, line)
        for p in line:
            # for each possible production of n_term
            # add the production to the grammar
            G.add_P(n_term, p)
            p = p.split(" ")

            for t in p:
                # for every term in the production
                # if term is not a non termianl, add it to the set of terminals for G
                if t not in G.variables:
                    G.add_T(t)

            # above does not ensure that all added term is not a non terminal,
            # so this func below will ensure correctness
        for i in G.variables:
            if i in G.terminals:
                G.terminals.remove(i)
        G.start = G.variables[0]
    return G
开发者ID:naveenholla,项目名称:pyParsing,代码行数:39,代码来源:gramtools.py


注:本文中的grammar.Grammar.add_P方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。