Get Preprocessor lines with antlr4 with parsing C

2020-07-24 05:02发布

问题:

I am using Antlr4 to parse C code and I am using the following grammar to parse:

Link to C.g4

The above grammar by default does not provide any parsing rules to get preprocessor statements.

I changed the grammar slightly to get the preprocessor lines by adding the following lines

externalDeclaration
:   functionDefinition
|   declaration
|   ';' // stray ;
|   preprocessorDeclaration
;

preprocessorDeclaration
:   PreprocessorBlock
;

PreprocessorBlock
:   '#' ~[\r\n]*
    -> channel(HIDDEN)
;

And in Java I am using the following listener to get the preprocessor lines

@Override
public void enterPreprocessorDeclaration(PreprocessorDeclarationContext ctx) {
    System.out.println("Preprocessor Directive found");
    System.out.println("Preprocessor: " + parser.getTokenStream().getText(ctx));
}

The method is never triggered. Can someone suggest a method to get the preprocessor lines?

Input:

#include <stdio.h>

int k = 10;
int f(int a, int b){
int i;
for(i = 0; i < 5; i++){
    printf("%d", i);
}

}

回答1:

Actually, with channel(HIDDEN), the rule preprocessorDeclaration produces no output.

If I remove -> channel(HIDDEN), it works :

preprocessorDeclaration
@after {System.out.println("Preprocessor found : " + $text);}
    :   PreprocessorBlock
    ;

PreprocessorBlock
    :   '#' ~[\r\n]*
//        -> channel(HIDDEN)
    ;

Execution :

$ grun C compilationUnit -tokens -diagnostics t2.text
[@0,0:17='#include <stdio.h>',<PreprocessorBlock>,1:0]
[@1,18:18='\n',<Newline>,channel=1,1:18]
[@2,19:19='\n',<Newline>,channel=1,2:0]
[@3,20:22='int',<'int'>,3:0]
...
[@72,115:114='<EOF>',<EOF>,10:0]
C last update 1159
Preprocessor found : #include <stdio.h>
line 4:11 reportAttemptingFullContext d=83 (parameterDeclaration), input='int a,'
line 4:11 reportAmbiguity d=83 (parameterDeclaration): ambigAlts={1, 2}, input='int a,'
...
#include <stdio.h>

int k = 10;
int f(int a, int b) {
    int i;
    for(i = 0; i < 5; i++) {
        printf("%d", i);
    }
}

In file CMyListener.java (from my previous answer) I have added :

public void enterPreprocessorDeclaration(CParser.PreprocessorDeclarationContext ctx) {
    System.out.println("Preprocessor Directive found");
    System.out.println("Preprocessor: " + parser.getTokenStream().getText(ctx));
}

Execution :

$ java test_c t2.text 
...
parsing ended
>>>> about to walk
Preprocessor Directive found
Preprocessor: #include <stdio.h>
>>> in CMyListener
#include <stdio.h>

int k = 10;
...
}


标签: java antlr4