मैं हास्केल का उपयोग कर मूल लेक्सर लिखने की कोशिश कर रहा हूं। डीएफए और एनएफए को लागू करने के लिए, मैंने कक्षाओं एफए और फासेट में कुछ सामान्य कार्यों को रखने का फैसला किया।बहु पैरामीटर कक्षाओं के साथ कोई उदाहरण त्रुटि
-- |A class for defining the common functionality of all finite automatons.
class FA a b where
mutateId :: a -> Int -> a -- ^Returns a new FA by changing the sId of the input FA.
mutateType :: a -> StateType -> a -- ^Returns a new FA by changing the stateType of the input FA.
addTransition :: a -> (b, a) -> a -- ^Returns a new FA by adding a new transition to the input FA.
-- |A class for defining the common functionality of all finite automaton states.
class FA a b => FAState a b where
sId :: a -> Int -- ^An unique identifier for the state(hence the prefix s).
sType :: a -> StateType -- ^The type of the state.
sTransitions :: a -> Transitions b a -- ^The transitions that occur from this state.
जहां,
-- |A type which identifies different types of a FA state.
data StateType = Start | Normal | Final
deriving (Show, Read, Eq)
-- |A type which represents a list of transitions on input a to b.
-- Eg. [(Char, DFA)] represents the transition on a Char input.
type Transitions a b = [(a, b)]
इसलिए, ख डेटा प्रकार जिसके लिए संक्रमण होते हैं प्रतिनिधित्व करता है। एक डीएफए के लिए, बी = चार, जबकि एक एनएफए के लिए, बी = प्रतीक।
data Symbol = Alphabet Char | Epsilon
deriving (Show, Read, Eq)
DFA और NFA क्रमशः के रूप में परिभाषित कर रहे हैं:
data DFA = DState Int StateType (Transitions Char DFA)
deriving (Show, Read)
data NFA = NState Int StateType (Transitions Symbol NFA)
deriving (Show, Read)
मैं एफए & FAState के कहने परिभाषा के साथ समस्या हो रही है:
instance FA DFA Char where
mutateId (DState i ty ts) new_i = DState new_i ty ts
mutateType (DState i ty ts) new_ty = DState i new_ty ts
addTransition (DState i ty ts) state = DState i ty (state:ts)
instance FAState DFA Char where
sId (DState i t ts) = i
sType (DState i t ts) = t
sTransitions (DState i t ts) = ts
instance FA NFA Symbol where
mutateId (NState i ty ts) new_i = NState new_i ty ts
mutateType (NState i ty ts) new_ty = NState i new_ty ts
addTransition (NState i ty ts) state = NState i ty (state:ts)
instance FAState NFA Symbol where
sId (NState i t ts) = i
sType (NState i t ts) = t
sTransitions (NState i t ts) = ts
के किसी भी भागने की कोशिश कर पर जिन कार्यों को मुझे कोई उदाहरण त्रुटि नहीं मिलती है:
>>sId egNFA
<interactive>:15:1:
No instance for (FAState NFA b0)
arising from a use of `sId'
Possible fix: add an instance declaration for (FAState NFA b0)
In the expression: sId egNFA
In an equation for `it': it = sId egNFA
मुझे समझ में नहीं आता कि यहां क्या हो रहा है।
पुरानी देरी एकीकरण चाल भी है, जहां आप एक प्रकार का पैरामीटर दिखाते हैं, यह बहुत सामान्य है और उसके बाद जीएचसी की बांह को एक उदाहरण के रूप में विशिष्ट बनाने में इसे मोड़ना है ... लेकिन यह विवादित शेंगेनियों में शामिल होने के लिए अधिकतर उपयोगी है। –
@ सीए.एमसीकैन आह, हाँ, अच्छा सुझाव। मैं इसे जोड़ दूंगा। –
प्रश्न पोस्ट करने से पहले मैंने फ़ंक्शन निर्भरताओं पर थोड़ा सा पढ़ा था, मुझे नहीं लगता था कि यह इस मामले पर लागू होता है क्योंकि मैं समझ नहीं पा रहा हूं कि संकलक बी के मूल्य को कैसे कम कर सकता है। मेरा मतलब है कि यह कहता है कि "बी का उपयोग करके पता चला जा सकता है", संकलक वास्तव में बी खोजने में मदद करते हैं? – Likhit