令牌与 antlr 语法谓词不匹配

发布于 2024-08-31 03:20:13 字数 422 浏览 3 评论 0原文

我的语法文件中有以下词法分析器规则:

   LINE    :    'F' | 'G';
RULE    :    (('->' ('F' | 'G')) => 'F' | 'G' )
                | LINE LINE + | LINE * (ROTATE + LINE+)+ ;
fragment ROTATE    :    ('/' | '\\');

我基本上是在尝试匹配看起来像 F -> 的产生式。 F/F\F\F/F。它成功匹配了上面的内容,但我猜我的语法谓词有问题,因为 G -> G 产生 MismatchedTokenException。该谓词用于消除“->”左侧的单个字母(我希望将其识别为 LINE 标记)和右侧的单个字母(应该是规则)之间的歧义。

知道我做错了什么吗?

I have the following lexer rules in my grammar file:

   LINE    :    'F' | 'G';
RULE    :    (('->' ('F' | 'G')) => 'F' | 'G' )
                | LINE LINE + | LINE * (ROTATE + LINE+)+ ;
fragment ROTATE    :    ('/' | '\\');

I'm basically trying to match productions that look like F -> F/F\F\F/F. It successfully matches stuff like the above, but I'm guessing there's a problem with my syntactic predicate, since G -> G produces a MismatchedTokenException. The predicate serves to disambiguate between single letters on the lhs of '->', which I want to be recognized as the LINE token, and those on the rhs, which should be RULEs.

Any idea what I'm doing wrong?

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

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

发布评论

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

评论(1

唯憾梦倾城 2024-09-07 03:20:13

请注意,规则:

RULE
  :  (('->' ('F' | 'G')) => 'F' | 'G')
  |  LINE LINE + 
  |  LINE * (ROTATE + LINE+)+ 
  ;

匹配单个 G 而不带谓词。上面的规则可以重写为:

RULE
  :  ( ('->' ('F' | 'G')) => 'F' 
     | 'G'
     )
  |  LINE LINE + 
  |  LINE * (ROTATE + LINE+)+ 
  ;

反过来又等于:

RULE
  :  ('->' ('F' | 'G')) => 'F' 
  |  'G'
  |  LINE LINE + 
  |  LINE * (ROTATE + LINE+)+ 
  ;

也许你打算做这样的事情:

RULE
  :  ('->' ('F' | 'G')) => ('F' | 'G')
  |  LINE LINE + 
  |  LINE * (ROTATE + LINE+)+ 
  ;

Note that the rule:

RULE
  :  (('->' ('F' | 'G')) => 'F' | 'G')
  |  LINE LINE + 
  |  LINE * (ROTATE + LINE+)+ 
  ;

matches a single G without the predicate. The rule above could be rewritten as:

RULE
  :  ( ('->' ('F' | 'G')) => 'F' 
     | 'G'
     )
  |  LINE LINE + 
  |  LINE * (ROTATE + LINE+)+ 
  ;

which in its turn equals:

RULE
  :  ('->' ('F' | 'G')) => 'F' 
  |  'G'
  |  LINE LINE + 
  |  LINE * (ROTATE + LINE+)+ 
  ;

Perhaps you meant to do something like this:

RULE
  :  ('->' ('F' | 'G')) => ('F' | 'G')
  |  LINE LINE + 
  |  LINE * (ROTATE + LINE+)+ 
  ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文