解析器/词法分析器规则的Antlr v3错误 [英] Antlr v3 error with parser/lexer rules

查看:92
本文介绍了解析器/词法分析器规则的Antlr v3错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Antlr语法有问题.我正在尝试为'typedident'编写一个解析器规则,该规则可以接受以下输入:

I am having problems with my Antlr grammar. I'm trying to write a parser rule for 'typedident' which can accept the following inputs:

'int a'或'char a'

'int a' or 'char a'

变量名"a"来自我的词法分析器规则"IDENT",其定义如下:

The variable name 'a' is from my lexer rule 'IDENT' which is defined as follows:

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

我的"typedident"解析器规则如下:

My 'typedident' parser rule is as follows:

typedident : (INT|CHAR) IDENT;

INTCHAR被定义为令牌.

我遇到的问题是,当我测试"typedident"时,变量名称必须超过一个字符.例如:

The problem I'm having is that when I test 'typedident' the variable name has to be more than one character. For example:

'int a'不被接受,而'int ab'被接受.

'int a' isn't accepted while 'int ab' is accepted.

我得到的输出错误是:

"MismatchedTokenException:输入'a'不匹配,预期为'$'"

"MismatchedTokenException: mismatched input 'a' expecting '$'"

知道为什么我会收到此错误吗?我对Antlr相当陌生,因此如果错误不重要,我们深表歉意.

Any idea why I'm getting this error? I'm fairly new to Antlr so apologies if the error is trivial.

编辑

我实际上只是让它工作了,我也不知道为什么.我还定义了其他两个词法分析器规则,如下所示:

I literally just got it working, and I don't know why. I also had two other lexer rules defined as follows:

ALPH : ('a'..'z'|'A'..'Z'); 
DIGIT : ('0'..'9'); 

我意识到这些根本没有被使用,所以我删除了它们,现在一切正常!我的猜测为何有效,是因为ALPHDIGIT覆盖了我的其他Lexer规则:

I realised these weren't being used at all so I deleted them and everything now works fine! My guess why this works is because ALPH and DIGIT were overriding my other Lexer rules:

NUMBER : ('0'..'9')+; 
CHARACTER : '\'' (~('\n' | '\r' |'\'')) '\''; 

有人知道这种情况吗?我很好奇为什么现在已经解决了这个问题.

Does anyone know if this is the case? I'm curious as to why this problem has now been solved.

推荐答案

'int a'不被接受,而'int ab'被接受. ... 我的猜测为何如此有效,是因为ALPH和DIGIT覆盖了...

'int a' isn't accepted while 'int ab' is accepted. ... My guess why this works is because ALPH and DIGIT were overriding ...

是的,似乎ALPH是在IDENT规则之前定义的,在这种情况下,单个字母被标记为ALPH标记.如果IDENT是在之前 ALPH定义的,则一切正常(对于您而言).

Yes, it appears ALPH was defined before the IDENT rule, in which case single letters were tokenized as ALPH tokens. If IDENT was defined before ALPH, it should all go okay (in your case).

总结ANTLR的词法分析器规则如何工作:

To summarize how ANTLR's lexer rules work:

  • lexer规则匹配尽可能多的字符(贪婪);
  • 如果2个(或更多)词法分析器规则匹配相同的输入,则首先定义的规则将获胜" .

您必须意识到,词法分析器不会根据解析器(当时)的需求来生成令牌.该词法分析器独立于解析器运行.

You must realize that the lexer does not produce tokens based on what the parser (at that time) needs. The lexer operates independently from the parser.

这篇关于解析器/词法分析器规则的Antlr v3错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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