首页 > 电脑网络 > 编程知识 > 文学编程方法

文学编程方法
2010-01-04 16:28:22   来源:   点击:

    文学编程是高德纳引入的用以替代20世纪70年代提出的结构化编程范型的编程方法。


    文学编程范型,正如高德纳所构想,不同于传统的由计算机强加的编写程序的方式和顺序,而代之以使程序员用他们自己的思维具有的逻辑和流程所要求的顺序来开发程序。文学编程不受约束地表达逻辑,而且用人类日常使用的语言写出来,就好像一篇文章一样,文章包括用以隐藏抽象的宏和传统的源代码。文学编程工具用来从文学源文件中得到两种表达:一种用于计算机进一步编译和执行,称作“纠缠”的代码;一种用于格式化的文档,称作从文学源代码中“编织”出来。。虽然第一代文学编程工具是特定于计算机语言的,但后来出现的工具可以不依赖具体语言并且存在于比编程语言更高的层次中。

    概念

    一个文学程序是用自然语言比如英语写的程序逻辑的解释,程序中交织点缀着宏和传统源代码小片断的。宏在文学源文件中是简单的,或与标题类似,或是人类语言中解决编程问题时用以描述抽象的解释性短语,它隐藏了大段代码或是较低层次的宏。这些宏与通常在计算机科学教学时用伪码写的算法相似。这些任意解释的短语成为新的精确的操作符,操作符在运行过程中由程序员创建,组成了在基本编程语言之上的“元语言”。 预处理器用于替换任意层级,说得更准确些是“在'网'和宏之间建立联系”,用命令("tangle")来产生可编译的源代码,用另一个命令("weave")来产生文档。预处理器还提供了写出宏的内容的能力和在文学程序源文件中的任何地方增加已创建的宏的能力,由此不必受传统编程语言强加的那些限制或是打断自己的思路。

    优点

    根据高德纳本人所说,文学编程为高质量程序而生,因为它强迫程序员显式地描述程序背后的思路,让不充分的设计决策无所遁形。高德纳还声称文学编程提供了第一流的文档系统,它不是插件,而是在编程中随着思路的慢慢展现而不断自然发展的过程。产生出来的文档使作者能在以后的任何时间重新找到自己的思路,也能使其他程序员更容易理解程序的建构过程。这与传统文档不同,那里程序员必须和编译器规定的代码顺序写在一起,还必须从代码和注释中重现当时的思路。文学编程的元语言能力也据称普遍利于思考,能从更高的层次统观代码,也能增加人的智能可成功保持和处理的概念数量。Applicability of the concept to programming on a large scale, that of commercial-grade programs is proven by an edition of TeX code as a literate program.

    误解

    文学编程常常被误解为不过是从有源代码和注释的文件中产生格式化文档,或是在代码里写大量的注释。这一误解导致那些文档析出工具,如Perl的Plain Old Documentation系统也被称为“文学编程工具”。尽管如此,因为这些工具没有实现隐藏在自然语言宏系统背后的“抽象概念网”,或是提供把机器规定的源代码顺序变为人类思维更容易理解的顺序的能力,它们不能在高德纳提出的意义下被称作文学编程工具。

    例子

    一个文学编程的经典例子是标准Unix单词计数程序wc的文学实现。高德纳在他的《文学编程》书中的第12章展示了这个例子的CWEB版本。后来它也为noweb文学编程工具而重写。这一例子漂亮地阐释了文学编程的基本元素。

    创建宏

    下面这个wc的文学编程代码片断展示了在文学编程中用来创建宏的自然语言的描述性词组有多随意,宏作为文学编程语言中新的“操作符”,并且隐藏了代码块或其它的宏。由两个尖括号组成("<<...>>")的标记符号表示宏,"@"符号在noweb文件中表示一节代码的结束。"<<*>>"符号表示“根”,即最上层节点,文学编程工具要从这里展开宏组成的网。实际上,扩展的源代码可通过任何节和小节(即标为"<<代码块名>>="的代码)写出来,所以一个文学程序文件可包括多个机器源代码文件。

    wc的目的是对多个文件中的行、单词和字母计数。文件中的行数是......../更多解释/
     
    这里是由noweb程序wc.nw定义的文件wc.c的概述: 
        <<*>>=
        <<包含头文件>>
        <<定义>>
        <<全局变量>>
        <<函数>>
        <<主程序>>
        @
     
    我们必须包含标准输入输出定义,因为我们想发送格式化的输出到stdout和stderr上。
        <<包含头文件>>=
        #include <stdio.h>
        @
    

    Note also that the unraveling of the chunks can be done in any place in the literate program text file, not necessarily in the order they are sequenced in the enclosing chunk, but as is demanded by the logic reflected in the explanatory text that envelops the whole program.

    作为网的程序 - 宏不只是节的名字

    宏和标准文档中的“节名”不同。文学编程的宏能隐藏任何代码块,并且被用于任何低层次的机器语言操作符内,常常在如"if", "while"或 "case"这样的逻辑操作符内。这会在下面这段文学程序wc的代码片断中解释。

     
    这里的代码块做了计数的工作,这正是wc存在的目的,实际上非常容易写。我们察看每一个字母并且如果它是一个单词的开始或结束,则会更改状态。
     
        <<扫描文件>>=
        while (1) {
          <<Fill buffer if it is empty; break at end of file>>
          c = *ptr++;
          if (c > ' ' && c < 0177) {
            /* visible ASCII codes */
            if (!in_word) {
              word_count++;
              in_word = 1;
            }
            continue;
          }
          if (c == '
    ') line_count++;
          else if (c != ' ' && c != '\t') continue;
          in_word = 0;
            /* c is newline, space, or tab */
        }
        @
    

    实际上,宏能代表任意的代码块和其它宏,并且因此比自顶向下或自底向上的代码块或小节更通用。高德纳说当他意识到这一点后,他开始把程序想成不同部分组成的“网”。

    人的逻辑顺序,而不是编译器的

    在noweb文学程序中,除了可以任意顺序展现代码外,宏背后的代码块,一旦由"<<...>>="引入,可以在文件后面的任何一个地方通过简单地写"<<代码块名>>="进行扩充并且往里添加更多的内容,如下面这个代码片断所示("+"为了可读性而被文档格式化器所添加,它并不在代码中)。

    The grand totals must be initialized to zero at the beginning of the program. 
    If we made these variables local to main, we would have to do this initialization 
    explicitly; however, C globals are automatically zeroed. (Or rather,``statically 
    zeroed.'' (Get it?) 
     
        <<Global variables>>+=
        long tot_word_count, tot_line_count, 
             tot_char_count;
          /* total number of words, lines, chars */
        @
    
    记录思想碰撞的火花,创建出类拔萃的文档

    文学编程的文档作为写程序的一部分而产生。Instead of comments provided as side notes to source code a literate program contains the explanation of concepts on each level, with lower level concepts deferred to their appropriate place, which allows for better communication of thought. The snippets of the literate wc above show how an explanation of the program and its source code are interwoven. Such exposition of ideas creates the flow of thought that is like a literary work. Knuth famously wrote a "novel" which explains the code of the computer strategy game Colossal Cave Adventure, perfectly readable.

    文学编程工具

    第一个发布的文学编程环境是WEB,由高德纳于1981年为他的TeX排版系统而引入。它使用Pascal作为其基础编程语言,使用TeX作为文档排版工具。The complete commented TeX source code was published in Knuth's TeX: The program, volume B of his 5-volume Computers and Typesetting. Knuth had privately used a literate programming system called DOC as early as 1979. He was inspired by the ideas of Pierre-Arnoul de Marneffe. The free CWEB, written by Knuth and Silvio Levy, is WEB adapted for C and C++, runs on most operating systems and can produce TeX and PDF documentation.

    其它的文学编程概念的实现有noweb和FunnelWeb,它们都是源代码独立的。Noweb以其简单而知名:just 2 text markup conventions and 2 tool invocations are needed to use it, and it allows for text formatting in HTML rather than going through the TeX system. FunnelWeb is another program without dependency on TeX which can produce HTML documentation output. It has more complicated markup (with "@" escaping any FunnelWeb command), but has many more flexible options.

    The Leo text editor is an outlining editor which supports optional noweb and CWEB markup. The author of Leo actually mixes two different approaches: first, Leo is an outlining editor, which helps with management of large texts, second, Leo incorporates some of the ideas of literate programming, which in its pure form (i.e. the way it is used by Knuth Web tool and/or tools like "noweb") is possible only with some degree of inventiveness and the use of the editor in a way not exactly envisioned by its author (in modified @root nodes). However this and other extensions (@file nodes) make outline programming and text management successful and easy and in some ways similar to literate programming.

    Haskell编程语言对半文学编程有原生支持,其来源于CWEB但用了较简单的实现。如果想要TeX输出,你可以写 a plain LaTeX file where source code is marked by a given surrounding environment; LaTeX can be set up to handle that environment, while the Haskell compiler looks for the right markers to identify Haskell statements to compile, removing the TeX documentation as if they were comments. However, as described above, this is not literate programming in the sense intended by Knuth. Haskell's functional, modular nature makes literate programming directly in the language somewhat easier, but it is not nearly as powerful as one of the a WEB tools where "tangle" can reorganize in arbitrary ways.

相关热词搜索:文学编程

上一篇:怎样汉化GBA游戏
下一篇:线性搜索是最简单的一种搜索算法