C # calculator--3 for Imitation Query Analyzer. Lexical analysis

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.