Ada 编程 / 算法 / Knuth-Morris-Pratt 模式匹配器
显示
	
	
-- pattern_match_knuth_morris_pratt_test.adb an implementation for fixed strings -- Written by Wikibob, 2004, from notes on the Knuth_Morris_Pratt pattern match algorithm -- adapted to fixed strings of characters. -- It is in the public domain. -- If you are using GNAT, use gnatmake to compile and link this program. -- To use the pattern match functions in your own software extract -- the inner package's specification and body into separate files. -- This program is self-contained and demonstrates a particular -- implementation of the Knuth_Morris_Pratt algorithm applied to -- fixed strings, with the following restrictions: -- * the search pattern is limited to a maximum of 256 characters -- * the caller must first call the function Pre_Compute on a pattern -- to obtain a context variable containing the pre-computed pattern. -- There is no limit to the number of contexts. -- * the caller must handle the exception Pattern_Error that will -- be raised if function Find_Location was unable to find the -- pattern in the given string. -- Suggested improvements to the inner package are: -- * add type Result_T is record Location : Index; Found : Boolean; end record; -- and use it instead of raising Pattern_Error. -- * produce a version that dispenses with the Context and has Find_Location -- perform the Pre_process internally. -- References: http://ww0.java3.datastructures.net/handouts/PatternMatching.pdfprocedurePattern_Match_Knuth_Morris_Pratt_Fixed_Testis-- You may extract this spec into file pattern_match.adspackagePattern_MatchisMax_Pattern_Length :constantPositive := 256;typeContextisprivate;functionPre_Compute (Pattern :inString)returnContext; -- precomputes the table of skips for the Pattern.functionFind_Location (Of_Context :inContext; In_Text :inString)returnPositive; Pattern_Error :exception; -- alternative is return Natural and use 0 to mean not found.privatesubtypePattern_Length_TisPositiverange1..Max_Pattern_Length;typeFailure_Function_Tisarray(Pattern_Length_T)ofPositive;subtypeSlided_Pattern_TisString (1 .. Max_Pattern_Length);typeContextisrecordFailure_Function : Failure_Function_T; M_Pattern : Slided_Pattern_T; Pattern_Length : Positive;endrecord;endPattern_Match; -- Variables and data for testing. IFPLID_Context : Pattern_Match.Context; SRC_Context : Pattern_Match.Context; Text_Test1 :constantString := "IMCHG DLH5877 -BEGIN ADDR -IFPLID AT05428113 -SRC FPL -RFL F330"; Text_Test2 :constantString := "IMCHG DLH5877 EDDKCLHD -BEGIN ADDR -FAC CFMUTACT AA05428113 FPL -STAR WLD5M -SRC "; IFPLID_Pos : Positive; IFPLID_Pos_2 : Positive := 1; SRC_Pos : Positive; SRC_Pos_2 : Positive; -- You may extract this spec into file pattern_match.adbpackagebodyPattern_MatchisfunctionPre_Compute (Pattern :inString)returnContextisI, J : Positive; Pattern_Context : Context;beginifPattern = ""thenraisePattern_Error;endif; Pattern_Context.M_Pattern (1..Pattern'Length) := Pattern; Pattern_Context.Pattern_Length := Pattern'Length; Pattern_Context.Failure_Function (1) := 1; I := 2; J := 1;whileI <= Pattern_Context.Pattern_LengthloopifPattern (I) = Pattern (J)then-- we have matched J + 1 chars. Pattern_Context.Failure_Function (I) := J + 1; I := I + 1; J := J + 1;elsifJ > 1then-- use failure function to shift Pattern J := Pattern_Context.Failure_Function (J - 1);elsePattern_Context.Failure_Function (I) := 1; I := I + 1;endif;endloop;returnPattern_Context;endPre_Compute;functionFind_Location (Of_Context :inContext; In_Text :inString)returnPositiveissubtypeSlided_Text_TisString (1 .. In_Text'Length); Slided_Text :constantSlided_Text_T := Slided_Text_T (In_Text); I, J : Positive;beginI := 1; J := 1;whileI <= Slided_Text'LastloopifSlided_Text (I) = Of_Context.M_Pattern (J)thenifJ = Of_Context.Pattern_LengththenreturnI - J + 1;elseI := I + 1; J := J + 1;endif;elsifJ > 1thenJ := Of_Context.Failure_Function (J - 1);elseI := I + 1;endif;endloop;raisePattern_Error; -- Or change function to return Natural and return 0.endFind_Location;endPattern_Match; -- You may extract the rest of this file into file pattern_match_test.adb -- and modify accordingly.procedureCheck_Pattern_Found (Pattern :inString; At_Location :inPositive; In_Text :inString)issubtypeSlided_Text_TisString (1 .. Pattern'Length); Slided_Pattern :constantSlided_Text_T := Slided_Text_T (Pattern);beginifAt_Location > In_Text'LastorelseAt_Location + Pattern'Length - 1 > In_Text'LastorelseSlided_Text_T (In_Text (At_Location .. At_Location + Pattern'Length - 1)) /= Slided_Patternthen-- We expected Find_Location to return the location of the pattern, as it did not there is a program error.raiseProgram_Error;endif;endCheck_Pattern_Found;beginIFPLID_Context := Pattern_Match.Pre_Compute ("-IFPLID "); SRC_Context := Pattern_Match.Pre_Compute ("-SRC "); Expect_Pattern_Found:beginIFPLID_Pos := Pattern_Match.Find_Location (Of_Context => IFPLID_Context, In_Text => Text_Test1);exceptionwhenPattern_Match.Pattern_Error => -- We expected Find_Location to find the pattern, but it did not so there is a program error.raiseProgram_Error;endExpect_Pattern_Found; Check_Pattern_Found (Pattern => "-IFPLID ", At_Location => IFPLID_Pos, In_Text => Text_Test1); Expect_Pattern_Not_Found:beginIFPLID_Pos_2 := Pattern_Match.Find_Location (Of_Context => IFPLID_Context, In_Text => Text_Test2); -- We expected Find_Location to NOT find the pattern, but it did so there is a program error.raiseProgram_Error;exceptionwhenPattern_Match.Pattern_Error => -- We expected Find_Location to NOT find the pattern, and it did not so there is no error.null;endExpect_Pattern_Not_Found;ifIFPLID_Pos_2 /= 1then-- We expected Find_Location to NOT return a result, so there is a program error.raiseProgram_Error;endif; Expect_Second_Pattern_Found:beginSRC_Pos := Pattern_Match.Find_Location (Of_Context => SRC_Context, In_Text => Text_Test1);exceptionwhenPattern_Match.Pattern_Error => -- We expected Find_Location to find the pattern, but it did not so there is a program error.raiseProgram_Error;endExpect_Second_Pattern_Found; Check_Pattern_Found (Pattern => "-SRC ", At_Location => SRC_Pos, In_Text => Text_Test1); Expect_Second_Pattern_Found_At_End:beginSRC_Pos_2 := Pattern_Match.Find_Location (Of_Context => SRC_Context, In_Text => Text_Test2);exceptionwhenPattern_Match.Pattern_Error => -- We expected Find_Location to find the pattern, but it did not so there is a program error.raiseProgram_Error;endExpect_Second_Pattern_Found_At_End; Check_Pattern_Found (Pattern => "-SRC ", At_Location => SRC_Pos_2, In_Text => Text_Test2);endPattern_Match_Knuth_Morris_Pratt_Fixed_Test;