boost.Spirit用户手册(1):前言

 "Examples of designs that meet most of the criteria for "goodness" (easy to understand, flexible, efficient) are a recursive-descent parser, which is traditional procedural code. Another example is the STL, which is a generic library of containers and algorithms depending crucially on both traditional procedural code and on parametric polymorphism."

Bjarne Stroustrup

History

历史:

A decade and a half ago, I wrote my first calculator in Pascal. It is one of my most unforgettable coding experiences. I was amazed how a mutually recursive set of functions can model a grammar specification. In time, the skills I acquired from that academic experience became very practical. Periodically I was tasked to do some parsing. For instance, whenever I need to perform any form of I/O, even in binary, I try to approach the task somewhat formally by writing a grammar using Pascal-like syntax diagrams and then write a corresponding recursive-descent parser. This worked very well.

10年半前,我用pascal写了自己的第一个计算器。那是最难忘的编程经历之一,我被“一组相互递归的函数是如何组成一个语法规则”这个东西所陶醉。随着时间的推移,我从学术活动中学到的技艺,变得越来越实际。我常常需要做一些解析任务。比如,只要我需要进行某种形式的I/O,甚至是二进制形式的,我都会试着以这样的方式来完成:以类似于pascal的语法图写出语法,接着再写出相应的解析器。这种方式是很有效的。

The arrival of the Internet and the World Wide Web magnified this thousand-fold. At one point I had to write an HTML parser for a Web browser project. I got a recursive-descent HTML parser working based on the W3C formal specifications easily. I was certainly glad that HTML had a formal grammar specification. Because of the influence of the Internet, I then had to do more parsing. RFC specifications were everywhere. SGML, HTML, XML, even email addresses and those seemingly trivial URLs were all formally specified using small EBNF-style grammar specifications. This made me wish for a tool similar to big-time parser generators such as YACC and ANTLR, where a parser is built automatically from a grammar specification. Yet, I want it to be extremely small; small enough to fit in my pocket, yet scalable.
 互联网和万维网的出现,把我的任务放大了千百倍。有一次我需要为一个网页浏览器项目写HTML解析器。我轻易地就写出了一个基于W3C的正规标准的递归下降解析器,自然,我很高兴HTML有一个正规的语法规则。由于互联网的影响,我接着又需要做更多的解析。RFC的标准无处不在。SGML,HTML, XML,甚至电子邮件地址还有那些看起来微不足道的URL地址都被正式的以小型的EBNF风格的语法规则来规范。这让我企盼某种工具,就像那些一流的解析器生成器,比如YACC和ANTLR,在这些软件里,解析器根据语法规则自动生成。然而,我希望它能够尽可能的小;小到能装进我的口袋里,同时又是可伸缩的。

It must be able to practically parse simple grammars such as email addresses to moderately complex grammars such as XML and perhaps some small to medium-sized scripting languages. Scalability is a prime goal. You should be able to use it for small tasks such as parsing command lines without incurring a heavy payload, as you do when you are using YACC or PCCTS. Even now that it has evolved and matured to become a multi-module library, true to its original intent, Spirit can still be used for extreme micro-parsing tasks. You only pay for features that you need. The power of Spirit comes from its modularity and extensibility. Instead of giving you a sledgehammer, it gives you the right ingredients to create a sledgehammer easily. For instance, it does not really have a lexer, but you have all the raw ingredients to write one, if you need one.

在实际运用中,它必须能解析从诸如电子邮件地址这样的简单语法到诸如XML甚至某些中等大小的脚本语言的相对复杂的语法。可伸缩性是它的主要目标。人们必须能把它用在诸如解析命令行这样的小目标上而不必付出过高的代价,就像在使用YACC或者PCCTS时那样。即使到了今天,spirit已经发展成熟为一个多模式(multi-module,多种模型?)的库,它依然符合最初的目标,依然能够运用在非常轻量级的解析上。用户只为需要的功能付出代价。spirit的威力来源于它的建模能力和扩展能力。它不是给你一把锤子,取而代之的是给你合适的元素,让你能够轻易地造出锤子。比如,它并没有包含一个词法分析器,但是它可以提供给你你所有的基本元素来写一个,如果你需要的话。

The result was Spirit. Spirit was a personal project that was conceived when I was doing R&D in Japan. Inspired by the GoF's composite and interpreter patterns, I realized that I can model a recursive-descent parser with hierarchical-object composition of primitives (terminals) and composites (productions). The original version was implemented with run-time polymorphic classes. A parser is generated at run time by feeding in production rule strings such as "prod ::= {‘A’ | ‘B’} ‘C’;"A compile function compiled the parser, dynamically creating a hierarchy of objects and linking semantic actions on the fly. A very early text can be found here.

