|
|
1.1 root 1: #ifndef lint
2: static char sccsid[] = "@(#)yylex.c 1.3 7/1/83";
3: #endif lint
4:
5: #define isid(a) ((fastab+COFF)[a]&IB)
6: #define IB 1
7: #if '\377' < 0 /* this works on tahoe */
8: #define COFF 128
9: #else
10: #define COFF 0
11: #endif
12:
13: yylex() {
14: static int ifdef=0;
15: static char *op2[]={"||", "&&" , ">>", "<<", ">=", "<=", "!=", "=="};
16: static int val2[]={OROR, ANDAND, RS, LS, GE, LE, NE, EQ};
17: static char *opc="b\bt\tn\nf\fr\r\\\\";
18: extern char fastab[];
19: extern char *outp,*inp,*newp; extern int flslvl;
20: register char savc, *s; char *skipbl(); int val;
21: register char **p2;
22: struct symtab {
23: char *name;
24: char *value;
25: } *sp, *lookup();
26:
27: for (;;) {
28: extern int passcom; /* this crap makes #if's work */
29: int opt_passcom = passcom; /* even with -C option */
30: passcom = 0; /* (else comments make syntax errs) */
31: newp=skipbl(newp);
32: passcom = opt_passcom; /* nb: lint uses -C so its useful! */
33: if (*inp=='\n') return(stop); /* end of #if */
34: savc= *newp; *newp='\0';
35: for (p2=op2+8; --p2>=op2; ) /* check 2-char ops */
36: if (0==strcmp(*p2,inp)) {val=val2[p2-op2]; goto ret;}
37: s="+-*/%<>&^|?:!~(),"; /* check 1-char ops */
38: while (*s) if (*s++== *inp) {val= *--s; goto ret;}
39: if (*inp<='9' && *inp>='0') {/* a number */
40: if (*inp=='0') yylval= (inp[1]=='x' || inp[1]=='X') ?
41: tobinary(inp+2,16) : tobinary(inp+1,8);
42: else yylval=tobinary(inp,10);
43: val=number;
44: } else if (isid(*inp)) {
45: if (0==strcmp(inp,"defined")) {ifdef=1; ++flslvl; val=DEFINED;}
46: else {
47: sp=lookup(inp,-1); if (ifdef!=0) {ifdef=0; --flslvl;}
48: yylval= (sp->value==0) ? 0 : 1;
49: val=number;
50: }
51: } else if (*inp=='\'') {/* character constant */
52: val=number;
53: if (inp[1]=='\\') {/* escaped */
54: char c; if (newp[-1]=='\'') newp[-1]='\0';
55: s=opc;
56: while (*s) if (*s++!=inp[2]) ++s; else {yylval= *s; goto ret;}
57: if (inp[2]<='9' && inp[2]>='0') yylval=c=tobinary(inp+2,8);
58: else yylval=inp[2];
59: } else yylval=inp[1];
60: } else if (0==strcmp("\\\n",inp)) {*newp=savc; continue;}
61: else {
62: *newp=savc; pperror("Illegal character %c in preprocessor if", *inp);
63: continue;
64: }
65: ret:
66: *newp=savc; outp=inp=newp; return(val);
67: }
68: }
69:
70: tobinary(st, b) char *st; {
71: int n, c, t;
72: char *s;
73: n=0;
74: s=st;
75: while (c = *s++) {
76: switch(c) {
77: case '0': case '1': case '2': case '3': case '4':
78: case '5': case '6': case '7': case '8': case '9':
79: t = c-'0'; break;
80: case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
81: t = c-'a'+10; if (b>10) break;
82: case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
83: t = c - 'A'+10; if (b>10) break;
84: default:
85: t = -1;
86: if ( c=='l' || c=='L') if (*s=='\0') break;
87: pperror("Illegal number %s", st);
88: }
89: if (t<0) break;
90: n = n*b+t;
91: }
92: return(n);
93: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.