public class LexerException : System.Exception
public LexerException(string msg)
protected char currentCh;
protected int currentCharValue;
protected System.IO.StringReader inputReader;
protected string inputString;
public Lexer(string input)
inputReader = new System.IO.StringReader(input);
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());
this.currentCharValue = this.inputReader.Read();
this.currentCh = (char)currentCharValue;
public virtual void Parse()
public class IntLexer : Lexer
protected System.Text.StringBuilder intString;
public IntLexer(string input)
intString = new System.Text.StringBuilder();
public override void Parse()
if (currentCh == '+' || currentCh == '-')
if (char.IsDigit(currentCh))
while (char.IsDigit(currentCh))
if (currentCharValue != -1)
System.Console.WriteLine("Integer is recognized");
System.Console.WriteLine("Recognized int: " + int.Parse(this.tester));
public class IdLexer : Lexer
protected System.Text.StringBuilder intString;
public IdLexer(string input)
intString = new System.Text.StringBuilder();
public override void Parse()
if (char.IsLetter(currentCh))
while (char.IsDigit(currentCh) || char.IsLetter(currentCh))
if (currentCharValue != -1)
System.Console.WriteLine("Id is recognized");
public class CorrectIntLexer : Lexer
protected System.Text.StringBuilder intString;
public CorrectIntLexer(string input)
intString = new System.Text.StringBuilder();
public override void Parse()
if (currentCh == '+' || currentCh == '-')
while (char.IsDigit(currentCh))
if (currentCharValue != -1)
System.Console.WriteLine("Correct int is recognized");
public class AlternateLetterDigitLexer : Lexer
protected System.Text.StringBuilder intString;
public AlternateLetterDigitLexer(string input)
intString = new System.Text.StringBuilder();
public override void Parse()
while (valid && currentCharValue != -1) {
if (char.IsLetter(currentCh)) {
if (currentCharValue == -1)
else if (char.IsDigit(currentCh))
System.Console.WriteLine("Correct alternate is recognized");
public class SeparatedLetters : Lexer
protected System.Text.StringBuilder intString;
public SeparatedLetters(string input)
intString = new System.Text.StringBuilder();
public override void Parse()
while (letterNext && valid && currentCharValue != -1) {
if (char.IsLetter(currentCh)) {
if (currentCharValue == -1)
else if (currentCh == ';' || currentCh == ',') {
if (!valid || letterNext)
System.Console.WriteLine("Separated letters is recognized");
System.Console.WriteLine("Separated string is: " + tester);
public static void Main()
string idIncorrect = "432b";
string correctInt1 = "+423432a";
string correctInt2 = "-33213";
string incorrectInt3 = "-3";
string alternate = "a2b3c";
string alternateIncorrect = "a22b";
string separatedIncorrect = "a";
Lexer intLex = new IntLexer(input);
Lexer idLex = new IdLexer(id);
Lexer idLex2 = new IdLexer(idIncorrect);
Lexer correctIntLex1 = new CorrectIntLexer(correctInt1);
Lexer correctIntLex2 = new CorrectIntLexer(correctInt2);
Lexer incorrectIntLex3 = new CorrectIntLexer(incorrectInt3);
Lexer altLex = new AlternateLetterDigitLexer(alternate);
Lexer altLex2 = new AlternateLetterDigitLexer(alternateIncorrect);
Lexer sepLex = new SeparatedLetters(separated);
Lexer sepLex2 = new SeparatedLetters(separatedIncorrect);
System.Console.WriteLine(e.Message);