结果,Spirit诞生了。它是个人项目,是我在日本从事R&D时构思的。从GoF的合成和解释器得到启发,我认识到可以用由简单元素(终结符)和合成物(产生式)合成的层次对象来构造一个递归下降的分析器。最初的版本是用运行时多态的类来实现的。分析器经由运行时提供的产生式规则字符串(比如 "prod ::= {‘A’ | ‘B’} ‘C’;")产生;一个编译函数编译产生分析器,动态的创建一个层次对象,并且自由地嵌入语义动作。早期的文档在http://spirit.sourceforge.net/dl_docs/pre-spirit.htm

The version that we have now is a complete rewrite of the original Spirit parser using expression templates and static polymorphism, inspired by the works of Todd Veldhuizen (" Expression Templates", C++ Report, June 1995). Initially, the static-Spirit version was meant only to replace the core of the original dynamic-Spirit. Dynamic-spirit needed a parser to implement itself anyway. The original employed a hand-coded recursive-descent parser to parse the input grammar specification strings.

现在的这个版本,是一个用表达式模板和编译期多态对原始的Spirit的完全重写,灵感来源于Todd Veldhuizen 的著作(" Expression Templates", C++ Report, June 1995)。最初,“静态版”Spirit只是打算用来替代动态版Spirit的核心部分。动态版Spirit里,分析器总要自我实现——最初的版本使用一个手写的递归下降的分析器来分析输入的语法规则字符串。

After its initial "open-source" debut in May 2001, static-Spirit became a success. At around November 2001, the Spirit website had an activity percentile of 98%, making it the number one parser tool at Source Forge at the time. Not bad for such a niche project such as a parser library. The "static" portion of Spirit was forgotten and static-Spirit simply became Spirit. The framework soon evolved to acquire more dynamic features.

在2001年5月,Spirit首次以开源的形式登场之后,它成功了。大约在2001年11月,Spirt的网页有98%是活动的。【the Spirit website had an activity percentile of 98%这句吃不准】,这使它在当时成为sourceforge上排名首位的分析工具。这境遇对于一个分析器库来说还不坏。“静态Spirit”的“静态”被遗忘了,静态Spirit变成了Spirit。它很快演进了【the framework是不是指Spirit本身?】,以获得更多的动态功能。

How to use this manual

如何使用本手册

The Spirit framework is organized in logical modules starting from the core. This documentation provides a user's guide and reference for each module in the framework. A simple and clear code example is worth a hundred lines of documentation; therefore, the user's guide is presented with abundant examples annotated and explained in step-wise manner. The user's guide is based on examples -lots of them.

Spirit框架从核心(core)开始,以一个逻辑性的模型来组织。这份文档提供了框架里各个模块的用户指南和参考。一个简单而关键的例子胜过上千行的说明文档;因此,这是一份有丰富的以步进式的方式来注释和解释的范例的用户指南。这份指南基于范例——很多范例。

As much as possible, forward information (i.e. citing a specific piece of information that has not yet been discussed) is avoided in the user's manual portion of each module. In many cases, though, it is unavoidable that advanced but related topics are interspersed with the normal flow of discussion. To alleviate this problem, topics categorized as "advanced" may be skipped at first reading.

在每个模块的用户手册部分,预备知识(forward information)都被尽可能多的排除了。但是,伴随正常的讨论过程,仍有高级的但是相关的主题零星出现。为了减轻这一问题,那些分类为“高级”的主题在第一次阅读时,可以跳过。

Some icons are used to mark certain topics indicative of their relevance. These icons precede some text to indicate:

一些图标用来标识主题的作用。以下图标表明紧接着图标的文字的作用:

Icons

Note

注记

Information provided is moderately important and should be noted by the reader.

 所提供的信息是比较重要的,需要读者注意。

Alert

警告

Information provided is of utmost importance.

信息非常重要

Detail

明细

Information provided is auxiliary but will give the reader a deeper insight into a specific topic. May be skipped.

信息是辅助性的,但会让读者更深刻的理解特定的主题,可以跳过。

Tip

小提示 

A potentially useful and helpful piece of information.

有潜在使用和帮助价值的短信息

Support

技术支持

Please direct all questions to Spirit's mailing list. You can subscribe to the mailing list here. The mailing list has a searchable archive. A search link to this archive is provided in Spirit's home page. You may also read and post messages to the mailing list through an NNTP news portal (thanks to www.gmane.org). The news group mirrors the mailing list. Here are two links to the archives: via gmane, via geocrawler.

请将所有的问题直接发到Spirit的邮件列表上,邮件列表在https://lists.sourceforge.net/lists/listinfo/spirit-general。这个列表有一个可搜索的结构。这个结构的搜索连接在http://spirit.sf.net/。你也可以通过一个NNTP 新闻入口(http://news.gmane.org/thread.php?group=gmane.comp.parsers.spirit.general)(感谢 www.gmane.org)来阅读和投递消息。新闻组是邮件列表的镜像。以下是如何链:通过gmane(http://dir.gmane.org/gmane.comp.parsers.spirit.general),通过geocrawler.(http://sourceforge.net/mailarchive/forum.php?forum_id=1595gmane.org

To my dear daughter Phoenix
献给我亲爱的女儿Phoenix
 

Joel de Guzman
September 2002

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值