java - Is it possible to configure ANTLR grammar to use two token having the same structure? -


for below grammar,

grammar names;  fullname : title? first_name last_name;  title : 'mr.' | 'ms.' | 'mrs.' ;  first_name : ('a'..'z' | 'a'..'z')+ ;  last_name : ('a'..'z' | 'a'..'z')+ ;  whitespace : ( '\t' | ' ' | '\r' | '\n'| '\u000c' )+ -> skip ; 

when parsing input "mr. john smith", throw exception

 mismatched input 'smith' expecting last_name 

is possible configure antlr handle case? if not possible, alternative way handle it?

please note it's not limited simple case.

there no syntactic difference between first_name , last_name; need assign them.

grammar names;  fullname : title? first=name last=name;  title : 'mr.' | 'ms.' | 'mrs.' ;  name : ('a'..'z' | 'a'..'z')+ ;  whitespace : ( '\t' | ' ' | '\r' | '\n'| '\u000c' )+ -> skip ; 

then can call get("first") , get("last") extract parsed values out of match.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -