BSD 4_3 development
[unix-history] / usr / contrib / kermit / ckwart.doc
CommitLineData
ee2abe03
C
1WART
2
3Wart is a program that implements a small subset of the Unix 'lex' lexical
4analyzer generator. Unlike lex, wart may be distributed without requirement
5for a Unix license. Wart was written by Jeff Damens at the Columbia University
6Center of Computing Activities to facilitate development of Unix Kermit.
7
8Wart is intended for production of state table switchers. It allows a set of
9states to be defined, along with a function for getting input, and a table of
10state transitions. A C program is generated which performs actions and
11switches states based on the current state and the input.
12
13The following short program demonstrates some of the capabilities and
14limitations of Wart. The program accepts from the command line a binary
15number, preceded by an optional minus sign, and optionally containing a
16fractional part. It prints the decimal equivalent.
17
18#include <stdio.h>
19
20int state, s = 1, m = 0, d;
21float f;
22char *b;
23
24%states sign mantissa fraction /* Declare wart states */
25
26%% /* Begin state table */
27<sign>- { s = -1; BEGIN mantissa; } /* Look for sign */
28<sign>0 { m = 0; BEGIN mantissa; } /* Got digit, start mantissa */
29<sign>1 { m = 1; BEGIN mantissa; }
30<sign>. { fatal("bad input"); } /* Detect bad format */
31<mantissa>0 { m *= 2; } /* Accumulate mantissa */
32<mantissa>1 { m = 2 * m + 1; }
33<mantissa>$ { printf("%d\n", s * m); return; }
34<mantissa>. { f = 0.0; d = 1; BEGIN fraction; } /* Start fraction */
35<fraction>0 { d *= 2; } /* Accumulate fraction */
36<fraction>1 { d *= 2; f += 1.0 / d; }
37<fraction>$ { printf("%f\n", s * (m + f) ); return; }
38<fraction>. { fatal("bad input"); }
39%%
40
41input() { /* Define input() function */
42 int x;
43 return(((x = *b++) == '\0') ? '$' : x );
44}
45
46fatal(s) char *s; { /* Error exit */
47 fprintf(stderr,"fatal - %s\n",s);
48 exit(1);
49}
50
51main(argc,argv) int argc; char **argv; { /* Main program */
52 if (argc < 1) exit(1);
53 b = *++argv;
54 state = sign; /* Initialize state */
55 wart(); /* Invoke state switcher */
56 exit(0); /* Done */
57}
58
59The wart program accepts as input a C program containing lines that start
60with "%" or sections delimited by "%%". The directive "%states" declares
61the program's states. The section enclosed by "%%" markers is the state
62table, with entries of the form
63
64 <state>X { action }
65
66which is read as "if in state <state> with input X perform { action }"
67
68The optional <state> field tells the current state or states the program must
69be in to perform the indicated action. If no state is specified, then it
70means the action will be performed regardless of the current state. If more
71than one state is specifed, then the action will be performed in any of the
72listed states. Multiple states are separated by commas.
73
74The required input field consists of a single literal character. When in
75the indicated state, if the input is the specified character, then the
76associated action will be performed. The character '.' matches any input
77character. No pattern matching or range notation is provided. The input
78character is obtained from the input() function, which you must define. It
79should be alphanumeric, or else one of the characters ".% -$@" (quotes not
80included). Note that the program above recognize the binary point '.'
81through a ruse.
82
83The action is a series of zero or more C language statements, enclosed in
84curly braces.
85
86The BEGIN macro is defined simply to be "state = ", as in lex.
87
88The wart() function is generated by the wart program based on the state
89declarations and the state transition table. It loops through calls to
90input(), using the result to index into a big case statement it has created
91from the state table.
92
93Wart is invoked as follows:
94
95 wart (Input from stdin, output to stdout)
96
97 wart fn1 (Input from fn1, output to stdout)
98
99 wart fn1 fn2 (Input from fn1, output to fn2. Example: wart a.w a.c)
100
101Wart programs have the conventional filetype '.w'.