0

我有以下代码使用简约解析算术表达式。它工作正常,但空格包含在解析树中。我们怎样才能去掉解析树中的空格,只保留有意义的标记?Lark 解析库通过%ignore WS. 是否有类似的简约或其他方式来达到相同的效果?

from parsimonious.grammar import Grammar

g = '''
    sum = (number plus sum) / (number plus prod)
    prod = (number times prod) / (left_par number plus prod right_par) / number
    number = (ws ~"[\d]+" ws) / (left_par sum right_par)
    plus = ws "+" ws
    times = ws "*" ws
    left_par = ws "(" ws
    right_par = ws ")" ws
    ws = ~"[\s]*"
    '''

grammar = Grammar(g)
print(grammar.parse(' (134 +77 + 56) + 10 * 30' ))

这是输出:

<Node called "bold_text" matching "((bold stuff))">
    <Node called "bold_open" matching "((">
    <RegexNode called "text" matching "bold stuff">
    <Node called "bold_close" matching "))">
<Node called "sum" matching " (134 +77 + 56) + 10 * 30">
    <Node matching " (134 +77 + 56) + 10 * 30">
        <Node called "number" matching " (134 +77 + 56) ">
            <Node matching " (134 +77 + 56) ">
                <Node called "left_par" matching " (">
                    <RegexNode called "ws" matching " ">
                    <Node matching "(">
                    <RegexNode called "ws" matching "">
                <Node called "sum" matching "134 +77 + 56">
                    <Node matching "134 +77 + 56">
                        <Node called "number" matching "134 ">
                            <Node matching "134 ">
                                <RegexNode called "ws" matching "">
                                <RegexNode matching "134">
                                <RegexNode called "ws" matching " ">
                        <Node called "plus" matching "+">
                            <RegexNode called "ws" matching "">
                            <Node matching "+">
                            <RegexNode called "ws" matching "">
                        <Node called "sum" matching "77 + 56">
                            <Node matching "77 + 56">
                                <Node called "number" matching "77 ">
                                    <Node matching "77 ">
                                        <RegexNode called "ws" matching "">
                                        <RegexNode matching "77">
                                        <RegexNode called "ws" matching " ">
                                <Node called "plus" matching "+ ">
                                    <RegexNode called "ws" matching "">
                                    <Node matching "+">
                                    <RegexNode called "ws" matching " ">
                                <Node called "prod" matching "56">
                                    <Node called "number" matching "56">
                                        <Node matching "56">
                                            <RegexNode called "ws" matching "">
                                            <RegexNode matching "56">
                                            <RegexNode called "ws" matching "">
                <Node called "right_par" matching ") ">
                    <RegexNode called "ws" matching "">
                    <Node matching ")">
                    <RegexNode called "ws" matching " ">
        <Node called "plus" matching "+ ">
            <RegexNode called "ws" matching "">
            <Node matching "+">
            <RegexNode called "ws" matching " ">
        <Node called "prod" matching "10 * 30">
            <Node matching "10 * 30">
                <Node called "number" matching "10 ">
                    <Node matching "10 ">
                        <RegexNode called "ws" matching "">
                        <RegexNode matching "10">
                        <RegexNode called "ws" matching " ">
                <Node called "times" matching "* ">
                    <RegexNode called "ws" matching "">
                    <Node matching "*">
                    <RegexNode called "ws" matching " ">
                <Node called "prod" matching "30">
                    <Node called "number" matching "30">
                        <Node matching "30">
                            <RegexNode called "ws" matching "">
                            <RegexNode matching "30">
                            <RegexNode called "ws" matching "">
4

0 回答 0