A Real CCIDE Logo CCIDE Decision Table Generator Example


The following example is taken from the ccide program itself:

Here is the original if-then-else code (can you find the logic error?)

extern int donotgenerate, notimestamp, uselocaltime, noinline,yydebug;
char *xstring;
int main(int argc, char **argv)
{
    int narg = 1;
    while (argc > narg) {
	if (strcmp("-b", argv[narg]) == 0) {
	    notimestamp = 1;
	} else {
	    if (strcmp("-l", argv[narg]) == 0) {
		uselocaltime = 1;
	    } else {
		if (strcmp("-e", argv[narg]) == 0) {
		    strcat(xstring, "- ");
		} else {
		    if (strcmp("-n", argv[narg]) == 0) {
			noinline = 1;
		    } else {
			if (strcmp("-s", argv[narg]) == 0) {
			    GenSkeleton();
			    exit(0);
			} else {
			    if (strcmp("-t", argv[narg]) == 0) {
				yydebug = 1;
			    } else {
				if (strcmp("-u", argv[narg]) == 0) {
				    noinline = 1;
				    donotgenerate = 1;
				} else {
				    if (strcmp("-V", argv[narg]) = 0) {
					ShowCopyright();
					exit(0);
				    } else {
					if (strcmp("-x", argv[narg]) == 0) {
					    strcat(xstring, "- ");
					} else {
					    Usage();
					    exit(0);
					}
				    }
				}
			    }
			}
		    }
		}
	    }
	}

	narg++;
    }
}

Compare this to the following:

Code using a decision table -- options.d

