Nt.SyntaxParser 1.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Nt.SyntaxParser --version 1.4.0
                    
NuGet\Install-Package Nt.SyntaxParser -Version 1.4.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.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nt.SyntaxParser" Version="1.4.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.4.0
                    
#r "nuget: Nt.SyntaxParser, 1.4.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.4.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.4.0
                    
Install as a Cake Addin
#tool nuget:?package=Nt.SyntaxParser&version=1.4.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

# Enhancement of the branching feature

The `branching` feature was useful to create additional data for elements of the grammar.
However, it was sometimes unclear where to store it and hard to link it to elements.
Now the branching feature allow to store the data directly inside elements of the grammar, making it easier to manage and link the data to the corresponding elements.
This enhancement improves the overall usability and organization of the branching feature, allowing for a more streamlined workflow when working with grammar elements.

Create data for a grammar element with:
```csharp
var data = new DataType();
<element>.CreateData(data);
```

Retrieve the stored data with:
```csharp
var data = <element>.GetData<DataType>(data);
```

Element that supports data storage:
- Terminals
- Non terminals
- Rules
- Regular expressions

More information in [the branching documentation](Doc/Branching.md).

# Trivy scanning

The workflow for building and publishing packages has been enhanced with a full Trivy scan and CodeQL reports, visible on [the Github security page](https://github.com/Natvs/Nt.SyntaxParser/security/code-scanning).

The SBOM is now generated with Trivy instead of CycloneDX, thus removing the alert due to CycloneDX using Node12.

# Other

- Automatic publication of comments on PR defining if the merge would create a new release or publish new packages.
- Possibility of publishing packages on local repository only for testing purposes with the `PackageLocalOnly` property in the .csproj file.