⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ckwart.doc

📁 linux终端仿真程序
💻 DOC
字号:
WARTWart is a program that implements a small subset of the Unix 'lex' lexicalanalyzer generator.  Unlike lex, wart may be distributed without requirementfor a Unix license.  Wart was written in 1985 by Jeff Damens at the ColumbiaUniversity Center of Computing Activities to facilitate development of UnixKermit, and modified over the ensuing years by Frank da Cruz.Wart is intended for production of state table switchers.  It allows a set ofstates to be defined, along with a function for getting input, and a table ofstate transitions.  A C program is generated which performs actions andswitches states based on the current state and the input.The following short program demonstrates some of the capabilities andlimitations of Wart.  The program accepts from the command line a binarynumber, preceded by an optional minus sign, and optionally containing afractional part.  It prints the decimal equivalent.#include <stdio.h>int state, s = 1, m = 0, d;float f;char *b;/* Declare wart states */%states sign mantissa fraction%%					    /* Begin state table */<sign>-      { s = -1; BEGIN mantissa; }    /* Look for sign */<sign>0      { m = 0;  BEGIN mantissa; }    /* Got digit, start mantissa */<sign>1      { m = 1;  BEGIN mantissa; }<sign>.      { fatal("bad input"); }	    /* Detect bad format */<mantissa>0  { m *= 2; }		    /* Accumulate mantissa */<mantissa>1  { m = 2 * m + 1; }<mantissa>$  { printf("%d\n", s * m); return; }<mantissa>.  { f = 0.0; d = 1; BEGIN fraction; }    /* Start fraction */<fraction>0  { d *= 2; }		    	    /* Accumulate fraction */<fraction>1  { d *= 2; f += 1.0 / d; }<fraction>$  { printf("%f\n", s * (m + f) ); return; }<fraction>.  { fatal("bad input"); }%%input() {				    /* Define input() function */    int x;    return(((x = *b++) == '\0') ? '$' : x );}fatal(s) char *s; {			    /* Error exit */    fprintf(stderr,"fatal - %s\n",s);    exit(1);}main(argc,argv) int argc; char **argv; {    /* Main program */    if (argc < 2) exit(1);    b = *++argv;    state = sign;			    /* Initialize state */    wart();				    /* Invoke state switcher */    exit(0);				    /* Done */}The wart program accepts as input a C program containing lines that startwith "%" or a section delimited by "%%" (there can be only one such section).The directive "%states" declares the program's states.  The section enclosedby "%%" markers is the state table, with entries of the form  <state>X { action }which is read as "if in state <state> with input X perform { action }"The optional <state> field tells the current state or states the program mustbe in to perform the indicated action.  If no state is specified, then itmeans the action will be performed regardless of the current state.  If morethan one state is specifed, then the action will be performed in any of thelisted states.  Multiple states are separated by commas.The required input field consists of a single literal printable 7-bit ASCIIcharacter (i.e. in the range 32 through 126).  Control characters and 8-bitcharacters are not allowed.  This is to prevent the state-table array (whosesize is the product of the number of states and the number of possible inputcharacters) small enough to be handled by any C compiler.When in the indicated state, if the input is the specified character, then theassociated action is performed.  The character '.' matches any inputcharacter.  No pattern matching or range notation is provided.  The inputcharacter is obtained from the input() function, which you must define.  Itshould be alphanumeric, or else one of the characters ".% -$@" (quotes notincluded).  Note that the program above recognizes the binary point '.'through a ruse.The action is a series of zero or more C language statements, enclosed incurly braces (even if the action consists of only one statement).The BEGIN macro is defined simply to be "state = ", as in lex.The wart() function is generated by the wart program based on the statedeclarations and the state transition table.  It loops through calls toinput(), using the result to index into a big case statement it has createdfrom the state table.The wart program is invoked as follows:	wart          (Input from stdin, output to stdout)	wart fn1      (Input from fn1, output to stdout)	wart fn1 fn2  (Input from fn1, output to fn2.  Example:  wart a.w a.c)Wart programs have the conventional filetype '.w'.- F. da Cruz, Columbia University, November 1991

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -