Literate Programming

Similar documents
L A TEX. COMPSCI 111 / 111G Mastering Cyberspace: An introduction to practical computing. LaTeX. Development. Why?

Literate Programming in Lisp (LP/Lisp)

CODING STYLE. Contents 1. List of style comments 1 2. Other chapters 3 References 5

SOFTWARE ARCHITECTURE 4. TEXT FORMATTING SYSTEM

Intro to LATEX I. Aaron Erlich POLS/CSSS 510, Why LATEX? Programming Document Structure Floats Tables Lists Math

My Mathematical Thesis

Using L A TEX. A numbered list is just that a collection of items sorted and labeled by number.

Contents 1 Introduction 3 2 Installation 4 3 Using Web-mode 5 4 Book Terminology and Automatic Happenings 6 5 Movement Within the WEB 7 6 Movement Amo

Programming Paradigms. Literate Programming

Latex Tutorial. CIS400 Senior Design 9/5/2013

The SimpleLit System for Literate Programming

An Introduction to. Rado Ivanov CIS400 Senior Design Tutorial September 18, 2014

Formatting with LaTeX

Fundamentals of Programming. Lecture 3: Introduction to C Programming

An Interactive Introduction to L A TEX

Introduction to MCS 220 and L A TEX

Introduction to Latex. A workshop by Dr. Ala Eshmawi

Outline. Installing LaTeX. Opening TeXShop. Intro to LaTeX. Intro to LaTeX interface Working with text Tabbing and tables Figures Math and equations

WYSIWYG Systems. CSCM10 Research Methodology Lecture 4 (11/2/16): A Taster of L A TEX WYSIWYM. Advantages/Disadvantages of WYSIWYG Systems

CSCM10 Research Methodology A Taster of L A TEX

WYSIWYG Systems. CSCM10 Research Methodology A Taster of L A TEX WYSIWYM. Advantages/Disadvantages of WYSIWYG Systems.

Introduction to Scientific Typesetting Lesson 1: Getting Started

Getting ready for L A TEX. Alexis Dimitriadis. Version: March 28, 2013

CSCM10 Research Methodology A Taster of L A TEX

Software Manual for Windows Z/EVES Version 2.3

CSCI 2132 Software Development. Lecture 8: Introduction to C

L A TEX for psychological researchers

Functions and text files

Introduction to L A TEX for MCS-236

Chapter 2, Part I Introduction to C Programming

Reproducible research with Emacs org-mode

4. Structure of a C++ program

Org mode (La)TEX macros for HTML and L A TEX export

Latex Tutorial 1 L A TEX. 1.1 Text

The authorindex Package

Contents. Foreword. Examples of GeoGebra Applet Construction 1 A Straight Line Graph... 1 A Quadratic Graph... 6 The Scalar Product...


1. Welcome. (1) Hello. My name is Dr. Christopher Raridan (Dr. R). (3) In this tutorial I will introduce you to the amsart documentclass.

L A T E X Week 11, Other Document Classes

Functions and text files

An Interactive Introduction to L A TEX. Part 1: The Basics. John Lees-Miller. writel A TEX

Reproducible research and knitr

Literate Programming

Introduzione a LaTex. Fabrizio Messina

Getting Started with Emacs

Lecture 1: Short summary of LaTeX basics

Isabelle/jEdit as IDE for domain-specific formal languages and informal text documents


27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

1 Lexical Considerations

Introduction to LAT E X

Document Preparation Using L A TEX

An Introduction to L A TEX

Global Search And Replace User s Manual

Introduction to lambda calculus Part 3

An Introduction to. Andrew G. West, Jian Chang CIS400 Senior Design Tutorial September 15, 2009

MadCap Software. Index Guide. Flare 2017 r2

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Square cells: an array cooking lesson

LuaTEX says goodbye to Pascal

Typing Mathematics. Darrin Doud

COMPILER DESIGN LECTURE NOTES

Pathologically Eclectic Rubbish Lister

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

