ANTLR 语法中的歧义

发布于 2024-12-25 12:15:07 字数 1222 浏览 3 评论 0原文

AntlrWorks 表示输入 {'AND','OR'..'XOR'} 可以通过两个替代项进行匹配。即使有图形显示,我也无法弄清楚比赛是如何发生的! 下面的语法到底是如何出现歧义的,有没有办法消除它?

grammar testg;

rul :  contains_expr    ;

contains_expr: 'CONTAINS' contains_expression
                  //'CONTAINS' contains_or
        ;

contains_expression :  primary  (('OR'|'AND'|'XOR') primary)*
       ;

primary options{backtrack = true;}
 : '(' contains_expression ')'
 | class_expression
 ;


class_expression :   simple_class_expr 
           | '(' simple_class_expr contains_expr ')'
           |( simple_class_expr contains_expr) 
        ;

simple_class_expr: identifier               // RM_TYPE_NAME
               | identifier identifier      // RM_TYPE_NAME variable
               | archetype_class_expr
         | versioned_class_expression
         | version_class_expression 
         // | identified_obj_expression     // need to be used once VersionedClassExpr is removed
        ;

identifier
    :   ID
    ;

archetype_class_expr
    :   '.ace'
    ;

versioned_class_expression
    :   '.vce'
    ;

version_class_expression
    :   '.vnce'
    ;

temp    :   
        ;


ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

AntlrWorks says that input {'AND','OR'..'XOR'} can be matched by two alternatives. Even with the graphical display, I could not figure out how the match happens!
How on earth the ambiguity occurs in the grammar below, and is there a way to remove it?

grammar testg;

rul :  contains_expr    ;

contains_expr: 'CONTAINS' contains_expression
                  //'CONTAINS' contains_or
        ;

contains_expression :  primary  (('OR'|'AND'|'XOR') primary)*
       ;

primary options{backtrack = true;}
 : '(' contains_expression ')'
 | class_expression
 ;


class_expression :   simple_class_expr 
           | '(' simple_class_expr contains_expr ')'
           |( simple_class_expr contains_expr) 
        ;

simple_class_expr: identifier               // RM_TYPE_NAME
               | identifier identifier      // RM_TYPE_NAME variable
               | archetype_class_expr
         | versioned_class_expression
         | version_class_expression 
         // | identified_obj_expression     // need to be used once VersionedClassExpr is removed
        ;

identifier
    :   ID
    ;

archetype_class_expr
    :   '.ace'
    ;

versioned_class_expression
    :   '.vce'
    ;

version_class_expression
    :   '.vnce'
    ;

temp    :   
        ;


ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2025-01-01 12:15:07

您希望您的语法如何解析 CONTAINS foo bar baz

contains_exprCONTAINS 匹配。

contains_expression“调用”primary

primary“调用”class_expression

class_expression“调用”simple_class_expr

simple_class_expr 可以匹配:identifieridentifier 标识符

因此我可以在这里看到几种可能的解析;我已将各个 simple_class_expr 匹配放入括号中:

CONTAINS (foo bar) (baz)
CONTAINS (foo) (bar) (baz)
CONTAINS (foo) (bar baz)

很抱歉,我对解析工具还很陌生,除了想知道什么之外,没有关于如何修复这个问题的建议identifier 标识符 可能意味着

How would you expect your grammar to parse CONTAINS foo bar baz?

contains_expr matches CONTAINS.

contains_expression "calls" primary.

primary "calls" class_expression.

class_expression "calls" simple_class_expr.

simple_class_expr can match: identifier or identifier identifier.

Thus I can see several possible parsings here; I've put individual simple_class_expr matches into parenthesis:

CONTAINS (foo bar) (baz)
CONTAINS (foo) (bar) (baz)
CONTAINS (foo) (bar baz)

I'm sorry to say that I'm new enough to parsing tools to not have suggestions how to fix this except wondering what identifier identifier might mean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文