ANTLR logo image is from. click
Visual Studio logo image is from. click
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
Step by Step - wiring up ANTLR4 on Visual Studio
Step by Step - Generating Parser
(C# Code) Practice - how to parse?
What is ANTLR4?
Open source parser generator.
I quote an article because it’s out of topic.
Parsing your own language with ANTLR4 by François Wouts
The official homepage for ANTLR4.
Requirements
Visual Studio (I use visual studio 2013)
JAVA 8
Step by Step - Wiring up ANTLR4
1. Prepare the Requirements.
2. Create a new project.
3. Run NuGet package downloader.
- 4. Search "antlr".
- 5. Install "antlr".
- 6. Make sure you have the right one.
- 7. Import
Step by Step - Generating Parser
- 1. You need to get ANTLR4 .jar file to create your parser.
Where can I get it? Here.
Do you need another version? Here.
- 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.
- 3. How to use it?
java -jar antlr-4.7.1-complete.jar -Dlanguage=CSharp Scala.g4
- 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
'잡다한 정보 Miscellaneous' 카테고리의 다른 글
Land your personal github webpage within 1 h (0) | 2018.04.15 |
---|---|
Where do you get information? (0) | 2018.04.14 |
Google search results shortcuts, chrome plugin search without click (0) | 2018.04.08 |
JAVA to write MS word file, make columns in the page (0) | 2013.11.05 |
Git에 대해 알아보자. (0) | 2013.09.29 |