我想在Python代码的ANTLR解析树中保留注解

7cwmlq89  于 2023-02-06  发布在  Python
关注(0)|答案(1)|浏览(123)

我在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代码行不在解析树中,但我希望它在解析树中

sg3maiej

sg3maiej1#

正如您在使用的lexer语法中所看到的:

SKIP_
 : ( SPACES | COMMENT | LINE_JOINING ) -> skip
 ;

fragment COMMENT
 : '#' ~[\r\n\f]*
 ;

标记化阶段跳过所有注解。这意味着解析器永远不会“看到”这些COMMENT标记。如果您想保留这些COMMENT标记,您必须将其从SKIP_规则中删除(并删除fragment关键字):

SKIP_
 : ( SPACES | LINE_JOINING ) -> skip
 ;

COMMENT
 : '#' ~[\r\n\f]*
 ;

然而,当你这样做的时候,你需要调整许多解析器规则来接受这些额外的COMMENT标记,这将是一项相当艰巨的任务,要考虑到所有可能出现注解的情况。

相关问题