-
Notifications
You must be signed in to change notification settings - Fork 1
/
c3ms.l
257 lines (209 loc) · 7.97 KB
/
c3ms.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
%{
/*
C3MS: C++ Code Complexity Measurement System
Copyright (C) 2009-2013 Basilio B. Fraguela. Universidade da Coruna
This file is part of C3MS.
C3MS is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2, or (at your option) any later version.
C3MS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
C3MS; see the file COPYING. If not, write to the Free Software Foundation, 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* Portions copied from http://www.lysator.liu.se/c/ANSI-C-grammar-l.html */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>
#include "CodeStatisticsGatherer.h"
#include "c3ms.tab.cpp.h"
extern YYSTYPE yylval;
int ParserLineno;
CodeStatisticsGatherer stat;
void resetLexer() {
YY_FLUSH_BUFFER;
}
void resetFileStatistics() {
ParserLineno = 0;
stat.reset();
}
%}
%option noyywrap
D [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
E [Ee][+-]?{D}+
FS (f|F|l|L)
IS (u|U|l|L)*
%x comment
%s includestate
%%
/***************** C comments ***************/
"/*" { BEGIN(comment); }
<comment>[^*\n]* { /* eat anything that's not a '*' */ }
<comment>"*"+[^*/\n]* { /* eat up '*'s not followed by '/'s */ }
<comment>\n { ParserLineno++; }
<comment>"*"+"/" { BEGIN(INITIAL); }
/********* Preprocessor ********************/
\#[\t ]*define { stat.keyword(yytext); }
\#[\t ]*else { stat.keyword(yytext); /*stat.cond();*/ }
\#[\t ]*elif { stat.keyword(yytext); stat.cond(); }
\#[\t ]*endif { /* nothing*/ }
\#[\t ]*if { stat.keyword(yytext); stat.cond(); }
\#[\t ]*ifdef { stat.keyword(yytext); stat.cond(); }
\#[\t ]*ifndef { stat.keyword(yytext); stat.cond(); }
\#[\t ]*include { stat.keyword(yytext); BEGIN(includestate); }
\#[\t ]*pragma { stat.keyword(yytext); }
\#[\t ]*line.* { }
<includestate>\< { /* do not count as operator */ }
<includestate>\> { /* do not count as operator */ }
<includestate>[\n\r] { ParserLineno++; BEGIN(INITIAL); }
/********* Eat Comments and lines **********/
"//".* { /* eat C++ style comments */}
[\n\r] { ParserLineno++; }
[\t ]+
/***************** Keywords ***************/
int { stat.type(yytext); }
float { stat.type(yytext); }
char { stat.type(yytext); }
double { stat.type(yytext); }
long { stat.type(yytext); }
short { stat.type(yytext); }
signed { stat.type(yytext); }
unsigned { stat.type(yytext); }
void { stat.type(yytext); }
auto { stat.cspec(yytext); }
extern { stat.cspec(yytext); }
register { stat.cspec(yytext); }
static { stat.cspec(yytext); }
typedef { stat.cspec(yytext); }
const { stat.cspec(yytext); }
volatile { stat.cspec(yytext); }
break { stat.keyword(yytext); }
case { stat.keyword(yytext); stat.cond(); }
continue { stat.keyword(yytext); }
default { stat.keyword(yytext); stat.cond(); }
do { stat.keyword(yytext); }
else { stat.keyword(yytext); /*stat.cond();*/ }
enum { stat.keyword(yytext); }
for { stat.keyword(yytext); stat.cond(); stat.decOp(); }
goto { stat.keyword(yytext); }
if { stat.keyword(yytext); stat.cond(); stat.decOp(); }
return { stat.keyword(yytext); }
sizeof { stat.keyword(yytext); }
struct { stat.keyword(yytext); }
switch { stat.keyword(yytext); /*cond counted in cases+default*/ stat.decOp(); }
union { stat.keyword(yytext); }
while { stat.keyword(yytext); stat.cond(); stat.decOp(); }
/***************** C++ - specific ***************/
bool { stat.type(yytext); }
wchar_t { stat.type(yytext); }
false { stat.constant(yytext); }
true { stat.constant(yytext); }
inline { stat.cspec(yytext); }
mutable { stat.cspec(yytext); }
virtual { stat.cspec(yytext); }
catch { stat.keyword(yytext); stat.decOp(); }
class { stat.keyword(yytext); }
const_cast { stat.keyword(yytext); stat.decOp(); }
delete { stat.keyword(yytext); }
dynamic_cast { stat.keyword(yytext); stat.decOp(); }
explicit { stat.keyword(yytext); }
export { stat.keyword(yytext); }
friend { stat.keyword(yytext); }
namespace { stat.keyword(yytext); }
new { stat.keyword(yytext); }
operator { stat.keyword(yytext); }
private { stat.keyword(yytext); }
protected { stat.keyword(yytext); }
public { stat.keyword(yytext); }
reinterpret_cast { stat.keyword(yytext); stat.decOp(); }
static_cast { stat.keyword(yytext); stat.decOp(); }
template { stat.keyword(yytext); stat.decOp(); /* either < or > is not an operator */ }
this { stat.keyword(yytext); }
throw { stat.keyword(yytext); }
try { stat.keyword(yytext); }
typeid { stat.keyword(yytext); }
typename { stat.keyword(yytext); }
using { stat.keyword(yytext); }
asm { stat.keyword(yytext); stat.op(yytext); }
/***************** CUDA specific ***************/
char{D} { stat.type(yytext); }
uchar{D} { stat.type(yytext); }
short{D} { stat.type(yytext); }
ushort{D} { stat.type(yytext); }
int{D} { stat.type(yytext); }
uint{D} { stat.type(yytext); }
long{D} { stat.type(yytext); }
ulong{D} { stat.type(yytext); }
float{D} { stat.type(yytext); }
double{D} { stat.type(yytext); }
__device__ { stat.keyword(yytext); }
__inline__ { stat.keyword(yytext); }
__shared__ { stat.keyword(yytext); }
__global__ { stat.keyword(yytext); }
{L}({L}|{D})* { stat.identifier(yytext); }
/***************** operators ***************/
"..." { stat.op(yytext); }
"::" { stat.op(yytext); }
">>=" { stat.op(yytext); }
"<<=" { stat.op(yytext); }
"+=" { stat.op(yytext); }
"-=" { stat.op(yytext); }
"*=" { stat.op(yytext); }
"/=" { stat.op(yytext); }
"%=" { stat.op(yytext); }
"&=" { stat.op(yytext); }
"^=" { stat.op(yytext); }
"|=" { stat.op(yytext); }
">>" { stat.op(yytext); }
"<<" { stat.op(yytext); }
"++" { stat.op(yytext); }
"--" { stat.op(yytext); }
"->" { stat.op(yytext); }
"&&" { stat.op(yytext); }
"||" { stat.op(yytext); }
"<=" { stat.op(yytext); }
">=" { stat.op(yytext); }
"==" { stat.op(yytext); }
"!=" { stat.op(yytext); }
";" { stat.op(yytext); }
("{"|"<%") { stat.openBracket(); }
("}"|"%>") { stat.closeBracket(); }
"," { stat.op(yytext); }
":" { }
"=" { stat.op(yytext); }
"(" { stat.op(yytext); }
")" { }
("["|"<:") { stat.op(yytext); }
("]"|":>") { }
"." { stat.op(yytext); }
"&" { stat.op(yytext); }
"!" { stat.op(yytext); }
"~" { stat.op(yytext); }
"-" { stat.op(yytext); }
"+" { stat.op(yytext); }
"*" { stat.op(yytext); }
"/" { stat.op(yytext); }
"%" { stat.op(yytext); }
"<" { stat.op(yytext); }
">" { stat.op(yytext); }
"^" { stat.op(yytext); }
"|" { stat.op(yytext); }
"?" { stat.op(yytext); stat.cond(); }
\\ { /* macro continuation */ }
\#\# { /* macro concatenation */ stat.op(yytext); }
/***************** constants ***************/
0[xX]{H}+{IS}? { stat.constant(yytext); }
0{D}+{IS}? { stat.constant(yytext); }
{D}+{IS}? { stat.constant(yytext); }
L?'(\\.|[^\\'])+' { stat.constant(yytext); }
{D}+{E}{FS}? { stat.constant(yytext); }
{D}*"."{D}+({E})?{FS}? { stat.constant(yytext); }
{D}+"."{D}*({E})?{FS}? { stat.constant(yytext); }
L?\"(\\.|[^\\"])*\" { stat.constant(yytext); /* STRING_LITERAL*/ }
. { printf("bad character: %c\n", *yytext); }
%%