I am currently trying to implement an Antlr parser.
I obtain strange MismatchedTokenException in a token that identifies string literals once I add escape sequence support.
Following is the Antlr parser example that causes the issue:
rule: STRING_LITERAL ;
STRING_LITERAL
:
'"' STRING_GUTS '"'
;
fragment
STRING_GUTS
:
( ESC | ~('\\' | '"') )*
;
ESC
:
'\\'
( '\\' | '"' )
;
Do you seen any issue in this code?
Note that if I remove ESC from the STRING_GUTS, the string parsing is working well…
You’ll have to post the input you’re getting this error with, the ANTLR version you’re using, and the way you’re running your test(s), because I see no problem with that grammar, as you can see:
T.g
Main.java
If I generate a lexer and parser from you grammar (1), compile all java-source files (2) and run the Main class (3):
The following is printed to the console:
I.e.: the input
srcis parsed as expected.If you’re encountering problems with ANTLRWorks’ interpreter: don’t use it, it’s a bit buggy. Either use ANTLRWorks’ debugger, or use a custom class as I did above.