pyVision
A Machine Learning and Signal Processing toolbox

Download .zip Download.tar.gz View on GitHub


ANTLR installation and usage on ubuntu 14.04

ANTLR installation and usage on ubuntu 14.04

In this article we will look at installation and usage of ANTLRsoftware

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It’s widely used to build languages, tools, and frameworks.

Download

Antlr repository can be downloaded by running the below commands

git clone https://github.com/antlr/antlr4.git

Pre Requisites

Antlr requires Apache maven build manager for Java projects.

sudo apt-get install maven maven2

python 3.5

  • Download the source files from
https://www.python.org/downloads/source/
  • build and install python 3.5
./configure
make
sudo make install

Compile

Antlr can be compiled by executing the below command

mvn compile
mvn install

This will deploy antlr in the default location

~/.m2/repository

The main antlr jar files is located at

~/.m2/repository/org/antlr/antlr4/4.5/antlr4-4.5.jar

to use this we need to set classpath as follows

export CLASSPATH=$CLASSPATH:~/.m2/repository/org/antlr/antlr4/4.5/antlr4-4.5.jar

Execution

Put the following grammar inside file Hello.g4

// Define a grammar called Hello
grammar Hello;
r  : 'hello' ID ;         // match keyword hello followed by an identifier
ID : [a-z]+ ;             // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

Then run ANTLR the tool on it:

$ alias antlr4='java -Xmx500M -cp "~/.m2/repository/org/antlr/antlr4/4.5/antlr4-4.5.jar:$CLASSPATH" org.antlr.v4.Tool'

$ alias grun='java org.antlr.v4.runtime.misc.TestRig'

$ antlr4 Hello.g4
$ javac Hello*.java

To test it run the following command

$ grun Hello r -tree input.txt

contents of input.txt file is hello world

This will give a GUI with parse tree displayed in the output

That pops up a dialog box showing that rule r matched keyword hello followed by identifier parrt.

Reference

  • https://raw.githubusercontent.com/antlr/antlr4/master/doc/getting-started.md1
blog comments powered by Disqus