I am writhing a simple language with antlr, I defined a Lexer grammar in AntlrWorks, but when I want to generate the java code, it gives me the error:
Antlr error : the following token definition can never be matched because prior tokens match the same input: FLOAT_OR_INT, OPEN_PAR, CLOSE_PAR, .... (almost for all the rules!)
I am new to antlr, I assume it is because of the order of rule locations, but I don't know how should they have to be, what is my mistake?
here is the grammar:
lexer grammar OurCompiler;
options
{
k=5;
}
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
protected
INT : ('0'..'9')+
;
protected
FLOAT : INT '.' INT
;
FLOAT_OR_INT : ( INT '.' ) => FLOAT { $setType(FLOAT); }
| INT { $setType(INT); }
;
OPENPAR_OR_OUTPUT_OPERATOR : '(' { $setType(OPEN_PAR); } | '(' '(' { $setType(OUTPUT_OPERATOR); }
;
CLOSEPAR_OR_INPUT_OPERATOR : ')' { $setType(CLOSE_PAR); } | ')' ')' { $setType(INPUT_OPERATOR); }
;
protected
OPEN_PAR : '(' ;
protected
CLOSE_PAR : ')' ;
protected
INPUT_OPERATOR : ')' ')' ;
protected
OUTPUT_OPERATOR : '(' '(' ;
BOOLEAN : 't' 'r' 'u' 'e' | 'f' 'a' 'l' 's' 'e' ;
LOWER : '<' ;
LOWER_EQUAL : LOWER '=' ;
UPPER : '>' ;
UPPER_EQUAL : UPPER '=' ;
ASSIGN : '=' ;
EQUAL : '=' '=' ;
NOT : '!' ;
NOT_EQUAL : NOT '=' ;
ADD : '+' ;
ADD_TO_PREVIOUS : ADD '=' ;
INCREMENT : ADD ADD ;
MINUS : '-' ;
MINUS_FROM_PREVIOUS : MINUS '=' ;
DECREMENT : MINUS MINUS ;
MULTIPLY : '*' ;
MULTIPLY_TO_PREVIOUS : MULTIPLY '=' ;
DIVIDE : '/' ;
DIVIDE_FROM_PREVIOUS : DIVIDE '=' ;
MODE : '%' ;
OPEN_BRAKET : '[' ;
CLOSE_BRAKET : ']' ;
OPEN_BRACE : '{' ;
CLOSE_BRACE : '}' ;
COLON : ':' ;
SEMICOLON : ';' ;
COMMA : ',' ;
SINGLE_LINE_COMMENT :
'#' '#' ( ~ ('\n'|'\r') )* ( '\n' | '\r' ('\n')? )? { $setType(Token.SKIP); newline(); }
;
MULTIPLE_LINE_COMMENT : '#' ( options {greedy=false;} : . )* '#' { $setType(Token.SKIP); }
;
WS :
( ' '
| '\t'
| '\r' { newline(); }
| '\n' { newline(); }
)
{ $setType(Token.SKIP); }
;
protected
ESC_SEQ : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
;
STRING :
'"' ( ESC_SEQ | ~('\\'|'"') )* '"'
;
CHAR :
'\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
;
INT_KEYWORD : 'i' 'n' 't' ;
FLOAT_KEYWORD : 'f' 'l' 'o' 'a' 't' ;
CHAR_KEYWORD : 'c' 'h' 'a' 'r' ;
STRING_KEYWORD : 's' 't' 'r' 'i' 'n' 'g' ;
BOOLEAN_KEYWORD : 'b' 'o' 'o' 'l' 'e' 'a' 'n' ;
INPUT_KEYWORD : 'i' 'n' ID { $setType(ID); }
| 'i' 'n'
;
OUTPUT_KEYWORD : 'o' 'u' 't' ID { $setType(ID); }
| 'o' 'u' 't' ;
IF_KEYWORD : 'i' 'f' ;
FOR_KEYWORD : 'f' 'o' 'r' ;
SWITCH_KEYWORD : 's' 'w' 'i' 't' 'c' 'h' ;
CASE_KEYWORD : 'c' 'a' 's' 'e' ;
BREAK_KEYWORD : 'b' 'r' 'e' 'a' 'k' ;
DEFAULT_KEYWORD : 'd' 'e' 'f' 'a' 'u' 'l' 't' ;
WHILE_KEYWORD : 'w' 'h' 'i' 'l' 'e' ;
ELSE_KEYWORD : 'e' 'l' 's' 'e' ;
ELSEIF_KEYWORD : 'e' 'l' 's' 'e' 'i' 'f' ;
AND_KEYWORD : 'a' 'n' 'd' ;
OR_KEYWORD : 'o' 'r' ;
NOT_KEYWORD : 'n' 'o' 't' ;
CONSTANT_KEYWORD : 'c' 'o' 'n' 's' 't' 'a' 'n' 't' ;