[antlr-interest] Info extraction, C grammar

Stuart Maclean stuart at apl.washington.edu
Wed Sep 26 12:55:57 PDT 2012


I have the C.g grammar for Antlr able to parse some C code I have.  I 
now want to extract function signatures.  So given input

int f(char c);

I want to extract

return type = int

param list = one param, whose type is 'char' and whose name is 'c'.  The 
end goal after extraction is to output more C code. Of course C's syntax 
can be gruesome to parse, so from the declaration

void (*f(int i ))(int);

I want to extract "f is the function name.  Its sole parameter is of 
type int and is called i, and its return type is void (*)(int)".

I am not sure how to go about this. Do I need AST and tree grammars,or 
templates?  Given my intended generation of C source code, it seems that 
templates would be useful?  But given the complexity of the C language 
and hence its grammar, it also seems like tree walking will be needed.

I have looked at examples-v3/java/Cminus.g and associated files and can 
see how the outer rule (function in that case) can set up scopes for 
nested rules, but am not sure if this is the way to go.

For debug and general education, I have been using the -debug option and 
printing out the parse tree. In the worst case I could walk this (large) 
string by hand, pulling out nodes of interest, but my inkling is that 
AST and/or templates are the 'right' way to do this??

Any help gratefully appreciated.

stu


More information about the antlr-interest mailing list