josh@crunch.uucp (Josh Siegel) (03/10/86)
I wrote this in about 20 minutes so that I could use it to shorten some identifers in my code. Because of the interest I thought I would post it. It is written in lex and is infact about 60 lines of code. I included the lex'ed file for those without lex and that accounts for the 650 lines of source. Play with it and see what you think. It is a BIG hack. Send me mail . . . I L O V E mail Josh Siegel {convex,ucbvax,gatech,csu-cs,anl-mcs,lanl-a}!unmvax \ !crunch!josh / {cmcl2,csu-cs,dirac,dspo,gel,ias,ihnp4,mtu,nmsu}!lanl!quark ------------------------Cut me -------------------------------- #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files: # idents # This archive created: Sun Mar 9 20:30:29 1986 export PATH; PATH=/bin:$PATH if test ! -d 'idents' then echo shar: creating directory "'idents'" mkdir 'idents' fi echo shar: entering directory "'idents'" cd 'idents' echo shar: extracting "'README'" '(1375 characters)' if test -f 'README' then echo shar: will not over-write existing file "'README'" else sed 's/^ X//' << \SHAR_EOF > 'README' XThe following is a very "short" program that will find all the X C identifiers in a program that are longer then 8 characters X long. It will output them to a header file. It is done X in two passes. The first pass finds the identifiers and then X second pass does something with them. Currently it will X create a set of defines for the identifiers. If you wish X you can change identp2.c to have it make a set of sed or X ed commands that will substitute the identifiers. X X XI hope this helps and that it ends the current problems with X long identifiers. I wrote pass1 in lex and then included a X lex'ed copy for those without lex. It "should" compile X without any problem. It had made one identifer longer then 8 X characters but I used ed and substituted a shorter identifer X for it using the output of the shortc. I hope it works. X X --Josh Siegel X XP.S. the shell script "shortc" is a hack. All it does it pass X stuff to the shortp1 and shortp2. I would be interested X in any better ideas somebody might have. X XFiles: X XMakefile A short makefile XREADME This file Xidentp1.h A header file that lets you set the MaxCharacters Xshortc* The shell script that drives it all Xshortc.1 A manual page for shortc Xshortp1.c The C code for the first pass of shortc Xshortp1.l The Lex source code for the first pass Xshortp2.c The second pass. This is the one you change. SHAR_EOF if test 1375 -ne "`wc -c < 'README'`" then echo shar: error transmitting "'README'" '(should have been 1375 characters)' fi fi # end of overwriting check echo shar: extracting "'shortp1.l'" '(457 characters)' if test -f 'shortp1.l' then echo shar: will not over-write existing file "'shortp1.l'" else sed 's/^ X//' << \SHAR_EOF > 'shortp1.l' X%% X[A-Za-z_]+[a-zA-Z_0-9]?+ printit(); X[\"] quote(); X%% XFILE *idents,*fopen(); X#include "identp1.h" Xmain() X{ X idents=fopen("ident","w"); X if(idents==NULL) { X fprintf(stderr,"panic! Cannot open header file!\n"); X exit(1); X } X yylex(); X} Xprintit() X{ X if(yyleng>IDENTLEN) X fprintf(idents,"%s\n",yytext); X} Xquote() X{ X int c; X X while(1){ X c=input(); X switch(c) { X case '"': X return(0); X case '\\': X c=input(); X } X } X} Xyywrap() X{ X return(1); X} SHAR_EOF if test 457 -ne "`wc -c < 'shortp1.l'`" then echo shar: error transmitting "'shortp1.l'" '(should have been 457 characters)' fi fi # end of overwriting check echo shar: extracting "'shortc'" '(61 characters)' if test -f 'shortc' then echo shar: will not over-write existing file "'shortc'" else sed 's/^ X//' << \SHAR_EOF > 'shortc' X/lib/cpp $1|shortp1>/dev/null Xsort -u ident|shortp2 Xrm ident SHAR_EOF if test 61 -ne "`wc -c < 'shortc'`" then echo shar: error transmitting "'shortc'" '(should have been 61 characters)' fi chmod +x 'shortc' fi # end of overwriting check echo shar: extracting "'shortc.1'" '(648 characters)' if test -f 'shortc.1' then echo shar: will not over-write existing file "'shortc.1'" else sed 's/^ X//' << \SHAR_EOF > 'shortc.1' X.TH SHORT L 3/6/86 X.UC 4 X.SH NAME Xshort -\ Creates a define table for compilers with short identifiers X.SH SYNOPSIS X.B short Xfile X.SH DESCRIPTION X.PP X.I short Xgoes out and looks at a piece of C code and finds all identifiers that Xare longer then 8 characters long. Then it will create a header Xfile that will use the #define command to re-define all the Xidentifers to short names. The output file is "ident.h". You Xcan load this into the code or use the #include command. X.SH FILES X ident X idents.h X.SH AUTHOR XJosh Siegel X.SH BUGS XWho me? write something with a bug? X XIt has not been tested on any machine with short identifiers. (Thats a bug?) SHAR_EOF if test 648 -ne "`wc -c < 'shortc.1'`" then echo shar: error transmitting "'shortc.1'" '(should have been 648 characters)' fi fi # end of overwriting check echo shar: extracting "'shortp2.c'" '(170 characters)' if test -f 'shortp2.c' then echo shar: will not over-write existing file "'shortp2.c'" else sed 's/^ X//' << \SHAR_EOF > 'shortp2.c' X#include <stdio.h> Xmain () { X char buff[255]; X int count; X X count = 1; X X while (gets (buff) != NULL) X printf ("#define %s LONG%d\n", buff, count++); X} SHAR_EOF if test 170 -ne "`wc -c < 'shortp2.c'`" then echo shar: error transmitting "'shortp2.c'" '(should have been 170 characters)' fi fi # end of overwriting check echo shar: extracting "'Makefile'" '(147 characters)' if test -f 'Makefile' then echo shar: will not over-write existing file "'Makefile'" else sed 's/^ X//' << \SHAR_EOF > 'Makefile' XCC=cc -O X Xall: shortp1 shortp2 X Xshortp1.c:shortp1.l Xshortp1:shortp1.o X ${CC} -o shortp1 shortp1.o X Xshortp2: shortp2.o X ${CC} -o shortp2 shortp2.o SHAR_EOF if test 147 -ne "`wc -c < 'Makefile'`" then echo shar: error transmitting "'Makefile'" '(should have been 147 characters)' fi fi # end of overwriting check echo shar: extracting "'shortp1.c'" '(10230 characters)' if test -f 'shortp1.c' then echo shar: will not over-write existing file "'shortp1.c'" else sed 's/^ X//' << \SHAR_EOF > 'shortp1.c' X# include "stdio.h" X# define U(x) x X# define NLSTATE yyprev=YYNEWLINE X# define BEGIN yybgin = yysvec + 1 + X# define INITIAL 0 X# define YYLERR yysvec X# define YYSTATE (yyestate-yysvec-1) X# define YYOPTIM 1 X# define YYLMAX 200 X# define output(c) putc(c,yyout) X# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar) X# define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;} X# define yymore() (yymorfg=1) X# define ECHO fprintf(yyout, "%s",yytext) X# define REJECT { nstr = yyreject(); goto yyfussy;} Xint yyleng; Xextern char yytext[]; Xint yymorfg; Xextern char *yysptr, X yysbuf[]; Xint yytchar; XFILE * yyin = { X stdin X}, *yyout = { X stdout X}; Xextern int yylineno; Xstruct yysvf { X struct yywork *yystoff; X struct yysvf *yyother; X int *yystops; X}; Xstruct yysvf *yyestate; Xextern struct yysvf yysvec[], X *yybgin; X# define YYNEWLINE 10 Xyylex () { X int nstr; X extern int yyprev; X while ((nstr = yylook ()) >= 0) Xyyfussy: switch (nstr) { X case 0: X if (yywrap ()) X return (0); X break; X case 1: X printit (); X break; X case 2: X quote (); X break; X case -1: X break; X default: X fprintf (yyout, "bad switch yylook %d", nstr); X } return (0); X} X/* end of yylex */ XFILE * idents, *fopen (); X#include "identp1.h" Xmain () { X idents = fopen ("ident", "w"); X if (idents == NULL) { X fprintf (stderr, "panic! Cannot open header file!\n"); X exit (1); X } X yylex (); X} Xprintit () { X if (yyleng > IDENTLEN) X fprintf (idents, "%s\n", yytext); X} Xquote () { X int c; X X while (1) { X c = input (); X switch (c) { X case '"': X return (0); X case '\\': X c = input (); X } X } X} Xyywrap () { X return (1); X} Xint yyvstop[] = { X 0, X X 2, X 0, X X 1, X 0, X X 1, X 0, X 0 X}; X# define YYTYPE char Xstruct yywork { X YYTYPE verify, advance; X} yycrank[] = { X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 1, 3, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0, 0, 0, 0, 0, X 0, 0, 0, 0,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, 5, 5, 5, 5, X 5, 5, 5, 5, 5, 5, 5, 5, X 5, 5, 0, 0, 0, 0, 0, 0, X 0, 0, 5, 5, 0, 0, 5, 5, X 5, 5, 5, 5, 5, 5, 5, 5, X 5, 5, 5, 5, 5, 5, 5, 5, X 5, 5, 5, 5, 5, 5, 5, 5, X 5, 5, 5, 5, 5, 5, 5, 5, X 5, 5, 5, 5, 5, 5, 5, 5, X 5, 5, 5, 5, 5, 5, 5, 5, X 5, 5, 0, 0, 0, 0, 0, 0, X 0, 0 X}; Xstruct yysvf yysvec[] = { X 0, 0, 0, X yycrank + 1, 0, 0, X yycrank + 0, yysvec + 1, 0, X yycrank + 0, 0, yyvstop + 1, X yycrank + 76, 0, yyvstop + 3, X yycrank + 134, yysvec + 4, yyvstop + 5, X 0, 0, 0 X}; Xstruct yywork *yytop = yycrank + 256; Xstruct yysvf *yybgin = yysvec + 1; Xchar yymatch[] = { X 00, 01, 01, 01, 01, 01, 01, 01, X 01, 01, 01, 01, 01, 01, 01, 01, X 01, 01, 01, 01, 01, 01, 01, 01, X 01, 01, 01, 01, 01, 01, 01, 01, X 01, 01, '"', 01, 01, 01, 01, 01, X 01, 01, 01, 01, 01, 01, 01, 01, X '0', '0', '0', '0', '0', '0', '0', '0', X '0', '0', 01, 01, 01, 01, 01, 01, X 01, 'A', 'A', 'A', 'A', 'A', 'A', 'A', X 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', X 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', X 'A', 'A', 'A', 01, 01, 01, 01, 'A', X 01, 'A', 'A', 'A', 'A', 'A', 'A', 'A', X 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', X 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', X 'A', 'A', 'A', 01, 01, 01, 01, 01, X 0 X}; Xchar yyextra[] = { X 0, 0, 0, 0, 0, 0, 0, 0, X 0 X}; X/* ncform 4.1 83/08/11 */ X Xint yylineno = 1; X# define YYU(x) x X# define NLSTATE yyprev=YYNEWLINE Xchar yytext[YYLMAX]; Xstruct yysvf *yylstate[YYLMAX], X **yylsp, X **yyolsp; Xchar yysbuf[YYLMAX]; Xchar *yysptr = yysbuf; Xint *yyfnd; Xextern struct yysvf *yyestate; Xint yyprev = YYNEWLINE; Xyylook () { X register struct yysvf *yystate, X **lsp; X register struct yywork *yyt; X struct yysvf *yyz; X int yych; X struct yywork *yyr; X# ifdef LEXDEBUG X int debug; X# endif X char *yylastch; X /* start off machines */ X# ifdef LEXDEBUG X debug = 0; X# endif X if (!yymorfg) X yylastch = yytext; X else { X yymorfg = 0; X yylastch = yytext + yyleng; X } X for (;;) { X lsp = yylstate; X yyestate = yystate = yybgin; X if (yyprev == YYNEWLINE) X yystate++; X for (;;) { X# ifdef LEXDEBUG X if (debug) X fprintf (yyout, "state %d\n", yystate - yysvec - 1); X# endif X yyt = yystate -> yystoff; X if (yyt == yycrank) {/* may not be any transitions */ X yyz = yystate -> yyother; X if (yyz == 0) X break; X if (yyz -> yystoff == yycrank) X break; X } X *yylastch++ = yych = input (); X tryagain: X# ifdef LEXDEBUG X if (debug) { X fprintf (yyout, "char "); X allprint (yych); X putchar ('\n'); X } X# endif X yyr = yyt; X if ((int) yyt > (int) yycrank) { X yyt = yyr + yych; X if (yyt <= yytop && yyt -> verify + yysvec == yystate) { X if (yyt -> advance + yysvec == YYLERR) { X /* error transitions */ X unput (*--yylastch); X break; X } X *lsp++ = yystate = yyt -> advance + yysvec; X goto contin; X } X } X# ifdef YYOPTIM X else X if ((int) yyt < (int) yycrank) {/* r < yycrank */ X yyt = yyr = yycrank + (yycrank - yyt); X# ifdef LEXDEBUG X if (debug) X fprintf (yyout, "compressed state\n"); X# endif X yyt = yyt + yych; X if (yyt <= yytop && yyt -> verify + yysvec == yystate) { X if (yyt -> advance + yysvec == YYLERR) { X /* error transitions */ X unput (*--yylastch); X break; X } X *lsp++ = yystate = yyt -> advance + yysvec; X goto contin; X } X yyt = yyr + YYU (yymatch[yych]); X# ifdef LEXDEBUG X if (debug) { X fprintf (yyout, "try fall back character "); X allprint (YYU (yymatch[yych])); X putchar ('\n'); X } X# endif X if (yyt <= yytop && yyt -> verify + yysvec == yystate) { X if (yyt -> advance + yysvec == YYLERR) { X /* error transition */ X unput (*--yylastch); X break; X } X *lsp++ = yystate = yyt -> advance + yysvec; X goto contin; X } X } X if ((yystate = yystate -> yyother) && (yyt = yystate -> yystoff) != yycrank) { X# ifdef LEXDEBUG X if (debug) X fprintf (yyout, "fall back to state %d\n", yystate - yysvec - 1); X# endif X goto tryagain; X } X# endif X else { X unput (*--yylastch); X break; X } X contin: X# ifdef LEXDEBUG X if (debug) { X fprintf (yyout, "state %d char ", yystate - yysvec - 1); X allprint (yych); X putchar ('\n'); X } X# endif X ; X } X# ifdef LEXDEBUG X if (debug) { X fprintf (yyout, "stopped at %d with ", *(lsp - 1) - yysvec - 1); X allprint (yych); X putchar ('\n'); X } X# endif X while (lsp-- > yylstate) { X *yylastch-- = 0; X if (*lsp != 0 && (yyfnd = (*lsp) -> yystops) && *yyfnd > 0) { X yyolsp = lsp; X if (yyextra[*yyfnd]) {/* must backup */ X while (yyback ((*lsp) -> yystops, -*yyfnd) != 1 && lsp > yylstate) { X lsp--; X unput (*yylastch--); X } X } X yyprev = YYU (*yylastch); X yylsp = lsp; X yyleng = yylastch - yytext + 1; X yytext[yyleng] = 0; X# ifdef LEXDEBUG X if (debug) { X fprintf (yyout, "\nmatch "); X sprint (yytext); X fprintf (yyout, " action %d\n", *yyfnd); X } X# endif X return (*yyfnd++); X } X unput (*yylastch); X } X if (yytext[0] == 0 /* && feof(yyin) */ ) { X yysptr = yysbuf; X return (0); X } X yyprev = yytext[0] = input (); X if (yyprev > 0) X output (yyprev); X yylastch = yytext; X# ifdef LEXDEBUG X if (debug) X putchar ('\n'); X# endif X } X} Xyyback (p, m) Xint *p; X{ X if (p == 0) X return (0); X while (*p) { X if (*p++ == m) X return (1); X } X return (0); X} X /* the following are only used in the lex library */ Xyyinput () { X return (input ()); X} Xyyoutput (c) Xint c; { X output (c); X} Xyyunput (c) Xint c; { X unput (c); X} SHAR_EOF if test 10230 -ne "`wc -c < 'shortp1.c'`" then echo shar: error transmitting "'shortp1.c'" '(should have been 10230 characters)' fi fi # end of overwriting check echo shar: extracting "'identp1.h'" '(19 characters)' if test -f 'identp1.h' then echo shar: will not over-write existing file "'identp1.h'" else sed 's/^ X//' << \SHAR_EOF > 'identp1.h' X#define IDENTLEN 8 SHAR_EOF if test 19 -ne "`wc -c < 'identp1.h'`" then echo shar: error transmitting "'identp1.h'" '(should have been 19 characters)' fi fi # end of overwriting check echo shar: done with directory "'idents'" cd .. # End of shell archive exit 0