Newer
Older
using System;
using System.Text;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Lexer
public class LexerException : System.Exception
public LexerException(string msg)
: base(msg)
{
}
public class Lexer
{
protected int position;
protected char currentCh; //
protected int currentCharValue; //
protected System.IO.StringReader inputReader;
protected string inputString;
public Lexer(string input)
{
inputReader = new System.IO.StringReader(input);
inputString = input;
}
public void Error()
{
System.Text.StringBuilder o = new System.Text.StringBuilder();
o.Append(inputString + '\n');
o.Append(new System.String(' ', position - 1) + "^\n");
o.AppendFormat("Error in symbol {0}", currentCh);
throw new LexerException(o.ToString());
}
protected void NextCh()
{
this.currentCharValue = this.inputReader.Read();
this.currentCh = (char) currentCharValue;
this.position += 1;
}
public virtual bool Parse()
{
return true;
}
public class IntLexer : Lexer
protected System.Text.StringBuilder intString;
public int parseResult = 0;
public IntLexer(string input)
: base(input)
{
intString = new System.Text.StringBuilder();
}
public override bool Parse()
{
NextCh();
if (currentCh == '+' || currentCh == '-')
{
NextCh();
}
if (char.IsDigit(currentCh))
{
NextCh();
}
else
{
Error();
}
while (char.IsDigit(currentCh))
{
NextCh();
}
if (currentCharValue != -1)
{
Error();
}
return true;
}
public class IdentLexer : Lexer
{
private string parseResult;
protected StringBuilder builder;
public string ParseResult
{
get { return parseResult; }
}
public IdentLexer(string input) : base(input)
{
builder = new StringBuilder();
}
Error();
var builder1 = new StringBuilder();
if (char.IsLetter(currentCh) || currentCh == '_')
{
builder1.Append(currentCh);
NextCh();
}
else
while (char.IsLetter(currentCh) || char.IsDigit(currentCh) || currentCh == '_')
{
builder1.Append(currentCh);
NextCh();
}
if (currentCharValue != -1)
{
Error();
}
parseResult = builder1.ToString();
return true;
}
}
public class IntNoZeroLexer : IntLexer
public IntNoZeroLexer(string input)
: base(input)
{
}
public override bool Parse()
{
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
NextCh();
if (currentCharValue == -1)
{
Error();
}
if (currentCh == '0')
Error();
if (currentCh == '+' || currentCh == '-')
{
NextCh();
if (currentCh == '0')
Error();
}
while (char.IsDigit(currentCh))
NextCh();
if (currentCharValue != -1)
{
Error();
}
return true;
//throw new NotImplementedException();
}
public class LetterDigitLexer : Lexer
protected StringBuilder builder;
protected string parseResult;
public string ParseResult
{
get { return parseResult; }
}
public LetterDigitLexer(string input)
: base(input)
{
builder = new StringBuilder();
}
public override bool Parse()
{
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
//throw new NotImplementedException();
NextCh();
if (currentCharValue == -1)
{
Error();
}
if (char.IsDigit(currentCh))
Error();
bool isDigit = false;
NextCh();
while ((char.IsDigit(currentCh) && !isDigit) || (char.IsLetter(currentCh) && isDigit))
{
isDigit = char.IsDigit(currentCh);
NextCh();
}
if (currentCharValue != -1)
{
Error();
}
return true;
}
public class LetterListLexer : Lexer
{
protected List<char> parseResult;
public List<char> ParseResult
{
get { return parseResult; }
}
public LetterListLexer(string input)
: base(input)
{
parseResult = new List<char>();
}
public override bool Parse()
{
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
NextCh();
List<char> res = new List<char>();
if (currentCharValue == -1)
{
Error();
}
if (!char.IsLetter(currentCh))
Error();
else
{
res.Add(currentCh);
NextCh();
}
while (currentCh == ',' || currentCh == ';')
{
NextCh();
if (char.IsLetter(currentCh))
res.Add(currentCh);
else
Error();
NextCh();
}
if (currentCharValue != -1)
{
Error();
}
parseResult = res;
return true;
}
}
public class DigitListLexer : Lexer
protected List<int> parseResult;
public List<int> ParseResult
{
get { return parseResult; }
}
public DigitListLexer(string input)
: base(input)
{
parseResult = new List<int>();
}
public override bool Parse()
{
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
NextCh();
List<int> res = new List<int>();
if (currentCharValue == -1)
{
Error();
}
if (!char.IsDigit(currentCh))
Error();
else
{
res.Add(currentCh - '0');
NextCh();
}
while (currentCh == ' ')
{
NextCh();
if (char.IsDigit(currentCh))
{
res.Add(currentCh - '0');
NextCh();
}
else if (currentCh == ' ')
continue;
else
Error();
}
if (currentCharValue != -1)
{
Error();
}
parseResult = res;
return true;
}
public class LetterDigitGroupLexer : Lexer
protected StringBuilder builder;
protected string parseResult;
public string ParseResult
get { return parseResult; }
}
public LetterDigitGroupLexer(string input)
: base(input)
{
builder = new StringBuilder();
public override bool Parse()
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
NextCh();
StringBuilder res = new StringBuilder("");
if (currentCharValue == -1)
{
Error();
}
while (char.IsLetterOrDigit(currentCh))
{
if (char.IsLetter(currentCh))
{
res.Append(currentCh);
NextCh();
}
else
Error();
if (char.IsLetter(currentCh))
{
res.Append(currentCh);
NextCh();
if (currentCharValue == -1)
break;
}
if (char.IsDigit(currentCh))
{
res.Append(currentCh);
NextCh();
if (currentCharValue == -1)
break;
}
if (char.IsDigit(currentCh))
{
res.Append(currentCh);
NextCh();
if (currentCharValue == -1)
break;
}
}
if (currentCharValue != -1)
{
Error();
}
parseResult = res.ToString();
return true;
}
}
public class DoubleLexer : Lexer
{
private StringBuilder builder;
private double parseResult;
public double ParseResult
{
get { return parseResult; }
public DoubleLexer(string input)
: base(input)
builder = new StringBuilder();
public override bool Parse()
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
NextCh();
StringBuilder res = new StringBuilder("");
if (currentCharValue == -1)
{
Error();
}
if (char.IsDigit(currentCh))
{
if (currentCh == '0')
{
res.Append(currentCh);
NextCh();
if (currentCharValue == -1)
{
parseResult = double.Parse(res.ToString());
return true;
}
else if (currentCh != '.' && currentCharValue != -1)
Error();
else if (currentCh == '.')
{
res.Append(currentCh);
NextCh();
if (!char.IsDigit(currentCh))
Error();
}
else
Error();
}
else
{
while (char.IsDigit(currentCh))
{
res.Append(currentCh);
NextCh();
}
if (currentCharValue == -1)
{
parseResult = double.Parse(res.ToString());
return true;
}
else if (currentCh != '.' && currentCharValue != -1)
Error();
else if (currentCh == '.')
{
res.Append(currentCh);
NextCh();
if (!char.IsDigit(currentCh))
Error();
}
else
Error();
}
}
else
Error();
while (char.IsDigit(currentCh))
{
res.Append(currentCh);
NextCh();
}
if (currentCharValue != -1)
{
Error();
}
parseResult = double.Parse(res.ToString());
return true;
}
public class StringLexer : Lexer
{
private StringBuilder builder;
private string parseResult;
public string ParseResult
get { return parseResult; }
public StringLexer(string input)
: base(input)
{
builder = new StringBuilder();
}
public override bool Parse()
{
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
NextCh();
StringBuilder res = new StringBuilder("");
if (currentCharValue == -1)
{
Error();
}
if (currentCh != '\'')
Error();
res.Append(currentCh);
NextCh();
while (currentCh != '\'')
{
if (currentCharValue == -1)
Error();
res.Append(currentCh);
NextCh();
}
if (currentCh != '\'')
Error();
res.Append(currentCh);
NextCh();
if (currentCharValue != -1)
{
Error();
}
parseResult = res.ToString();
return true;
}
public class CommentLexer : Lexer
{
private StringBuilder builder;
private string parseResult;
public string ParseResult
{
get { return parseResult; }
}
public CommentLexer(string input)
: base(input)
{
builder = new StringBuilder();
}
public override bool Parse()
{
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
NextCh();
StringBuilder res = new StringBuilder("");
if (currentCharValue == -1)
Error();
if (currentCh != '/')
Error();
res.Append(currentCh);
NextCh();
if (currentCh != '*')
Error();
res.Append(currentCh);
NextCh();
while (true)
{
if (currentCharValue == -1)
Error();
if (currentCh == '*')
{
res.Append(currentCh);
NextCh();
if (currentCh == '/')
{
res.Append(currentCh);
NextCh();
if (currentCharValue != -1)
Error();
break;
}
}
res.Append(currentCh);
NextCh();
}
parseResult = res.ToString();
return true;
}
}
public class IdentChainLexer : Lexer
private StringBuilder builder;
private List<string> parseResult;
public List<string> ParseResult
{
get { return parseResult; }
}
public IdentChainLexer(string input)
: base(input)
builder = new StringBuilder();
parseResult = new List<string>();
public override bool Parse()
NextCh();
StringBuilder res = new StringBuilder("");
StringBuilder ident = new StringBuilder("");
if (currentCharValue == -1)
Error();
IdentLexer l = new IdentLexer("abc22");
return true;
}
public class Program
{
public static void Main()
{
}