Software Prophets
Software Prophets

Reputation: 2976

ANTLR lexer mode match any text - extraneous input

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

Answers (1)

GRosenberg
GRosenberg

Reputation: 6001

The STR mode is emitting both TEXT and STRING tokens. The 'quotedString' rule needs to be:

quotedString : LQUOTE content STRING ;

Upvotes: 1

Related Questions