home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
comp_lan
/
86_june
/
xref1.ltg
< prev
next >
Wrap
Text File
|
1986-04-07
|
8KB
|
336 lines
#include <stdio.h>
#define MAX_DEF 200 /* maximum language definitions */
#define NAME_CHARS "._" /* Punctuation Allowed in Names */
#define DQUOTE 0x22 /* ASCII Double Quote Character */
#define SQUOTE 0x27 /* ASCII Single Quote Character */
FILE *fopen(), *infile, *outfile;
char lbuff[120]; /* current line holder */
int lcnt = 1; /* line counter starts at line 1 */
int lptr = 0; /* assume empty line buffer */
int is_c = FALSE; /* is input source in "C"? Allow { } */
struct numrec {
int number; /* line number of symbol occurance */
struct numrec *nnext; /* next number of symbol line */
};
struct symrec {
char *symbol; /* symbol string */
int count; /* symbol occurance counters */
struct numrec *lnum; /* line number records */
struct symrec *lnode; /* left branch of binary tree */
struct symrec *rnode; /* right branch of binary tree */
};
struct symrec *symtab = NULL; /* declare main symbol table */
char *Exceptions[ MAX_DEF ]; /* language definition table structure */
main( argc, argv )
char *argv[];
int argc;
{
int c;
if( (argc<2) || (argc>3) ){
printf("XRF: Usage is XRF [input name] <output name>\n");
exit();}
if( NULL==(infile = fopen(argv[1],"r")) ){ /* open input file */
printf("ERROR: Cannot open input file - %s\n",argv[1]);
exit();}
if( argc==3 ){
if( NULL==(outfile = fopen( argv[2],"w")) ){ /* open output file */
printf("ERROR: Cannot open output file - %s\n",argv[2]);
exit();}
printf("Generating Listing File - %s\n", argv[2] );
}
else{
outfile = stdout;
}
load_tbl( argv[1] ); /* load language table */
printf("Language Table Loaded\n");
/* Main input and parse loop. */
while( (c=getc(infile)) != EOF ) filter( c );
fprintf( outfile, "Symbol Table for File - %s\n", upper(argv[1]) );
symprint( symtab );
printf("\nEnd.\n");
} /* end of main routine */
/* */
/* comment processor, quoted text, and punctuation */
/* */
filter( c )
char c;
{
char c1;
static is_com = FALSE; /* assume not in comment */
if( c==DQUOTE ){ /* filter between double quotes */
while( (c=getc(infile))!=DQUOTE ) if( c==EOF ) return;
next_char( ' ' ); }
else if( c==SQUOTE ){ /* filter between single quotes */
while( (c=getc(infile))!=SQUOTE ) if( c==EOF ) return;
next_char( ' ' ); }
else if( (c=='{') && !is_c ){ /* if not "C" code, strip between {} */
while( (c=getc(infile))!='}' ) ;
next_char( ' ' );
}
else if( c=='(' ) { /* remove all between ( * * ) pairs*/
c1 = getc(infile);
if( c1!='*' ){
next_char( ' ' );
ungetc( c1, infile );}
else {
is_com = TRUE;
while( is_com ){
while( (c=getc(infile))!='*' ) ;
c1 = getc(infile);
if( c1==')' ) is_com = FALSE;
else ungetc( c1, infile );
} /* end of while is_com */
next_char( ' ' );
} /* end of else comment */
} /* end of if paren */
else if( c=='/' ) { /* remove all between / * * / pairs */
c1 = getc(infile);
if( c1!='*' ){
next_char( ' ' );
ungetc( c1, infile );}
else {
is_com = TRUE;
while( is_com ){
while( (c=getc(infile))!='*' ) ;
c1 = getc(infile);
if( c1=='/' ) is_com = FALSE;
else ungetc( c1, infile );
} /* end of while is_com */
next_char( ' ' );
} /* end of else comment */
} /* end of if slash */
else{ /* filter non-NAME_CHARS punctuation */
if( isalnum(c) || strchr(NAME_CHARS,c) || isspace(c) )
next_char( c );
else /* special case to allow -> as token */
if( c=='-' ) {
if( '>' == (c=getc( infile )) ){ /* if - followed by > then allow */
next_char( '-' );
next_char( '>' );
}
else { /* else, put c back and send space */
ungetc( c, infile );
next_char( ' ' );
}
}
else next_char(' '); /* if not minus, send space */
}
}
/* symbol process (after comments and quotes have been stripped) */
next_char( a )
char a;
{
if( isspace(a) ){ /* filter excess white space */
if( lptr==0 ){
if( a == '\n' ) ++lcnt; /* count line in file */
return;}
lbuff[lptr] = '\0';
lptr = 0;
next_sym( lbuff, lcnt );
if( a == '\n' ) ++lcnt; /* count line in file */
}
else{
lbuff[lptr++] = a; /* insert character into buffer */
}
}
next_sym( s, n )
char *s;
int n;
{
struct symrec *newsym();
if( isdigit(s[0]) ) return; /* filter strings beginning with # */
if( is_lang(s) ) return; /* filter strings in Language Table */
symtab = newsym( symtab, s, n ); /* insert new symbol in symbol table*/
}
struct symrec *newsym( q, w, n )
struct symrec *q;
char *w;
int n;
{
struct symrec *salloc();
struct numrec *newnum();
char *strsave();
int cond;
if( q==NULL ) {
q = salloc(); /* make a new node */
upper( w ); /* save upper case */
q->symbol = strsave( w ); /* store string in record */
q->count = 1;
q->lnum = q->lnode = q->rnode = NULL;
q->lnum = newnum( q->lnum, lcnt ); }
else
if( (cond = strcmp(w, q->symbol)) == 0 ){ /* reoccurance */
q->count++;
q->lnum = newnum( q->lnum, n );
}
else
if( cond<0 ) /* lower goes into left tree */
q->lnode = newsym( q->lnode, w, n );
else
q->rnode = newsym( q->rnode, w, n );
return( q );
}
struct numrec *newnum( p, n )
struct numrec *p;
int n;
{
struct numrec *nalloc();
if( p==NULL ){ /* new node at end of tree */
p = nalloc(); /* create new storage element */
p->number = n; /* save number into space */
p->nnext = NULL; }
else{
p->nnext = newnum( p->nnext, n );} /* continue along tree */
return( p );
}
struct symrec *salloc()
{
char *alloc();
return( (struct symrec *) alloc( sizeof(struct symrec) ) );
}
struct numrec *nalloc()
{
char *alloc();
return( (struct numrec *) alloc( sizeof(struct numrec) ) );
}
char *strsave( str )
char *str;
{
char *p, *alloc();
if( (p=alloc(strlen(str)+1)) != NULL ) strcpy( p, str );
if( p==NULL ) printf("ERROR: Out of string space.\n");
return( p );
}
symprint( p ) /* print out symbol table */
struct symrec *p;
{
if( p!=NULL ){
symprint(p->lnode);
fprintf(outfile, "%-15s (%d)\t:\n", p->symbol, p->count );
numprint(p->lnum, 0);
symprint(p->rnode);
}
}
numprint( p, i ) /* print out page number table */
struct numrec *p;
{
if( p!=NULL ){
fprintf( outfile, " %4d", p->number );
if( !((i+1)%10) ) putc( '\n', outfile );
numprint( p->nnext, ++i );
}
else{
putc( '\n', outfile );
}
}
is_lang( str )
char *str;
{
int i, j;
i = 0;
while( Exceptions[i]!=NULL ){
if( ( j=strcmp( upper(str), Exceptions[i++] ) ) == 0 ) return( 1 );
else if( j<0 ) return( 0 );
}
return( 0 );
}
load_tbl( iname )
char *iname;
{
char *cptr;
char fname[65], symbol[40];
FILE *tfile;
int i = 0;
if( NULL == (cptr=strchr( iname, '.' ) )){
printf("ERROR: No extension on input file name: %s\n", iname );
exit();}
sscanf( ++cptr, "%s", fname );
strcat( fname, ".XRF" );
upper( fname );
if( 0 == strncmp( fname, "C.XRF", 5 ) ) is_c = TRUE;
if( NULL==(tfile = fopen(fname,"r")) ){ /* open table file */
printf("ERROR: Cannot open Language Table file: %s\n", fname );
exit();}
while( i < MAX_DEF ){
if( NULL == fscanf( tfile, "%s", symbol ) ){ /* end of file */
fclose( tfile );
return; }
else{ /* insert new symbol */
Exceptions[i++] = strsave( upper(symbol) );
}
}
printf("ERROR: Language Definition File is too long (Max = %d).\n", MAX_DEF );
exit();
}