본문 바로가기

잡다한 정보 Miscellaneous

ANTLR4 C# Step by Step with Visual Studio

ANTLR4 C# with Visual Studio

ANTLR logo image is from. click

Visual Studio logo image is from. click


C# target for ANTLR4


C# ANTLR4 getting start


This post is a just little bit visually improved version of the official document. You can find the official documents above by the author of ANTLR4. Besides, I would like to address some issues(in terms of usage) when you play with ANTLR. Here is the outline.


Outline



What is ANTLR4?



Requirements

  • Visual Studio (I use visual studio 2013)

  • JAVA 8



Step by Step - Wiring up ANTLR4

  1. 1. Prepare the Requirements.

  2. 2. Create a new project.

  3. 3. Run NuGet package downloader.

  1. 4. Search "antlr".

  1. 5. Install "antlr".

  1. 6. Make sure you have the right one.

  1. 7. Import



Step by Step - Generating Parser

  1. 1. You need to get ANTLR4 .jar file to create your parser.

Where can I get it? Here.

Do you need another version? Here.

  1. 2. Where's the grammar file?

There's a big repo containing almost every programming language. But, some grammar file is only to target specific language.

Grammar repo

  1. 3. How to use it?
    java -jar antlr-4.7.1-complete.jar -Dlanguage=CSharp Scala.g4 

  1. 4. If you need visitor?

It's simple. Please add "-visitor" option to the command. The command on 3rd creates a parser with a listener(default).

    java -jar antlr-4.7.1-complete.jar -Dlanguage=CSharp Scala.g4 -visitor



(C# Code) Practice - how to parse?

First, make sure importing the generated parser into your project.

   string scala = "object HelloWorld extends App {" + "\n\t" + 
                        "println(\"Hello, world!\")" + "\n" + 
                        "}";

   ICharStream target = new AntlrInputStream(scala);
   ITokenSource lexer = new ScalaLexer(target);
   ITokenStream tokens = new CommonTokenStream(lexer);
   ScalaParser parser = new ScalaParser(tokens);
   parser.BuildParseTree = true;

   ScalaParser.LiteralContext result = parser.literal();
   Console.Write(result.ToStringTree());

From here, you will need to traverse the parse tree with listener(default) or visitor. For the more information, please following the references.



Useful References

Thank you for following this post. Now, I say I’m a novice too. But, here in this post, I want to clear up how to set ANTLR with Visual Studio, C#. Since every information is distributed sporadically, I would like to summarize it. Anyway here is a handy list that makes you go further.

  • Here is one awesome, big, humongous, fantastic article. ANTLR mega tutorial.

  • An advanced concept, creating AST tree with ANTLR4. Go


This post might have an incorrect grammar. Please feel free to report this by replying. English is my second language. So, I take care not to make wrong and also try to get better. I used Grammarly for correct some of the errors, but I know this post might have strange sentence or expression. Thank you