【发布时间】:2014-02-05 21:23:42
【问题描述】:
我已经编写了一个编译器程序,现在我正在尝试从终端运行它。我在我的程序中使用 antlr 库。这段代码在使用 eclipse 编译时运行良好。
我在终端中使用以下命令
javac -classpath antlr-runtime-3.2.jar Main.java
文件 antlr-runtime-3.2.jar 存在,但我仍然收到以下错误
- Main.java:34: 错误:找不到符号 MiniJavaLexer lexer = new MiniJavaLexer(charStream)
- Main.java:42:错误:找不到符号 PrintVisitor dfsPrint = new PrintVisitor()
- Main.java:46: 错误:找不到符号 SymbolTableVisitor stVisitor = new SymbolTableVisitor()
我正在使用的每个课程都会出现此错误。
我想编译然后使用终端运行程序
main.java 文件包含
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();
}
}
【问题讨论】:
-
你不要导入它们...
-
@DaveNewton 我没有导入它们,因为它们在同一个包中。
-
那你的编译类路径不对。
标签: java eclipse macos compiler-construction terminal