I am having problems with my Antlr grammar. I'm trying to write a parser rule for 'typedident' which can accept the following inputs:
'int a' or 'char a'
The variable name 'a' is from my lexer rule 'IDENT' which is defined as follows:
IDENT : (('a'..'z'|'A'..'Z') | '_') (('a'..'z'|'A'..'Z')|('0'..'9')| '_')*;
My 'typedident' parser rule is as follows:
typedident : (INT|CHAR) IDENT;
INT
and CHAR
having been defined as tokens.
The problem I'm having is that when I test 'typedident' the variable name has to be more than one character. For example:
'int a'
isn't accepted while 'int ab'
is accepted.
The outputed error I get is:
"MismatchedTokenException: mismatched input 'a' expecting '$'"
Any idea why I'm getting this error? I'm fairly new to Antlr so apologies if the error is trivial.
EDIT
I literally just got it working, and I don't know why. I also had two other lexer rules defined as follows:
ALPH : ('a'..'z'|'A'..'Z');
DIGIT : ('0'..'9');
I realised these weren't being used at all so I deleted them and everything now works fine! My guess why this works is because ALPH
and DIGIT
were overriding my other Lexer rules:
NUMBER : ('0'..'9')+;
CHARACTER : ''' (~('
' | '
' |''')) ''';
Does anyone know if this is the case? I'm curious as to why this problem has now been solved.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…