I have following grammar in ANTLR4
grammar DEF;
def
:
'DEF' vartypes ID ';'
;
ID
:
(
'A' .. 'Z'|'a'..'z'
)+
;
vartypes
:
INT
| REAL
;
INT:'INT';
REAL:'REAL';
VARIABLE
:
(
'A' .. 'Z'
| 'a' .. 'z'
| '0' .. '9'
| '_'
| '-'
)+
;
fragment VARTYPEREAL
:
'REAL'
;
fragment VARTYPEINT
:
'INT'
;
LINENUMBER
:
'N' INTVALUE
;
INTVALUE
:
(
'-'
)?
(
'0' .. '9'
)+
;
WS
:
(
' '
| '\t'
| '\n'
| '\r'
)+ -> skip
;
And when I parse the string 'DEF REAL test;' I get the following error:
line 1:4 missing {'INT', 'REAL'} at 'REAL'
line 1:9 extraneous input 'test' expecting ';'
But, when I change my grammar to
grammar DEF;
def
:
'DEF' vartypes ID ';'
;
ID
:
(
'A' .. 'Z'|'a'..'z'
)+
;
vartypes
:
'INT'
| 'REAL'
;
VARIABLE
:
(
'A' .. 'Z'
| 'a' .. 'z'
| '0' .. '9'
| '_'
| '-'
)+
;
fragment VARTYPEREAL
:
'REAL'
;
fragment VARTYPEINT
:
'INT'
;
LINENUMBER
:
'N' INTVALUE
;
INTVALUE
:
(
'-'
)?
(
'0' .. '9'
)+
;
WS
:
(
' '
| '\t'
| '\n'
| '\r'
)+ -> skip
;
everything is fine. Where I'm wrong in the grammar 1?