Farkle is a .NET library for building fast, reliable text parsers in C# and F#. It creates LALR(1) and IELR(1) parsers through an API that resembles parser combinators, combining generated-parser performance and reliability, with a source-code-first development experience.
Farkle follows the GOLD Parser approach of serializing grammars to a binary format. This makes grammars available for introspection, and enables features such as Scriban template rendering, and ahead-of-time precompilation for faster startup and compile-time grammar validation.
Install Farkle from NuGet, then define and build a grammar in code:
using Farkle;
using Farkle.Builder;
var number = Terminals.Int32("Number");
var addExpression = Nonterminal.Create("Add Expression",
number.Extended().Append("+").Extend(number).Finish((n1, n2) => n1 + n2)
);
var parser = addExpression.Build();
var result = parser.Parse("1 + 2"); // Whitespace is ignored by default.
Console.WriteLine(result); // 3open Farkle
open Farkle.Builder
let number = Terminals.Int32("Number")
let addExpression = "Add Expression" ||= [
!@ number .>> "+" .>>. number => (fun n1 n2 -> n1 + n2)
]
let parser = GrammarBuilder.build addExpression
let result = CharParser.parseString parser "1 + 2" // Whitespace is ignored by default.
printfn "%O" result // 3Issues and pull requests are welcome at teo-tsirpanis/Farkle. See the contribution guide for development and submission guidance.