Cross Reference of: XRF1.C;4 Fri May 16 14:11:16 1986 Page 1 1: /* 2: * *************** 3: * * X R F 1 . C * 4: * *************** 5: * 6: * Lexical processing for C xref'er. Sifts through C source code 7: * and pulls out the identifiers. Recognizes reserved words and 8: * disregards them. Returns non-zero integer (true) if an id was 9: * successfully moved into 'idbuf', zero (false) if end-of-line 10: * was reached. I took care to test the C compiler's reaction to 11: * Formfeeds with respect to line numbers, and made sure the line 12: * numbers assigned by xrf act the same. 13: * 14: * Version V1.3 9-May-80 15: * Version V1.4 10-Jul-80 MM Allow $ in identifiers, bummed code 16: * Version V1.5 21-Jul-80 MM Dropped newline from ctime() 17: * Version V1.6 22-Jul-80 MM '.' is not an alpha char. 18: */ 19: 20: #include 21: #include "xrf.h" 22: 23: #define A 1 /* Alpha character */ 24: #define C 2 /* Start of comment possibly */ 25: #define L 3 /* Literal delimiter */ 26: #define N 4 /* Numeric character */ 27: #define Z 5 /* End of string */ 28: 29: #define NRW 28 /* # reserved words */ 30: 31: static int cmtflg = 0; /* Comment flag */ 32: 33: static char ctype[] = { /* Character action lookup */ 34: Z,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 35: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 36: 0,0,L,0,A,0,0,L,0,0,0,0,0,0,0,C, 37: N,N,N,N,N,N,N,N,N,N,0,0,0,0,0,0, 38: 0,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A, 39: A,A,A,A,A,A,A,A,A,A,A,0,0,0,0,A, 40: 0,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A, 41: A,A,A,A,A,A,A,A,A,A,A,0,0,0,0,0 42: }; 43: 44: static char *reswrd[NRW] = { 45: "auto", "break", "case", "char", 46: "continue", "default", "do", "double", 47: "else", "entry", "extern", "float", 48: "for", "goto", "if", "int", 49: "long", "register", "return", "short", 50: "sizeof", "static", "struct", "switch", 51: "typedef", "union", "unsigned", "while" 52: }; Cross Reference of: XRF1.C;4 Fri May 16 14:11:16 1986 Page 2 53: 54: 55: /* 56: * Scan off a non reserved identifier. Put it into 'idbuf'. 57: * Returns +1 if successful, 0 if end-of-line was reached. 58: * 59: */ 60: 61: getid() /* Get an identifier into idbuf */ 62: 63: { 64: register char *p ; /* Fast scan pointer */ 65: register char *i ; /* Fast id buf pointer */ 66: register int c; /* Dispatch code */ 67: 68: p = scanp; /* Init fast pointers */ 69: i = idbuf; 70: 71: while ((c = *p) != 0) /* Scan till end of string */ 72: { 73: 74: if(c == '\014') /* If formfeed, */ 75: { 76: c = *p = ' '; /* Convert to harmless blank */ 77: newpage(); /* Force new page */ 78: } 79: 80: if (cmtflg) /* If comment flag is on */ 81: { 82: while(*p && *p++ != '*'); /* Scan to '*' or end of string */ 83: if(*p == '/') /* If we found end of comment */ 84: cmtflg = 0; /* Turn off comment flag */ 85: continue; /* and recycle */ 86: } 87: 88: switch(ctype[c]) /* Dispatch on the type */ 89: { 90: case A: /* Start of an identifier */ 91: i = idbuf; /* Reset the id buffer pointer */ 92: do 93: if( i < &idbuf[NCPS] ) /* Copy into idbuf */ 94: *i++ = *p++; 95: else /* Unless it gets full */ 96: p++; 97: while( (c=ctype[*p]) == A || c == N ); /* While alphanumeric */ 98: 99: fprintf(stderr,"Character after identifier %s was %c\n",idbuf, *p); 100: 101: if (*p == '(') { 102: while( i < &idbuf[NCPS] ) /* Pad idbuf with nulls */ 103: *i++ = '\0'; 104: if(nonres(idbuf)) /* If it's not a reserved word */ 105: { 106: scanp = p; /* Update the scan pointer */ 107: return (1); /* Return with success */ 108: } 109: } Cross Reference of: XRF1.C;4 Fri May 16 14:11:16 1986 Page 3 110: break; /* End of identifier processing */ 111: 112: case N: /* Scan by a number string */ 113: do p++; while( (c=ctype[*p]) == N || c == A ); 114: break; 115: 116: case L: /* Scan by a literal */ 117: while (*++p != c && *p) { /* Scan to the matching trailing */ 118: if (*p == '\\') p++; /* Quote, ignoring backslash quoted */ 119: } /* Characters. If not at the end */ 120: if (*p) p++; /* Of the line, skip to the char. */ 121: break; /* Following the trailing quote */ 122: 123: case C: 124: if(*++p == '*') /* Start a comment */ 125: cmtflg = 1; /* by setting comment flag */ 126: break; 127: 128: default: /* Otherwise just scan it off */ 129: p++; 130: break; 131: } /* End of switch statement */ 132: 133: } /* If we exit here, end-of line. */ 134: return(0); /* Return with failure indication */ 135: } 136: Cross Reference of: XRF1.C;4 Fri May 16 14:11:16 1986 Page 4 137: 138: 139: /* 140: * Search for reserved word. Return true (1) if NOT reserved word. 141: * Uses binary search. 142: */ 143: 144: nonres( bufp ) /* Test if not reserved word */ 145: char *bufp; 146: 147: { 148: register int low ; /* Low pointer index */ 149: register int mid ; /* Mid ... */ 150: register int hi ; /* hi ... */ 151: 152: int cond; /* Condition from strcmp */ 153: 154: low = 0; 155: hi = NRW-1; 156: 157: while (low <= hi) 158: { 159: mid = (low + hi) / 2; 160: if((cond = strcmp(bufp, reswrd[mid])) < 0) 161: hi = mid - 1; 162: else if (cond > 0) 163: low = mid + 1; 164: else 165: return(0); /* False, it IS reserved */ 166: } 167: return(1); /* True, it's NOT reserved */ 168: } 169: Cross Reference of: XRF1.C;4 Fri May 16 14:11:16 1986 Page 5 fprintf 99 getid 61 newpage 77 nonres 104 144 strcmp 160