Overview
In the field of application software development, the application of expression calculation has been widely used. For example, in report development, often provide user with formula input function, so as to achieve more flexible report summary; In workflow application software, the dynamic configuration of logic conditions is often used to provide more flexible process configuration. Also, in some UI development, you need to dynamically control the UI by evaluating the results of an expression of a property The display of the component. All of these applications can be summed up as a common model, that is, expression parsing and computation. The purpose of this paper is to provide an extensible expression analysis and its computational method.
General conditions and factors of expression parsing
The expression described in this article is a string that is expressed by a certain set of rules of operation, and a result can be obtained by parsing the expression string and using the rules represented by the operation. Expression parsing generally requires the following conditions to be met:
Supported set of Operators
Precedence of Operators
The set of action rules represented by the operator
The set of supported delimiters and the meaning that the delimiter represents
Supported collection of data types
Syntax constraints, such as naming rules, syntax rules represented by delimiters, and so on
Expression parsing in addition to the criteria that must be met, in some expression environments, functions and variables may also be supported. In conjunction with the issues to be addressed in this article, the following are the optional conditions:
Supported collection of internal functions
Supported internal global variables
Support function Customization
Support for custom variables
Support functions and operator overloading
The last three optional conditions are the conditions that the extensible support of an expression resolution engine needs to meet.
Again on the principle of compiling
The general compilation process mainly includes lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, target code generation, as shown in the following figure. Among them, the lexical analysis main task is the input source program, carries on the scanning and the decomposition to the program which forms the source program, identifies one word. Words are the most basic structure of language with independent meaning. Generally these words include reserved words, data types, operators, delimiters, and program-defined identifiers, constants, and so on, that are supported by the program language.
The main task of grammatical analysis is to combine word sequences into various grammatical units (usually represented as syntax trees) on the basis of lexical analysis and grammatical rules of language.
The main task of semantic analysis is to further analyze whether the program of grammatical structure conforms to the semantic constraint rules of source program, such as context constraint and operation compatibility.
Figure 1. Compiling general procedures
This is a very brief introduction of the general principles of compiler knowledge, detailed knowledge needs to refer to the compilation of books and documents related to the principle. As for the expression parsing in this article, I think it is essentially similar to program compiling, but it will be much simpler in the complexity of the problem. The expression parsing also needs to decompose the input expression string into a single word first, then combine the word sequences into grammatical units, and then analyze and compute the grammatical units according to the semantic constraints defined by the language environment of the expression. Therefore, we can use the basic method of compiling the program selectively to the expression parsing.