-
Notifications
You must be signed in to change notification settings - Fork 0
/
cscan.l
186 lines (154 loc) · 2.83 KB
/
cscan.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
** cscan.l
**
** Does the major work of removing identifiers and constants
** from the input stream, for Cxref. Its output is then extensively
** postprocessed.
**
** Arnold Robbins, Information and Computer Science, Georgia Tech
** gatech!arnold
** Copyright (c) 1984 by Arnold Robbins.
** All rights reserved.
** This program may not be sold, but may be distributed
** provided this header is included.
*/
%{
extern int line_no;
extern char *fname, *basename();
%}
letter [A-Za-z_]
digit [0-9]
%%
int |
char |
float |
double |
struct |
union |
long |
short |
unsigned |
auto |
extern |
register |
typedef |
static |
goto |
return |
sizeof |
break |
continue |
if |
else |
for |
do |
while |
switch |
case |
default |
entry |
enum |
void |
define |
undef |
include |
ifdef |
ifndef |
defined |
endif ; /* ignore C and cpp keywords */
"<".+">" ; /* forget about include-file names */
"\n" line_no++;
"/*" { /* get rid of comments */
register char c, c1;
loop: while((c = input()) != '*' && c != 0)
if(c == '\n')
line_no++;
if(c == 0)
{
fprintf(stderr,
"unexpected EOF in comment at line %d, file %s\n",
line_no, basename(fname));
exit(1);
}
if((c1 = input()) != '/')
{
unput(c1); /* could be '*' before '/' */
goto loop;
}
}
{letter}({letter}|{digit})* outid(); /* what we actually want */
'[^\\']' |
'\\{digit}{1,3}' |
'\\[\\bfrnlt']' outchar();
\" { /* collect quoted strings */
register char c;
register int i;
for(i = 1, c = input(); ; i++, c = input())
switch (c) {
case '"':
yytext[i] = c;
yytext[++i] = '\0';
yyleng = i - 1;
goto fini;
case '\\':
yytext[i] = '\\';
yytext[++i] = input();
if (yytext[i] == '\n')
{
line_no++;
yytext[i] = 'N';
/* make visible */
}
break;
case 0:
fprintf(stderr,
"unexpected EOF inside string at line %d, file %s\n",
line_no, basename(fname));
exit(1);
break;
default:
yytext[i] = c;
break;
}
fini:
outstring();
}
[+-]?{digit}+[lL]? |
[+-]?0[Xx]({digit}|[a-fA-F])+[lL]? outint();
[+-]?{digit}*"."{digit}+([Ee][+-]?{digit}+)? |
[+-]?{digit}+"."{digit}*([Ee][+-]?{digit}+)? |
[+-]?{digit}+[Ee][+-]?{digit}+ outfloat();
. ; /* delete everything else */
%%
yywrap() /* wrap up for lex, return 1 */
{
return(1);
}
#include "constdefs.h"
extern char *fname;
extern char *basename();
outchar()
{
outtext(CHAR);
}
outstring()
{
outtext(STRING);
}
outint()
{
int i = strlen(yytext);
/* handle long integer constants */
if (yytext[i-1] == 'l' || yytext[i-1] == 'L')
yytext[i-1] = '\0';
outtext(INT);
}
outfloat()
{
outtext(FLOAT);
}
outtext(type)
char type;
{
printf("~%c%s\t%s\t%d\n", type, yytext, basename(fname), line_no);
}