1

I'm trying to write a grammar to parse an input of format

--
sfasfa af adfa
sfsdfsadfa
--

and this is what I came up with, but it says mismatchedTokenException.

grammar abc;

key :   MARK password MARK;

password:   char+ ;

char:   CHAR;

WS: (' '|'\r'|'\t'|'\u000C'|'\n') {channel=99;};

CHAR:   ('a'..'z'|'A'..'Z'|'0'..'9'|'/'|'+'|'='|'_'|'-'|':')*;

MARK    :   '--';

I want to fetch the password.

1 Answers1

0

There are a couple of things wrong:

  • lexer rules should always match at least 1 char (your CHAR rule can match the empty string);
  • the word channel should be preceded with a $;
  • preferably, don't use magic numbers (not 99, but HIDDEN).

Try:

grammar abc;

key      : HEADER password END;
password : char+ ;
char     : CHAR;
WS       : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;};
CHAR     : ('a'..'z'|'A'..'Z'|'0'..'9'|'/'|'+'|'='|'_'|'-'|':');
HEADER   : '--BEGIN--';
END      : '--END--';
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Thanks, That solved all the problems. Can you tell me whats the difference between lexer and parser rules? –  Nov 22 '11 at 07:02
  • @Buxme, have a look at this previous Q&A: http://stackoverflow.com/questions/4297770/practical-difference-between-parser-rules-and-lexer-rules-in-antlr – Bart Kiers Nov 22 '11 at 07:04