extern int donotgenerate, notimestamp, uselocaltime, noinline, yydebug;
char *xstring;
//CCIDE_INLINE_CODE:
int main(int argc, char **argv)
{
#define Argis(P) strcmp( #P, argv[narg]) == 0 
    while( argc>narg ) { 
  	//DECISION_TABLE:
  	//  Y - - - - - - - - - | Argis(-b)
  	//  - Y - - - - - - - - | Argis(-l)
  	//  - - - - - - - - Y - | Argis(-e)
  	//  - - Y - - - - - - - | Argis(-n)
  	//  - - - Y - - - - - - | Argis(-s)
  	//  - - - - Y - - - - - | Argis(-t)
  	//  - - - - - Y - - - - | Argis(-u)
  	//  - - - - - - Y - - - | Argis(-V)
  	//  - - - - - - - - - Y | Argis(-x)
  	// _________________________________
  	//  X - - - - - - - - - | notimestamp=1;
  	//  - X - - - - - - - - | uselocaltime=1;
  	//  - - X - - X - - - - | noinline=1;
  	//  - - - X - - - - - - | GenSkeleton();
  	//  - - - - X - - - - - | yydebug=1;
  	//  - - - - - X - - - - | donotgenerate=1;
  	//  - - - - - - X - - - | ShowCopyright(); 
  	//  - - - - - - - X - - | Usage();
  	//  - - - - - - - - X - | checkequal=0;    
  	//  - - - - - - - - - X | strcat(xstring, "- ");
  	//  - - - X - - X X - - | exit(0);
  	//END_TABLE:  	
	narg++;
    }


Results after expansion with 'ccide < options.d > options.c'

extern int donotgenerate, notimestamp, uselocaltime, noinline, yydebug;
char *xstring;
//CCIDE_INLINE_CODE:
//GENERATED_CODE:
#ifndef __CCIDE_INLINE_C
#define __CCIDE_INLINE_C

/*
ccide-0.0.7-0
Copyright (C) 2002-2004 Thomas W. Young, e-mail:  ccide@twyoung.com
This code (generated by ccide) is licensed under the GNU Library
General Public License 
As such, it may be freely used, even link-editted with proprietary code
*/

typedef unsigned int CCIDE_BIT;
int ccide_group=1;

#ifndef UINT_MAX
#include "limits.h"
#endif 

int CcideFindRule(               /* Return rule number */
	int nbrrules,  CCIDE_BIT ccide_table, CCIDE_BIT yes[], CCIDE_BIT no[])
{
        int r=0;
        CCIDE_BIT nstate;

        nstate = UINT_MAX ^ ccide_table;

        while (
		( (yes[r] & nstate) || (no[r]  & ccide_table) )
		 && ( ++r < nbrrules ) 
	) {};

        return r;
}

int CcideFindRuleYes(             /* Return rule number */
	int nbrrules, CCIDE_BIT ccide_table, CCIDE_BIT yes[])
{
        int r=0;
        CCIDE_BIT nstate;

        nstate = UINT_MAX ^ ccide_table;
        while ( (yes[r] & nstate) && ( ++r < nbrrules ) ) {};
        return r;
}
#endif /* End ifndef  __CCIDE_INLINE_C  */
//END_GENERATED_CODE:  
int main(int argc, char **argv)
{
#define Argis(P) strcmp( #P, argv[narg]) == 0 
    while( argc>narg ) { 
  	//DECISION_TABLE:
  	//  Y - - - - - - - - - | Argis(-b)
  	//  - Y - - - - - - - - | Argis(-l)
  	//  - - - - - - - - Y - | Argis(-e)
  	//  - - Y - - - - - - - | Argis(-n)
  	//  - - - Y - - - - - - | Argis(-s)
  	//  - - - - Y - - - - - | Argis(-t)
  	//  - - - - - Y - - - - | Argis(-u)
  	//  - - - - - - Y - - - | Argis(-V)
  	//  - - - - - - - - - Y | Argis(-x)
  	// _________________________________
  	//  X - - - - - - - - - | notimestamp=1;
  	//  - X - - - - - - - - | uselocaltime=1;
  	//  - - X - - X - - - - | noinline=1;
  	//  - - - X - - - - - - | GenSkeleton();
  	//  - - - - X - - - - - | yydebug=1;
  	//  - - - - - X - - - - | donotgenerate=1;
  	//  - - - - - - X - - - | ShowCopyright(); 
  	//  - - - - - - - X - - | Usage();
  	//  - - - - - - - - X - | checkequal=0;    
  	//  - - - - - - - - - X | strcat(xstring, "- ");
  	//  - - - X - - X X - - | exit(0);
  	//END_TABLE:  	
  	//GENERATED_CODE: FOR TABLE_1. 	10 Rules, 9 conditions, and 11 actions.
  	 {	CCIDE_BIT CCIDE_table1_yes[10]={ 256UL, 128UL,  64UL,  32UL,  16UL,   8UL,   4UL,   2UL,   1UL,   0UL};

  	CCIDE_TABLE_1:
  		switch(CcideFindRuleYes(10,
  			  (Argis(-b))
  			| (Argis(-l))<<1
  			| (Argis(-e))<<2
  			| (Argis(-n))<<3
  			| (Argis(-s))<<4
  			| (Argis(-t))<<5
  			| (Argis(-u))<<6
  			| (Argis(-V))<<7
  			| (Argis(-x))<<8
  			  ,CCIDE_table1_yes)) {
  		case 9:		//  Rule 8  
  		     Usage();
  		     exit(0);
  			break;
  		case 1:		//  Rule 7  
  		     ShowCopyright(); 
  		     exit(0);
  			break;
  		case 4:		//  Rule 4  
  		     GenSkeleton();
  		     exit(0);
  			break;
  		case 0:		//  Rule 10  
  		     strcat(xstring, "- ");
  			break;
  		case 6:		//  Rule 9  
  		     checkequal=0;    
  			break;
  		case 2:		//  Rule 6  
  		     noinline=1;
  		     donotgenerate=1;
  			break;
  		case 3:		//  Rule 5  
  		     yydebug=1;
  			break;
  		case 5:		//  Rule 3  
  		     noinline=1;
  			break;
  		case 7:		//  Rule 2  
  		     uselocaltime=1;
  			break;
  		case 8:		//  Rule 1  
  		     notimestamp=1;
  			break;
  		} // End Switch 
  	}
  	//END_GENERATED_CODE: FOR TABLE_1, by ccide-0.0.7-0 Mon Aug 23 14:49:17 2004
	narg++;
    }


Results after undo with 'ccide -u -x -x -x < options.c > options2.d'

extern int donotgenerate, notimestamp, uselocaltime, noinline, yydebug; char *xstring; //CCIDE_INLINE_CODE: int main(int argc, char **argv) { #define Argis(P) strcmp( #P, argv[narg]) == 0 while( argc>narg ) { //DECISION_TABLE: // Y - - - - - - - - - - - - | Argis(-b) // - Y - - - - - - - - - - - | Argis(-l) // - - - - - - - - Y - - - - | Argis(-e) // - - Y - - - - - - - - - - | Argis(-n) // - - - Y - - - - - - - - - | Argis(-s) // - - - - Y - - - - - - - - | Argis(-t) // - - - - - Y - - - - - - - | Argis(-u) // - - - - - - Y - - - - - - | Argis(-V) // - - - - - - - - - Y - - - | Argis(-x) // _________________________________ // X - - - - - - - - - - - - | notimestamp=1; // - X - - - - - - - - - - - | uselocaltime=1; // - - X - - X - - - - - - - | noinline=1; // - - - X - - - - - - - - - | GenSkeleton(); // - - - - X - - - - - - - - | yydebug=1; // - - - - - X - - - - - - - | donotgenerate=1; // - - - - - - X - - - - - - | ShowCopyright(); // - - - - - - - X - - - - - | Usage(); // - - - - - - - - X - - - - | checkequal=0; // - - - - - - - - - X - - - | strcat(xstring, "- "); // - - - X - - X X - - - - - | exit(0); //END_TABLE: narg++; }
N.B. Do not try anything like 'ccide -u < foobar.c > foobar.c'
foobar.c will be empty!
The -x -x -x options cause ccide to add three rules to the decision table.


CCIDE Project Home Page
Support This Project

Last changed 2010/07/13