Saturday, April 7, 2018

ANTLR

http://www.antlr.org/




Source code: https://pragprog.com/titles/tpantlr2/source_code

(I am using GIT bash)
on Windows:
export CLASSPATH=/d/pierre/downloads/antlr/antlr-4.0-complete.jar:.
alias antlr="java -jar /d/pierre/downloads/antlr/antlr-4.0-complete.jar"
alias grun='java org.antlr.v4.runtime.misc.TestRig'

on Linux
export CLASSPATH=/home/centos/antlr/antlr-4.0-complete.jar:.
alias grun='java -cp /home/centos/antlr/antlr-4.0-complete.jar:. org.antlr.v4.runtime.misc.TestRig'


grun Hello r -tokens

I could not find which EOF key to press on Windows (it's CTRL-D in Linux).



If more people knew how to write a grammar and a parser, we probably would not be wasting so much of our life manually sorting horrendous Maven pom.xml files, or hacking HTML pages embedded with all sort of obscure hocus-pocus additions, or even typing unreadable GIT commands to undo a commit.

But we don't want to be monkeys, we want to learn the fundamentals of our profession : elaborate languages to explain, in a clear concise and unambiguous way, what we expect from a computing machine.



Troubleshooting:


If you get
java.lang.NullPointerException
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:311)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.antlr.v4.runtime.misc.TestRig.process(TestRig.java:157)
at org.antlr.v4.runtime.misc.TestRig.main(TestRig.java:142)

check this:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/misc/Launcher.java?av=f

public Class<?>  [More ...] loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
int i = name.lastIndexOf('.');
if (i != -1) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPackageAccess(name.substring(0, i));
}
}

and this http://grepcode.com/file/repo1.maven.org/maven2/org.antlr/antlr4/4.0/org/antlr/v4/runtime/misc/TestRig.java?av=f

try {
lexerClass = cl.loadClass(lexerName).asSubclass(Lexer.class);
}

At this point I am pretty hopeless.... just ignore this issue, when you run grun with parameters, it works.

See https://github.com/antlr/antlr4/issues/174


No comments: