本文整理汇总了Python中pyparsing.Word.addParseAction方法的典型用法代码示例。如果您正苦于以下问题:Python Word.addParseAction方法的具体用法?Python Word.addParseAction怎么用?Python Word.addParseAction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing.Word
的用法示例。
在下文中一共展示了Word.addParseAction方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _define_grammer
# 需要导入模块: from pyparsing import Word [as 别名]
# 或者: from pyparsing.Word import addParseAction [as 别名]
def _define_grammer(self):
"""Define the grammar to be used, and add the various actions"""
self._define_actions()
node_name = Word(alphas, alphanums)
lb = Literal("(").suppress()
clb = Literal("(").suppress()
rb = Literal(")").suppress()
crb = Literal(")").suppress()
node = Forward()
children = clb + Group(ZeroOrMore(node)) + crb
node << lb + node_name + Optional(children) + rb
empty_tree = lb + rb
tree = node | empty_tree
self._grammar = tree
node_name.addParseAction(self._name_action)
clb.addParseAction(self._clb_action)
crb.addParseAction(self._crb_action)
示例2: len
# 需要导入模块: from pyparsing import Word [as 别名]
# 或者: from pyparsing.Word import addParseAction [as 别名]
if len(node_stack) > 0:
node_stack[len(node_stack) - 1].add_child(current_node)
def push_current():
global current_node, node_stack
node_stack.append(current_node)
current_node = None
def pop_current():
global current_node, node_stack
current_node = node_stack.pop()
node_name = Word(alphas, alphanums)
node_name.addParseAction(create_node)
lb = Literal('(').suppress()
clb = Literal('(').suppress()
clb.addParseAction(push_current)
rb = Literal(')').suppress()
crb = Literal(')').suppress()
crb.addParseAction(pop_current)
node = Forward()
children = clb + Group(ZeroOrMore(node)) + crb
node << lb + node_name + Optional(children) + rb
tree_str = '(c1 ((c2) (c3 ((c4) (c5))) (c6)))'
results = node.parseString(tree_str)
print(results)
writer = IndentedStringWriter()