本文整理汇总了Python中grammar.Grammar.add_T方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.add_T方法的具体用法?Python Grammar.add_T怎么用?Python Grammar.add_T使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grammar.Grammar
的用法示例。
在下文中一共展示了Grammar.add_T方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_grammar
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import add_T [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