防止ABNF中的重复字符

时间:2014-11-03 18:45:16

标签: grammar abnf

我想创建一个包含字符" imsxeADSUXju"的ABNF规则。每个字符都是可选的。订单无关紧要,但字符可能不会出现多次。

例如:它必须匹配

"i" "im" "mi" "" "uUsejXx" "imsxeADSUXju"

但不匹配

"iim" "UmUu" "imsss"

我创建了以下规则,但它不会阻止某个字符出现多次:

options = 0*12( "i" / "m" / "s" / "x" / "e" / "A" / "D" / "S" / "U" / "X" / "j" / "u" )

在这条规则中,顺序很重要:

options = [ "i" ] [ "m" ] [ "s" ] [ "x" ] [ "e" ] [ "A" ] [ "D" ] [ "S" ] [ "U" ] [ "X" ] [ "j" ] [ "u" ]

如何编写忽略顺序但又能阻止双打的规则?

1 个答案:

答案 0 :(得分:3)

不幸的是,你必须写出所有的替代品。忽略ABNF不区分大小写,它必须是

  S        = "i" after-i /
             "m" after-m /
             ...
  after-i  = "m" after-im /
             "s" after-is /
             ...
  after-im = "s" after-ims /
             ...

这里的语言是常规的,如果你考虑语言的最小DFA是什么样的,它必须有2 ^ 12个状态,每个状态对应于字母表的powerset元素,编码哪个已经看过角色了。