我正在尝试用C#编写一个非常简单的解析器.
我需要一个词法分析器 - 让我将正则表达式与标记相关联的东西,所以它读取正则表达式并给我回符号.
看起来我应该能够使用正则表达式进行实际繁重的工作,但我看不出一个简单的方法.首先,Regex似乎只能处理字符串,而不是流(为什么会这样!?!?).
基本上,我想要一个以下接口的实现:
interface ILexer : IDisposable
{
/// <summary>
/// Return true if there are more tokens to read
/// </summary>
bool HasMoreTokens { get; }
/// <summary>
/// The actual contents that matched the token
/// </summary>
string TokenContents { get; }
/// <summary>
/// The particular token in "tokenDefinitions" that was matched (e.g. "STRING", "NUMBER", "OPEN PARENS", "CLOSE PARENS"
/// </summary>
object Token { get; }
/// <summary>
/// Move to the next token …
Run Code Online (Sandbox Code Playgroud)