0
votes

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?

1
As a comment, it is not easy to spot the difference between your grammars. To avoid the votes to close next time, describe what exactly you changed.doublep

1 Answers

0
votes

In your first grammar INT and REAL rules come after ID, so strings "INT" and "REAL" are matched with ID rule. In your second grammar you use strings instead, which (in mixed grammars like you have) create implicit lexer rule that take precedence to the explicit rules.

To fix the first grammar move INT and REAL above ID rule.