我正在尝试解析下面的EBNF(在代码中注释),我正在努力解决可选注释的STRING部分。。(在我的测试字符串中写为额外注释)
from pyparsing import *
# SQL HINT EBNF
'''
{ /*+ hint [ string ]
[ hint [ string ] ]... */
| --+ hint [ string ]
[ hint [ string ]...
}
'''
test_string = "/*+ALL_ROWS extra comment FIRST_ROWS CACHE*/"
LCOMMENT = Literal("/*+")
RCOMMENT = Literal("*/")
grammar = Forward()
hint_all_rows = Literal("ALL_ROWS")
hint_first_rows = Literal("FIRST_ROWS")
hint_cache = Literal("CACHE")
comment_in_hint = Word(printables)
all_hints = (hint_all_rows | hint_first_rows | hint_cache)+ ZeroOrMore(comment_in_hint)
grammar << all_hints + ZeroOrMore(grammar)
all_grammar = LCOMMENT + grammar + RCOMMENT
p = all_grammar.parseString(test_string)
print p
这是由于Paul McGuire在OP评论中的帮助而现在运行的代码。在最初设置答案时,我确实去掉了forward函数。但是通过将结果名称附加到不同的元素来检查代码,我注意到我在这里的第一个答案是将除了第一个提示之外的所有提示都分类为注释。因此,我保留了前锋,但利用了保罗的一些其他建议。
from pyparsing import *
# SQL HINT EBNF
'''
{ /*+ hint [ string ]
[ hint [ string ] ]... */
| --+ hint [ string ]
[ hint [ string ]...
}
'''
LCOMMENT = Literal("/*+")
RCOMMENT = Literal("*/")
grammar = Forward()
hint_all_rows = Keyword("ALL_ROWS")
hint_first_rows = Keyword("FIRST_ROWS")
hint_cache = Keyword("CACHE")
comment_in_hint = Word(printables, excludeChars='*')
grammar = Forward()
all_hints = (hint_all_rows | hint_first_rows | hint_cache).setResultsName("Hints", listAllMatches=True) + Optional(comment_in_hint)("Comments*")
grammar << all_hints + ZeroOrMore(grammar)
all_grammar = LCOMMENT + grammar + RCOMMENT
p = all_grammar.parseString("/*+ ALL_ROWS aaaaaaa FIRST_ROWS bbbbb */")
print p["Hints"]
print p["Comments"]