# Include # Include Using namespace STD; Enum tokentype { Boom_error =-1, // aha, error Number = 1, Identifier = 2, If = 4 }; Int dfa_table [] [37] = { // 0 1 2 3 4 5 6 7 8 9 a B c d e f g h I j k l m n o p q r s t u v w x Y Z! , -1}, // S0 -- Starting Status {,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1}, // S1 -- here it is a number {3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1}, // S2 -- Variable {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 }, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1} // S4 -- this is if }; // // Match: // Specify a string 'str' to determine the string type. // // Example: // If, returns if // Number, return number // Variable, returns identifier // Tokentype match (string Str) { Int state = 0;
For (string: iterator iter = Str. Begin (); ITER! = Str. End (); ++ ITER) // some people may not get used to it here. Why do you use ++ ITER instead of the one most people get used? // Because ITER ++ returns the ITER value first and then executes ++, a copy operation is involved here. If your ITER is a very large variable // Performance may be affected, but ++ ITER only performs ++ operations { Char c = * ITER; Int Index = 0; If (C> = '0' & C <= '9 ') { Index = C-'0 '; } Else if (C> = 'A' & C <= 'Z ') { Index = C-'A' + 10; // index value of column A in dfa_table } Else { Index = 36 ;//! The index value of the column in dfa_table, which does not match } State = dfa_table [State] [Index]; If (State = boom_error) Break; } Return (tokentype) State; } Int g_line = 0; Void print (tokentype type) { Switch (type) { Case boom_error: Cout <++ g_line <": boom_error/N" <> Break;
Case if: Cout <++ g_line <": If/N" <> Break;
Case number: Cout <++ g_line <": Number/N" <> Break;
Case identifier: Cout <++ g_line <": identifier/N" <> Break;
Default: Cout <++ g_line <": Error/N" <> Break; } }
Int main () { Print (MATCH ("if ")); Print (MATCH ("IFF ")); Print (MATCH ("if0 ")); Print (MATCH ("0if ")); Print (MATCH ("i0f ")); Print (MATCH ("IA ")); Print (MATCH ("01 ")); Print (matching ("123 ")); Print (MATCH ("1f ")); Print (MATCH ("ABCD ")); Print (MATCH ("AB ")); Print (MATCH ("")); Print (MATCH ("0 ")); Print (MATCH ("I ")); Print (MATCH ("_"));
Return 0; }
|