2

我试图弄清楚如何使用这个漂亮的库来解析 BigIP 配置文件......语法应该是这样的:

stanza :: name { content }
name   :: several words, might contain alphas nums dot dash underscore or slash
content:: stanza OR ZeroOrMore(printable characters)

为了使事情稍微复杂一点,一个例外:

如果名称以“规则”开头,则内容不能是“节”

我从这个开始:

from pyparsing import *
def parse(config):
    def BNF():
        """
        Example:
        ...
        ltm virtual /Common/vdi.uis.test.com_80_vs {
            destination /Common/1.2.3.4:80
            http-class {
                /Common/http2https
            }
            ip-protocol tcp
            mask 255.255.255.255
            profiles {
                /Common/http { }
                /Common/tcp { }
            }
            vlans-disabled
        }
        ...
        """        
        lcb, rcb, slash, dot, underscore, dash = [c for c in '{}/._-']
        name_word = Word(alphas + nums + dot + underscore + slash + dash)
        name = OneOrMore(name_word).setResultsName("name")
        stanza = Forward()
        content = OneOrMore(stanza | ZeroOrMore(OneOrMore(Word(printables)))).setResultsName("content")
        stanza << Group(name + lcb + content + rcb).setResultsName("stanza")
        return stanza


    return [x for x in BNF().scanString(config)]

上面的代码似乎锁定在某个无限循环中。如果“名称”以“规则”开头,它也缺少我排除查找“节”的要求。

4

1 回答 1

3

OneOrMore(ZeroOrMore(OneOrMore(Word(printables))) 将始终匹配,从而导致无限循环。

此外,printables 包括一个右大括号,它被内容术语消耗,并且不再可用于该节。(如果您的内容可以包含右括号,则需要定义一些东西来转义它,以区分内容括号和节括号。)

要解决名称规则,您需要另一个内容定义,一个不包含节的内容定义和一个“规则规则”。

def parse(config):
    def BNF():
        lcb, rcb, slash, dot, underscore, dash = [c for c in '{}/._-']
        printables_no_rcb = Word(printables, excludeChars=rcb)
        name_word = Word(alphas + nums + dot + underscore + slash + dash)
        name = OneOrMore(name_word).setResultsName("name")
        rule = Group(Literal('rule') + name).setResultsName("name")
        rule_content = OneOrMore(printables_no_rcb).setResultsName("content")
        stanza = Forward()
        content = OneOrMore(stanza | OneOrMore(printables_no_rcb)).setResultsName("content")
        stanza << Group(rule + lcb + rule_content + rcb | name + lcb + content + rcb).setResultsName("stanza")
        return stanza
    return [x for x in BNF().scanString(config)]
于 2013-04-09T14:35:36.937 回答