Cannot use "." as separator in antlr4

109 Views Asked by At

I am writing a parser to parse the string like aaa.bb = "[email protected]"

The antlr4 grammar file is like:

    DOT : [\-\_\.\@\>]+ ; 
    fragment CHAR: [a-zA-Z_0-9];
    fragment CHAR2:[\-\_\.\@];
    DECIMAL : '-'?[0-9]+('.'[0-9]+)? ;
    ATTR_ID : CHAR+;
    VALUE_ID: ATTR_ID+ (CHAR2? ATTR_ID)*;
    attribute
    :
    (attrGroup DOT attrKey) | attrKey;
    value :  DECIMAL | VALUE_ID | DATE;

The question now is when I remove the VALUE_ID lexer def, the attribute can be parsed like:

attrGroup: aaa
attrKey: bbb

If I add back the VALUE_ID def, it gives me the error:

attribute:1:0: mismatched
input 'aaa.bb' expecting ATTR_ID

Note: it works if the input string is: aaa>bbb = "[email protected]"

Did I make any mistakes?

1

There are 1 best solutions below

0
On

I would suggest using Parser rules for ValueID. This will allow the lexer to match ATTR_ID, but then determine that tokens should form a value_id

DECIMAL : '-'?[0-9]+('.'[0-9]+)? ;
DOT : [\-\_\.\@\>]+ ;
ATTR_ID : [a-zA-Z0-9]+;


value_id: ATTR_ID+ (DOT? ATTR_ID)*;