CS 301. Lecture 05 Applications of Regular Languages. Stephen Checkoway. January 31, 2018

Math 235: Introduction to LaTeX

COMP496/901: Academic Presentation and Writing Skills Using LaTeX

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

CIS 110: Introduction to Computer Programming

2.8. Decision Making: Equality and Relational Operators

CGS 3066: Spring 2015 JavaScript Reference

LaTeX. Information Literacy II EN(IL2) Course

Tutorial No. 2 - Solution (Overview of C)

Organizing research projects with an efficient open-source tool (emacs org-mode)

Dynamic Documents. Using knitr. Benjamin Hofner

A Brief Introduction to L A T E X

Typesetting with TEX

A Fast Review of C Essentials Part I

PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

Extending xcom. Chapter Overview of xcom

What is LaTeX. Is a document markup language and document preparation system for the TeX typesetting program

Compiler phases. Non-tokens

1 CLWEB INTRODUCTION 1

LaTeX A Tutorial. Mohsen Alimomeni, 2010

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Fundamentals of Programming Session 4

L A TEX for Psychological Researchers

1.3.4 case and case* macro since 1.2. Listing Conditional Branching, Fast Switch. Listing Contract

Introduction to LaTeX. Paul Fodor Stony Brook University

Programming for Engineers Introduction to C

A Document Class and a Package for handling multi-files projects

Fundamental of Programming (C)

INTRODUCTION 1 AND REVIEW

The KBibTeX Handbook. Yuri Chornoivan

Chapter 2: Overview of C++

BIBTEX-based Manuscript Writing Support System for Researchers *

Professional outputs with ODS LATEX

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Symbol Table Generator (New and Improved) Jim Johnson, JKL Consulting, North Wales, PA

Transcription:

Literate Programming Andreas Klein March 11, 2009 Contents 1 Introduction to Literate Programming 1 2 Pweb Desgin goals 2 3 Pweb Manual 2 3.1 Structure of a WEB-Document................... 2 3.2 Text sections............................. 3 3.3 Code sections and Modules..................... 3 3.4 Marcos................................. 4 3.5 Special variable names........................ 5 3.6 Include files.............................. 5 3.7 Conditional Compilation....................... 6 3.8 Compatibility Features........................ 6 3.9 Editing Pweb documents...................... 6 3.10 Extending Pweb........................... 7 1 Introduction to Literate Programming Most People working in mathematics know D. E. Knuth name]knuth, Donald Ervin TEX typesetting system. Less known is that in companion with TEX Knuth developed a new programming style called literate programming (see [2]). The idea is that a program should read like an article. A literate programming environment also called web-system consist of two programs called tangle and weave. The tangle program extracts the source code of the web-file and generates an input file for the compiler (interpreter) of our programming language. It must resort the code from the human readable form into the machine readable form. The weave program generates the input for a text processor. It must apply syntax highlighting to our code and generate an index, etc. 1

weave woven output Text Processing TEX, HTML,... documentation Web-file tangle source code Compiler C, C++, Lisp,... computer program Figure 1: The literate programming environment In principle you can use any text processing tool and any computer language as basis for a literate programming environment. In practice the weave and tangle program need some information about the destination language, so a specific literate programming environment support only few languages. 2 Pweb Desgin goals When I started this project, I found that no literate programming satisfies exactly my needs. So I began with the development of a new literate programming tool. I called pweb, since its main part is a bunch of Perl scripts. I designed pweb with the following goals in mind: It should be easy to add support for additional programming language. To guarantee this pweb should only use regular expressions and not a complete parser. I was willing to scarify advanced formating functions, like auto-detection of variables, for this goal. Not every language have such a good preprocessor like LISP. Therefore pweb should provide its own preprocessor. I decided to use the macro processor M4 for that purpose. It should be possible to have (in documentation) variable names like α, x, ^x. 3 Pweb Manual 3.1 Structure of a WEB-Document All web commands start with a @ symbol. The first line of a pweb-document must be of the form @<programing language>2<formating language>. For example a C++ Program that is documented in L A TEX starts with @cpp2latex. 2

