A .NET template for creating Antlr4 projects in C#, including a sample CSV parser.
$ dotnet add package Atypical.Antlr4Library.TemplatesA .NET template for creating Antlr4 projects in C#, including a sample CSV parser. This template provides a starting point for building language parsers or interpreters using Antlr4 in a C# environment.
This template includes:
CSV.g4) for parsing CSV files.Install the template using the dotnet new command:
dotnet new install Atypical.Antlr4Library.Templates
Create a new project using the template:
dotnet new antlr4 -n YourProjectName
This command creates a new directory YourProjectName with the template contents.

You can create a new project using JetBrains Rider or Visual Studio by selecting the Antlr4 Library template from the project creation dialog.
For color syntax highlighting and code completion, install the ANTLR v4 plugin by Terence Parr.
Grammar/CSV.g4: The Antlr4 grammar file for CSV parsing.Models/CSV.cs: The model class representing the CSV data.Services/CSVService.cs: Service class to parse CSV data using the generated parser.Services/CSVVisitor.cs: Visitor class to build the CSV model from the parse tree.Antlr4Library.csproj: Project file with Antlr4 build tasks and runtime dependencies.Program.cs: Sample program demonstrating how to use the CSVService.Edit the Grammar/CSV.g4 file to change the grammar rules or create a new grammar for your specific language or data format.
After modifying the grammar, rebuild the project to regenerate the parser and lexer classes:
dotnet build
Modify CSVVisitor.cs to implement custom logic for traversing the parse tree generated by your grammar.
Use CSVService.cs as a reference to create your own service classes for parsing different types of data.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request to the repository.
This project is licensed under the MIT License. See the LICENSE file for details.
Developed and maintained by Philippe Matray, founder of Atypical Consulting SRL.
Created by Terence Parr, Antlr4 is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build language parsers, interpreters, and compilers for various programming languages and data formats.
It has been around for many years and has a large community of users and contributors. Antlr4 is available for multiple programming languages, including Java, C#, Python, and JavaScript.
Install the Template
dotnet new install Atypical.Antlr4Library.Templates
Create a New Solution
mkdir AntlrDemo
cd AntlrDemo
dotnet new sln -n MyAntlrSolution
Create a New Antlr4 Project
dotnet new antlr4 -n MyAntlrLibrary
dotnet sln add MyAntlrLibrary
Create a Console Application with the demo code
dotnet new console -n MyAntlrApp
dotnet add MyAntlrApp reference MyAntlrLibrary
dotnet sln add MyAntlrApp
Replace the contents of Program.cs with the following code:
using MyAntlrLibrary.Services;
const string csvInput =
""""
Details,Month,Amount
Mid Bonus,June,"$2,000"
,January,"""zippo"""
Total Bonuses,"","$5,000"
"""";
var service = new CSVService();
var csv = service.Parse(csvInput);
Console.WriteLine(string.Join(", ", csv.Header));
Console.WriteLine("---------------------");
foreach (var row in csv.Rows)
{
Console.WriteLine(string.Join(", ", row));
}
Then build and run the application:
dotnet run --project MyAntlrApp
You should see the parsed CSV output in the console.
For support or inquiries, please contact Philippe Matray at philippe@atypical.consulting or open an issue in the repository.