Hello,
I just started to learn ANTLR (just finished reading chapter 4 of "The definitive ANTLR 4 reference").
I am trying to match something like this:
print: anything, but at least one character followed by newlineMy grammar is the following:
grammar NotSoSimple;
print: PRINT text=NOT_NEWLINE NEWLINE;
PRINT : 'print: ' ;
NOT_NEWLINE : ~[\r\n]+ ;
NEWLINE : '\r'? '\n' | '\r' ;
Of course, this doesn't work because NOT_NEWLINE eats up all the characters, including 'print: '.
I have 4 questions:
1) how can I solve this?
2) why does NOT_NEWLINE take precedence when I listed PRINT before it?
3) is there a simple solution to this? Something that does not look as a workaround and that is not completely counterintuitive.
Thinking out loud:
It looks to me that lexing just introduces a lot of complexity.
Obviously what token is formed by NOT_NEWLINE depends on the context.
That suggest to skip lexing completely (parsing is the phase that deals with context). But (in case this is possible) I'm afraid it would lead to a big performance hit.
4) Is there any case where the lexer makes things easier, given that is so inflexible (not context dependent)? Why do we even have a lexing phase?
P.S.: Obviously the above grammar grow to be much larger. Otherwise I would just do a substring and call it a day.
Thanks for your help,
Cristian