1

I need some guidance parsing a '.drl' AKA a Drools Rule File. A drl file is defined by ANTLR3 grammar which can be found here. I have previously worked with g4 grammar using ANTLR v4 grammar plugin for IntelliJ , I could easily generate i) Parse tree for a sample file AND ii) A visitor.java file for that particular grammar.

I need to generate the above two for ANTLR3 as well, i.e i) Parse Tree for Sample.drl file ii) A visitor.java file for DroolsRule.g which can take in Sample.java as input for parsing.

I have attempted to generate a .dot file for an AST by following steps for Bart Kiers answer to this question. But while attempting to generate lexers and parsers in step 1 as mentioned like

java -cp antlr-3.3.jar org.antlr.Tool Java.g

I get the following error

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: antlr/ANTLRException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: antlr.ANTLRException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

Also, i could not find if it is possible to generate a visitor.java. like function for DroolsRule.g using ANTLR3.

95_96
  • 341
  • 2
  • 12
  • ANTLRException is Antlr2. Try 2.7.7. rather than 3.3. Alternatively you could take a look at the actual official drools grammars: https://github.com/kiegroup/drools/tree/master/drools-compiler/src/main/resources/org/drools/compiler/lang – Roddy of the Frozen Peas Mar 24 '20 at 01:29
  • So basically you want to parse the drool source code using ANTRL3 instead of ANTLR4? @95_96 – Abhishek Attri Mar 24 '20 at 08:41
  • @AbhishekAttri Yes, i am not able to figure out if there is a way to generate visitor.java class similar to how we can do for ANTLR4 grammar – 95_96 Mar 24 '20 at 08:46
  • @RoddyoftheFrozenPeas I am pretty sure the grammar i used is ANTLR3, I used the grammar DRL6Expressions.g which is in the official Drools Repo you have mentioned. On trying with antlr-2.7.7. it gives me `DRL6Expression.g:24:1: unexpected char: '@'` error. Also , as a side note, I am not too familiar with writing an antlr grammar, my use case bends more towards traversing through the parse tree generated for a drools file. – 95_96 Mar 24 '20 at 09:15

0 Answers0