antlr3 always matching the longest possible token

32 views
Skip to first unread message

Adithya Chakilam

unread,
Aug 8, 2022, 12:45:29 AM8/8/22
to antlr-discussion
Let's suppose that I have input which matches two tokens, antlr is always choosing the longest match. Instead how do I configure it start from shortest match and then go to longest if not possible ?

Example:

rule
 : USER PATH
 | PATH
 ;

USER
 : '@' ('a'..'z' | 'A'..'Z' | '0-9' | '_')+
 ;

PATH
 : URL_ALLOWED_CHARS+ '.config'
 ;

fragment URL_ALLOWED_CHARS
 : ':' | '/' | '?' | '#' | '['
 | ']' | '@' |'!' | '$' | '&'
 | '\'' | '(' | ')' | '*'
 | '+' | ',' | ';' | '='
 | '%' | 'A'..'Z' | 'a'..'z'
 | '0'..'9' | '_' | '.'
 | '\\' | '-' | '~'
 ;


For the grammar above, input such as @random_user/file.config

option1 on rule should match and I should get two tokens: @random_user for USER and /file.config for FILE.

Instead, grammar matches the option 2 of the rule and the complete input is matched as PATH. How could I avoid it ?

Thanks
Adithya
Reply all
Reply to author
Forward
0 new messages