英文:
posix disallows alternation in case. is this a typo?
问题
POSIX 规定:
case 结构的格式如下:
case word in [(] pattern1 ) compound-list ;; [[(] pattern[ | pattern] ... ) compound-list ;;] ... [[(] pattern[ | pattern] ... ) compound-list] esac
这似乎不允许在第一个 compound-list 中使用交替模式(即多个模式)。
显然,实际实现并非如此。
$ for cmd in bash dash 'busybox sh' ksh; do
> $cmd -c 'case a in a|b|c) echo ok ;; *) echo no ; esac'
> done
ok
ok
ok
ok
$
也不是语法本身在该页面下面进一步定义的方式:
case_clause : Case WORD linebreak in linebreak case_list Esac | Case WORD linebreak in linebreak case_list_ns Esac | Case WORD linebreak in linebreak Esac ; case_list_ns : case_list case_item_ns | case_item_ns ; case_list : case_list case_item | case_item ; case_item_ns : pattern ')' linebreak | pattern ')' compound_list | '(' pattern ')' linebreak | '(' pattern ')' compound_list ; case_item : pattern ')' linebreak DSEMI linebreak | pattern ')' compound_list DSEMI linebreak | '(' pattern ')' linebreak DSEMI linebreak | '(' pattern ')' compound_list DSEMI linebreak ; pattern : WORD /* Apply rule 4 */ | pattern '|' WORD /* Do not apply rule 4 */ ;
这是否是规范中的错误?(特别是 pattern1
中的 1
看起来像是拼写错误的 ]
,好像该行的一部分丢失/损坏了。)
同样的定义文本早在2004 版本中就存在。
英文:
POSIX states:
> The format for the case construct is as follows:
>
>
> case word in
> [(] pattern1 ) compound-list ;;
> [[(] pattern[ | pattern] ... ) compound-list ;;] ...
> [[(] pattern[ | pattern] ... ) compound-list]
> esac
>
This appears to disallow alternation (ie. multiple patterns)
with the first compound-list.
Clearly, this is not how implementations behave.
$ for cmd in bash dash 'busybox sh' ksh; do
> $cmd -c 'case a in a|b|c) echo ok ;; *) echo no ; esac'
> done
ok
ok
ok
ok
$
Nor how the grammar itself defines it further down on that page:
>
> case_clause : Case WORD linebreak in linebreak case_list Esac
> | Case WORD linebreak in linebreak case_list_ns Esac
> | Case WORD linebreak in linebreak Esac
> ;
> case_list_ns : case_list case_item_ns
> | case_item_ns
> ;
> case_list : case_list case_item
> | case_item
> ;
> case_item_ns : pattern ')' linebreak
> | pattern ')' compound_list
> | '(' pattern ')' linebreak
> | '(' pattern ')' compound_list
> ;
> case_item : pattern ')' linebreak DSEMI linebreak
> | pattern ')' compound_list DSEMI linebreak
> | '(' pattern ')' linebreak DSEMI linebreak
> | '(' pattern ')' compound_list DSEMI linebreak
> ;
> pattern : WORD /* Apply rule 4 */
> | pattern '|' WORD /* Do not apply rule 4 */
> ;
>
Is this a bug in the spec? (In particular the 1
in pattern1
looks suspiciously like a mistyped ]
- as if part of that line had been lost/corrupted.)
The same definition text appears as far back as the 2004 edition.
答案1
得分: 1
是的,这是一个错误。这一行已经从标准中删除,原因是在2021年提交的这个错误报告。问题8的草案中有以下文本:
case 构造的格式如下:
case word in [[(] pattern[ | pattern] ... ) compound-list terminator] ... [[(] pattern[ | pattern] ... ) compound-list] esac
其中 terminator 可以是 ";;" 或 ";&",对于最后的 compound-list 是可选的。
英文:
Yes, it's a bug. That line was removed from the standard as a result of this bug report filed back in 2021. The issue 8 drafts have the following text instead:
>The format for the case construct is as follows:
> none
>case word in
> [[(] pattern[ | pattern] ... ) compound-list terminator] ...
> [[(] pattern[ | pattern] ... ) compound-list]
>esac
>
>Where terminator is either ";;" or ";&" and is optional for the last compound-list.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论