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