0

I have written a compiler program and now I am trying to run it from terminal. I am using antlr library inside my program. This code works pretty well when compiled using eclipse.

I am using the following command in terminal

javac -classpath antlr-runtime-3.2.jar Main.java

The file antlr-runtime-3.2.jar is present but still I am receiving the following errors

  • Main.java:34: error: cannot find symbol MiniJavaLexer lexer = new MiniJavaLexer(charStream)
  • Main.java:42: error: cannot find symbol PrintVisitor dfsPrint = new PrintVisitor()
  • Main.java:46: error: cannot find symbol SymbolTableVisitor stVisitor = new SymbolTableVisitor()

This error is coming for every class I am using.

I want to compile and then run the program using terminal

The main.java file contains

package mini.java.compiler;

import java.io.IOException;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.Tree;

public class Main {

    public static void main(String[] args) throws RecognitionException,
            IOException {
        // TODO Auto-generated method stub
        String file = "samples/factorial.java";

        CharStream charStream = new ANTLRFileStream(file);
        MiniJavaLexer lexer = new MiniJavaLexer(charStream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        MiniJavaParser parser = new MiniJavaParser(tokens);
        MiniJavaParser.goal_return res = parser.goal();

        Tree tree = (Tree) res.getTree(); // The root node.

        /* Print tree */
        PrintVisitor dfsPrint = new PrintVisitor();
        dfsPrint.visit(tree);

        /* Symbol table construction */
        SymbolTableVisitor stVisitor = new SymbolTableVisitor();
        SymbolTable symTab = (SymbolTable) stVisitor.visit(tree);
        System.out.println("-------------------");
        System.out.println("Symbol Table");
        System.out.println("-------------------");
        symTab.printTable();
        symTab.resetTable();

        /* Type checking */
        TypeCheckingVisitor tcVisitor = new TypeCheckingVisitor(symTab);
        tcVisitor.visit(tree);
        symTab.resetTable();

        /* Byte Code Generation */
        CodeGenerationVisitor cgVisitor = new CodeGenerationVisitor(symTab);
        ClassFile cf = (ClassFile) cgVisitor.visit(tree);
        System.out.println("-------------------");
        System.out.println("Stack Machine Code");
        System.out.println("-------------------");
        cf.print();
        cf.writeToFile();
    }
}
3
  • 1
    You don't import them... Commented Jan 16, 2014 at 19:16
  • @DaveNewton I did not import them because they are in the same package. Commented Jan 17, 2014 at 19:32
  • 1
    Then your compilation classpath is incorrect. Commented Jan 17, 2014 at 20:05

1 Answer 1

1

Looks like MiniJavaLexer and MiniJavaParser are not on your class path. Just make sure they do and try to compile again.

Looks like they are declared in the same package as there are no imports - that's why you are able to run this in eclipse.

Say, those classes are already compiled and are near Main.java try running javac -classpath .:antlr-runtime-3.2.jar Main.java - (use ; to separate paths on Windows and : on Unix)

If you only have sources for them they need to be compiled to.

One reason they may not be here with ANTLR is that if you are using grammar files to declare lexer and parser and thus first must generate those classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.