To undertake the previous article, this article on how to convert the expression into a token object, here is involved in the compiler principle of lexical analysis. On the principle of compiling I do not want to say, after all, I am not familiar with, now only know that there is a finite automaton concept. No matter what the concept, using code to achieve the ultimate goal.
Because it is not clear what characters in the string, can only be processed by one character, using a loop to take a character back to judge. This creates a tokenfactory notation "factory" class, which is responsible for parsing the expression and "producing" the Tokenrecord object. These include two methods, Lexicalanalysis and Producetoken. Lexicalanalysis is used for lexical analysis, and the Producetoken method is called after analyzing the marked object conforming to the rule, and the corresponding Tokenrecord object is produced. It steals a bit of laziness and writes all the methods in a static way so that you don't have to instantiate more than one subclass.
Derive multiple subcategories from this class:
Tokenkeywordfactory: for working with keywords
Tokensymbolfactory: For handling operators
Tokenstringfactory: For handling strings
Tokennumberfactory: for working with numbers
The class diagram is as follows: