我在Python解析器中使用ANTLR,但是我发现解析器不能识别注解。因为我需要识别解析代码中的注解,所以有没有办法解决这个问题?
我已经使用这个方法生成了我的解析器:I'm trying to generate the parse tree for Antlr4 Python3.g4 grammar file, to parse python3 code
下面是测试代码的示例:
class myClass:
x=5
print("hello world")
#Comment
Python解析器的结果
(file_input (stmt (compound_stmt (classdef class (name myClass) : (block (stmt (simple_stmts (simple_stmt (expr_stmt (testlist_star_expr (test (or_test (and_test (not_test (comparison (expr (xor_expr (and_expr (shift_expr (arith_expr (term (factor (power (atom_expr (atom (name x))))))))))))))))) = (testlist_star_expr (test (or_test (and_test (not_test (comparison (expr (xor_expr (and_expr (shift_expr (arith_expr (term (factor (power (atom_expr (atom 5)))))))))))))))))) )) (stmt (simple_stmts (simple_stmt (expr_stmt (testlist_star_expr (test (or_test (and_test (not_test (comparison (expr (xor_expr (and_expr (shift_expr (arith_expr (term (factor (power (atom_expr (atom (name print)) (trailer ( (arglist (argument (test (or_test (and_test (not_test (comparison (expr (xor_expr (and_expr (shift_expr (arith_expr (term (factor (power (atom_expr (atom "hello world"))))))))))))))))) ))))))))))))))))))) \n)) \n)))) <EOF>)
正如您所看到的,#comment代码行不在解析树中,但我希望它在解析树中
1条答案
按热度按时间sg3maiej1#
正如您在使用的lexer语法中所看到的:
标记化阶段跳过所有注解。这意味着解析器永远不会“看到”这些
COMMENT
标记。如果您想保留这些COMMENT
标记,您必须将其从SKIP_
规则中删除(并删除fragment
关键字):然而,当你这样做的时候,你需要调整许多解析器规则来接受这些额外的
COMMENT
标记,这将是一项相当艰巨的任务,要考虑到所有可能出现注解的情况。