Функциональные вызовы распознаются по правилу postfixexpression
:
postfixexpression
: primaryexpression
| postfixexpression '[' expression ']'
| postfixexpression '[' bracedinitlist ']'
| postfixexpression '(' expressionlist? ')' // <---- this alternative!
| simpletypespecifier '(' expressionlist? ')'
| typenamespecifier '(' expressionlist? ')'
| simpletypespecifier bracedinitlist
| typenamespecifier bracedinitlist
| postfixexpression '.' Template? idexpression
| postfixexpression '->' Template? idexpression
| postfixexpression '.' pseudodestructorname
| postfixexpression '->' pseudodestructorname
| postfixexpression '++'
| postfixexpression '--'
| Dynamic_cast '<' thetypeid '>' '(' expression ')'
| Static_cast '<' thetypeid '>' '(' expression ')'
| Reinterpret_cast '<' thetypeid '>' '(' expression ')'
| Const_cast '<' thetypeid '>' '(' expression ')'
| typeidofthetypeid '(' expression ')'
| typeidofthetypeid '(' thetypeid ')'
;
Так что, если вы добавите это к своему посетителю:
def visitPostfixexpression(self, ctx:CPP14Parser.PostfixexpressionContext):
print(ctx.getText())
return self.visitChildren(ctx)
Он будет напечатан. Обратите внимание, что теперь он будет печатать намного больше, чем вызовы функций, поскольку он соответствует гораздо большему. Вы можете пометить альтернативы :
postfixexpression
: primaryexpression #otherPostfixexpression
| postfixexpression '[' expression ']' #otherPostfixexpression
| postfixexpression '[' bracedinitlist ']' #otherPostfixexpression
| postfixexpression '(' expressionlist? ')' #functionCallPostfixexpression
| simpletypespecifier '(' expressionlist? ')' #otherPostfixexpression
| typenamespecifier '(' expressionlist? ')' #otherPostfixexpression
| simpletypespecifier bracedinitlist #otherPostfixexpression
| typenamespecifier bracedinitlist #otherPostfixexpression
| postfixexpression '.' Template? idexpression #otherPostfixexpression
| postfixexpression '->' Template? idexpression #otherPostfixexpression
| postfixexpression '.' pseudodestructorname #otherPostfixexpression
| postfixexpression '->' pseudodestructorname #otherPostfixexpression
| postfixexpression '++' #otherPostfixexpression
| postfixexpression '--' #otherPostfixexpression
| Dynamic_cast '<' thetypeid '>' '(' expression ')' #otherPostfixexpression
| Static_cast '<' thetypeid '>' '(' expression ')' #otherPostfixexpression
| Reinterpret_cast '<' thetypeid '>' '(' expression ')' #otherPostfixexpression
| Const_cast '<' thetypeid '>' '(' expression ')' #otherPostfixexpression
| typeidofthetypeid '(' expression ')' #otherPostfixexpression
| typeidofthetypeid '(' thetypeid ')' #otherPostfixexpression
;
и затем сделать:
def visitFunctionCallPostfixexpression(self, ctx:CPP14Parser.FunctionCallPostfixexpressionContext):
print(ctx.getText())
return self.visitChildren(ctx)
, и тогда будет напечатан только foo(1,3)
(обратите внимание, что вы можете захотеть пометить больше правил как functionCallPostfixexpression
внутри правила postfixexpression
).
Есть ли способ вывести дерево ANTLR как AST в формате JSON?
Нет.
Но вы, конечно, можете легко создать что-то самостоятельно: объекты, возвращаемые каждым правилом синтаксического анализатора, например translationunit
, содержат все дерево. Быстрый и грязный пример:
import antlr4
from antlr4.tree.Tree import TerminalNodeImpl
import json
# import CPP14Lexer, CPP14Parser, ...
def to_dict(root):
obj = {}
_fill(obj, root)
return obj
def _fill(obj, node):
if isinstance(node, TerminalNodeImpl):
obj["type"] = node.symbol.type
obj["text"] = node.getText()
return
class_name = type(node).__name__.replace('Context', '')
rule_name = '{}{}'.format(class_name[0].lower(), class_name[1:])
arr = []
obj[rule_name] = arr
for child_node in node.children:
child_obj = {}
arr.append(child_obj)
_fill(child_obj, child_node)
if __name__ == '__main__':
source = """
#include <iostream>
using namespace std;
int foo(int i, int i2)
{
return i * i2;
}
int main(int argc, char *argv[])
{
cout << "test" << endl;
foo(1, 3);
return 0;
}
"""
lexer = CPP14Lexer(antlr4.InputStream(source))
parser = CPP14Parser(antlr4.CommonTokenStream(lexer))
tree = parser.translationunit()
tree_dict = to_dict(tree)
json_str = json.dumps(tree_dict, indent=2)
print(json_str)