0

我想找到下一个问题的解决方案,但我需要以年-月-日和月-日-年两种格式编写时间的 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?我错过了什么吗?

4

1 回答 1

0

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

<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> ))

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

于 2016-03-14T15:27:46.257 回答