我正在尝试解析下面的 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