Sprache is a powerful yet lightweight framework for writing parsers in .NET. There is also a Sprache NuGet package. To give you an idea of the framework here is one of the samples that can parse a simple arithmetic expression into an .NET expression tree. Pretty amazing I would say.
using System;
using System.Linq.Expressions;
using Sprache;
namespace LinqyCalculator
{
static class ExpressionParser
{
public static Expression> ParseExpression(string text)
{
return Lambda.Parse(text);
}
static Parser Operator(string op, ExpressionType opType)
{
return Parse.String(op).Token().Return(opType);
}
static readonly Parser Add = Operator("+", ExpressionType.AddChecked);
static readonly Parser Subtract = Operator("-", ExpressionType.SubtractChecked);
static readonly Parser Multiply = Operator("*", ExpressionType.MultiplyChecked);
static readonly Parser Divide = Operator("/", ExpressionType.Divide);
static readonly Parser Constant =
(from d in Parse.Decimal.Token()
select (Expression)Expression.Constant(decimal.Parse(d))).Named("number");
static readonly Parser Factor =
((from lparen in Parse.Char('(')
from expr in Parse.Ref(() => Expr)
from rparen in Parse.Char(')')
select expr).Named("expression")
.XOr(Constant)).Token();
static readonly Parser Term = Parse.ChainOperator(Multiply.Or(Divide), Factor, Expression.MakeBinary);
static readonly Parser Expr = Parse.ChainOperator(Add.Or(Subtract), Term, Expression.MakeBinary);
static readonly Parser>> Lambda =
Expr.End().Select(body => Expression.Lambda>(body));
}
}