简体   繁体   English

如何在ANTLR中处理语法规则中的模糊标记?

[英]How to handle ambiguous token in grammar rule in ANTLR?

I have the following grammar for parsing person name after normalizing it. 规范化后,我有以下语法解析人名。

exp : fullName EOF;
fullName : title? f=name m=name? l=name;

title: TITLE;
name : NAME;

TITLE : 'mr'| 'mrs' | 'ms';
NAME : ('a'..'z')+;

WHITESPACE : ('\t' | ' ' | '\r' | '\n'| '\u0020' | '\u000C' )+ -> skip ;

When I parse a name like "mr john me smith" it works correctly but when one of the title tokens appears as a name like "mr john mr smith", I got the following error 当我解析像“约翰我史密斯先生”这样的名字时,它可以正常工作,但当其中一个标题令牌出现像“mr john mr smith”这样的名字时,我收到以下错误

line 1:8 extraneous input 'mr' expecting NAME
line 1:16 missing NAME at '<EOF>'
(exp (fullName (title mr) (name john) (name mr smith) (name <missing NAME>)) <EOF>)

Is there a way to use the token according to it position in the rule only and neglect it if it appeared in another location? 有没有办法根据它在规则中的位置使用令牌,如果它出现在另一个位置则忽略它?

As long as the lexer will not be able to neglect it, the parser rule should be changed to 只要词法分析器不能忽略它,解析器规则就应该改为

name : NAME | TITLE;

Modifying the lexer rule will not fix the problem and will generate another error. 修改词法分析器规则不会解决问题,并会生成另一个错误。

It's been a while since I used antlr, but try using 我使用antlr已经有一段时间了,但尝试使用

NAME : TITLE | ('a'..'z')+;

I don't think you can neglect it.. antlr sees that the token is a TITLE and therefore it stops looking. 我不认为你可以忽略它.. antlr看到令牌是一个标题,因此它停止寻找。 Saying that titles are also NAMES you have a workaround for this case. 说这些标题也是NAMES,你有一个针对这种情况的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM