Reputation: 2976
I switch modes so I can accept any text inside a grammar. This example doesn't include the complexity of the real life situation. I adapted an exampled from the ANTLR book.
Lexer
parser grammar StringsParser;
@header {
package lexertest;
}
options { tokenVocab=StringsLexer; }
test:quotedString+;
quotedString:LQUOTE content;
content:TEXT+?;
Parser
lexer grammar StringsLexer;
LQUOTE : '"' -> mode(STR) ;
WS : [ \r\t\n]+ -> skip ;
mode STR;
STRING : '"' -> mode(DEFAULT_MODE);
TEXT : .;
Test Input
"hi"
"mom"
Gives me:
line 1:3 extraneous input '"' expecting {<EOF>, LQUOTE, TEXT}
line 2:4 extraneous input '"' expecting {<EOF>, LQUOTE, TEXT}
How do I fix this so I don't get the above error?
Upvotes: 1
Views: 446
Reputation: 6001
The STR mode is emitting both TEXT and STRING tokens. The 'quotedString' rule needs to be:
quotedString : LQUOTE content STRING ;
Upvotes: 1