[back] [toc]

Visual Literate Programming

Given a style file fsa.sty that associates translations to pictorial elements of finite state automata, one can specify the code that is associated with a finite state automaton through pictorial specifications similar to the following one:

                        ----|
                           ||- q0O  ---------else--------|-q10OO
0-9|-               -----------|||-------   A -Z
-  |-      --------- ----- ---|||||---  -----a-z
    ||-----     0-9-+-  ---  || | || ---eof  -------
 q1OO          |-----    ----  ||  |  |_  ----        ----||
 |      q3OO  |      |---    ||*  /   ||    ---             q9OO
 |.                ||     ||    |    ||    |||          ||  -
 ||             q4OO       ||-    |    ||       q8OO        -||||
                               ||                        A -Z
 qOO  ||--               q5OO              q7OO                 a-z
 2  ||||0-9                    q6OO    -|  -                0-9
      ||                             -|_||
The code created from the above example is introduced to the following program.

<..prog..>

       #include <stdio.h>
       <.defines.>
       <.spec of fsa.>
       void main()
       {       char *str;
               FILE  *file_in, *file_out;
         file_in  = fopen("scan.in", "r");
         file_out = fopen("scan.out", "w");
         WHILE  TRUE  DO
           str = fsa(file_in);
           IF  (((int) *str) == 8) |
               (((int) *str) == 10)  THEN
             exit(0);
           FI
           fprintf(file_out, "'%s' -> %d\n",
                           (str + 1), *str);
           free(str);
         END
       }
       -_-_-

<..defines..>

       #define IF     if (
       #define THEN   ) {
       #define ELSE   }else{
       #define ELSEIF }else if(
       #define FI     }
       #define WHILE  while(
       #define FOR    for(
       #define WITH   ;
       #define STEP   ;
       #define DO     ){
       #define BEGIN  {
       #define END    }
       #define CASE   switch(
       #define OF     ){
       #define AND    &
       #define FALSE  0
       #define TRUE   1
       -_-_-
The program prog.c, obtained from the compilation of the source literate program, produces the output file scan.out on input file scan.in.
[back] [toc]