boost.spirit用户手册翻译(24):动态分析器

22 篇文章 0 订阅
 

Dynamic Parsers

动态分析器


 

We see dynamic parsing everywhere in Spirit. A special group of parsers, aptly named dynamic parsers, form the most basic building blocks to dynamic parsing. This chapter focuses on these critters. You'll notice the similarity of these parsers with C++'s control structures. The similarity is not a coincidence. These parsers give an imperative flavor to parsing, and, since imperative constructs are not native to declarative EBNF, mimicking the host language, C++, should make their use immediately familiar.

在Spirit中动态分析随处可见。有一组特别的分析器,被恰当地命名为动态分析器,组成了动态分析中最基本的成分。这章的焦点集中于这些关键要素上。你将看到这些分析器和C++控制结构的相似性。但相似性并非一致性。这些分析器带来了指令式分析,并且,由于指令式结构并非声明式的EBNF的特征,模仿宿主语言,C++,将使这些分析器的使用方法能够被迅速熟悉。

Dynamic parsers modify the parsing behavior according to conditions. Constructing dynamic parsers requires a condition argument and a body parser argument. Additional arguments are required by some parsers.

动态分析通过条件改变分析行为。构造动态分析器需要一个条件参数和一个分析体参数。某些分析器需要更多的参数。

Conditions

条件

Functions or functors returning values convertable to bool can be used as conditions. When the evaluation of the function/functor yields true it will be considered as meeting the condition.


返回值可以转换为bool型的函数或仿函数可以用作条件。当函数/仿函数的演算值为真时,则认为匹配了条件。

Parsers can be used as conditions, as well. When the parser matches the condition is met. Parsers used as conditions work in an all-or-nothing manner: the scanner will not be advanced when they don't match.

分析器也可以当成条件,当分析器的匹配符合条件时。被当成条件使用的分析器以一种"零或全部"的行为工作:当不匹配时,扫描器不会前进。

A failure to meet the condition will not result in a parse error.

失败的条件匹配不会引起分析错误。

if_p

if_p can be used with or without an else-part. The syntax is:

if_p的else部分可有可无。它的语法:

    if_p(condition)[then-parser]

or

    if_p(condition)[then-parser].else_p[else-parser]

When the condition is met the then-parser is used next in the parsing process. When the condition is not met and an else-parser is available the else-parser is used next. When the condition isn't met and no else-parser is available then the whole parser matches the empty sequence. ( Note: older versions of if_p report a failure when the condition isn't met and no else-parser is available.)

当条件匹配时,then-分析器将被用于下一步的分析过程。当条件不匹配同时else分析器存在时else分析器被接着调用。当条件不匹配而else分析器也不存在时那么整个分析器将产生空匹配(注意:老版本的if_p在条件不匹配且没有else分析器时会报告一个失败)

Example:

例子:

    if_p("0x")[hex_p].else_p[uint_p]

while_p, do_p

while_p/do_p syntax is:

while_p/do_p的语法是:

    while_p(condition)[body-parser]
    do_p[body-parser].while_p(condition)

As long as the condition is met the dynamic parser constructed by while_p will try to match the body-parser. do_p returns a parser that tries to match the body-parser and then behaves just like the parser returned by while_p. A failure to match the body-parser will cause a failure to be reported by the while/do-parser.

只要条件匹配,由while_p构造的动态分析器将一直尝试匹配body分析器。do_p则返回一个首先尝试匹配body分析器,接着就干和while_p一样的事情的分析器。body分析器的失败将使while/do-parser失败。

Example:

例子:

    uint_p[assign_a(sum)] >> while_p('+')[uint_p(add(sum)]
    '"' >> while_p(~eps_p('"'))[c_escape_ch_p[push_back_a(result)]] >> '"'

for_p

for_p requires four arguments. The syntax is:

for_p需要四个参数。语法:

    for_p(init, condition, step)[body-parser]

init and step have to be 0-ary functions/functors. for_p returns a parser that will:

init和step必须是无参数函数/仿函数。for_p返回的分析器将:

  1. call init 调用init
  2. check the condition, if the condition isn't met then a match is returned. The match will cover everything that has been matched successfully up to this point.检查condition,如果条件不符合则返回一个匹配,这个匹配迭代着到目前为止所匹配的所有东西。
  3. tries to match the body-parser. A failure to match the body-parser will cause a failure to be reported by the for-parser尝试匹配body分析器,body分析器匹配失败会使for_p报告失败。
  4. calls step 调用step
  5. goes to 2. 返回步骤2
 



Powered by Zoundry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值