EBNF嵌套可选/分组

2024-09-30 04:40:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我观察了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)] ]

Tags: test表单is选项语法manual感兴趣形式
1条回答
网友
1楼 · 发布于 2024-09-30 04:40:07

如果

['*' [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

等等。你知道吗

相关问题 更多 >

    热门问题