Table of Contents

What is Farkle?

Farkle is a .NET library that helps you create text parsers. Most projects in this field are either parser generators — which are faster and easier to debug, but require writing a grammar definition in a different language — or parser combinator libraries — which are defined in source code and have a better development experience, but are slower and more error-prone. Farkle however combines both approaches: it uses the same algorithm as generated parsers, while allowing you to define your grammar in source code, providing performance, reliability and ease of use at the same time.

Farkle follows the paradigm introduced by GOLD Parser, which uses a binary file format to serialize grammars. This allows you to write code that introspects your grammars and use them for things like rendering Scriban templates, but also precompile your grammars ahead of time, providing high startup performance and compile-time error checking.

You can learn more about Farkle's features, compared with other .NET parsers.

Quick start

Farkle can be installed from NuGet. Afterwards, you can proceed with writing your first parser:

using Farkle;
using Farkle.Builder;

// Define a grammar for simple addition expressions
var number = Terminals.Int32("Number");
var addExpression = Nonterminal.Create("Add Expression",
    number.Extended().Append("+").Extend(number).Finish((n1, n2) => n1 + n2)
);

// Build the grammar and get a parser object
var parser = addExpression.Build();

// Parse a simple expression
var result = parser.Parse("1 + 2"); // Whitespace is ignored by default
Console.WriteLine(result); // Outputs: 3

Documentation

The library comes with comprehensive documentation.

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests.

The library is available under the MIT license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.