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