Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepting grammar with single quotes

Tags:

antlr

antlr4

I need to write a grammar in antlr4 that accepts the date and time within the single quotes and then have to print the same as output. For example: the date/time = '2007-08-01T01:00:00,000+01:00'. If this is my input, it has to accept as (date or time) or both date and time in the grammar and output should print as same as input which we have given.

PART OF MY GRAMMAR

predicateOperand : objectPath | operand;

operand : STRING | INTEGER | FLOAT | DATE | PARAMETER | BOOLEAN;

DATE :  '\'' DIGIT DIGIT DIGIT DIGIT '-' DIGIT DIGIT '-' DIGIT DIGIT
        'T' DIGIT DIGIT ':' DIGIT DIGIT ':' DIGIT DIGIT ',' DIGIT DIGIT
        DIGIT '+' DIGIT DIGIT ':' DIGIT DIGIT '\'';

STRING :  '\'' ( ESC_SEQ | ~('\\'|'\'') )* '\''
          |'"' ( ESC_SEQ | ~('\\'|'"') )* '"';

This is part of my grammar. But, if I give '2007-08-01T01:00:00,000+01:00' as input, its not accepting as date instead its accepting as string.

If someone helps that might be so grateful to me. Thank you.

like image 940
user2085189 Avatar asked Nov 11 '22 22:11

user2085189


1 Answers

There is no error with your grammar.

Although the input which "STRING" matches covers that of "DATE", a input as '2007-08-01T01:00:00,000+01:00' will still be recognized as "DATE". "ANTLR resolves lexical ambiguities by matching the input string to the lexical rule specified first in the grammar.". The relateship is similar to that of "KEYWORD" and "IDENTIFIER". Your can refer to "Pragmatic.The Definitive ANTLR 4 Reference" for further details.

Lexers try to match the longest string possible for each token, so such an input '2007-08-01T01:00:00,000+01:00 ' will be recognized as "STRIGN".

You can test it on Antlr 4.

like image 69
frankli22586 Avatar answered Jan 04 '23 01:01

frankli22586