Nt.SyntaxParser 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Nt.SyntaxParser --version 1.3.0
                    
NuGet\Install-Package Nt.SyntaxParser -Version 1.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nt.SyntaxParser" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nt.SyntaxParser" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Nt.SyntaxParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nt.SyntaxParser --version 1.3.0
                    
#r "nuget: Nt.SyntaxParser, 1.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Nt.SyntaxParser@1.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nt.SyntaxParser&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Nt.SyntaxParser&version=1.3.0
                    
Install as a Cake Tool

SyntaxParser

Introduction

This project takes a grammar file as input and parses into a grammar structure. It aims to be used when creating new software languages.

Prerequisites

Install the Nt.SyntaxParser package

Install the NuGet package Nt.SyntaxParser in your project from Nuget.org:

dotnet add package Nt.SyntaxParser --version 1.2.1

or add the following line to your project file:

<PackageReference Include="Nt.SyntaxParser" Version="1.2.1" />

Define the grammar syntax

Grammar structure

A grammar contains the following elements:

  • A set of terminal symbols (constant strings)
  • A set of non-terminal symbols (symbols to derive)
  • A set of derivation rules (rules to derive non-terminals into sequences of terminals and non-terminals)
  • A set of regular expression rules (rules to derive non-terminals into regular expressions)
  • An axiom (the starting non-terminal symbol for derivations)

A rule is composed of a symbol (or token) and a derivation. The derivation is a sequence of terminals and non-terminals that the symbol can be derived into.

The syntax parser uses a grammar file to create a grammar structure. A grammar file is a text file that describes the grammar of a language. It contains the list of symbols used in the grammar and the rules. The syntax of the grammar file is described in the grammar file syntax documentation.

Using the syntax parser

To use the syntax parser, you first need to create an instance of the SyntaxParser class. There are two ways of parsing a grammar file: from a string or from a file.

To parse from a string

using Nt.Syntax;
using Nt.Syntax.Structures;

var generator = new SyntaxParser();
Grammar grammar = generator.ParseString(grammarString);

To parse from a file

using Nt.Syntax;
using Nt.Syntax.Structures;

var generator = new SyntaxParser();
Grammar grammar = generator.ParseFile(filePath);

Also parsing a grammar always applies the pre-parser on it, it's also possible to get a string representing the grammar after pre-parsing with the method PreParseString(string grammarString).

The unit of the parser is the symbol. The lists of terminals and non-terminals, or even the derivation rules are all made of symbols. These symbols are by default an instance of the Symbol class and only contain a name. For advanced use cases, you may need to create your own symbol class. In this case, the custom symbol must extend the ISymbol interface and you have to provide a custom symbol factory implementing the ISymbolFactory. You can then set this factory as default factory for parsing with SyntaxParserConfig.GetInstance().SetSymbolFactory(new_factory).

Additional features of the parser

Branching

The syntax parser supports parsing additional features written in the grammar file.

For example, it is possible to define priority values associated to terminals.

T = {A}
PRIORITY = 2

The parser will read it, and interpret it as long as you provide it with the logic. Then, you can add some logic to your code to handle the priority.

Branching is described in the branching documentation which contains more information about how to use it and how to implement your own branching logic.

Export the grammar

The grammar is meant to get different transformation in your code. After that, you may need to get the grammar displayed or written back in a file. This is why this project supports exporting grammars.

For example, you can display the grammar using the method ToString(). This method will display the content of the grammar in a user-friendly way.

Grammar file:

TERMINALS: a, b;
NON TERMINALS: S, D;
AXIOM: S
RULES: S -> D, D -> a b;

The grammar after exportation:

Terminals:
- a
- b

Non Terminals:
- S
- D

Axiom: S

Rules:
- S -> D
- D -> a b

There are different modes of exporting a grammar, please read the exportation documentation which describes how to export a grammar and contains more informations about methods of exporting it.

Exceptions

This project contains several custom exceptions to handle errors that may occur during the parsing of grammar files or input strings. See a list of exceptions here.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.2 93 11/14/2026
1.4.1 92 11/14/2026
1.4.0 94 11/13/2026
1.3.0 100 11/8/2026
1.2.1 110 10/21/2026

# Save grammar to files
This update introduces a new method for exporting grammars.

While the previous one is about exporting it to readable texts by the users, this one is about exporting it to files syntax so that it can be later parsed back to a grammar.

For that, import the `Nt.Syntax.Exportation` namespace and then use the `Save` extended method on the grammar object.

```csharp
using Nt.Syntax.Exportation;

var grammar_string = grammar.Save(ExportationMode.Initial);
```

# Exportation enhancement
Now when exporting a grammar, the subsequent rules are grouped with ` | ` for better readability.

As a consequence, when exporting with the `ExportationMode.Grouped` or `ExportationMode.Alphabetical` modes, all the rules with a common symbol will be grouped together.

# Security integration
The project integrates code security check at build time using the default Dotnet tools.