This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




7年前关闭。




如果我遍历C++源文件并仅查找变量类型,然后再查找正确的C++变量名称,是否会导致缺少变量或错误变量的情况而需要克服适当的解析器?

一个简单的测试用例正确地捕获了所有原语,但是如果有很多我没有考虑的无关紧要的案例,我宁愿不花太多时间。到目前为止,我只是在寻找关键字,获取下一个单词并检查名称的有效性。除了检查变量名的有效性外,我没有使用正则表达式,并且我知道正则表达式不足以进行解析。

我的程序是用Java编写的。我做了该程序的Java目标版本,并使用了JavaParser,效果很好。我认为,仅在源代码中查找原始变量时,ANTLR或类似的代码就显得过分杀伤力。

最佳答案

您是否尝试过在一行中声明多个变量?

int i, *j, k[2];

函数参数声明呢?
int func(int i, int j);

那么typedef呢?
typedef int Integer;
Integer i;

没有适当的解析器(和相当复杂的解析器)就很难解析C++。

09-10 03:32
查看更多