-
Notifications
You must be signed in to change notification settings - Fork 1
/
clex.mll
329 lines (319 loc) · 13.9 KB
/
clex.mll
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
{
(*
* Copyright (c) 2005 by Laboratoire Spécification et Vérification (LSV),
* UMR 8643 CNRS & ENS Cachan.
* Written by Jean Goubault-Larrecq. Derived from the csur project.
* Adapted by Neven Villani
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to redistribute it freely,
* subject to the following restrictions:
*
* 1. Neither the author nor its employer is responsible for the consequences of use of
* this software, no matter how awful, even if they arise
* from defects in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
* NOTE: This is an altered version
*
* 4. This software is restricted to non-commercial use only. Commercial
* use is subject to a specific license, obtainable from LSV.
*
*)
(* Analyse lexicale d'un sous-ensemble (tres) reduit de C.
*)
open Cparse
open Error
open CAST
let string_buf = Buffer.create 256
let count yytext =
oldcline := !cline; oldccol := !ccol;
String.iter (fun c -> match c with
| '\n' -> (cline := !cline+1; ccol := 0)
(* | '\t' -> (ccol := !ccol + 8 - (!ccol mod 8)) *)
| _ -> ccol := !ccol+1
) yytext
let parse_hex yytext tend =
let n = ref 0 in
let len = String.length yytext-tend in
for i = 2 to len-1 do
let c = yytext.[i] in
match c with
| '0'..'9' -> n := 16 * !n + (int_of_char c - int_of_char '0')
| 'a'..'f' -> n := 16 * !n + (int_of_char c + 10 - int_of_char 'a')
| 'A'..'F' -> n := 16 * !n + (int_of_char c + 10 - int_of_char 'A')
| _ -> fatal
(Some (!cfile, !cline, !ccol-len, !cline, !ccol))
("invalid hexadecimal number " ^ yytext)
done;
!n
let parse_oct yytext start tend =
let n = ref 0 in
let len = String.length yytext-tend in
for i = start to len-1 do
let c = yytext.[i] in
match c with
| '0'..'7' -> n := 8 * !n + (int_of_char c - int_of_char '0')
| _ -> fatal
(Some (!cfile, !cline, !ccol-len, !cline, !ccol))
("invalid octal number " ^ yytext)
done;
!n
let parse_dec yytext tend =
let n = ref 0 in
let len = String.length yytext-tend in
for i = 0 to len-1 do
let c = yytext.[i] in
match c with
| '0'..'9' -> n := 10 * !n + (int_of_char c - int_of_char '0')
| _ -> fatal
(Some (!cfile, !cline, !ccol-len, !cline, !ccol))
("invalid number " ^ yytext)
done;
!n
}
let digit = ['0'-'9']
let letter = ['a'-'z' 'A'-'Z' '_']
let hex = ['a'-'f' 'A'-'F' '0'-'9']
let expo = ['E' 'e'] ['+' '-']? digit+
let fs = ['f' 'F' 'l' 'L']
let is = ['u' 'U' 'l' 'L']*
rule ctoken = parse
| "/*" { count (Lexing.lexeme lexbuf); comment lexbuf; ctoken lexbuf }
| "//" { count (Lexing.lexeme lexbuf); inlcomment lexbuf; ctoken lexbuf }
| "auto" { count (Lexing.lexeme lexbuf); AUTO }
| "break" { count (Lexing.lexeme lexbuf); BREAK }
| "case" { count (Lexing.lexeme lexbuf); CASE }
| "catch" { count (Lexing.lexeme lexbuf); CATCH }
| "char" { count (Lexing.lexeme lexbuf); CHAR }
| "const" { count (Lexing.lexeme lexbuf); CONST }
| "continue" { count (Lexing.lexeme lexbuf); CONTINUE }
| "default" { count (Lexing.lexeme lexbuf); DEFAULT }
| "do" { count (Lexing.lexeme lexbuf); DO }
| "double" { count (Lexing.lexeme lexbuf); DOUBLE }
| "else" { count (Lexing.lexeme lexbuf); ELSE }
| "enum" { count (Lexing.lexeme lexbuf); ENUM }
| "extern" { count (Lexing.lexeme lexbuf); EXTERN }
| "finally" { count (Lexing.lexeme lexbuf); FINALLY }
| "float" { count (Lexing.lexeme lexbuf); FLOATING }
| "for" { count (Lexing.lexeme lexbuf); FOR }
| "goto" { count (Lexing.lexeme lexbuf); GOTO }
| "if" { count (Lexing.lexeme lexbuf); IF }
| "int" { count (Lexing.lexeme lexbuf); INTEGER }
| "long" { count (Lexing.lexeme lexbuf); LONG }
| "register" { count (Lexing.lexeme lexbuf); REGISTER }
| "return" { count (Lexing.lexeme lexbuf); RETURN }
| "short" { count (Lexing.lexeme lexbuf); SHORT }
| "signed" { count (Lexing.lexeme lexbuf); SIGNED }
| "sizeof" { count (Lexing.lexeme lexbuf); SIZEOF }
| "static" { count (Lexing.lexeme lexbuf); STATIC }
| "struct" { count (Lexing.lexeme lexbuf); STRUCT }
| "switch" { count (Lexing.lexeme lexbuf); SWITCH }
| "throw" { count (Lexing.lexeme lexbuf); THROW }
| "try" { count (Lexing.lexeme lexbuf); TRY }
| "typedef" { count (Lexing.lexeme lexbuf); TYPEDEF }
| "union" { count (Lexing.lexeme lexbuf); UNION }
| "unsigned" { count (Lexing.lexeme lexbuf); UNSIGNED }
| "void" { count (Lexing.lexeme lexbuf); VOID }
| "volatile" { count (Lexing.lexeme lexbuf); VOLATILE }
| "while" { count (Lexing.lexeme lexbuf); WHILE }
| letter (letter | digit)* {
count (Lexing.lexeme lexbuf);
let yytext = Lexing.lexeme lexbuf in
IDENTIFIER yytext }
| '0' ['x' 'X'] hex+ {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_hex (Lexing.lexeme lexbuf) 0) }
| '0' ['x' 'X'] hex+ ['u' 'U'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_hex (Lexing.lexeme lexbuf) 1) }
| '0' ['x' 'X'] hex+ ['l' 'L'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_hex (Lexing.lexeme lexbuf) 1) }
| '0' ['x' 'X'] hex+ ['u' 'U'] ['l' 'L'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_hex (Lexing.lexeme lexbuf) 2) }
| '0' ['x' 'X'] hex+ ['u' 'U'] ['l' 'L'] ['l' 'L'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_hex (Lexing.lexeme lexbuf) 3) }
| '0' ['0'-'7']+ {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_oct (Lexing.lexeme lexbuf) 1 0) }
| '0' ['0'-'7']+ ['u' 'U'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_oct (Lexing.lexeme lexbuf) 1 1) }
| '0' ['0'-'7']+ ['l' 'L'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_oct (Lexing.lexeme lexbuf) 1 1) }
| '0' ['0'-'7']+ ['u' 'U'] ['l' 'L'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_oct (Lexing.lexeme lexbuf) 1 2) }
| '0' ['0'-'7']+ ['u' 'U'] ['l' 'L'] ['l' 'L'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_oct (Lexing.lexeme lexbuf) 1 3) }
| digit+ {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_dec (Lexing.lexeme lexbuf) 0) }
| digit+ ['u' 'U'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_dec (Lexing.lexeme lexbuf) 1) }
| digit+ ['l' 'L'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_dec (Lexing.lexeme lexbuf) 1) }
| digit+ ['l' 'L' ] ['l' 'L' ] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_dec (Lexing.lexeme lexbuf) 1) }
| digit+ ['u' 'U'] ['l' 'L'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_dec (Lexing.lexeme lexbuf) 2) }
| digit+ ['u' 'U'] ['l' 'L'] ['l' 'L'] {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_dec (Lexing.lexeme lexbuf) 3) }
| '\'' [^ '\'' '\\'] '\'' {
count (Lexing.lexeme lexbuf);
CONSTANT (int_of_char (Lexing.lexeme_char lexbuf 1)) }
| '\'' '\\' ['0'-'7'] ['0'-'7']? ['0'-'7']? '\'' {
count (Lexing.lexeme lexbuf);
CONSTANT (parse_oct (Lexing.lexeme lexbuf) 2 1) }
| '\'' '\\' 'a' '\'' {
count (Lexing.lexeme lexbuf);
CONSTANT 7 (* bell, ^G *) }
| '\'' '\\' 'b' '\'' {
count (Lexing.lexeme lexbuf);
CONSTANT (int_of_char '\b') }
| '\'' '\\' 'f' '\'' {
count (Lexing.lexeme lexbuf);
CONSTANT 12 (* form feed, ^L *) }
| '\'' '\\' 'n' '\'' {
count (Lexing.lexeme lexbuf);
CONSTANT (int_of_char '\n') }
| '\'' '\\' 'r' '\'' {
count (Lexing.lexeme lexbuf);
CONSTANT (int_of_char '\r') }
| '\'' '\\' 't' '\'' {
count (Lexing.lexeme lexbuf);
CONSTANT (int_of_char '\t')
(* bell, ^G *) }
| '\'' '\\' 'v' '\'' {
count (Lexing.lexeme lexbuf);
CONSTANT 11 (* vertical tab, ^K *) }
| '\'' '\\' _ '\'' { count (Lexing.lexeme lexbuf);
CONSTANT (int_of_char (Lexing.lexeme_char lexbuf 2)) }
| "\"" {
count (Lexing.lexeme lexbuf); Buffer.reset string_buf;
string lexbuf;
STRING_LITERAL (Buffer.contents string_buf) }
| "..." { count (Lexing.lexeme lexbuf); ELLIPSIS }
| ">>=" { count (Lexing.lexeme lexbuf); RIGHT_ASSIGN }
| "<<=" { count (Lexing.lexeme lexbuf); LEFT_ASSIGN }
| "+=" { count (Lexing.lexeme lexbuf); ADD_ASSIGN }
| "-=" { count (Lexing.lexeme lexbuf); SUB_ASSIGN }
| "*=" { count (Lexing.lexeme lexbuf); MUL_ASSIGN }
| "/=" { count (Lexing.lexeme lexbuf); DIV_ASSIGN }
| "%=" { count (Lexing.lexeme lexbuf); MOD_ASSIGN }
| "&=" { count (Lexing.lexeme lexbuf); AND_ASSIGN }
| "^=" { count (Lexing.lexeme lexbuf); XOR_ASSIGN }
| "|=" { count (Lexing.lexeme lexbuf); OR_ASSIGN }
| ">>" { count (Lexing.lexeme lexbuf); RIGHT_OP }
| "<<" { count (Lexing.lexeme lexbuf); LEFT_OP }
| "++" { count (Lexing.lexeme lexbuf); INC_OP }
| "--" { count (Lexing.lexeme lexbuf); DEC_OP }
| "->" { count (Lexing.lexeme lexbuf); PTR_OP }
| "&&" { count (Lexing.lexeme lexbuf); AND_OP }
| "||" { count (Lexing.lexeme lexbuf); OR_OP }
| "<=" { count (Lexing.lexeme lexbuf); LE_OP }
| ">=" { count (Lexing.lexeme lexbuf); GE_OP }
| "==" { count (Lexing.lexeme lexbuf); EQ_OP }
| "!=" { count (Lexing.lexeme lexbuf); NE_OP }
| ";" { count (Lexing.lexeme lexbuf); SEMI_CHR }
| ("{" | "<%") { count (Lexing.lexeme lexbuf); OPEN_BRACE_CHR }
| ("}" | "%>") { count (Lexing.lexeme lexbuf); CLOSE_BRACE_CHR }
| "," { count (Lexing.lexeme lexbuf); COMMA_CHR }
| ":" { count (Lexing.lexeme lexbuf); COLON_CHR }
| "=" { count (Lexing.lexeme lexbuf); EQ_CHR }
| "(" { count (Lexing.lexeme lexbuf); OPEN_PAREN_CHR }
| ")" { count (Lexing.lexeme lexbuf); CLOSE_PAREN_CHR }
| ("[" | "<:") { count (Lexing.lexeme lexbuf); OPEN_BRACKET_CHR }
| ("]" | ":>") { count (Lexing.lexeme lexbuf); CLOSE_BRACKET_CHR }
| "." { count (Lexing.lexeme lexbuf); DOT_CHR }
| "&" { count (Lexing.lexeme lexbuf); AND_CHR }
| "|" { count (Lexing.lexeme lexbuf); OR_CHR }
| "^" { count (Lexing.lexeme lexbuf); XOR_CHR }
| "!" { count (Lexing.lexeme lexbuf); BANG_CHR }
| "~" { count (Lexing.lexeme lexbuf); TILDE_CHR }
| "+" { count (Lexing.lexeme lexbuf); ADD_CHR }
| "-" { count (Lexing.lexeme lexbuf); SUB_CHR }
| "*" { count (Lexing.lexeme lexbuf); STAR_CHR }
| "/" { count (Lexing.lexeme lexbuf); DIV_CHR }
| "%" { count (Lexing.lexeme lexbuf); MOD_CHR }
| "<" { count (Lexing.lexeme lexbuf); OPEN_ANGLE_CHR }
| ">" { count (Lexing.lexeme lexbuf); CLOSE_ANGLE_CHR }
| "?" { count (Lexing.lexeme lexbuf); QUES_CHR }
| '#' { count (Lexing.lexeme lexbuf); line lexbuf }
| [' ' '\t' '\012' '\013' '\n' '\014']+ { count (Lexing.lexeme lexbuf); ctoken lexbuf }
| _ { fatal
(Some (!cfile, !cline, !ccol, !cline, !ccol+1))
("bad character '" ^ (Lexing.lexeme lexbuf) ^ "'") }
| eof { EOF }
and comment = parse
| "*/" { count (Lexing.lexeme lexbuf) }
| [^ '*']* { count (Lexing.lexeme lexbuf); comment lexbuf }
| eof { fatal
(Some (!cfile, !cline, !ccol, !cline, !ccol))
"end of file reached inside comment" }
and inlcomment = parse
| "\n" { count (Lexing.lexeme lexbuf) }
| [^ '\n']* { count (Lexing.lexeme lexbuf); inlcomment lexbuf }
and string = parse
| '"' { () }
| '\n'+ { string lexbuf }
| '\\' ['0'-'7'] ['0'-'7']? ['0'-'7']? {
Buffer.add_char string_buf (Char.chr (parse_oct (Lexing.lexeme lexbuf) 1 0));
string lexbuf }
| '\\' 'a' { Buffer.add_char string_buf '\007'; string lexbuf }
| '\\' 'b' { Buffer.add_char string_buf '\b'; string lexbuf }
| '\\' 'f' { Buffer.add_char string_buf '\014'; string lexbuf }
| '\\' 'n' { Buffer.add_char string_buf '\n'; string lexbuf }
| '\\' 'r' { Buffer.add_char string_buf '\r'; string lexbuf }
| '\\' 't' { Buffer.add_char string_buf '\t'; string lexbuf }
| '\\' 'v' { Buffer.add_char string_buf '\013'; string lexbuf }
| '\\' _ { Buffer.add_char string_buf (Lexing.lexeme_char lexbuf 1); string lexbuf }
| [^ '\\' '\n' '"']+ { Buffer.add_string string_buf (Lexing.lexeme lexbuf); string lexbuf }
| _ { Buffer.add_char string_buf (Lexing.lexeme_char lexbuf 0); string lexbuf }
| eof { fatal
(Some (!cfile, !cline, !ccol, !cline, !ccol))
"end of file reached inside string literal" }
and line = parse
| ['0'-'9']+ { cline := parse_dec (Lexing.lexeme lexbuf) 0 - 1; line2 lexbuf }
| [' ' '\t']+ { count (Lexing.lexeme lexbuf); line lexbuf }
| '\n' { count (Lexing.lexeme lexbuf); ctoken lexbuf }
| "\"" {
count (Lexing.lexeme lexbuf); Buffer.reset string_buf;
string lexbuf;
cfile := Buffer.contents string_buf;
ctoken lexbuf }
| eof { fatal
(Some (!cfile, !cline, !ccol, !cline, !ccol))
"end of file reached inside # directive" }
and line2 = parse
| [' ' '\t']+ { count (Lexing.lexeme lexbuf); line2 lexbuf }
| '\n' { count (Lexing.lexeme lexbuf); ctoken lexbuf }
| "\"" {
count (Lexing.lexeme lexbuf);
Buffer.reset string_buf;
string lexbuf;
cfile := Buffer.contents string_buf;
line3 lexbuf }
| eof { fatal
(Some (!cfile, !cline, !ccol, !cline, !ccol))
"end of file reached inside # directive" }
and line3 = parse
| '\n' { count (Lexing.lexeme lexbuf); ctoken lexbuf }
| _ { count (Lexing.lexeme lexbuf); line3 lexbuf }
| eof { fatal
(Some (!cfile, !cline, !ccol, !cline, !ccol))
"end of file reached inside # directive" }