Using ANTLR with python
Viresh Gupta (~virresh) |
This talk aims at introducing ANTLR for python 3, and talk about Abstract Syntax Trees. It will present an overview of the process, the intricacies and will end with a concrete example to show the utility.
ANTLRv4 is a tool that can generate parse trees for any compatible grammar, and provide tools to walk through that tree, so I will illustrate how to use that rather than dwelling more on the theory aspect of the parse trees and boost up the development of language tools.
There is a speciality with ANTLRv4, we can separate context from the grammar (so we can get very close to the expectation that grammars are context free).
I expect the session to be beginner friendly so no pre-requisites save some basic python expected. Also I will cover some basic examples, and also a demo of an actual language grammar to create a meta-program if time permits.
The session is expected to have the following things:
- What is a grammar ?
- What are Parse trees and how do they compare to ASTs ?
- What is ANTLR ? (The parser generator and the runtime provided)
- How do we use a parse tree ? (dwelling on setting up the environment for ANTLR based development and a short, basic calculator building example)
- Visitors and Listeners
- A short real world example on detecting technical constricts in actual programming languages (probably Python itself)
A working knowledge of python basics and some familiarity with some sort of command line interface is ideal
(best suited if you are familiar with any unix/linux based systems, simple script invocation etc)
A library for ANTLR that is being built by me is available here: https://gitlab.com/virresh/coala-antlr
ANTLR's official page: http://www.antlr.org/
My blogs related to ANTLR in Python: https://virresh.wordpress.com/tag/antlr/
An example calculator: https://github.com/virresh/ANTLR4-Example
I'm a student presently pursuing BTech in CSE at IIIT-Delhi, and am a GSoC student this year at coala.io and have been programming various stuff using python for around two years.
I am developing a library to facilitate easy usage of ANTLR for building linting tools.
I've worked on a large array of technologies in any area that I get to know about, ranging from Full stack development, to Systems programming to Language tools. I do my best to pick up and experiment with whatever technologies I can, and I love to learn !