Question

I'm trying to figure out this bug. Currently, when I supply a few different inputs, I get either the correct result, or the ANTLRWorks debugger loops infinitely.

1) {v0, p1} = Infinite loop or mismatched token exception
2) {v0..v1} Works!
3) {v0,p1} Works? Why?! Notice the lack of space?

If anyone could help me understand why this is ambiguous, I'd really appreciate it. I've tried changing operator precedence, and still, I cannot get it to work the way I think it should. Thank you!

    WHITESPACE    :  ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; };
    LEFTCURL      : '{';
    RIGHTCURL     : '}';
    REGISTER      : ('v'|'p') NUMBER+;

    REGISTERLIST  : LEFTCURL REGISTER (',' REGISTER)* RIGHTCURL;
    REGISTERANGE  : LEFTCURL REGISTER '..' REGISTER RIGHTCURL;
Was it helpful?

Solution

WHITESPACEs are only put on the HIDDEN channel inside parser rules. Since both REGISTERLIST and REGISTERANGE are lexer rules, the spaces are not hidden, as you noticed. Instead of accounting for spaces inside these rules, these look to be more suitable as parser rules:

registerList  : LEFTCURL REGISTER (',' REGISTER)* RIGHTCURL;
registerRange : LEFTCURL REGISTER '..' REGISTER RIGHTCURL;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top