Python PEG节点检测器

Python PEG节点检测器,python,parsing,peg,Python,Parsing,Peg,想象一下像这样的一个小语法 from parsimonious.grammar import Grammar from parsimonious.nodes import NodeVisitor grammar = Grammar( r""" term = lpar (number comma? ws?)+ rpar number = ~"\d+" lpar = "(" rpar = ")" comma = ","

想象一下像这样的一个小语法

from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor

grammar = Grammar(
    r"""
    term    = lpar (number comma? ws?)+ rpar
    number  = ~"\d+"
    lpar    = "("
    rpar    = ")"
    comma   = ","
    ws      = ~"\s*"
    """
)

tree = grammar.parse("(5, 4, 3)")
print(tree)
哪个输出

<Node called "term" matching "(5, 4, 3)">
    <Node called "lpar" matching "(">
    <Node matching "5, 4, 3">
        <Node matching "5, ">
            <RegexNode called "number" matching "5">
            <Node matching ",">
                <Node called "comma" matching ",">
            <Node matching " ">
                <RegexNode called "ws" matching " ">
        <Node matching "4, ">
            <RegexNode called "number" matching "4">
            <Node matching ",">
                <Node called "comma" matching ",">
            <Node matching " ">
                <RegexNode called "ws" matching " ">
        <Node matching "3">
            <RegexNode called "number" matching "3">
            <Node matching "">
            <Node matching "">
                <RegexNode called "ws" matching "">
    <Node called "rpar" matching ")">

如何从本例中的术语中获取数字regex部分?我知道我可以使用NodeVisitor类并检查每个数字,但我希望在学期内获得正则表达式部分。

使用NodeVisitor类并以这种方式遍历树可能更好,但这里有另一个简单的解决方案:

from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor

grammar = Grammar(
    r"""
    term    = lpar (number comma? ws?)+ rpar
    number  = ~"\d+"
    lpar    = "("
    rpar    = ")"
    comma   = ","
    ws      = ~"\s*"
    """
)

tree = grammar.parse("(5, 4, 3)")

def walk(node):
    if node.expr_name == 'number':
        print(node)
    for child in node.children:
        walk(child)

walk(tree)

# <RegexNode called "number" matching "5">
# <RegexNode called "number" matching "4">
# <RegexNode called "number" matching "3">

非常感谢,我现在正在使用一个组合。