[英]How to implement the priority of expressions Bakus-Naur Form
There is a grammar of this kind described in the documentation:文档中描述了这种语法:
grammar
=
| ['()'] ['$'] {'#' &'#'} '#'
| ['()'] {'#' &'#'} '#%'
| ['()'] ['$'] {'0' &'0'} '0'
| ['()'] {'0' &'0%'} '0%'
| ['()'] ['$'] {'#' &'0'} {'0' &'0'} '0'
;
How to correctly describe the grammar so that when you try to parse a string, you get the following result:如何正确描述语法,以便在尝试解析字符串时得到以下结果:
For string '######'
we get the result (['#', '#', '#', '#', '#'], '#')
it's True (worked first rule)对于字符串
'######'
,我们得到结果(['#', '#', '#', '#', '#'], '#')
为真(第一个规则有效)
For string '#####%'
we get the result (['#', '#', '#', '#'], '#')
it's False it should be (['#', '#', '#', '#'], '#%')
(worked first but should have been second rule)对于字符串
'#####%'
我们得到结果(['#', '#', '#', '#'], '#')
它应该是 False 它应该是(['#', '#', '#', '#'], '#%')
(首先工作,但应该是第二条规则)
For string '000000'
we get the result (['0', '0', '0', '0', '0'], '0')
it's True (worked third rule)对于字符串
'000000'
,我们得到结果(['0', '0', '0', '0', '0'], '0')
它是 True (使用第三条规则)
For string '###000'
we get the result (['#', '#'], '#')
it's False (worked first but should have been fifth rule)对于字符串
'###000'
我们得到结果(['#', '#'], '#')
它是 False (首先工作但应该是第五条规则)
Are the rules given in the documentation absurd or am I doing something wrong?文档中给出的规则是荒谬的还是我做错了什么?
Tatsu try rules in the order they are declared. Tatsu 按照声明的顺序尝试规则。
So in your example:所以在你的例子中:
| ['()'] ['$'] {'#' &'#'} '#'
| ['()'] {'#' &'#'} '#%'
The first rule will successfully match #####
before even reading the %
第一条规则将在读取
%
之前成功匹配#####
Reversing the two options will make Tatsu try to parse #%
first, and only try #
if it fails.颠倒这两个选项将使 Tatsu 尝试首先解析
#%
,并且只有在解析失败时才尝试#
。
NB: the ~
symbol can also be used to avoid trying other options for a rule once a pattern has been successfully parsed.注意:一旦成功解析了模式,
~
符号也可用于避免尝试其他规则选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.