如何编译carc++;eclipse中java程序中的代码并进行错误记录? 我必须开发一个java程序,它以C或C++文件作为输入,编译文件并给出程序中发现的错误的位置、位置、类型。我更关心语法错误。我需要一种在EclipseJava中实现这一点的方法。我已经安装了EclipseCDT插件,我不知道是否可以从java程序内部调用编译器并获得行号、位置和位置
我已经尝试安装了一个编译器,即MinGW编译器,并且能够打印错误,但是我没有得到我想要的错误。 这是我尝试的链接如何编译carc++;eclipse中java程序中的代码并进行错误记录? 我必须开发一个java程序,它以C或C++文件作为输入,编译文件并给出程序中发现的错误的位置、位置、类型。我更关心语法错误。我需要一种在EclipseJava中实现这一点的方法。我已经安装了EclipseCDT插件,我不知道是否可以从java程序内部调用编译器并获得行号、位置和位置,java,eclipse,compiler-errors,mingw,Java,Eclipse,Compiler Errors,Mingw,我已经尝试安装了一个编译器,即MinGW编译器,并且能够打印错误,但是我没有得到我想要的错误。 这是我尝试的链接 有人能帮我捕获数组中C程序中发现的语法错误的行号和位置吗?您必须解析来自stdout和stderr的结果。在您链接的问题中,您可以找到如何访问它们的解决方案 您还提到希望有一个编译器错误的侦听器 public interface CompilerErrorListener { public void onSyntaxError(String filename, String
有人能帮我捕获数组中C程序中发现的语法错误的行号和位置吗?您必须解析来自
stdout
和stderr
的结果。在您链接的问题中,您可以找到如何访问它们的解决方案
您还提到希望有一个编译器错误的侦听器
public interface CompilerErrorListener {
public void onSyntaxError(String filename, String functionName,
int lineNumber, int position, String message);
public void invalidOutputLine(String line);
}
您可以通过任何实现来实现这一点:
public class CompilerErrorProcessor implements CompilerErrorListener {
public void onSyntaxError(String filename, String functionName,
int lineNumber, int position, String message) {
....
// your code here to process the errors, e.g. put them into an ArrayList
}
public void invalidOutputLine(String line) {
// your error handling here
}
}
那么,让我们来解析编译器输出!(注意,这取决于编译器的输出,您只提供了2行输出)
这不是一个完整的例子,当然,可能还有更多的可能性,但希望你能理解。不要忘记记录无效/未处理的分支
由于使用特定的编译器,因此必须适应其输出。此外,编译器版本的更改可能会导致解析代码的更改。要特别注意发现输出的结构,当你想解析它时,它会给你带来回报。如果你必须自己编写编译器,那么
Runtime.getRuntime().exec()
方法不是正确的方向,然后花点时间编写词法分析器,等等。如果你可以使用外部编译器,谷歌一个为您的平台,产生预期的结果。不,我不必写编译器。我想有一个编译器来扫描我的C代码,并有一种方法来捕获所有的诊断。你能帮我一下吗?请发布一个编译器输出示例,并解释更多你想要的。我的问题上面有一个链接。我已经运行了上面链接中的程序,并安装了MinGW编译器。它工作的很好,但我得到的错误在一个不适当的方式。我刚收到最后一行电话号码和一堆信息。我需要捕获文件中每个错误的行号和位置,并将它们输入到一个数组中以供以后处理。我问这个问题是希望有一种新的方法来解决我的问题。注:请查阅链接。谢谢。我在那里只能看到两行示例编译器输出。
BufferedReader stdError = ...
String line;
CompilerErrorListener listener = new CompilerErrorProcessor(...);
String fileName = "";
String functionName = "";
int lineNumber = -1;
int position = -1;
String message = null;
while ((line = stdError.readLine()) != null) {
// the line always starts with "filename.c:"
String[] a1 = line.split(":", 2);
if (a1.length == 2) {
// a1[0] is the filename, a1[1] is the rest
if (! a1[0].equals(fileName)) {
// on new file
fileName = a1[0];
functionName = "";
}
// here is the compiler message
String msg = a1[1];
if (msg.startsWith("In ")) {
// "In function 'main':" - we cut the text between '-s
String[] a2 = msg.split("'");
if (a2.length == 3) {
functionName = a2[1];
} else {
listener.invalidOutputLine(line);
}
} else {
// "9:1: error: expected ';' before '}' token"
String[] a2 = msg.split(":", 3);
if (a2.length == 3) {
lineNumber = Integer.parseInt(a2[0]);
position = Integer.parseInt(a2[1]);
message = a2[2];
// Notify the listener about the error:
listener.onSyntaxError(filename, functionName, lineNumber, position, message);
} else {
listener.invalidOutputLine(line);
}
}
} else {
listener.invalidOutputLine(line);
}
}