The next lines up to the fist @* command form the limbo section. The contents of the limbo section are copied directly into the header of the documentation (the part between \documentclass{article} and \begin{document} if you use L A TEX or the part between <head> and </head> if you use HTML). If you use latex as formating language the limbo section must define the title and the author. The remaining part of the web-document is split in text sections, code sections, named modules and macros. A text section must preceded each other type. 3.2 Text sections First line of a text section has the form @*n <title of the section>. where n N. This define a section of depth n with the given title. pweave transform it to the L A TEX commands \section, \subsection and so on or the HTML tags <H1>... </H1>. It depends on the chosen formating language how many substructures exist. @* is a short form for the highest level @*0. Inside a text section you can use all commands from the chosen formating language (like \emph{...} in L A TEX or <em>...</em> in HTML). Of course once you start using these commands you cannot change the formating language. 3.3 Code sections and Modules A code section starts either with @a or @c and ends at the next text section. Both commands are equivalent and the only reason for having two commands is the smooth transition form a other literate programming environments (cweb uses @c and fweb uses @a). I recommend to use only @c in new pweb projects. The effect of a code section is that ptangle copies that code directly in to the source code of the program. In addition to codes section fweb knows modules. A module starts with the line @<Module name@>= and end at the next text section. You can insert a module in any code section or any other module by @<Module name@>. A typical use of a module is initialisation code like: @* Initialise variables. Explain what has to be done @<init@>= set some variables... @* The main Program. Explain the program. 3

@c int main() { @<init@> } Do something... The presence of modules changes the programming style. It is no longer necessary to use functions for structuring the program. The only define is you mean a function in all other case a module does a better job. In addition you can split the code in smaller parts than it is normally possible in a programming language. D. E. Knuth [1] recommends that a single section contain no more than 12 lines of code. If you every find your self writing lager code section you should consider define a new module to structure the code. 3.4 Marcos Macros are useful technique for doing computation at compile time, but the support of macros is very different in the programming languages. For example Common Lisp has a perfect macro support, where macros can be written in almost the same syntax as the rest of the programming language. C has limited macro support (no recursion in macros) and the macro language differs completely from the programming language. Java has no macros at all. pweb offers an interface to the M4 macro processor, that can used in addition to the macros provided by the chosen programming language. A simple macro has the form @define MACRO-NAME SUBSTITUTION and the effect is that in the, whole program MACRO-NAME is replaced by SUBSTITUTION. This corresponds to the C-Marco #define MACRO-NAME SUBSTITUTION More complex macros must be define in one section. The macro section must start with @m and ends at the begin of the next text section. Inside a macro section you can use the whole syntax of the M4 macro language. Especially you have conditions and recursion. Here is a small example of how to define a macro for the factorial function. @m define( FACTORIAL, ifelse($1,1,1, eval($1* FACTORIAL(eval($1-1) ))) ) 4

A complete describtion of M4 can be found in [3]. A side effect of using macros is that a single line of the fweb document expands to multiple lines in the programming language. This tweaks up the line numbering and makes it difficult to interpret a later error message which referrers to the wrong line number. To help you fweb contains a special command @l which resets the line numbering. You should long macros always like this: @c some commands A_BIG_LONG_MACRO @l some more commands The @l command only works with programming languages that support redirection of error messages like C and C++ do with #line macro. 3.5 Special variable names Most programming languages require that a variable name must start with a letter and allow only letters, digits and the underscore sign (_) afterwards. But in mathematics we often use variables like α, x or ^x. Fweb pay attention to this by proving special commands for such variables. With the command @alpha you define a variable (or to be more precise a code fragment) named alpha in the source code and printed α in the documentation. All Greek letters are supported by this facility. With x@ you define a variable x. It becomes x_prime or xprime in the programming language. Similarly you can define variables ^x, x by x@hat and x@tilde, respectively. Combinations like @beta@ for β are possible. 3.6 Include files You can use @include <filename> to include other pweb documents into your document. The include mechanism is similar to the one used by C and C++. If the include-file has a preamble it will be ignored. Each include-file must start with a text-section and it can contain only complete sections. The @include command must stand alone in a line. 5

