Boost.Spirit用户手册翻译(20):参数化分析器

22 篇文章 0 订阅

Parametric Parsers
参数化分析器

 

We already have a hint of the dynamic nature of the Spirit framework. This capability is fundamental to Spirit. Dynamic parsing is a very powerful concept. We shall take this concept further through run-time parametric parsers. We are able to handle parsing tasks that are impossible to do with any EBNF syntax alone.

对Spirit框架的动态特性我们已经有了初步的印象。这种动态的能力是Spirit的基础。动态分析是非常强大的概念。我们将通过运行时参数化分析器来进一步了解这个概念。这样我们将可以解析那些仅仅借助EBNF语法无法完成的目标。

A Little Secret

一个小秘密

A little critter called boost::ref lurking in the boost distribution is quite powerful beast when used with Spirit's primitive parsers. We are used to seeing the Spirit primitive parsers created with string or character literals such as:

一个小生物,名为boost::ref,潜伏于boost的分发包中,在与Spirit的元素分析器一起使用时,却是相当强大的野兽。我们已经见过类似如下形式以字符或者字符串创建的Spirit元素分析器:

    ch_p('A')
range_p('A', 'Z')
str_p("Hello World")

str_p has a second form that accepts two iterators over the string:

str_p有第二种形态,接受一个string的两个迭代器:

    char const* first = "My oh my";
char const* last = first + std::strlen(first);

str_p(first, last)

What is not obvious is that we can use boost::ref as well:

但不显眼的是,我们也可以使用boost::ref:

    char ch = 'A';
char from = 'A';
char to = 'Z';

ch_p(boost::ref(ch))
range_p(boost::ref(from), boost::ref(to))

When boost::ref is used, the actual parameters to ch_p and range_p are held by reference. This means that we can change the values of ch, from and to anytime and the corresponding ch_p and range_p parser will follow their dynamic values. Of course, since they are held by reference, you must make sure that the referenced object is not destructed while parsing.

当boost::ref被使用,传递给ch_p和range_p的参数实际上是引用。这意味着我们可以在任何时候改变ch,from和to的值,而 相应的ch_p和range_p分析器也将追随这些动态的赋值。当然,因为这些参数是对象的引用,所以你必须确保在分析时被引用的对象不会被析构。

What about str_p?

那么str_p呢?

While the first form of str_p (the single argument form) is reserved for null terminated string constants, the second form (the two argument first/last iterator form) may be used:

虽然str_p的第一种形式(但参数的形式)是为零终结的常量字符串保留的,但第二种形式(两个参数为first/last迭代器的形式)可以这么用:

    char const* first = "My oh my";
char const* last = first + std::strlen(first);

str_p(boost::ref(first), boost::ref(last))
Hey, don't forget chseq_p. All these apply to this seldom used primitive as well.
嘿,别忘了chseq_p。以上这些同样适用于这个很少被使用的元素。

Functional Parametric Primitives

函数式参数化元素

    #include <boost/spirit/attribute/parametric.hpp>

Taking this further, Spirit includes functional versions of the primitives. Rather than taking in characters, strings or references to characters and strings (using boost::ref), the functional versions take in functions or functors.

更进一步,Spirit包含了函数式版的元素。不是使用字符、字符串或者字符和字符串的引用(通过boost::ref),函数式版使用的是函数或者仿函数。

f_chlit and f_ch_p

f_chlit 和 f_ch_p

The functional version of chlit. This parser takes in a function or functor (function object). The function is expected to have an interface compatible with:

chlit的函数式版本。这个分析器使用函数或者仿函数(函数对象)。所使用的函数需要兼容如下的接口:

    CharT func()

where CharT is the character type (e.g. char, int, wchar_t).

这里CharT是字符类型(比如char、int、wchar_t)。

The functor is expected to have an interface compatible with:

仿函数则要兼容如下接口:

    struct functor
{
CharT operator()() const;
};

where CharT is the character type (e.g. char, int, wchar_t).

这里CharT是字符类型(比如char、int、wchar_t)。

Here's a contrived example:

这是个简单的例子:

    struct X
{
char operator()() const
{
return 'X';
}
};

Now we can use X to create our f_chlit parser:

现在我们可以使用X来创建我们的f_chlit分析器:

    f_ch_p(X())

f_range and f_range_p

f_range 和 f_range_p

The functional version of range. This parser takes in a function or functor compatible with the interfaces above. The difference is that f_range (and f_range_p) expects two functors. One for the start and one for the end of the range.

range的函数式版。分析器使用的函数或仿函数需有兼容于上面的接口。不同的是,f_range(以及f_range_p)需要两个仿函数。一个对应范围的起点另一个对应终点。

f_chseq and f_chseq_p

f_chseq 和 f_chseq_p

The functional version of chseq. This parser takes in two functions or functors. One for the begin iterator and one for the end iterator. The function is expected to have an interface compatible with:

函数式版的chseq。分析器使用两个函数或仿函数。一个对应begin迭代器另一个对应end迭代器。所使用的函数要求与如下接口兼容:

    IteratorT func()

where IteratorT is the iterator type (e.g. char const*, wchar_t const*).

The functor is expected to have an interface compatible with:

而仿函数要求与如下接口兼容:

    struct functor
{
IteratorT operator()() const;
};

where IteratorT is the iterator type (e.g. char const*, wchar_t const*).

这里IteratorT是迭代器类型(比如char const*, wchar_t const*)。

f_strlit and f_str_p

f_strlit 和 f_str_p

The functional version of strlit. This parser takes in two functions or functors compatible with the interfaces that f_chseq expects.

函数式版的strlit。分析器使用与上面的接口兼容的函数或仿函数。

 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值