为 year/month/day 编写 EBNF 表格

Write EBNF form for a year/month/day

我想找到下一个问题的解决方案,但我需要将时间的 EBNF 写成年-月-日和月-日-年两种格式以查看差异:

确定将日期写成结构化整数的一个优点:年、月、日 (1954-02-10) 而不是按正常顺序 (02-10-1954)。

格式:年-月-日。 这是我想出的:

<NonZeroDigit> ::= ("1" | "2" | ... | "9")
<Month> ::= ( "0" <NonZeroDigit> ) | ( "1" ( "0" | "1" | "2" ) ) 
<Day> ::= ( "0" <NonZeroDigit> ) | ( ("1" | "2") <NonZeroDigit> ) | ("3" ( "0" | "1" ) )
<Year> ::= ( "000" <NonZeroDigit> ) | 
            ( "00" <NonZeroDigit> <NonZeroDigit> ) | 
            ( "0" <NonZeroDigit> <NonZeroDigit> <NonZeroDigit> ) | 
            ( "1"  <NonZeroDigit> <NonZeroDigit> <NonZeroDigit>) |
            ( "20" <NonZeroDigit> <NonZeroDigit> ) )

今年是 2099 年,我想这没问题,这些规则有效,但是有没有更好的方法来编写当时的 EBNF? 我错过了什么吗?

所以我想到的解决方案是这样的:

<FebruaryNum> ::= "02"
<Dash> ::= "-"
<NonLeapDigit> ::= "1" | "2" | "3" | "5" | "6" | "7"
<LeapDigit> ::= "4" | "8"
<NonZeroOrNineDigit> ::= <NonLeapDigit> | <LeapDigit>
<Digit> ::= "0" | <NonZeroOrNineDigit> | "9"
<ThreeDigits> ::= <Digit> <Digit> <Digit>
<DaysFebNonLeap> ::= ( "0" <Digit> ) | ( ("1" | "2" ) ( <NonZeroOrNineDigit> | "0")
<DaysFebLeap> ::= <DaysFebNonLeap> | "29"
<Days30> ::= <DaysFebLeap> | "30"
<Days31> ::= <Days30> | "31" 
<LeapYear> ::= <ThreeDigits> ( <LeapDigit> | "0" )
<NonLeapYear> ::= <ThreeDigits> ( "0" | <NonLeapDigit> | "9" )
<Month31Days> ::= ( ( "0" ( "1" | "3" | "5" | "7" | "8") ) | ( "1" ( "0" | "2" ) ) ) <Dash> <Days31>
<Month30Days> ::= ( ( "0" ( "4" | "6" | "9" ) ) | "11" ) <Dash> <Days30> 
<FebruaryLeap> ::= <FebruaryNum> <Dash> <DaysFebLeap>
<FebruaryNonLeap> ::= <FebruaryNum> <Dash> <DaysFebNonLeap>

<DateYearMonthDay> ::= ( <LeapYear> <Dash> ( <FebruaryLeap> | <Month30Days> | <Month31Days> ) ) | 
                        ( <NonLeapYear> <Dash> ( <FebruaryNonLeap> | <Month30Days> | <Month31Days> ))

写年-月-日而不是日-月-年或月-日-年的好处是,日取决于月,月取决于年,因此更容易可以按年-月-日格式打印日期的程序自动机。