Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9046667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:42:32+00:00 2026-06-16T11:42:32+00:00

I would like to parse a string of French dates using Antlr. I have

  • 0

I would like to parse a string of French dates using Antlr.

I have three types of date :

  • date_day : 3 Octobre 2004 (the hour/minutes is midnight)
  • date_time : 12h (the day, month and year are completed with the current date)
  • date_complete : 3 Octobre 2004 12h. As you can see date_complete : date_day date_hour

The document I want to parse is just a chain of date_day, date_time and date_complete (with no separator).

Here’s an example of the strings I want to parse

3 Octobre 2005 12h 13h 5 Octobre 2004 3 Septembre 2005 11h
Expected : date_complete date_time date_day date_complete

12h
Expected : date_time

3 Octobre 2005 5 Octobre 2004 12h 13h 3 Septembre 2005 11h
Expected : date_day date_complete date_time date_complete

**// NEW REQUIREMENTS**

3 Octobre 2005
Expected : date_day 

3 Octobre 
Expected : date_day 

3 
Expected : date_day 

I tried many things, and Antlr v3 always says that my grammar is ambiguous :

warning(200): /meleo.dates/src/Grammar.g:25:48: 
Decision can match input such as "{FRI, MON..TUE, WED} TWO_DIGITS DECEMBER FOUR_DIGITS {FRI..HOURG, MON..WED}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
 |---> date_day (date_day | date_complete | date_hour)+

What is the proper to write that grammar ?

Here’s the grammar :

grammar MeleoDates;

options {
  language = Java;
}

@header {
  package meleo.data.dates ; 

  import rainstudios.meleo.crawler.data.Dates ;
  import rainstudios.meleo.crawler.data.EventDate ;
}

@lexer::header {
  package meleo.data.dates ;   

  import rainstudios.meleo.crawler.data.EventDate ;
 }

input           returns [Dates dates] 
                @init {Dates r = new Dates() ; } : 
                (   date 
                    {r.addDay($date.date);}
                    DATE_SEP?)+ 
                EOF
                    {$dates = r ;}
                ;

date            returns [EventDate date] :
                (date_complete)=> date_complete 
                    {$date = $date_complete.date;}
                | date_day 
                    {$date = $date_day.date;}
                | date_time 
                    {$date = $date_time.date;}
                ;

date_complete  returns  [EventDate date]   
                @init   {EventDateBuilder builder = new EventDateBuilder() ; } : 
                 day=date_day 
                    {builder.addDay($day.date);}
                 HOUR_SEP? 
                 time=date_time 
                    {builder.addTime($time.date);}
                    {$date = builder.toDate();}
                ;

date_day        returns [EventDate date] 
                @init   {EventDateBuilder builder = new EventDateBuilder() ; } :
                (
                dayOfWeek=( 
                     MON
                   | TUE
                   | WED
                   | THU
                   | FRI
                   | SAT
                   | SUN
                )?
                (day=INT)=> INT 
                    {builder.addDay($day.text);}
                (   m=ID 
                        {builder.addMonth($m.text);}
                    year=INT ?
                        {builder.addMonth($year.text);}
                )?
                )
                    {$date = builder.toDate();}
                ;

date_time       returns [EventDate date]  
                @init   {EventDateBuilder builder = new EventDateBuilder() ; } :
                    TIME 
                    {builder.addTime($TIME.text);}
                    {$date = builder.toDate();}
                ;

month   : DECEMBER | JANUARY ;

MON 
 : 'lundi'  
 | 'lun' 
 ;

 TUE 
 : 'mardi'  
 | 'mar' 
 ;

 WED 
 : 'mercredi'  
 | 'mer' 
 ;

 THU 
 : 'jeudi'  
 | 'jeu' 
 ;

 FRI 
 : 'venredi'  
 | 'ven' 
 ;

 SAT 
 : 'samedi'  
 | 'sam' 
 ;

 SUN 
 : 'dimanche'  
 | 'dim' 
 ;

DECEMBER    : 'dec' | 'decembre' ;
JANUARY     : 'jan' | 'janvier' ;

DATE_SEP    : 'et'| ',' | '-'; 
HOUR_SEP    : 'à' | 'a' ;
INT         : ('0'..'9')+;
TIME_SEP    : ':'  | 'h' ;
TIME        : INT TIME_SEP INT?;
ID          : ('a'..'z'|'A'..'Z')+;

WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};

** edited : added new requirements (optional month and year for date_day) **

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:42:33+00:00Added an answer on June 16, 2026 at 11:42 am

    Consider using a syntactic predicate:

    input      : date+;
    date       : (date_complete) => date_complete
               | date_day
               | date_time
               ;
    

    This, in effect, tells ANTLR to try for a date_complete before trying to match whatever it finds like usual (this is probably not a technically accurate description, but you get the idea). Without this, the date rule can match multiple options with the same input and ANTLR (v3, anyway) can’t address that itself.

    Here’s a full grammar for testing:

    grammar AmbiguousDates;
    
    
    input           : date+ EOF;
    
    date            : (date_complete)=> date_complete 
                        {System.out.println("date_complete: " + $date_complete.str);}
                    | date_day 
                        {System.out.println("date_day: " + $date_day.str);}
                    | date_time 
                        {System.out.println("date_time: " + $date_time.str);}
                    ;
    
    date_complete   returns [String str]
                    : date_day date_time 
                        {$str = String.format("\%s \%s", $date_day.str, $date_time.str);}
                    ;
    
    date_day        returns [String str]
                    : day=INT ID year=INT 
                        {$str = String.format("\%s \%s \%s", $day.text, $ID.text, $year.text);}
                    ;
    
    date_time       returns [String str]
                    : TIME 
                        {$str = $TIME.text;}
                    ;
    
    INT     : ('0'..'9')+;
    TIME    : INT 'h';
    ID      : ('a'..'z'|'A'..'Z')+;
    WS      : (' '|'\t'|'\f'|'\r'|'\n')+ {skip();};
    

    Input

    3 Octobre 2005 12h 13h 5 Octobre 2004 3 Septembre 2005 11h
    

    Output

    date_complete: 3 Octobre 2005 12h
    date_time: 13h
    date_day: 5 Octobre 2004
    date_complete: 3 Septembre 2005 11h
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to parse the HTML DOM, using PHP; I have a string
I have a text file and I would like to parse it using regular
I have a following string that I would like to parse into either a
I have a string 20100524 (2010 05 24) and I would like to parse
I have a string: [{id:1,gameName:arizona,cost:0.5E1,email:hi@gmail.com,requests:0},{id:2,gameName:arizona,cost:0.5E1,email:hi@gmail.com,requests:0},{id:3,gameName:arizona,cost:0.5E1,email:hi@gmail.com,requests:0}] However, I would like to parse this string into
I have a string like this: key=value, key2=value2 and I would like to parse
Hello I have an unusual date format that I would like to parse into
I have string with product information and I would like to parse that string
I would like to parse string literals using FParsec. By string literals I mean
I would like to parse a string using whitespace as delimiters. I know it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.