6
votes

I am trying to run ANTLR C grammar file (DummyC.g) from command line to parse C source and header files (a.h). When I run it with antlr.jar file, it generates parser and lexer files. but when I compile test file Main.java. It gives error of missing ANTLR packages as shown below.

C:\antlr-2.7.6\test>javac Main.java

Main.java:1: package org.antlr.tool does not exist import org.antlr.tool.;
^ Main.java:2: package org.antlr.runtime does not exist import org.antlr.runtime.
;
^ Main.java:3: package org.antlr.runtime.tree does not exist import org.antlr.runtime.tree.;
^ Main.java:4: package org.antlr.stringtemplate does not exist import org.antlr.stringtemplate.
;
^ Main.java:8: cannot find symbol symbol : class CommonTree location: class Main CommonTree tree = DummyCParser.start("a.h");

Main.java

import org.antlr.tool.*;   
import org.antlr.runtime.*;   
import org.antlr.runtime.tree.*;   
import org.antlr.stringtemplate.*;   

public class Main {   
    public static void main(String[] args) throws Exception {   
        CommonTree tree = DummyCParser.start("a.h");    
        DOTTreeGenerator gen = new DOTTreeGenerator();   
        StringTemplate st = gen.toDOT(tree);    
        System.out.println(st);   
      }  
}  

What could be the problem?

1
Why don't you download a free IDE such as Eclipse or intelliJ It will help you see visually where there are problems. In this case you probably need to refer to the location of the relevant jars on the classpath when you compile.David Victor
Also, I previously recommended you to go through my other ANTLR answers to see how to compile classes, but nowhere did I suggest to compile it like you're trying to do now.Bart Kiers
@Bart I managed to make (grammar) run and store it in .dot file using help in your previous ANTLR examples and later viewed it online using graph.gafol.net. But I am not able to run your grammar with ANTLRworks-3.2. It generates lexer/parser files but when I use debug option it couldn't find javac compiler, though I have given the path.user628127
@user628127, I'm pretty sure that the reason you're having all this trouble is because you are trying to run before being able to crawl. You've absolutely no idea what the grammar you've been handed does, or how the tools surrounding it actually work. Sorry to sound so harsh, but I highly recommend you stop what you're doing, and start with the very basics of ANTLR. Whatever you do, best of luck, I can't assist you anymore.Bart Kiers

1 Answers

6
votes

You're using the antlr runtime so you'll have to specify the antlr jar files as part of the classpath so the compiler can find the antlr classes you use e.g.

   javac -classpath c:\java\antlr-3.3\lib\antlr-3.3-complete.jar Main.java