If you use grammars-v4/cpp, make sure that your input to the parser is output from the C++ preprocessor (g++ -E file.cc
). The grammar cannot parse code that contains macros.
To find the functions that call some nested function, you will want to find postfix_expression corresponding to the following XPath2 expression: //postfix_expression[expression_list//postfix_expression//Identifier]
. In English, you will want to find postfix_expression nodes that contains an immediate expression_list child that contains a postfix_expression that contains an Identifier.
This is easy to implement using my Trash toolkit for Antlr4 parsing. It implements XPath2 searching in Antlr4 parse trees. Or, you can do this by writing an Antlr4 visitor that traverses a parse tree, starting at an Identifier terminal node then goes up the tree looking for postfix_expression/expression_list/postfix_expression in the parent chain.
So, for your input, using the command trparse tst.txt | trxgrep ' //postfix_expression[expression_list//postfix_expression//Identifier]' | trtext
, the output is:
bar(test())
foo(bar(test()))
Note, the cpp grammar is only good for the Java target. I have a port to C#, which is a requirement for Trash. Let me know if you need that grammar.