boost.spirit用户手册翻译(2) 简介

Spirit is an object-oriented recursive-descent parser generator framework implemented using template meta-programming techniques. Expression templates allow us to approximate the syntax of Extended Backus-Normal Form (EBNF) completely in C++.

Spirit是一个面相对象的递归下降分析器生成器框架,以模板元编程技术实现。表达式模板使我们能够在C++中使用近似于EBNF范式的语法。

The Spirit framework enables a target grammar to be written exclusively in C++. Inline EBNF grammar specifications can mix freely with other C++ code and, thanks to the generative power of C++ templates, are immediately executable. In retrospect, conventional compiler-compilers or parser-generators have to perform an additional translation step from the source EBNF code to C or C++ code.

Spirit框架允许只用C++书写语法规则。内联的EBNF语法规则可以和任意的其他C++代码混合,并且,感谢C++模板的生成能力,这些都可以直接执行。过去,传统的编译器的编译器或者分析器生成器需要额外的转换步骤将EBNF代码转换成C或C++的代码。

A simple EBNF grammar snippet:

一个简单的EBNF语法片断: 

    group       ::= '(' expression ')'
     factor      ::= integer | group
     term        ::= factor (('*' factor) | ('/' factor))*
     expression  ::= term (('+' term) | ('-' term))*

is approximated using Spirit's facilities as seen in this code snippet:

接近于以下使用Spirit书写的代码片段:

    group       = '(' >> expression >> ')';
     factor      = integer | group;
     term        = factor >> *(('*' >> factor) | ('/' >> factor));
     expression  = term >> *(('+' >> term) | ('-' >> term));
 

Through the magic of expression templates, this is perfectly valid and executable C++ code. The production rule expression is in fact an object that has a member function parse that does the work given a source code written in the grammar that we have just declared. Yes, it's a calculator. We shall simplify for now by skipping the type declarations and the definition of the rule integer invoked by factor. The production rule expression in our grammar specification, traditionally called the start symbol, can recognize inputs such as:

借助于表达式模板的魔力,以上的代码是完全有效且可执行的C++代码。 expression 产生式实际上是一个对象,这个对象有一个成员函数parse来完成上面的代码片段中声明的语法规则。没错,这是一个计算器。方便起见,目前我们先省略掉类型声明和factor规则中的integer 的定义。语法中的expression 产生式,传统上称为起始符【是不是这个叫法?记不清了】,可以 识别诸如以下的输入:

    12345
     -12345
     +12345
     1 + 2
     1 * 2
     1/2 + 3/4
     1 + 2 + 3 + 4
     1 * 2 * 3 * 4
     (1 + 2) * (3 + 4)
     (-1 + 2) * (3 + -4)
     1 + ((6 * 200) - 20) / 6
     (1 + (2 + (3 + (4 + 5))))
 

Certainly we have done some modifications to the original EBNF syntax. This is done to conform to C++ syntax rules. Most notably we see the abundance of shift >> operators. Since there are no 'empty' operators in C++, it is simply not possible to write something like:

当然,我们必须对原始的EBNF语法作一些修改。这是为了适应C++的语法规则。最醒目的是,我们可以看到大量的右移位符>>。因为在C++中没有“空白”操作符,自然无法写出类似:

    a b 

as seen in math syntax, for example, to mean multiplication or, in our case, as seen in EBNF syntax to mean sequencing (b should follow a). The framework uses the shift >> operator instead for this purpose. We take the >> operator, with arrows pointing to the right, to mean "is followed by". Thus we write:

的东西,这在数学上表示乘法,而对我们来说,在EBNF上,表示并置(b紧跟着a)。于是框架使用右位移符>>来达到这一目的。 我们用>>操作符,箭头指向右边,来表示“跟随”。因此我们这么写:

    a >> b 

The alternative operator | and the parentheses () remain as is. The assignment operator = is used in place of EBNF's ::=. Last but not least, the Kleene star * which used to be a postfix operator in EBNF becomes a prefix. Instead of:

选择操作符|和括号()的用法与EBNF相同。等号=用来替代EBNF的 ::=。最后但并非不重要的,在EBNF中后置的星号*被前置的代替了:

    a* //... in EBNF syntax,

we write:

我们写:

    *a //... in Spirit. 

since there are no postfix stars, "*", in C/C++. Finally, we terminate each rule with the ubiquitous semi-colon, ";".  

是因为C++没有后置的星号。最后,我们以一贯的分号来结束每个规则。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值