-
Notifications
You must be signed in to change notification settings - Fork 7
/
c.lex
138 lines (121 loc) · 7.05 KB
/
c.lex
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
<?php # vim:ft=php
include 'jlex.php';
include '../lemon/c.php';
/* This is a lexer for the C language */
%%
%{
/* blah */
%}
%function nextToken
%line
%char
%state COMMENT
%class CLexer
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)
%%
<YYINITIAL> "/*" {
$this->commentTok = $this->createToken(CParser::TK_COMMENT);
$this->yybegin(self::COMMENT);
}
<YYINITIAL> //[^\r\n]* { return $this->createToken(CParser::TK_COMMENT); }
<COMMENT> "*/" {
$this->commentTok->value .= $this->yytext();
$this->yybegin(self::YYINITIAL);
return $this->commentTok;
}
<COMMENT> (.|[\r\n]) { $this->commentTok->value .= $this->yytext(); }
<YYINITIAL> #[^\r\n]* { return $this->createToken(CParser::TK_PRAGMA); }
<YYINITIAL> "auto" { return $this->createToken(CParser::TK_AUTO); }
<YYINITIAL> "break" { return $this->createToken(CParser::TK_BREAK); }
<YYINITIAL> "case" { return $this->createToken(CParser::TK_CASE); }
<YYINITIAL> "char" { return $this->createToken(CParser::TK_CHAR); }
<YYINITIAL> "const" { return $this->createToken(CParser::TK_CONST); }
<YYINITIAL> "continue" { return $this->createToken(CParser::TK_CONTINUE); }
<YYINITIAL> "default" { return $this->createToken(CParser::TK_DEFAULT); }
<YYINITIAL> "do" { return $this->createToken(CParser::TK_DO); }
<YYINITIAL> "double" { return $this->createToken(CParser::TK_DOUBLE); }
<YYINITIAL> "else" { return $this->createToken(CParser::TK_ELSE); }
<YYINITIAL> "enum" { return $this->createToken(CParser::TK_ENUM); }
<YYINITIAL> "extern" { return $this->createToken(CParser::TK_EXTERN); }
<YYINITIAL> "float" { return $this->createToken(CParser::TK_FLOAT); }
<YYINITIAL> "for" { return $this->createToken(CParser::TK_FOR); }
<YYINITIAL> "goto" { return $this->createToken(CParser::TK_GOTO); }
<YYINITIAL> "if" { return $this->createToken(CParser::TK_IF); }
<YYINITIAL> "int" { return $this->createToken(CParser::TK_INT); }
<YYINITIAL> "long" { return $this->createToken(CParser::TK_LONG); }
<YYINITIAL> "register" { return $this->createToken(CParser::TK_REGISTER); }
<YYINITIAL> "return" { return $this->createToken(CParser::TK_RETURN); }
<YYINITIAL> "short" { return $this->createToken(CParser::TK_SHORT); }
<YYINITIAL> "signed" { return $this->createToken(CParser::TK_SIGNED); }
<YYINITIAL> "sizeof" { return $this->createToken(CParser::TK_SIZEOF); }
<YYINITIAL> "static" { return $this->createToken(CParser::TK_STATIC); }
<YYINITIAL> "struct" { return $this->createToken(CParser::TK_STRUCT); }
<YYINITIAL> "switch" { return $this->createToken(CParser::TK_SWITCH); }
<YYINITIAL> "typedef" { return $this->createToken(CParser::TK_TYPEDEF); }
<YYINITIAL> "union" { return $this->createToken(CParser::TK_UNION); }
<YYINITIAL> "unsigned" { return $this->createToken(CParser::TK_UNSIGNED); }
<YYINITIAL> "void" { return $this->createToken(CParser::TK_VOID); }
<YYINITIAL> "volatile" { return $this->createToken(CParser::TK_VOLATILE); }
<YYINITIAL> "while" { return $this->createToken(CParser::TK_WHILE); }
<YYINITIAL> {L}({L}|{D})* { return $this->createToken(CParser::TK_IDENTIFIER); }
<YYINITIAL> 0[xX]{H}+{IS}? { return $this->createToken(CParser::TK_CONSTANT); }
<YYINITIAL> 0{D}+{IS}? { return $this->createToken(CParser::TK_CONSTANT); }
<YYINITIAL> {D}+{IS}? { return $this->createToken(CParser::TK_CONSTANT); }
<YYINITIAL> L?\'(\\.|[^\\\'])+\' { return $this->createToken(CParser::TK_CONSTANT); }
<YYINITIAL> {D}+{E}{FS}? { return $this->createToken(CParser::TK_CONSTANT); }
<YYINITIAL> {D}*"."{D}+({E})?{FS}? { return $this->createToken(CParser::TK_CONSTANT); }
<YYINITIAL> {D}+"."{D}*({E})?{FS}? { return $this->createToken(CParser::TK_CONSTANT); }
<YYINITIAL> L?\"(\\.|[^\\\"])*\" { return $this->createToken(CParser::TK_STRING_LITERAL); }
<YYINITIAL> "..." { return $this->createToken(CParser::TK_ELLIPSIS); }
<YYINITIAL> ">>=" { return $this->createToken(CParser::TK_RIGHT_ASSIGN); }
<YYINITIAL> "<<=" { return $this->createToken(CParser::TK_LEFT_ASSIGN); }
<YYINITIAL> "+=" { return $this->createToken(CParser::TK_ADD_ASSIGN); }
<YYINITIAL> "-=" { return $this->createToken(CParser::TK_SUB_ASSIGN); }
<YYINITIAL> "*=" { return $this->createToken(CParser::TK_MUL_ASSIGN); }
<YYINITIAL> "/=" { return $this->createToken(CParser::TK_DIV_ASSIGN); }
<YYINITIAL> "%=" { return $this->createToken(CParser::TK_MOD_ASSIGN); }
<YYINITIAL> "&=" { return $this->createToken(CParser::TK_AND_ASSIGN); }
<YYINITIAL> "^=" { return $this->createToken(CParser::TK_XOR_ASSIGN); }
<YYINITIAL> "|=" { return $this->createToken(CParser::TK_OR_ASSIGN); }
<YYINITIAL> ">>" { return $this->createToken(CParser::TK_RIGHT_OP); }
<YYINITIAL> "<<" { return $this->createToken(CParser::TK_LEFT_OP); }
<YYINITIAL> "++" { return $this->createToken(CParser::TK_INC_OP); }
<YYINITIAL> "--" { return $this->createToken(CParser::TK_DEC_OP); }
<YYINITIAL> "->" { return $this->createToken(CParser::TK_PTR_OP); }
<YYINITIAL> "&&" { return $this->createToken(CParser::TK_AND_OP); }
<YYINITIAL> "||" { return $this->createToken(CParser::TK_OR_OP); }
<YYINITIAL> "<=" { return $this->createToken(CParser::TK_LE_OP); }
<YYINITIAL> ">=" { return $this->createToken(CParser::TK_GE_OP); }
<YYINITIAL> "==" { return $this->createToken(CParser::TK_EQ_OP); }
<YYINITIAL> "!=" { return $this->createToken(CParser::TK_NE_OP); }
<YYINITIAL> ";" { return $this->createToken(CParser::TK_SEMIC); }
<YYINITIAL> ("{"|"<%") { return $this->createToken(CParser::TK_LCURLY); }
<YYINITIAL> ("}"|"%>") { return $this->createToken(CParser::TK_RCURLY); }
<YYINITIAL> "," { return $this->createToken(CParser::TK_COMMA); }
<YYINITIAL> ":" { return $this->createToken(CParser::TK_COLON); }
<YYINITIAL> "=" { return $this->createToken(CParser::TK_EQUALS); }
<YYINITIAL> "(" { return $this->createToken(CParser::TK_LPAREN); }
<YYINITIAL> ")" { return $this->createToken(CParser::TK_RPAREN); }
<YYINITIAL> ("["|"<:") { return $this->createToken(CParser::TK_LSQUARE); }
<YYINITIAL> ("]"|":>") { return $this->createToken(CParser::TK_RSQUARE); }
<YYINITIAL> "." { return $this->createToken(CParser::TK_PERIOD); }
<YYINITIAL> "&" { return $this->createToken(CParser::TK_AMP); }
<YYINITIAL> "!" { return $this->createToken(CParser::TK_EXCLAM); }
<YYINITIAL> "~" { return $this->createToken(CParser::TK_TILDE); }
<YYINITIAL> "-" { return $this->createToken(CParser::TK_MINUS); }
<YYINITIAL> "+" { return $this->createToken(CParser::TK_PLUS); }
<YYINITIAL> "*" { return $this->createToken(CParser::TK_STAR); }
<YYINITIAL> "/" { return $this->createToken(CParser::TK_SLASH); }
<YYINITIAL> "%" { return $this->createToken(CParser::TK_PERCENT); }
<YYINITIAL> "<" { return $this->createToken(CParser::TK_LANGLE); }
<YYINITIAL> ">" { return $this->createToken(CParser::TK_RANGLE); }
<YYINITIAL> "^" { return $this->createToken(CParser::TK_CARET); }
<YYINITIAL> "|" { return $this->createToken(CParser::TK_PIPE); }
<YYINITIAL> "?" { return $this->createToken(CParser::TK_QUESTION); }
<YYINITIAL> [ \t\v\n\f] { }
. { /* ignore bad characters */ }