3.7 Conditional Compilation In addition to the conditional compilation features that may be provided by the underlying programming languages pweb provides an extra mechanism. Code between @tangle off and @tangle on are ignored by ptangle. The commands @tangle off and @tangle on must stand alone in a line and are not nestable. Typically one use them to give a short example code in the documentation. @* How to use the new functions. Some explanation... @tangle off @c some lines of example code... @tangle on Similar pweave ignores the code between @weave off and @weave on. One can use these commands to hide some definitions in the documentation. Since the purpose of literate programming is to provide a full documentation, one should use @weave off and @weave on rarely. A exception is that it can be a good idea, to use code like @weave off @include somefile.web @weave on and run pweave direct on somefile.web. This gives to separate articles one for the main program and one for the include-file. 3.8 Compatibility Features To improve the comparability with other literate programming systems pweb ignores the following commands: @; used by fweb as invisible semicolon. @\ used by fweb as forced line break. @~ used by fweb to inhibit a line break. You should not use any these commands in a new pweb project. 3.9 Editing Pweb documents You can use any text editor to create you pweb documents. But the correct syntax highlighting can be problem. If you use Emacs for editing you can use the 6

mmm-mode from M. A. Shulman [4] to patch together the syntax highlighting for L A TEX, C and M4. Install the MMM Mode and add the following code to your.emacs. (mmm-add-group web ((web1 :submode c-mode :face mmm-code-submode-face :delimiter-mode nil :front "@<[^>]*@>=" :back "@[* ]" ) (web2 :submode c-mode :face mmm-code-submode-face :delimiter-mode nil :front "@[ac]" :back "@[* ]" ) (web3 :submode m4-mode :face mmm-code-submode-face :delimiter-mode nil :front "@[m]" :back "@[* ]" ) )) (setq mmm-global-mode maybe) (mmm-add-mode-ext-class latex-mode "\\.web\\ " web) (add-to-list auto-mode-alist ("\\.web\\ ". latex-mode)) 3.10 Extending Pweb The formating of pweb is driven by a bunch of perl-scripts. For each programming language lang and each formating language text you need the following files. In principle you can add any combination of programming language and formatting language. 7

begin_lang2text.pl middle_lang2text.pl end_lang2text.pl formater_lang2text formater_m4lang2text codestart_lang2text codeend_lang2tex codestart_m4lang2text codeend_m4lang2text Prints the text at the begin of the formatted document (like \documentclass{article} if you use L A TEX). Prints what ever come after the limbo section (if you use HTML it would be most likely </head><body>). Prints the text that closes the formatted document (like \end{document} if you use L A TEX). Converts the quell code of the programming language in printable code. A typical entry of formater_cpp2tex is s/(?<![a-za-z0-9_])\b(asm auto bool... while)\b/\{\\rm \\bf $1\}/g; This regular expresion prints the keywords in boldface. Like formater_lang2text but treads the macro sections. Contains things that have to be printed at the start of a code section Same as before but for the end of the code section. Contains things that have to be printed at the start of a macro section End of a macro section Table 1: Files needed by pweb References [1] D. E. Knuth. Literate programming. The Computer Journal, 27:97 111, 1985. Reprintet with correction in [2]. [2] Donald E. Knuth. Literate Programming. Number 27 in CSLI Lecture Notes. Center for the Study of Language and Information, 1992. Stanford, California. [3] R. Seindal, F. Pinard, G. V. Vaughan, and E. Blake. GNU M4, version 1.4.11, 2008. Available online: http://www.gnu.org/software/m4/ manual/index.html. [4] Michael Abraham Shulman. MMM Mode for Emacs, version 0.4.8 edition, 2004. http://mmm-mode.sourceforge.net/. 8