Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Antlr4 C# Application Tutorial/Example

Tags:

c#

antlr

antlr4

I want to use Antlr4 to parse some files in my C# application. I have been able to generate the parser and lexer files so far given my grammer. Now I would like to use read in the files and apply the parser and lexer to them. I have been searching for documentation on how to do that but I am coming up short. I have found some old examples using previous versions of Antlr but they don't appear to work for Antlr4. Any help would be appreciated. Thanks.

like image 353
Neil Pittman Avatar asked Oct 11 '13 21:10

Neil Pittman


People also ask

What is antlr4 used for?

ANTLR v4. ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks.

What languages use ANTLR?

While Version 3 supported generating code in the programming languages Ada95, ActionScript, C, C#, Java, JavaScript, Objective-C, Perl, Python, Ruby, and Standard ML, Version 4 at present targets C#, C++, Dart, Java, JavaScript, Go, PHP, Python (2 and 3), and Swift.

Can ANTLR parse C++?

Getting Started with ANTLR in C++ ANTLR can generate parsers in many languages: Java, C#, Python (2 and 3), JavaScript, Go, Swift, Dart, PHP and C++.

Who created ANTLR?

Guido van Rossum, Inventor of Python Our grammars are clean and concise, and the generated code is efficient and stable. The book is our go-to reference for ANTLR v4 -- engaging writing, clear descriptions and practical examples all in one place.


2 Answers

  • In Visual Studio, go to Tools -> Extensions and Updates and search the Online section for "ANTLR Language Support" by Sam Harwell. More information can be found on the GitHub project site
    • This does a few things:
      • Adds Templates for the combined grammars.
      • Adds Syntax Highlighting
      • Adds an MSBuild target for the grammar to generate the parser.
  • In your solution, set up your project structure like this:
    • Solution
      • GrammarProject
        • Calculator.g4
      • ImplementationProject
        • GeneratedFiles (All files in this folder are added as Links to files located in GrammarProject\obj\Debug)
          • CalculatorBaseListener.cs
          • CalculatorBaseVisitor.cs
          • CalculatorLexer.cs
          • CalculatorListener.cs
          • CalculatorParser.cs
          • CalculatorVistor.cs
        • MyCalcualtorImplementation.cs
  • Write and Compile your grammar.
  • In your folder for the Links to Generated Files, Right-Click the folder and click Add -> Existing Item
  • Browse to Grammar Project\obj\Debug and select all the generated parser files.
  • This next step is important. On the Add button there is a little drop-down arrow. Click the drop-down arrow and click "Add As Link".
    • This will add the generated files to the implementation project using a symbolic link instead of a direct copy.
    • This gives the added benefit of not having to remove and re-add the parser files if you have to change your grammar later.
  • Once you have gotten this far, then you can inherit from GrammarProject.CalculatorBaseListener or GrammarProject.CalculatorBaseParser depending on what development pattern you have decided to use.

As a side note, "The Definitive ANTLR 4 Reference" by Terence Parr is an excellent resource to understand how ANTLR4 works and the difference development patterns. All the examples are in java, but the concepts apply to both Java and C#.

like image 84
Esten Avatar answered Oct 17 '22 05:10

Esten


try with

using (StreamReader fileStream = new StreamReader(fileName)) {
    AntlrInputStream inputStream = new AntlrInputStream(fileStream);

    SearchLexer lexer = new SearchLexer(inputStream);
    CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
    SearchParser parser = new SearchParser(commonTokenStream);

    parser.RemoveErrorListeners();
    parser.AddErrorListener(new ErrorListener()); // add ours

    parser.root();
}
like image 11
Narkha Avatar answered Oct 17 '22 04:10

Narkha