Boost.Spirit用户手册翻译(10):空串

22 篇文章 0 订阅
 
 

Epsilon

空串


 

The Epsilon (epsilon_p and eps_p) is a multi-purpose parser that returns a zero length match.

空串 (epsilon_p 和 eps_p) 是一个总是返回零长度匹配,有多种用途的分析器。

Simple Form

简单形式

In its simplest form, epsilon_p matches the null string and always returns a match of zero length:

在最简单的形式下,epsilon_p匹配空串并且永远返回一个零长度匹配。

    epsilon_p // always returns a zero-length match

This form is usually used to trigger a semantic action unconditionally. For example, it is useful in triggering error messages when a set of alternatives fail:

这种形式通常用于无条件触发一个语义动作。比如,可用于在选择的某个集合失败的情况下触发错误信息:

    r = A | B | C | eps_p[error]; // error if A, B, or C fails to match

Semantic Predicate

语义断言

Semantic predicates allow you to attach a function anywhere in the grammar. In this role, the epsilon takes a 0-ary (nullary) function/functor. The run-time function/functor is typically a test that is called upon to resolve ambiguity in the grammar. A parse failure will be reported when the function/functor result evaluates to false. Otherwise an empty match will be reported. The general form is:

语义断言使你能够在语法的任意点挂接某个函数。在这种情况下,空串使用一个零元(无参数)函数/函数对象。运行时的函数/函数对象通常是用于解决语法二义性的一个测试。如果函数/函数对象的结果推演为假,将报告一个分析失败,否则报告空匹配。通用的格式为:

    eps_p(f) >> rest; 

The nullary function f is called to do a semantic test (say, checking if a symbol is in the symbol table). If test returns true, rest will be evaluated. Otherwise, the production will return early with a no-match without ever touching rest.

无参数函数f被调用,以进行语义测试(比如,检测一个符号是否在符号表中)。如果测试返回真,则剩余部分将被

Syntactic Predicate

句意断言

Similar to Semantic predicates, Syntactic predicates assert a certain conditional syntax to be satisfied before evaluating another production. This time, epsilon_p accepts a (conditional) parser. The general form is:

类似于语义断言,句意断言断言了在推演其他产生式之前必须满足某个确定的条件句意。这次,epsilon_p接受一个(条件)分析器。通用的格式是:

    eps_p(p) >> rest; 

If p is matched on the input stream then attempt to recognize rest. The parser p is called to do a syntax check. Regardless of p's success, eps_p(p) will always return a zero length match (i.e. the input is not consumed). If test returns true, rest will be evaluated. Otherwise, the production will return early with a no-match without ever touching rest.

如果输入流匹配p,则将进行剩下的识别。分析器p用于作一个句意检查。无论p的匹配是否成功,eps_p(p)将总是返回零长匹配。(比如,没有消耗输入)。如果测试返回真,余下的部分将被推演。否则,产生式将不再接触剩余部分,并且提前返回一个匹配失败。

Example:

例子:

    eps_p('0') >> oct_p // note that '0' is actually a ch_p('0') 

Epsilon here is used as a syntactic predicate. oct_p (see numerics) is parsed only if we see a leading '0'. Wrapping the leading '0' inside an epsilon makes the parser not consume anything from the input. If a '0' is seen, epsilon_p reports a successful match with zero length.

空串在这里被作为句意断言使用。oct_p(查看数值)只在有看到有先导的'0'时才会产生分析。将先导的'0'封装在空串中使得分析器不会消耗任何输入。如果发现一个'0',epsilon_p将报告一个零长度的成功匹配。

Primitive arguments
内置类参数

Epsilon allows primitive type arguments such as char, int, wchar_t, char const*, wchar_t const* and so on. Examples:
空串允许使用语言的内置类型诸如char,int,wchar_t,char const*,wchar_t const* 等等作为参数。例子:

eps_p("hello") // same as eps_p(str_p("hello"))
eps_p
('x') // same as eps_p(ch_p('x'))

Inhibiting Semantic Actions

受限制的语义动作

In a syntactic predicate eps_p(p), any semantic action directly or indirectly attached to the conditional parser p will not be called. However, semantic actions attached to epsilon itself will always be called. The following code snippets illustrates the behavior:

在一个句意断言eps_p(p)中,任何直接或间接地挂接于条件分析器都将不被调用。然而,挂接到空集自身的语义动作却总是被调用。下面的代码片断描绘了这一特征:

    eps_p(c[f])  // f not called
eps_p(c)[f] // f is called
eps_p[f] // f is called

Actually, the conditional parser p is implicitly wrapped in a no_actions_d directive:

实际上,条件分析器p隐式地被封装在一个no_actions_d定向器中:

    no_actions_d[p]

The conditional parser is required to be free from side-effects (semantic actions). The conditional parser's purpose is to resolve ambiguity by looking ahead in the input stream for a certain pattern. Ambiguity and semantic actions do not mix well. On an ambiguous grammar, backtracking happens. And when it happens, we cannot undo the effects of triggered semantic actions.

作为条件的分析器被要求不带有副作用(语义动作)。条件分析器的目的是通过在输入中超前寻找特定的模式来消除二义性。二义性和语义动作不能很好地混合。在一个有二义性的语法中,会发生回溯。当回溯发生时,是无法将已经触发的语义动作也回溯的。

Negation

Operator ~ is defined for parsers constructed by epsilon_p/eps_p. It performs negation by complementing the results reported. ~~eps_p(x) is identical to eps_p(x).

由epsilon_p/eps_p构建的分析器定义有操作符~。它通过对报告的结果求补来求反。~~eps_p(x)与eps_p(x)等价

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值