boost.spirit用户手册翻译(33):列表分析器

List Parsers

列表分析器


List Parsers are generated by the special predefined parser generator object list_p, which generates parsers recognizing list structures of the type


列表分析器生成自特殊的预定义分析器生成器对象list_p,所生成的分析器识别如下类型的列表

    item >> *(delimiter >> item) >> !end

where item is an expression, delimiter is a delimiter and end is an optional closing expression. As you can see, the list_p generated parser does not recognize empty lists, i.e. the parser must find at least one item in the input stream to return a successful match. If you wish to also match an empty list, you can make your list_p optional with operator! An example where this utility parser is helpful is parsing comma separated C/C++ strings, which can be easily formulated as:

这里item是一个表达式,delimiter是分隔符而end是一个可选的结尾表达式。正如你所见,list_p所生成的分析器并不识别空列表,比如,只有在输入中匹配了至少一个item,分析器才会返回成功匹配。如果希望能同时识别空列表,可以为list_p加上!(可选)操作符。下面的例子是这一工具分析器对分析逗号分隔的C/C++字符串助益良多的例子,可以很轻易的列出这个表达式:

    rule<> list_of_c_strings_rule
        =   list_p(confix_p('/"', *c_escape_char_p, '/"'), ',')
        ;

The confix_p and c_escape_char_p parser generators are described here and here.

confix_pc_escape_char_p分析器可以在这里这里查阅。

The list_p parser generator object can be used to generate the following different types of List Parsers:

list_p分析器生成器对象可用于生成下列类型的列表分析器:

List Parsers

列表分析器

list_p

list_p used by itself parses comma separated lists without special item formatting, i.e. everything in between two commas is matched as an item, no end of list token is matched

list_p这一形式用于识别没有特殊格式的列表项的逗号分隔列表,比如,两个逗号之间的所有东西都被匹配为一个item,而列表标记的end则不会匹配。

list_p(delimiter)

generates a list parser, which recognizes lists with the given delimiter and matches everything in between them as an item, no end of list token is matched

生成一个列表分析器,它识别以给定的delimiter作为分隔符的列表,且把两个分隔符之间的任意内容识别为一个item,作为列表标记结尾的end则不会匹配

list_p(item, delimiter)

generates a list parser, which recognizes lists with the given delimiter and matches items based on the given item parser, no end of list token is matched

生成一个列表分析器,它识别拥有给定的delimiter作为分隔符,且把给定的item分析器的匹配作为列表项,作为列表标记结尾的end则不会匹配

list_p(item, delimiter, end)

generates a list parser, which recognizes lists with the given delimiter and matches items based on the given item parser and additionally recognizes an optional end expression

生成一个列表分析器,它识别拥有给定的delimiter作为分隔符,且把给定的item分析器的匹配作为列表项,最后,它还以一个可选的end表达式作为列表的结尾

All of the parameters to list_p can be single characters, strings or, if more complex parsing logic is required, auxiliary parsers, each of which is automatically converted to the corresponding parser type needed for successful parsing.

list_p的所有参数可以是单独的字符、字符串、或者,如果需要更复杂的分析逻辑的话,任意的分析器,这些东西都被自动的转化为成功分析所需要的分析器的类型。

If the item parser is an action_parser_category type (parser with an attached semantic action) we have to do something special. This happens, if the user wrote something like:

如果item分析器属于action_parser_category类型(挂接语义动作的分析器),则需要特殊处理。这发生在用户写出类似如下的代码:

    list_p(item[func], delim)

where item is the parser matching one item of the list sequence and func is a functor to be called after matching one item. If we would do nothing, the resulting code would parse the sequence as follows:

这里item列表项分析器而func是在匹配列表项之后被调用的仿函数。如果我们什么都不做,所产生的代码就会以如下的方式进行分析:

    (item[func] - delim) >> *(delim >> (item[func] - delim))

what in most cases is not what the user expects. (If this is what you've expected, then please use one of the list_p generator functions direct(), which will inhibit refactoring of the item parser). To make the list parser behave as expected:

大多数情况下这并未所期望的。(如果这的确是你想要的,那么请用list_p生成器函数direct(),它会集成item分析器的重构后的产物)。要使列表分析器的行为如你所愿:

    (item - delim)[func] >> *(delim >> (item - delim)[func])

the actor attached to the item parser has to be re-attached to the (item - delim) parser construct, which will make the resulting list parser 'do the right thing'. This refactoring is done by the help of the Refactoring Parsers. Additionally special care must be taken, if the item parser is a unary_parser_category type parser as for instance:

挂接到item分析器上的动作器必须重新挂接到(item - delim)分析器结构上,才能使得所产生的分析器"行为正确"。这一重构在重构分析器的帮助下完成。需要额外注意的是,如果列表项分析器是一个unary_parser_category类型的分析器,就像下面的:

    list_p(*anychar_p, ',')

which without any refactoring would result in

这如果不重构就变成

        (*anychar_p - ch_p(','))
    >> *( ch_p(',') >> (*anychar_p - ch_p(',')) )

and will not give the expected result (the first *anychar_p will eat up all the input up to the end of the input stream). So we have to refactor this into:


而这将不会得到所期望的结果(第一个*anychar_p将把输入流里的直到末尾的输入全部吃掉)。所以我们需要把它重构为:

       *(anychar_p - ch_p(','))
    >> *( ch_p(',') >> *(anychar_p - ch_p(',')) )

what will give the correct result.

这将得到正确的结果。

The case, where the item parser is a combination of the two mentioned problems (i.e. the item parser is a unary parser with an attached action), is handled accordingly too:

列表分析器混合了上面提到的两个问题的情况(比如列表分析器是一个带着语义动作的一元分析器),也将以一致的方式被处理:

    list_p((*anychar_p)[func], ',')

will be parsed as expected:

将会被如所期望的那样变为:

        (*(anychar_p - ch_p(',')))[func]
    >> *( ch_p(',') >> (*(anychar_p - ch_p(',')))[func] )

The required refactoring is implemented with the help of the Refactoring Parsers.

所需要的重构都是借由重构分析器的帮助来实现的。

Summary of List Parser refactorings


列表分析器汇总

You write it as:

源代码

It is refactored to:

重构为

list_p(item, delimiter)(item - delimiter)
>> *(
delimiter >> (item - delimiter))
list_p(item[func], delimiter)(item - delimiter)[func]
>> *(
delimiter >> (item - delimiter)[func])
list_p(*item, delimiter)*(item - delimiter)
>> *(
delimiter >> *(item - delimiter))
list_p((*item)[func], delimiter)(*(item - delimiter))[func]
>> *(
delimiter >> (*(item - delimiter))[func])

list_parser.cpp sample shows the usage of the list_p utility parser:

list_parser.cpp里的例子展示了list_p工具分析器的使用方法:

  1. parsing a simple ',' delimited list w/o item formatting
    分析一个简单的","分隔列表,列表项格式为w/o
  2. parsing a CSV list (comma separated values - strings, integers or reals)
    分析一个CVS列表(逗号分隔值表--字符串、整数或者实数)
  3. parsing a token list (token separated values - strings, integers or reals)
    with an action parser directly attached to the item part of the list_p generated parser

    分析一个标记表(标记分隔列表--字符串、整数或者实数)并且把一个动作分析器直接挂接到list_p所生成的分析器的列表项部分上

This is part of the Spirit distribution.

这是Spirit发布包的组成部分。



Powered by Zoundry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值