
XLParser
Open Source C# Excel Formula Parser
Parse, Analyze & Traverse Excel Formulas with High Accuracy Using Compact Parse Trees via Open Source C# Library.
What is XLParser?
XLParser is a high-accuracy C# library for parsing Microsoft Excel formulas, built on the Irony parser framework. It delivers a 99.9% success rate on over a million real-world formulas and is specifically designed for spreadsheet analysis and refactoring. Its compact grammar—fewer than 100 tokens and production rules—makes it easy to understand, maintain, or port to other languages. XLParser serves as the reference implementation of the Excel grammar published in the paper “A Grammar for Spreadsheet Formulas Evaluated on Two Large Datasets” and supports the Enron and EUSES benchmark datasets.
XLParser produces compact parse trees, enabling efficient traversal, analysis, and transformation of formulas. Developers can use the `ExcelFormulaParser` class to parse formulas and extract function names, references, and subexpressions. The `FormulaAnalyzer` class further supports advanced operations like `ParserReferences()` to retrieve all cell, range, and named references. With its focus on research and tooling, XLParser is ideal for spreadsheet quality assurance, formula refactoring, and educational tools. You can try the online demo, install via NuGet, or build from source on [XLParser GitHub](https://github.com/spreadsheetlab/XLParser).
Getting Started with XLParser
XLParser is available via NuGet and can be installed using the Package Manager Console or Visual Studio’s NuGet UI. The simplest way is to run `Install-Package XLParser` in the Package Manager Console. Alternatively, you can download the latest release from [XLParser GitHub](https://github.com/spreadsheetlab/XLParser), build the solution, and reference the DLL directly. The library is built on the Irony parser framework and requires no additional dependencies. Once installed, you can start parsing Excel formulas using the `ExcelFormulaParser` class. Its `Parse()` method accepts a formula string and returns a parse tree for further analysis or transformation.
Install XLParser via NuGet
Install-Package XLParserHigh Compatibility Excel Formula Parsing
XLParser delivers a 99.9% successful parse rate on over a million real-world Excel formulas, making it one of the most reliable C# formula parsers available. It was validated on the widely used Enron and EUSES benchmark datasets and is designed to handle complex, nested, and real-world formulas—including array expressions, references, and functions. While XLParser is not overly restrictive (it may parse some formulas Excel would reject), this leniency is intentional for research and analysis use cases. Developers can trust XLParser to parse the vast majority of formulas encountered in production spreadsheets, enabling robust formula analysis, refactoring, and quality assurance workflows. Its compact grammar ensures maintainability and portability across parser frameworks.
Parse an Excel Formula and Inspect the Parse Tree
using XLParser;
var parser = new ExcelFormulaParser();
var tree = parser.Parse("SUM(A1:B10, C12)");
Console.WriteLine(tree.Print());Extract All References from Excel Formulas
XLParser includes the `FormulaAnalyzer` class, which provides powerful methods for analyzing parsed formulas. One of its key features is `ParserReferences()`, which returns all cell, range, named range, and horizontal/vertical references used in a formula. This is invaluable for dependency tracking, impact analysis, and refactoring tools. For example, given a formula like `SUM(A1:B10, C12, NamedRange)`, `ParserReferences()` will return a list of `A1:B10`, `C12`, and `NamedRange`. This enables developers to build spreadsheet auditing tools, visualize data flow, or detect circular dependencies. The method works seamlessly with the compact parse tree structure, ensuring fast and accurate extraction even for large formulas.
Extract All References Using FormulaAnalyzer
using XLParser;
var parser = new ExcelFormulaParser();
var tree = parser.Parse("SUM(A1:B10, C12, NamedRange)");
var analyzer = new FormulaAnalyzer(tree);
var references = analyzer.ParserReferences();
foreach (var refNode in references)
{
Console.WriteLine(refNode.Print());
}Traverse and Analyze Parse Trees
XLParser’s parse trees are designed for efficient traversal and analysis. The `ExcelFormulaParser` class provides methods like `AllNodes` to iterate over every node in the tree, enabling custom analysis, transformation, or validation logic. Developers can filter nodes by type (e.g., functions, references, operators) and perform operations such as counting function occurrences, detecting unused named ranges, or identifying potential errors. The `GetFunction()` method simplifies extracting function names from function-call nodes, while `Print()` allows quick debugging or logging of any node’s structure. These capabilities make XLParser ideal for building spreadsheet quality tools, refactoring assistants, and educational visualizations.
Traverse All Nodes and Extract Function Names
using XLParser;
var parser = new ExcelFormulaParser();
var tree = parser.Parse("SUM(A1, A2) + AVERAGE(B1:B5)");
var functions = tree.AllNodes.Where(n => n.IsFunction());
foreach (var func in functions)
{
Console.WriteLine($"Function: {func.GetFunction()}");
}Debug and Visualize Parse Trees
XLParser integrates tightly with Irony’s GrammarExplorer tool for interactive debugging and tree visualization. After building XLParser and the GrammarExplorer solution, you can load `XLParser.dll` as a grammar and test formulas in real time. During development, Visual Studio’s debugger supports inspecting parse tree nodes via the watch window using `yournode.Print(),ac`. This feature accelerates understanding of complex formulas, testing edge cases, and validating grammar rules. The compact tree structure ensures that even deeply nested formulas remain readable and manageable. Whether you’re building a spreadsheet analysis tool or researching Excel formula behavior, XLParser’s debugging support simplifies development and troubleshooting.
Debug a Parse Tree Node in Visual Studio Watch Window
var tree = parser.Parse("IF(A1>0, A1*2, 0)");
// In Watch window: tree.Print(),ac