2
我使用Antlr4解析C代码,我使用下面的语法解析:获取预处理线与解析C代码antlr4
默认情况下上面的语法不提供任何解析规则获得预处理器语句。
我改变了语法稍微得到通过添加以下行
externalDeclaration
: functionDefinition
| declaration
| ';' // stray ;
| preprocessorDeclaration
;
preprocessorDeclaration
: PreprocessorBlock
;
PreprocessorBlock
: '#' ~[\r\n]*
-> channel(HIDDEN)
;
而且在Java中,我使用下面的听众得到预处理线
@Override
public void enterPreprocessorDeclaration(PreprocessorDeclarationContext ctx) {
System.out.println("Preprocessor Directive found");
System.out.println("Preprocessor: " + parser.getTokenStream().getText(ctx));
}
的方法是预处理线从未触发。有人可以提出一种获得预处理器行的方法吗?
输入:
#include <stdio.h>
int k = 10;
int f(int a, int b){
int i;
for(i = 0; i < 5; i++){
printf("%d", i);
}
}
语法在给出的链接中。 –
您能否提供至少一行您尝试解析的输入,从#开始? – BernardK
#include int main(){ int a = 5; } –