How to write grammar for your own language with ANTLR .? (Part -I) : Setting up the Environment( Intellij with ANTLR)


Ever wondered about this question that actually how can we write our own language?

How the grammer of clojure or scala can be written.?

Here is the answer , we can write grammar for our own language using ANTLR. This is a powerful tool for generating Lexer, Parser and  Visitors for the grammar written by our self.

So in this blog we will discuss how to configure ANTLR with our Intellij for getting started to write our own language.

So these are the steps for adding plugins to your Intellij Idea so that you can work with ANTLR and generate your own lexers, parser and visitors:

  1. Download plugin from here. So that your intellij idea may recognize .g4 file.
  2. Add this plugin to your intellij using the Plugins option in your settings.
  3. Now is the time for adding a plugin for generating the lexer and parser. This is a great plugin named ANTLR Works. This plugin allows you to generate the parser, lexer and the visitor with a small change in the configuration. The plugin will be added to the idea same as the above.
  4. Now restart your Intellij Idea and then start configuring your antlr works.

Steps for configuring your Antlr plugin:

  1. Navigate to Tools and then to Configure ANTLR.
  2. A box would appear like this:Antlr
  3. Add the details like where you want the output like the files for Lexer and Parser, the location of the grammar file (.g4 file) .
  4. Leave the Grammar file encoding, if you have not used any encoding.
  5. Add the package name that you want to see in the Java file in which the lexer and parser files will be created.
  6. Add the Language in which you want the output like  Java , Python etc.
  7. Tick the generate parser tree listener and generate tree visitor if you want to modify the visitor.
  8. Now the configuration is done.

Now you are fully ready to write your g4 file and play around with it. In further blogs we will explain how to write a grammar file.

Here are some samples of the grammar file ,take a look here and later in the blogs we will discuss how to write one of our own.

If you want to know how can we start writing scala itself you can take a look here.

Till then Happy Reading ! 🙂

Advertisements

About shiv4nsh

Coder, Gamer, Learner..!!
This entry was posted in Scala and tagged , , , , , , , , , , . Bookmark the permalink.

2 Responses to How to write grammar for your own language with ANTLR .? (Part -I) : Setting up the Environment( Intellij with ANTLR)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s