boost.spirit用户手册翻译(35):重构分析器

Refactoring Parsers

重构分析器


There are three types of Refactoring Parsers implemented right now, which help to abstract common parser refactoring tasks. Parser refactoring means, that a concrete parser construct is replaced (refactored) by another very similar parser construct. Two of the Refactoring Parsers described here (refactor_unary_parser and refactor_action_parser) are introduced to allow a simple and more expressive notation while using Confix Parsers and List Parsers. The third Refactoring Parser (attach_action_parser) is implemented to abstract some functionality required for the Grouping Parser. Nevertheless these Refactoring Parsers may help in solving other complex parsing tasks too.

目前已经实现了三种重构分析器,它们有助于抽象分析器的一般重构方法。分析器重构意味着,一个特定的分析器构造被一个非常相像的分析器构造所替代。这节所描述的两种重构分析器(refactor_unary_parserrefactor_action_parser)的作用是在使用片段分析器列表分析器时,能够使用简单乃至更加复杂的符号。第三个所实现的抽象分析器用于抽象在为分析器编组时所需的某些功能。然而以上这些重构分析器在应对其他复杂的分析上仍有帮助。

Refactoring unary parsers

重构一元分析器

The refactor_unary_d parser generator, which should be used to generate a unary refactoring parser, transforms a construct of the following type

refactor_unary_d分析器生成器,用于生成一元重构分析器,转换下面类型的构造

    refactor_unary_d[*some_parser - another_parser]

to

    *(some_parser - another_parser)

where refactor_unary_d is a predefined object of the parser generator struct refactor_unary_gen<>

这里refactor_unary_d是refactor_unary_gen<>的一个预定义对象

The refactor_unary_d parser generator generates a new parser as shown above, only if the original construct is an auxilliary binary parser (here the difference parser) and the left operand of this binary parser is an auxilliary unary parser (here the kleene star operator). If the original parser isn't a binary parser the compilation will fail. If the left operand isn't an unary parser, no refactoring will take place.

refactor_unary_d分析器生成器只在原始的构造为辅助的二元分析器(这里为减号)且这个二元分析器左边的算子为辅助的一元分析器(这里是克林星)时,才生成如上所示的新的分析器。如果原始的分析器不是二元分析器则产生编译失败。如果左边的算子不是一元分析器,则重构不会发生。

Refactoring action parsers

重构动作分析器

The refactor_action_d parser generator, which should be used to generate an action refactoring parser, transforms a construct of the following type

refactor_action_d,用于生成动作重构分析器,转换如下类型的结构

    refactor_action_d[some_parser[some_actor] - another_parser]

to

    (some_parser - another_parser)[some_actor]

where refactor_action_d is a predefined object of the parser generator struct refactor_action_gen<>

这里refactor_action_d是refactor_action_gen<>分析器生成器一个预定义的对象。

The refactor_action_d parser generator generates a new parser as shown above, only if the original construct is an auxilliary binary parser (here the difference parser) and the left operand of this binary parser is an auxilliary parser generated by an attached semantic action. If the original parser isn't a binary parser the compilation will fail. If the left operand isn't an action parser, no refactoring will take place.

refactor_action_d分析器生成器仅当原始的构造为辅助的二元分析器(这里为减号)且这个二元分析器左边的算子为生成自所挂接的语义动作的辅助分析器时,才生成如上所示的新的分析器。如果原始的分析器不是二元分析器则产生编译失败。如果左边的算子不是动作分析器,则重构不会发生。

Attach action refactoring

挂接动作重构

The attach_action_d parser generator, which should be used to generate an attach action refactoring parser, transforms a construct of the following type

attach_action_d分析器生成器,用于生成挂接动作重构分析器,转换如下类型的结构

    attach_action_d[(some_parser >> another_parser)[some_actor]]

to

    some_parser[some_actor] >> another_parser[some_actor]

where attach_action_d is a predefined object of the parser generator struct attach_action_gen<>

这里attach_action_d是attach_action_gen<>分析器生成器一个预定义的对象

The attach_action_d parser generator generates a new parser as shown above, only if the original construct is an auxilliary action parser and the parser to it this action is attached is an auxilliary binary parser (here the sequence parser). If the original parser isn't a action parser the compilation will fail. If the parser to which the action is attached isn't an binary parser, no refactoring will take place.

attach_action_d分析器生成器仅当原始构造是一个动作分析器且这个动作所挂接的分析器是一个二元分析器(这里是并置分析器)时,才生成如上所示新的分析器。如果原始的分析器不是动作分析器则编译失败。如果动作所挂接到的分析器不是二元分析器,则重构不会发生。

Nested refactoring

嵌套重构

Sometimes it is required to nest different types of refactoring, i.e. to transform constructs like

有时需要嵌套不同类型的重构,比如转化类似下面的结构

    (*some_parser)[some_actor] - another_parser

to

    (*(some_parser - another_parser))[some_actor]

To simplify the construction of such nested refactoring parsers the refactor_unary_gen<> and refactor_action_gen<> both can take another refactoring parser generator type as their respective template parameter. For instance, to construct a refactoring parser generator for the mentioned nested transformation we should write:

为了简化这类嵌套重构分析器的构造,refactor_unary_gen<>refactor_action_gen<> 都可以接受对方的分析器生成器类型为自己的模板参数。比如,要构造一个我们上面提到的分析器生成器,应该这么写:

    typedef refactor_action_gen<refactor_unary_gen<> > refactor_t;
    const refactor_t refactor_nested_d = refactor_t(refactor_unary_d);

Now we could use it as follows to get the required result:

现在我们可以像下面那样使用以获得所需的结果:

refactor_nested_d[(*some_parser)[some_actor] - another_parser]

An empty template parameter means not to nest this particular refactoring parser. The default template parameter is non_nesting_refactoring, a predefined helper structure for inhibiting nesting. Sometimes it is required to nest a particular refactoring parser with itself. This is achieved by providing the predefined helper structure self_nested_refactoring as the template parameter to the corresponding refactoring parser generator template.

空的模板参数意味着不嵌套特定的重构分析器。默认的模板参数为non_nesting_refactoring,一个用于限制嵌套的预定义的辅助结构。有时特定的重构分析器需要嵌套自身。这是借由把预定义的辅助结构 self_nested_refactoring 作为对应的分析器生成器的模板参数来实现的。

See refactoring.cpp for a compilable example. This is part of the Spirit distribution.

可编译的例子见refactoring.cpp,这是Spirit发布包的组成部分。




Powered by Zoundry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值