0
votes

Used:
Visual Studio - 2019

To install ANTLR I use - tunnelvisionlabs/antlr4cs // Github.com

I took the grammar here - antlr/grammars-v4 // Github.com
Grammar:
- CSharpLexer.g4;
- CSharpParser.g4;
- CSharpPreprocessorParser.g4;

I did:
1. Installed ANTLR;
2. Created a WinFrom application;
3. Added grammar;
Right-click the project or folder in Solution Explorer and select
AddExisting Element.

Question.
How to create Lexer/Parser?
The instructions say that additional classes should be created, but this does not happen.

enter image description here enter image description here enter image description here enter image description here


Update 1.

I did:
1. I closed Visual Studio 2019;
2. I opened the project in Visual Studio 2017;
3. I deleted the files:
- CSharpLexer.g4;
- CSharpParser.g4;
- CSharpPreprocessorParser.g4;
4. I clicked "Add New Item";
5. I chose "ANTL 4 Lexer";
Note: there are no "ANTL" templates in "VS-2019". 6. I set the name to "CSharpLexer";
7. I clicked "Add";
8. Result:
- VS created a file - CSharpLexer.g4;
- VS created a file - CSharpLexer.g4.cs;
- VS for the file CSharpLexer.g4 set the property" Action at assembly "=" Antlr4 ".

Questions. Update-1.
1. Am I right?
2. Where can I get data to fill out the file CSharpLexer.g4?
Can I take data from the file CSharpLexer.g4, which is located at the link  https://github.com/antlr/grammars-v4/tree/master/csharp?
3. Do the files "CSharpParser.g4", "CSharpPreprocessorParser.g4" be created in the same way?
4. What template to use for the file - "CSharpPreprocessorParser.g4"?

enter image description here enter image description here

2
Did you create a grammer file? once created and build the solution, your files will be created by ANTlr - NirMH
@NirMH ** 1. ** I downloaded the files CSharpLexer.g4,` CSharpParser.g4, CSharpPreprocessorParser.g4` from the link - [antlr / grammars-v4 // Github.com] (github.com/antlr/grammars-v4/tree/master/csharp) ** 2. ** Added to the project Right-click the project or folder in Solution Explorer and select Add → Existing Element. ** 3. ** Pressed F6. Question. Am I doing the right thing? - eusataf
If you are already using VS2019, may I suggest you use my AntlrVSIX extension (following installation instructions), and just create an application from the Antlr template in the extension. Then after building, copy your grammar files to the project, and make sure the property for each .g4 is set to Antlr so that the Antlr tool runs on the grammar. If you want a library, go into properties of the project and change output type to a class library. Since the grammars contain embedded Java or C#, you'll have to pick Sam's copy or take the Java-based grammars and convert the Java code to C#. - kaby76

2 Answers

1
votes

By adding the Antlr package into the VS project, including a grammer file and building the project, the output files (lexer/Parser) will be auto generated in the same folder the grammer file is located.

You should then use the VS "Add existing item" to add the generated cs files into your project so you can compile them as "C#" files.

1
votes

If you failed to test with what @NirMH suggested, I strongly recommend you to try with Java runtime firstly as Java is the first-citizen in ANTLR project; https://github.com/antlr/antlr4/blob/master/doc/targets.md

Whatever target grammar you choose (in your case C#), to generate ANTLR4 result with Java runtime is stable than any other language. I've tested with JS, C#, and Python generator but all of them were not stable enough. The stable means, you might have an eternal loop during parsing in non-java ones.

Of course, you can parse C# file with the Java ANTLR runtime. To generate lexer/parser files, please walkthrough the tutorial. The generated results are plain .java files, so you should create your own jar library file from them.

I agree that the entire process is not one-click-solves-all, but language treatment is always dirty.

To invoke jar in WinForm, you can use IKVM nuget package in order to convert jar to dll as below, and then add the dll into a WinForm project.

C:\ikvmc.exe -target:library csharp-lexer-parser.jar