boost.spirit用户手册翻译(27):选择分析器

The Select Parser

选择分析器


Select parsers may be used to identify a single parser from a given list of parsers, which successfully recognizes the current input sequence. Example:

选择分析器可用于从给定的分析器表中选择一个成功识别当前输入的分析器。例子:

    rule<> rule_select =
        select_p
        (
            parser_a
          , parser_b
            /* ... */
          , parser_n
        );

The parsers (parser_a, parser_b etc.) are tried sequentially from left to right until a parser matches the current input sequence. If there is a matching parser found, the select_p parser returns the parser's position (zero based index). For instance, in the example above, 1 is returned if parser_b matches.

列表中的分析器(parser_a, parser_b 等等)将从左到右逐个测试,直到有一个分析器匹配当前输入。如果找到一个匹配的分析器,select_p将返回分析器的位置(零基数索引)。比如,上面的例子里,parser_b匹配时返回的是1。

There are two predefined parsers of the select parser family: select_p and select_fail_p. These parsers differ in the way the no match case is handled (when none of the parsers match the current input sequence). While the select_p parser will return -1 if no matching parser is found, the select_fail_p parser will not match at all.

在选择分析器族中有两个预定义的分析器:select_p和select_fail_p。这些分析器的不同之处在于无匹配情况的处理上(即没有分析器匹配当前输入串)。如果没有匹配,select_p分析器返回-1而select_fail_p分析器将完全不匹配。

The following sample shows how the select parser may be used very conveniently in conjunction with a switch parser:

下面的例子展示了选择分析器是如何便利地与跳转分析器一起使用的:

    int choice = -1;
    rule<> rule_select =
            select_fail_p('a', 'b', 'c', 'd')[assign_a(choice)]
        >>  switch_p(var(choice)) 
            [
case_p<0>(int_p),
case_p<1>(ch_p(',')),
case_p<2>(str_p("bcd")),
default_p
]
;

This example shows a rule, which matches:

这个例子里的规则将匹配:

  • 'a' followed by an integer 'a'之后紧接着整数
  • 'b' followed by a ',' 'b'之后跟着a
  • 'c' followed by "bcd" 'c'后面跟着"bcd"
  • a single 'd'. 单独的"b"

For other input sequences the give rule does not match at all.

对于其他的输入分析器完全不匹配。

BOOST_SPIRIT_SELECT_LIMIT

The number of possible entries inside the select_p parser is limited by the Spirit compile time constant BOOST_SPIRIT_SELECT_LIMIT, which defaults to 3. This value should not be greater than the compile time constant given by PHOENIX_LIMIT (see phoenix). Example:

select_p中的条目的数量受限于Spirit的编译期常量BOOST_SPIRIT_SELECT_LIMIT,默认值为3。这个值不能比编译时常量PHOENIX_LIMIT (见 phoenix)更大。例子:

// Define these before including anything else
#define PHOENIX_LIMIT 10
#define BOOST_SPIRIT_SELECT_LIMIT 10





Powered by Zoundry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值