forked from matsud224/c2ws
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmylexer.mll
182 lines (177 loc) · 2.9 KB
/
mylexer.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
{
open Myparser
open String
}
let space = [' ' '\t' '\n' '\r']
let digit = ['0'-'9']
let alpha = ['A'-'Z' 'a'-'z' '_']
let escape = '\\' ['a' 'b' 'f' 'n' 'r' 't' 'v' '\\' '?' '\'' '\"' '0']
rule token = parse
| "//"
{ linecomment lexbuf }
| "/*"
{ blockcomment 1 lexbuf }
| "sizeof"
{ SIZEOF }
| "static"
{ STATIC }
| "typedef"
{ TYPEDEF }
| "const"
{ CONST }
| "void"
{ VOID }
| "char"
{ CHAR }
| "short"
{ SHORT }
| "int"
{ INT }
| "struct"
{ STRUCT }
| "union"
{ UNION }
| "enum"
{ ENUM }
| "case"
{ CASE }
| "default"
{ DEFAULT }
| "if"
{ IF }
| "else"
{ ELSE }
| "switch"
{ SWITCH }
| "while"
{ WHILE }
| "do"
{ DO }
| "for"
{ FOR }
| "goto"
{ GOTO }
| "continue"
{ CONTINUE }
| "break"
{ BREAK }
| "return"
{ RETURN }
| alpha (digit|alpha)*
(* 以上の予約語にマッチしなければ変数名として処理 *)
{ Id(Lexing.lexeme lexbuf) }
| digit+
{ IntConst(int_of_string (Lexing.lexeme lexbuf)) }
| '\'' ([' ' - '~'] | escape) '\''
{
let len=(length (Lexing.lexeme lexbuf)) - 2 in
let cut=sub (Lexing.lexeme lexbuf) 1 len in
let escapedstr=Scanf.unescaped cut in
IntConst(int_of_char (get escapedstr 0))
}
| '\"' ([' ' - '~'])* '\"'
{
let len = (length (Lexing.lexeme lexbuf))-2 in
StringConst(Scanf.unescaped (sub (Lexing.lexeme lexbuf) 1 len))
}
| space+
(* 空白をスキップして字句解析を続行 *)
{ token lexbuf }
| '='
{ ASSIGNEQ }
| "=="
{ EQUAL }
| "!="
{ NOTEQUAL }
| '+'
{ PLUS }
| '-'
{ MINUS }
| '*'
{ ASTERISK }
| '/'
{ SLASH }
| '%'
{ PERCENT }
| "<<"
{ LSHIFT }
| ">>"
{ RSHIFT }
| "+="
{ ASSIGNPLUS }
| "-="
{ ASSIGNMINUS }
| "*="
{ ASSIGNASTERISK }
| "/="
{ ASSIGNSLASH }
| "%="
{ ASSIGNPERCENT }
| '['
{ LBRACKET }
| ']'
{ RBRACKET }
| '('
{ LPAREN }
| ')'
{ RPAREN }
| '{'
{ LBRACE }
| '}'
{ RBRACE }
| '>'
{ GREATER }
| '<'
{ LESSER }
| ">="
{ GREATEREQ }
| "<="
{ LESSEREQ }
| ';'
{ SEMICOLON }
| ','
{ COMMA }
| '.'
{ DOT }
| "..."
{ THREEDOT }
| ':'
{ COLON }
| '?'
{ QUESTION }
| '!'
{ EXCLAMATION }
| '&'
{ AND }
| "&&"
{ LOGICALAND }
| "||"
{ LOGICALOR }
| "->"
{ ARROW }
| "++"
{ INCREMENT }
| "--"
{ DECREMENT }
| eof
{ EOF }
| _
(* 以上にマッチしない場合はエラーとして例外を発生 *)
{ failwith
(Printf.sprintf
"unknown token %s near characters %d-%d"
(Lexing.lexeme lexbuf)
(Lexing.lexeme_start lexbuf)
(Lexing.lexeme_end lexbuf)) }
and blockcomment depth = parse
| "/*"
{ blockcomment (depth+1) lexbuf }
| "*/"
{ if (depth-1)=0 then token lexbuf (*ふつうのモードへ戻る*) else blockcomment (depth-1) lexbuf }
| _
{ blockcomment depth lexbuf }
and linecomment = parse
| '\n' | eof
{ token lexbuf }
| _
{ linecomment lexbuf }