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