Antlr: Writing a Cassandra Parser

This article is going to be about antlr (ANother Tool for Language Recognition) and how do we build a parser that parses Cassandra Query Language(CQL) for us. Antlr is used to build languages(like CQL). Antlr generates a powerful parser using the grammar of a language that can build and walk the parse trees.

I hope you guys are having a nice time. I am on a 1 week vacation, I wanted to distract myself from the things I am usually engrossed in and explore on a lot of other things. I will be reading and writing a lot during these days. So please follow this blog to stay updated.

Here is how you can get started on antlr on OSX –

$ cd /usr/local/lib
$ sudo curl -O https://www.antlr.org/download/antlr-4.7.2-complete.jar
$ export CLASSPATH=".:/usr/local/lib/antlr-4.7.2-complete.jar:$CLASSPATH"
$ alias antlr4='java -jar /usr/local/lib/antlr-4.7.2-complete.jar'
$ alias grun='java org.antlr.v4.gui.TestRig'

Great! Now we have antlr installed in our system.

Now we will get the grammar of cassandra query language –

$ git clone https://github.com/antlr/grammars-v4.git

$ cd grammars-v4

$ cd cql3/

antlr4 Cql*.g4

javac Cql*.java

echo “SELECT * FROM temp” | grun Cql root -gui

That is all!

You will get following result –

antlr4_parse_tree.png

Yay! We now have a parser cql !

Note – You can use antlr to design your own language. Also you can use antlr to do language translation. Can you create a sql to cql translator?

If you are interested, then here are some references which can help you learn more about antlr –

One thought on “Antlr: Writing a Cassandra Parser

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s