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
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?
mr
will always be parsed as aTITLE
. See @SaraVF 's solution – Adrian Leonhard