您可以使用选项:
rule = [element1:'abc'] [element2:'def'] [element3:'ghi'] ;
您将使用语义操作rule
来检查是否至少匹配了一个标记:
def rule(self, ast):
if not (ast.element1 or ast.element2 or ast.element3):
raise FailedSemantics('Expecting at least one token')
return ast
另一种选择是使用多种选择:
rule
=
element1:'abc' [element2:'def'] [element3:'ghi']
| [element1:'abc'] element2:'def' [element3:'ghi']
| [element1:'abc'] [element2:'def'] element3:'ghi'
;
缓存将使后者与前者一样高效。
然后,您可以添加剪切元素以提高效率和更有意义的错误消息:
rule
=
element1:'abc' ~ [element2:'def' ~] [element3:'ghi' ~]
| [element1:'abc' ~] element2:'def' ~ [element3:'ghi' ~]
| [element1:'abc' ~] [element2:'def' ~] element3:'ghi' ~
;
或者:
rule = [element1:'abc' ~] [element2:'def' ~] [element3:'ghi' ~] ;