java - ANTLR4 Rule for one or more options *in order* -


I have found a rule for matching the time period specified in the rule, time, months, days, hours, minutes and seconds . For example:

  • For 2 months,
  • 3 months, 5 days

    For example: 17 seconds

    but not:

    • 1 day, 1 month
    • 5 minutes, 2 hours

      My current rule is:

        Duration: year = months of the year (LIST_DELIM months = unity months)? (LIST_DELIM days = integral days)? (LIST_DELIM hours = integral hours)? (LIST_DELIM minutes = INTEGER minutes)? (LIST_DELIM seconds = INTEGER seconds)? | Months = only one month (LIST_DELIM days = integral days)? (LIST_DELIM hours = integral hours)? (LIST_DELIM minutes = INTEGER minutes)? (LIST_DELIM seconds = INTEGER seconds)? | Day = Integer Day (LIST_DELIM hours = impermeable hours)? (LIST_DELIM minutes = INTEGER minutes)? (LIST_DELIM seconds = INTEGER seconds)? | Hours = integer hours (LIST_DELIM minutes = INTEGER minutes)? (LIST_DELIM seconds = INTEGER seconds)? | Min = INTEGER MINUTES (LIST_DELIM seconds = INTEGER SECONDS)? | Second = eccentric second;   

      It seems like a fair amount of redundancy is there any way to simplify it?

      To match more grit in your grammar and read a listener or

      For, you can create different parser rules so that you do not need to label them:

        Duration: Year (',' month)? (',' day)? (',' Hours)? (',' Minute)? (',' Seconds)? | Months (',' days)? (',' Hours)? (',' Minute)? (',' Seconds)? | ... | Seconds; Year: In Years; Months: Best wishes in months; ... LIST_DELIM: ',';   

      A related question & amp; A:

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -