|
|
1.1 root 1: /*#ident "@(#)cpp:common/yylex.c 1.11"*/
2:
3: #include "y.tab.h"
4: #ifdef FLEXNAMES
5: # define NCPS 128
6: #else
7: # define NCPS 8
8: #endif
9: extern int ncps; /* actual number of chars. */
10: #ifdef CXREF
11: extern int xline;
12: #endif
13: extern int yylval;
14:
15: #define isid(a) ((fastab+COFF)[a]&IB)
16: #define IB 1
17: /* #if '\377' < 0 it would be nice if this worked properly!!!!! */
18: #if pdp11 | vax
19: #define COFF 128
20: #else
21: #define COFF 0
22: #endif
23:
24: yylex()
25: {
26: static int ifdef = 0;
27: static char *op2[] = {"||", "&&" , ">>", "<<", ">=", "<=", "!=", "=="};
28: static int val2[] = {OROR, ANDAND, RS, LS, GE, LE, NE, EQ};
29: static char *opc = "b\bt\tn\nf\fr\r\\\\";
30: extern char fastab[];
31: extern char *outp, *inp, *newp;
32: extern int flslvl;
33: register char savc, *s;
34: char *skipbl();
35: int val;
36: register char **p2;
37: struct symtab
38: {
39: char *name;
40: char *value;
41: } *sp, *lookup();
42:
43: for ( ;; )
44: {
45: newp = skipbl( newp );
46: if ( *inp == '\n' ) /* end of #if */
47: return( stop );
48: savc = *newp;
49: *newp = '\0';
50: if ( *inp == '/' && inp[1] == '*' )
51: {
52: /* found a comment with -C option, still toss here */
53: *newp = savc;
54: outp = inp = newp;
55: continue;
56: }
57: for ( p2 = op2 + 8; --p2 >= op2; ) /* check 2-char ops */
58: if ( strcmp( *p2, inp ) == 0 )
59: {
60: val = val2[ p2 - op2 ];
61: goto ret;
62: }
63: s = "+-*/%<>&^|?:!~(),"; /* check 1-char ops */
64: while ( *s )
65: if ( *s++ == *inp )
66: {
67: val= *--s;
68: goto ret;
69: }
70: if ( *inp<='9' && *inp>='0' ) /* a number */
71: {
72: if ( *inp == '0' )
73: yylval= ( inp[1] == 'x' || inp[1] == 'X' ) ?
74: tobinary( inp + 2, 16 ) :
75: tobinary( inp + 1, 8 );
76: else
77: yylval = tobinary( inp, 10 );
78: val = number;
79: }
80: else if ( isid( *inp ) )
81: {
82: if ( strcmp( inp, "defined" ) == 0 )
83: {
84: ifdef = 1;
85: ++flslvl;
86: val = DEFINED;
87: }
88: else
89: {
90: if ( ifdef != 0 )
91: {
92: register char *p;
93: register int savech;
94:
95: /* make sure names <= ncps chars */
96: if ( ( newp - inp ) > ncps )
97: p = inp + ncps;
98: else
99: p = newp;
100: savech = *p;
101: *p = '\0';
102: sp = lookup( inp, -1 );
103: *p = savech;
104: ifdef = 0;
105: --flslvl;
106: }
107: else
108: sp = lookup( inp, -1 );
109: #ifdef CXREF
110: ref(inp, xline);
111: #endif
112: yylval = ( sp->value == 0 ) ? 0 : 1;
113: val = number;
114: }
115: }
116: else if ( *inp == '\'' ) /* character constant */
117: {
118: val = number;
119: if ( inp[1] == '\\' ) /* escaped */
120: {
121: char c;
122:
123: if ( newp[-1] == '\'' )
124: newp[-1] = '\0';
125: s = opc;
126: while ( *s )
127: if ( *s++ != inp[2] )
128: ++s;
129: else
130: {
131: yylval = *s;
132: goto ret;
133: }
134: if ( inp[2] <= '9' && inp[2] >= '0' )
135: yylval = c = tobinary( inp + 2, 8 );
136: else
137: yylval = inp[2];
138: }
139: else
140: yylval = inp[1];
141: }
142: else if ( strcmp( "\\\n", inp ) == 0 )
143: {
144: *newp = savc;
145: continue;
146: }
147: else
148: {
149: *newp = savc;
150: pperror( "Illegal character %c in preprocessor if",
151: *inp );
152: continue;
153: }
154: ret:
155: /* check for non-ident after defined (note need the paren!) */
156: if ( ifdef && val != '(' && val != DEFINED )
157: {
158: pperror( "\"defined\" modifying non-identifier \"%s\" in preprocessor if", inp );
159: ifdef = 0;
160: flslvl--;
161: }
162: *newp = savc;
163: outp = inp = newp;
164: return( val );
165: }
166: }
167:
168: tobinary( st, b )
169: char *st;
170: {
171: int n, c, t;
172: char *s;
173: int warned = 0;
174:
175: n = 0;
176: s = st;
177: while ( c = *s++ )
178: {
179: switch( c )
180: {
181: case '8': case '9':
182: if (b <= 8 && !warned) {
183: ppwarn("Illegal octal number %s", st);
184: warned = 1;
185: }
186: case '0': case '1': case '2': case '3': case '4':
187: case '5': case '6': case '7':
188: t = c - '0';
189: break;
190: case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
191: t = (c - 'a') + 10;
192: if ( b > 10 )
193: break;
194: case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
195: t = (c - 'A') + 10;
196: if ( b > 10 )
197: break;
198: default:
199: t = -1;
200: if ( c == 'l' || c == 'L' )
201: if ( *s == '\0' )
202: break;
203: pperror( "Illegal number %s", st );
204: }
205: if ( t < 0 )
206: break;
207: n = n * b + t;
208: }
209: return( n );
210: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.