目標
識別unsigned float,float,unsigned int,int。
整數類型下識別八進制(076),十進制(76),十六進制表示法(0x7f)。
構造正則定義
基本正則定義
ϵ→空串
Space→空格
Spaces→(Space)(Space)∗
Digit→(0∣1∣2∣3∣4∣5∣6∣7∣8∣9)
Digits→DigitDigit∗
HexaDigits→(1∣2∣3∣4∣5∣6∣7∣8∣9∣a∣b∣c∣d∣e∣f∣A∣B∣C∣D∣E∣F)(Digit∣a∣b∣c∣d∣e∣f∣A∣B∣C∣D∣E∣F)∗
OctalDigits→(1∣2∣3∣4∣5∣6∣7)(0∣1∣2∣3∣4∣5∣6∣7)∗
Signed→(ϵ∣+∣−)
HexaDecimal→0(x∣X)HexaDigits
Decimal→(1∣2∣3∣4∣5∣6∣7∣8∣9)(1∣2∣3∣4∣5∣6∣7∣8∣9∣0)∗
OctalDecimal→0OctalDigits
UnsigneInt→OctalDecimal∣Decimal∣HexaDecimal
Int→Signed(UnsignedInt)
Fraction→.Digits
Exponent→ϵ∣((e∣E)(Signed)(Digits))
UnsignedFloat→(Digits∣Fraction∣Exponent)−Exponent
Float→(Signed)(UnsignedFloat)
Equal→Space∗=Space∗
ID→(_∣Letter)(_∣Letter∣Number)∗
標識符,以下劃線或字母開頭,後續可以接着下劃線或字母或數字
數的正則定義
有符號浮點數→(float)(Spaces)(ID)(Equal)(Float)
無符號浮點數→(unsigned float)(Spaces)(ID)(Equal)(UnsignedFloat)
有符號整數→(int)(Spaces)(ID)(Equal)(Int)
無符號整數→(unsigned int)(Spaces)(ID)(Equal)(UnsignedInt)
數的定義→(有符號浮點數∣無符號浮點數∣有符號整數∣無符號整數);以;
作爲結束符。
正則定義轉NFA(非確定的有窮自動機)
ϵ邊轉換後結果,加雙引號表示字符串。
無符號浮點數(unsigned float)(Spaces)(ID)(Equal)(UnsignedFloat)
ID
unsigned float
Equal
UnsignedFloat
UnsignedFloat→(Digits∣Fraction∣Exponent)−Exponent
浮點數的三個部分可以在一定條件下自由組合。指數位不可以單獨出現。
Digits
Fraction
Exponent
Signed
有符號浮點數(float)(Spaces)(ID)(Equal)(Float)
float
Float
無符號整數(unsigned int)(Spaces)(ID)(Equal)(UnsignedInt)
unsigned int
UnsignedInt
UnsigneInt→OctalDecimal∣Decimal∣HexaDecimal
OctalDecimal
Decimal
畫錯了,Start→0的邊應該是1∣2∣3∣4∣5∣6∣7∣8∣9,不能以0開頭。
HexaDecimal
畫錯了,3→4不能以0開頭,否則毫無意義。
UnsignedInt(簡化後)
有符號整數(int)(Spaces)(ID)(Equal)(Int)
Int
數的識別(簡化後)
定義關係依賴圖
下圖爲正則定義之類的依賴關係圖,爲了保證每一步開發完都能測試,將從葉節點開始自底向上開發。