【问题标题】:EBNF Nested Optional/GroupingEBNF 嵌套可选/分组
【发布时间】:2018-07-20 14:51:44
【问题描述】:

我正在观察manual 中列出的 python 语法,并考虑其 EBNF 形式的输出,特别是 varargslist:

varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
'*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
| '**' vfpdef [',']]]
| '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
| '**' vfpdef [',']

虽然我对这个部分特别感兴趣:

['*' [vfpdef] (',' vfpdef ['=' test])* ]

我解释为:

[ [ non-terminal1 ] ( non-terminal2) ]

我都知道

non-terminal1 (non-terminal2)
(non-terminal2)

是此表单中的有效选项,但是否包括:

non-terminal1

也一样? EBNF 状态的wiki 页面

That is, everything that is set within the square brackets may be 
present just once, or not at all

但这是否将方括号内的所有内容组合为一个可能只出现一次的实体,或者选项是选择性的,例如:

[ [non-terminal1] [(non-terminal2)] ]

【问题讨论】:

    标签: python grammar ebnf


    【解决方案1】:

    如果

    ['*' [vfpdef] (',' vfpdef ['=' test])* ]
    

    [ [ non-terminal1 ] non-terminal2 ]    -- parentheses deleted as redundant
    

    那么non-terminal2代表

    non-terminal3 *
    

    根据定义可以为空。 (也就是说,它可能是空的。)

    所以,严格来说,一旦你完成了转换

    non-terminal1
    

    不是有效的结果。解析必须是

    non-terminal1 non-terminal2
    

    non-terminal2 匹配一个空字符串。

    但实际解析逻辑更可能要使用公式

    [ [ non-terminal1 ] non-terminal3... ]   -- Not EBNF syntax, but I hope you get the idea
    

    其中non-terminal2 已被消除,以分散对结果解析的注意力。在这种情况下,由于 0 次或多次重复可以是 0 次重复,因此正确的结果将包括

                                              -- nothing :-)
    non-terminal1
                  non-terminal3
    non-terminal1 non-terminal3
                  non-terminal3 non-terminal3
    

    等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      相关资源
      最近更新 更多