public
Created

Ragel with Java

  • Download Gist
Lexer.java
Java
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
 
// line 1 "Lexer.rl"
public class Lexer {
// line 19 "Lexer.rl"
 
 
// line 10 "Lexer.java"
private static byte[] init__simple_lexer_actions_0()
{
return new byte [] {
0, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1,
5, 1, 6, 1, 7, 1, 8
};
}
 
private static final byte _simple_lexer_actions[] = init__simple_lexer_actions_0();
 
 
private static byte[] init__simple_lexer_key_offsets_0()
{
return new byte [] {
0, 0, 2, 4, 9, 21, 24, 26
};
}
 
private static final byte _simple_lexer_key_offsets[] = init__simple_lexer_key_offsets_0();
 
 
private static char[] init__simple_lexer_trans_keys_0()
{
return new char [] {
48, 57, 48, 57, 95, 65, 90, 97, 122, 32, 43, 45,
61, 9, 13, 48, 57, 65, 90, 97, 122, 46, 48, 57,
48, 57, 95, 65, 90, 97, 122, 0
};
}
 
private static final char _simple_lexer_trans_keys[] = init__simple_lexer_trans_keys_0();
 
 
private static byte[] init__simple_lexer_single_lengths_0()
{
return new byte [] {
0, 0, 0, 1, 4, 1, 0, 1
};
}
 
private static final byte _simple_lexer_single_lengths[] = init__simple_lexer_single_lengths_0();
 
 
private static byte[] init__simple_lexer_range_lengths_0()
{
return new byte [] {
0, 1, 1, 2, 4, 1, 1, 2
};
}
 
private static final byte _simple_lexer_range_lengths[] = init__simple_lexer_range_lengths_0();
 
 
private static byte[] init__simple_lexer_index_offsets_0()
{
return new byte [] {
0, 0, 2, 4, 8, 17, 20, 22
};
}
 
private static final byte _simple_lexer_index_offsets[] = init__simple_lexer_index_offsets_0();
 
 
private static byte[] init__simple_lexer_trans_targs_0()
{
return new byte [] {
5, 0, 6, 4, 7, 7, 7, 0, 4, 1, 1, 4,
4, 5, 3, 3, 0, 2, 5, 4, 6, 4, 7, 7,
7, 4, 4, 4, 4, 4, 0
};
}
 
private static final byte _simple_lexer_trans_targs[] = init__simple_lexer_trans_targs_0();
 
 
private static byte[] init__simple_lexer_trans_actions_0()
{
return new byte [] {
5, 0, 0, 17, 0, 0, 0, 0, 9, 0, 0, 7,
9, 5, 0, 0, 0, 0, 5, 11, 0, 13, 0, 0,
0, 15, 17, 11, 13, 15, 0
};
}
 
private static final byte _simple_lexer_trans_actions[] = init__simple_lexer_trans_actions_0();
 
 
private static byte[] init__simple_lexer_to_state_actions_0()
{
return new byte [] {
0, 0, 0, 0, 1, 0, 0, 0
};
}
 
private static final byte _simple_lexer_to_state_actions[] = init__simple_lexer_to_state_actions_0();
 
 
private static byte[] init__simple_lexer_from_state_actions_0()
{
return new byte [] {
0, 0, 0, 0, 3, 0, 0, 0
};
}
 
private static final byte _simple_lexer_from_state_actions[] = init__simple_lexer_from_state_actions_0();
 
 
private static byte[] init__simple_lexer_eof_trans_0()
{
return new byte [] {
0, 0, 27, 0, 0, 28, 29, 30
};
}
 
private static final byte _simple_lexer_eof_trans[] = init__simple_lexer_eof_trans_0();
 
 
static final int simple_lexer_start = 4;
static final int simple_lexer_first_final = 4;
static final int simple_lexer_error = 0;
 
static final int simple_lexer_en_main = 4;
 
 
// line 22 "Lexer.rl"
 
public static void emit(String token) {
System.out.println(token);
}
 
public static void main(String[] args) {
// line 143 "Lexer.java"
4
// line 29 "Lexer.rl"
 
// line 148 "Lexer.java"
{
cs = simple_lexer_start;
ts = -1;
te = -1;
act = 0;
}
 
// line 31 "Lexer.rl"
 
// line 159 "Lexer.java"
{
int _klen;
int _trans = 0;
int _acts;
int _nacts;
int _keys;
int _goto_targ = 0;
 
_goto: while (true) {
switch ( _goto_targ ) {
case 0:
if ( p == pe ) {
_goto_targ = 4;
continue _goto;
}
if ( cs == 0 ) {
_goto_targ = 5;
continue _goto;
}
case 1:
_acts = _simple_lexer_from_state_actions[cs];
_nacts = (int) _simple_lexer_actions[_acts++];
while ( _nacts-- > 0 ) {
switch ( _simple_lexer_actions[_acts++] ) {
case 1:
// line 1 "NONE"
{ts = p;}
break;
// line 188 "Lexer.java"
}
}
 
_match: do {
_keys = _simple_lexer_key_offsets[cs];
_trans = _simple_lexer_index_offsets[cs];
_klen = _simple_lexer_single_lengths[cs];
if ( _klen > 0 ) {
int _lower = _keys;
int _mid;
int _upper = _keys + _klen - 1;
while (true) {
if ( _upper < _lower )
break;
 
_mid = _lower + ((_upper-_lower) >> 1);
if ( data[p] < _simple_lexer_trans_keys[_mid] )
_upper = _mid - 1;
else if ( data[p] > _simple_lexer_trans_keys[_mid] )
_lower = _mid + 1;
else {
_trans += (_mid - _keys);
break _match;
}
}
_keys += _klen;
_trans += _klen;
}
 
_klen = _simple_lexer_range_lengths[cs];
if ( _klen > 0 ) {
int _lower = _keys;
int _mid;
int _upper = _keys + (_klen<<1) - 2;
while (true) {
if ( _upper < _lower )
break;
 
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
if ( data[p] < _simple_lexer_trans_keys[_mid] )
_upper = _mid - 2;
else if ( data[p] > _simple_lexer_trans_keys[_mid+1] )
_lower = _mid + 2;
else {
_trans += ((_mid - _keys)>>1);
break _match;
}
}
_trans += _klen;
}
} while (false);
 
case 3:
cs = _simple_lexer_trans_targs[_trans];
 
if ( _simple_lexer_trans_actions[_trans] != 0 ) {
_acts = _simple_lexer_trans_actions[_trans];
_nacts = (int) _simple_lexer_actions[_acts++];
while ( _nacts-- > 0 )
{
switch ( _simple_lexer_actions[_acts++] )
{
case 2:
// line 1 "NONE"
{te = p+1;}
break;
case 3:
// line 14 "Lexer.rl"
{te = p+1;{ emit("assignment"); }}
break;
case 4:
// line 16 "Lexer.rl"
{te = p+1;{ emit("space"); }}
break;
case 5:
// line 12 "Lexer.rl"
{te = p;p--;{ emit("integer"); }}
break;
case 6:
// line 13 "Lexer.rl"
{te = p;p--;{ emit("float"); }}
break;
case 7:
// line 15 "Lexer.rl"
{te = p;p--;{ emit("identifier"); }}
break;
case 8:
// line 12 "Lexer.rl"
{{p = ((te))-1;}{ emit("integer"); }}
break;
// line 279 "Lexer.java"
}
}
}
 
case 2:
_acts = _simple_lexer_to_state_actions[cs];
_nacts = (int) _simple_lexer_actions[_acts++];
while ( _nacts-- > 0 ) {
switch ( _simple_lexer_actions[_acts++] ) {
case 0:
// line 1 "NONE"
{ts = -1;}
break;
// line 293 "Lexer.java"
}
}
 
if ( cs == 0 ) {
_goto_targ = 5;
continue _goto;
}
if ( ++p != pe ) {
_goto_targ = 1;
continue _goto;
}
case 4:
if ( p == eof )
{
if ( _simple_lexer_eof_trans[cs] > 0 ) {
_trans = _simple_lexer_eof_trans[cs] - 1;
_goto_targ = 3;
continue _goto;
}
}
 
case 5:
}
break; }
}
 
// line 33 "Lexer.rl"
}
}
Lexer.rl
Ragel in Ruby Host
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
public class Lexer {
%%{
 
machine simple_lexer;
integer = ('+'|'-')?[0-9]+;
float = ('+'|'-')?[0-9]+'.'[0-9]+;
assignment = '=';
identifier = [a-zA-Z][a-zA-Z_]+;
main := |*
integer => { emit("integer"); };
float => { emit("float"); };
assignment => { emit("assignment"); };
identifier => { emit("identifier"); };
space => { emit("space"); };
*|;
 
}%%
 
%% write data;
 
public static void emit(String token) {
System.out.println(token);
}
 
public static void main(String[] args) {
%% write init;
 
%% write exec;
}
}
errors when run javac Lexer.java
Java
1 2 3 4 5 6 7 8
[~/Projetos/Ragel]$ javac Lexer.java [1.9.3-p125]
Lexer.java:143: not a statement
4
^
Lexer.java:143: ';' expected
4
^
2 errors

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.