Nt.SyntaxParser
1.4.1
See the version list below for details.
dotnet add package Nt.SyntaxParser --version 1.4.1
NuGet\Install-Package Nt.SyntaxParser -Version 1.4.1
<PackageReference Include="Nt.SyntaxParser" Version="1.4.1" />
<PackageVersion Include="Nt.SyntaxParser" Version="1.4.1" />
<PackageReference Include="Nt.SyntaxParser" />
paket add Nt.SyntaxParser --version 1.4.1
#r "nuget: Nt.SyntaxParser, 1.4.1"
#:package Nt.SyntaxParser@1.4.1
#addin nuget:?package=Nt.SyntaxParser&version=1.4.1
#tool nuget:?package=Nt.SyntaxParser&version=1.4.1
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 theSymbolclass 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 theISymbolinterface and you have to provide a custom symbol factory implementing theISymbolFactory. You can then set this factory as default factory for parsing withSyntaxParserConfig.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 | Versions 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. |
-
net10.0
- Nt.Automaton (>= 1.3.1)
- Nt.Parser (>= 1.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
# Bug fixes
- Fix a bug with the parser not always providing symbols of the type defined in configurations