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()
{
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
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()
{
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
//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()
{
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
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()
{
throw new NotImplementedException();
}
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()
throw new NotImplementedException();
}
}
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()
throw new NotImplementedException();
}
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()
{
throw new NotImplementedException();
}
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()
{
throw new NotImplementedException();
}
}
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()
throw new NotImplementedException();
}
public class Program
{
public static void Main()
{
string input = "154216";
Lexer L = new IntLexer(input);
try
{
L.Parse();
}
catch (LexerException e)
{
System.Console.WriteLine(e.Message);
}
}