在ANTLR BNF语法符号中,epsilon的等价物是什么? [英] What is the equivalent for epsilon in ANTLR BNF grammar notation?

查看:289
本文介绍了在ANTLR BNF语法符号中,epsilon的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在利用ANTLR 3.3时,我正在改变当前语法以支持没有括号的输入。这是我的语法的第一个版本:

During taking advantage of ANTLR 3.3, I'm changing the current grammar to support inputs without parenthesis too. Here's the first version of my grammar :

grammar PropLogic;

        NOT : '!' ;
        OR  : '+' ;
        AND : '.' ;
        IMPLIES : '->' ;
        SYMBOLS : ('a'..'z') | '~' ;
        OP : '(' ;
        CP : ')' ;

    prog    : formula EOF ;


    formula : NOT formula
        | OP formula( AND formula CP | OR formula CP | IMPLIES formula CP)
        | SYMBOLS ;


    WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+    { $channel = HIDDEN; } ;

然后我改变它以支持相应的功能:

Then I changed it this way to support the appropriate features :

grammar PropLogic;

    NOT : '!' ;
    OR  : '+' ;
    AND : '.' ;
    IMPLIES : '->' ;
    SYMBOL : ('a'..'z') | '~' ;
    OP : '(' ;
    CP : ')' ;
    EM : '' ;

prog    : formula EOF ;


formula : OP formula( AND formula CP | OR formula CP | IMPLIES formula CP)
    | ( NOT formula | SYMBOL )( AND formula | OR formula | IMPLIES formula | EM ) ;


WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+    { $channel = HIDDEN; } ;

但我遇到了以下错误:

error<100>:  syntax error: invalid char literal: ''
error<100>:  syntax error: invalid char literal: ''

有人知道我怎样才能克服这个错误?

Does anybody know that how can I overcome this error?

推荐答案

您的 EM 令牌:

EM : '' ;

无效:您无法匹配词法规则中的空字符串。

is invalid: you can't match an empty string in lexer rules.

要匹配epsilon(无),你应该这样做:

To match epsilon (nothing), you should do:

rule 
  :  A 
  |  B 
  |  /* epsilon */ 
  ;

当然,评论 / * epsilon * / 可以安全地删除。

Of course, the comment /* epsilon */ can safely be removed.

请注意,当你在当前语法中这样做时,ANTLR会抱怨可以使用多个替代方案匹配规则。这是因为你的语法含糊不清。

Note that when you do it like that in your current grammar, ANTLR will complain that there can be rules matched using multiple alternatives. This is because your grammar is ambiguous.

这篇关于在ANTLR BNF语法符号中,epsilon的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