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