I have an ANTLR 4 grammar:
grammar Test;
start : NonZeroDigit '.' Digit Digit? EOF
;
DOT : '.' ;
PLUS : '+' ;
MINUS : '-' ;
COLON : ':' ;
COMMA : ',' ;
QUOTE : '\"' ;
EQUALS : '=' ;
SEMICOLON : ';' ;
UNDERLINE : '_' ;
BACKSLASH : '\\' ;
SINGLEQUOTE : '\'' ;
RESULT_TYPE_NONE : 'NONE' ;
RESULT_TYPE_RESULT : 'RESULT' ;
RESULT_TYPE_RESULT_SET : 'RESULT_SET' ;
TYPE_INT : 'Int' ;
TYPE_LONG : 'Long' ;
TYPE_BOOL : 'Bool' ;
TYPE_DATE : 'Date' ;
TYPE_DOUBLE : 'Double' ;
TYPE_STRING : 'String' ;
TYPE_INT_LIST : 'List<Int>' ;
TYPE_LONG_LIST : 'List<Long>' ;
TYPE_BOOL_LIST : 'List<Bool>' ;
TYPE_DATE_LIST : 'List<Date>' ;
TYPE_DOUBLE_LIST : 'List<Double>' ;
TYPE_STRING_LIST : 'List<String>' ;
LONG_END : 'L' ;
DOUBLE_END : 'd' ;
DATE_NOW : 'NOW' ;
BOOL_TRUE : 'true' ;
BOOL_FALSE : 'false' ;
BLOCK_OPEN : '{' ;
BLOCK_CLOSE : '}' ;
GENERIC_OPEN : '<' ;
GENERIC_CLOSE : '>' ;
BRACKET_OPEN : '(' ;
BRACKET_CLOSE : ')' ;
MAP : 'Map' ;
LIST : 'List' ;
GROUP : 'Group' ;
BY : 'by' ;
DEFAULT : 'default' ;
JSON_NAME : 'JSONName' ;
INTERFACE : 'interface' ;
CLASS : 'class' ;
ABSTRACT : 'abstract' ;
IMPLEMENTS : 'implements' ;
EXTENDS : 'extends' ;
CACHEABLE : 'cacheable' ;
FUNCTION : 'function' ;
REQUEST : 'request' ;
NAMED_QUERY : 'namedQuery' ;
INPUT : 'input' ;
OUTPUT : 'output' ;
RESULT_TYPE : 'resultType' ;
PACKAGE : 'package' ;
SCHEMA : 'schema' ;
VERSION : 'version' ;
MIN_VERSION : 'minVersion' ;
fragment
NonZeroDigit : [1-9]
;
fragment
Digit : '0' | NonZeroDigit
;
fragment
Digits : Digit+
;
fragment
IntegerNumber : '0' | ( NonZeroDigit Digits? )
;
fragment
SignedIntegerNumber : ( '+' | '-' )? IntegerNumber
;
fragment
FloatingNumber : IntegerNumber ( '.' Digits )?
;
fragment
SignedFloatingNumber : ( '+' | '-' )? FloatingNumber
;
fragment
Letter : [a-z]
;
fragment
Letters : Letter+
;
fragment
CapitalLetter : [A-Z]
;
fragment
CapitalLetters : CapitalLetter+
;
fragment
LetterOrDigitOrUnderline : Letter | CapitalLetter | Digit | '_'
;
fragment
EscapeSequence : ( '\\' ( 'b' | 't' | 'n' | 'f' | 'r' | '\"' | '\'' | '\\' ) )
| UnicodeEscape
| OctalEscape
;
fragment
HexDigit : [0-9] | [a-f] | [A-F]
;
fragment
UnicodeEscape : '\\' 'u' HexDigit HexDigit HexDigit HexDigit
;
fragment
OctalEscape : ( '\\' [0-3] [0-7] [0-7] )
| ( '\\' [0-7] [0-7] )
| ( '\\' [0-7] )
;
WS : [ \t\r\n]+ -> skip
;
I'm using it like this:
final ByteArrayInputStream input = new ByteArrayInputStream("1.11".getBytes());
final TestLexer lexer = new TestLexer(new ANTLRInputStream(input));
final TestParser parser = new TestParser(new CommonTokenStream(lexer));
parser.start();
But this gives me:
line 1:0 token recognition error at: '1'
line 1:2 token recognition error at: '1'
line 1:3 token recognition error at: '1'
line 1:1 missing NonZeroDigit at '.'
line 1:4 missing Digit at '<EOF>'
What am I doing wrong? I'm using antlr v4.1.
Thanks in advance for helping.
fragment
lexer rules can only be used by other lexer rules: these will never become a token on their own. Therefor, you cannot use fragment
rules in parser rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With