A virtual stack-based expression parsing method and system

Through the expression parsing method based on the virtual stack, complex expressions are decomposed into simple computational expressions and converted into three-address code instruction sets, which solves the problem of high CPU resource occupancy under traditional methods and improves the operational sensitivity of embedded protection devices.

CN114064121BActive Publication Date: 2025-05-06XJ GRP CORP
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111338146.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-11
Publication Date
2025-05-06
Estimated Expiration
2041-11-11

AI Technical Summary

Technical Problem

When traditional LUA script conversion methods frequently call expression calculation formulas, the CPU resource occupancy rate of embedded protection devices is too high, reducing operational sensitivity.

Method used

Using a virtual stack-based expression parsing method, complex expressions are decomposed into multiple simple computational expressions and converted into a three-address code instruction set that can be efficiently executed by an embedded protection device.

Benefits of technology

The CPU resource occupancy rate of embedded protection devices is reduced and the operational sensitivity is improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114064121B_ABST
    Figure CN114064121B_ABST
Patent Text Reader

Abstract

The invention discloses an expression parsing method and system based on a virtual stack, wherein the method comprises: obtaining the number of expression data items and the number of expression operators; when the number of expression data items is greater than the number of expression operators and the next character unit is a data item or when the number of expression data items is less than or equal to the number of expression operators and the next character unit is an operator, outputting an expression format error mark; when the number of expression data items is greater than the number of expression operators and the next character unit is an operator, pushing the operator into a second virtual stack according to the priority of the operator in the next character unit; when the number of expression data items is less than or equal to the number of expression operators and the next character unit is a data item, pushing the data item into a first virtual stack, and ending the expression parsing when the second virtual stack is empty. By adopting a virtual stack to parse the expression, the expression is converted into an instruction set that can be efficiently executed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of power equipment control, and in particular to an expression parsing method and system based on a virtual stack. Background Art

[0002] Embedded protection devices in power systems need to be configured at the construction site according to user needs to implement some logical functions. The tool converts the user-configured logical expression text into a format that the device can recognize and downloads it to the protection device. The protection device needs to be able to correctly parse and execute the expression. Traditionally, the LUA script conversion method is used to achieve this requirement, such as Figure 1 As shown. After the user completes the configuration of the policy table formula, the expression is converted into a LUA script through the tool, the script file is downloaded to the device, and the main program calls the script file to calculate the expression. However, the execution efficiency of the LUA script is low. For frequently called expression calculation formulas, the operation of a large number of LUA scripts will additionally occupy the CPU resources of the embedded protection device, reducing the operating sensitivity of the embedded protection device. Summary of the invention

[0003] The purpose of an embodiment of the present invention is to provide an expression parsing method and system based on a virtual stack. By adopting a virtual stack to parse the expression, a complex expression is decomposed into multiple simple calculation expressions, and the simple calculation expressions are converted into a three-address code instruction set that can be efficiently executed by an embedded protection device, thereby reducing the CPU resource occupancy of the embedded protection device and improving the operation sensitivity.

[0004] To solve the above technical problems, a first aspect of an embodiment of the present invention provides an expression parsing method based on a virtual stack, wherein a first virtual stack stores expression data items, and a second virtual stack stores expression operators, and the method comprises the following steps:

[0005] Obtain the number of expression data items and the number of expression operators;

[0006] When the number of expression data items is greater than the number of expression operators and the next character unit is the data item, or when the number of expression data items is less than or equal to the number of expression operators and the next character unit is the operator, an expression format error flag is output;

[0007] When the number of expression data items is greater than the number of expression operators and the next character unit is the operator, the operator is pushed into the second virtual stack according to the priority of the operator in the next character unit;

[0008] When the number of expression data items is less than or equal to the number of expression operators and the next character unit is the data item, the data item is pushed into the first virtual stack, and parsing of the expression ends when the second virtual stack is empty.

[0009] Further, the step of pushing the operator into the second virtual stack according to the priority of the operator in the next character unit comprises:

[0010] If the priority of the operator in the next character unit is lower than the priority of the operator at the top of the second virtual stack, pushing the operator in the next character unit into the second virtual stack;

[0011] If the priority of the operator in the next character unit is higher than the priority of the operator at the top of the second virtual stack, two of the data items are popped from the first virtual stack and one of the operators is popped from the second virtual stack to generate a three-address code instruction, and the operation result of the three-address code instruction is pushed into the first virtual stack and the operator in the next character unit is pushed into the second virtual stack.

[0012] Further, after the parsing of the expression is finished when the second virtual stack is empty, the method further includes:

[0013] When the second virtual stack is not empty, the first virtual stack pops two of the data items and the second virtual stack pops one of the operators to generate a three-address code instruction, and the operation result of the three-address code instruction is pushed into the first virtual stack.

[0014] Furthermore, the three-address code instruction includes: an operator, a first operation object, a second operation object and an operation result.

[0015] Further, after pushing the data item into the first virtual stack, the method further includes:

[0016] Determine whether the character parsing is completed;

[0017] If yes, executing the step of ending parsing the expression when the second virtual stack is empty;

[0018] If not, execute the step of obtaining the number of expression data items and the number of expression operators.

[0019] Accordingly, a second aspect of an embodiment of the present invention provides an expression parsing system based on a virtual stack, wherein a first virtual stack stores expression data items, and a second virtual stack stores expression operators, including:

[0020] An acquisition module, which is used to acquire the number of expression data items and the number of expression operators;

[0021] A first control module, which is used to output an expression format error flag when the number of expression data items is greater than the number of expression operators and the next character unit is the data item, or when the number of expression data items is less than or equal to the number of expression operators and the next character unit is the operator;

[0022] A second control module, which is used for, when the number of the expression data items is greater than the number of the expression operators and the next character unit is the operator, pushing the operator into the second virtual stack according to the priority of the operator in the next character unit;

[0023] The third control module is used to push the data item into the first virtual stack when the number of the expression data items is less than or equal to the number of the expression operators and the next character unit is the data item, and end parsing the expression when the second virtual stack is empty.

[0024] Furthermore, the second control module includes:

[0025] a first control unit, configured to push the operator in the next character unit into the second virtual stack when the priority of the operator in the next character unit is lower than the priority of the operator at the top of the second virtual stack;

[0026] A second control unit is used to pop two of the data items from the first virtual stack and pop one of the operators from the second virtual stack when the priority of the operator in the next character unit is higher than the priority of the operator at the top of the second virtual stack, generate a three-address code instruction, push the operation result of the three-address code instruction into the first virtual stack and push the operator in the next character unit into the second virtual stack.

[0027] Furthermore, the third control module is also used to, when the second virtual stack is not empty, pop two of the data items from the first virtual stack and pop one of the operators from the second virtual stack, generate a three-address code instruction, and push the operation result of the three-address code instruction into the first virtual stack.

[0028] Furthermore, the three-address code instruction includes: an operator, a first operation object, a second operation object and an operation result.

[0029] Furthermore, the third control module further includes:

[0030] A first judging unit, used for judging whether the character parsing is finished;

[0031] A third control unit, configured to execute a step of ending parsing of the expression when the second virtual stack is empty at the end of the character parsing;

[0032] The third control unit is also used to execute the step of obtaining the number of expression data items and the number of expression operators when the character parsing is not completed.

[0033] The above technical solution of the embodiment of the present invention has the following beneficial technical effects:

[0034] By using a virtual stack to parse expressions, complex expressions are decomposed into multiple simple calculation expressions, and then the simple calculation expressions are converted into a three-address code instruction set that can be efficiently executed by the embedded protection device, thereby reducing the CPU resource occupancy of the embedded protection device and improving the operation sensitivity. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] Figure 1 It is a schematic diagram of the traditional expression calculation method;

[0036] Figure 2 It is a schematic diagram of an instruction set formatting implementation method provided by an embodiment of the present invention;

[0037] Figure 3 It is a step diagram of an expression parsing method based on a virtual stack provided by an embodiment of the present invention;

[0038] Figure 4 It is a logic diagram of an expression parsing method based on a virtual stack provided by an embodiment of the present invention;

[0039] Figure 5 It is a schematic diagram of stacking and popping provided by an embodiment of the present invention;

[0040] Figure 6 It is a module block diagram of an expression parsing system based on a virtual stack provided by an embodiment of the present invention;

[0041] Figure 7 is a block diagram of a second control module provided by an embodiment of the present invention;

[0042] Figure 8 This is a block diagram of a third control module provided in an embodiment of the present invention.

[0043] Reference numerals:

[0044] 1. Acquisition module, 2. First control module, 3. Second control module, 31. First control unit, 32. Second control unit, 4. Third control module, 41. First judgment unit, 42. Third control unit. DETAILED DESCRIPTION

[0045] In order to make the purpose, technical scheme and advantages of the present invention clearer, the present invention is further described in detail below in conjunction with specific embodiments and with reference to the accompanying drawings. It should be understood that these descriptions are only exemplary and are not intended to limit the scope of the present invention. In addition, in the following description, the description of well-known structures and technologies is omitted to avoid unnecessary confusion of the concept of the present invention.

[0046] Please refer to Figure 2 and Figure 3 A first aspect of an embodiment of the present invention provides an expression parsing method based on a virtual stack, wherein a first virtual stack (i.e., stack 1#) stores expression data items, and a second virtual stack (i.e., stack 2#) stores expression operators, comprising the following steps:

[0047] Step S100, obtaining the number of expression data items and the number of expression operators.

[0048] Step S200, when the number of expression data items is greater than the number of expression operators and the next character unit is a data item, or when the number of expression data items is less than or equal to the number of expression operators and the next character unit is an operator, an expression format error flag is output.

[0049] Step S300, when the number of expression data items is greater than the number of expression operators and the next character unit is an operator, the operator is pushed into the second virtual stack according to the priority of the operator in the next character unit.

[0050] Step S400, when the number of expression data items is less than or equal to the number of expression operators and the next character unit is a data item, the data item is pushed into the first virtual stack, and expression parsing is terminated when the second virtual stack is empty.

[0051] The above method converts expressions into an implementation method of an instruction set. The parsing process creates virtual stacks for data items and operators respectively. During the expression parsing process, the data items and operators are pushed into their respective stacks. When certain conditions are met, the data and operators are popped out of the stack. A push operation of data can be converted into an assignment instruction; a pop operation of data can be converted into an operation instruction, which is stored in the format of three-address code instructions (each instruction contains an operator, an operation object 1, an operation object 2 and a result), and can be executed quickly and efficiently by embedded devices.

[0052] Specifically, in step S300, the operator is pushed into the second virtual stack according to the priority of the operator in the next character unit, including:

[0053] Step S310: If the priority of the operator in the next character unit is lower than the priority of the top operator in the second virtual stack, the operator in the next character unit is pushed into the second virtual stack.

[0054] Step S320, if the priority of the operator in the next character unit is higher than the priority of the top operator of the second virtual stack, two data items are popped from the first virtual stack and one operator is popped from the second virtual stack, a three-address code instruction is generated, and the operation result of the three-address code instruction is pushed into the first virtual stack and the operator in the next character unit is pushed into the second virtual stack.

[0055] Specifically, in step S400, after the parsing expression is finished when the second virtual stack is empty, the following steps are further included:

[0056] Step S410, when the second virtual stack is not empty, two data items are popped from the first virtual stack and one operator is popped from the second virtual stack to generate a three-address code instruction, and the operation result of the three-address code instruction is pushed into the first virtual stack.

[0057] Furthermore, the three-address code instruction includes: an operator, a first operation object, a second operation object and an operation result.

[0058] In addition, in step S400, after pushing the data item into the first virtual stack, the method further includes:

[0059] Step S421, determining whether character parsing is completed.

[0060] Step S422: If yes, execute the step of ending the parsing expression when the second virtual stack is empty.

[0061] Step S423, if not, execute the step of obtaining the number of expression data items and the number of expression operators.

[0062] Please refer to Figure 4 In a specific implementation of the embodiment of the present invention, the expression parsing process based on the virtual stack is as follows:

[0063] 1) Create stack #1 to store the data items in the expression; create stack #2 to store the operators in the expression;

[0064] 2) Determine whether the number of data items in stack 1# is greater than the number of operators in stack 2#? If it is greater, the process goes to step 9); if it is less than or equal to, the process goes to step 3);

[0065] 3) Parse the next valid character unit, is it a data item? If yes, the process goes to step 4); if not, the process goes to step 14);

[0066] 4) Push the data item onto the stack and generate a data assignment instruction;

[0067] 5) Determine whether character parsing is finished? If yes, the process goes to step 6); if no, the process goes to step 2);

[0068] 6) Determine whether stack 2 is empty? If yes, the process goes to step 15); if no, the process goes to step 7);

[0069] 7) Pop two data items from stack #1 and one operator from stack #2. The original positions of the two data items in the stack are used as identifiers of operand 1 and operand 2. The top position of the current stack is defined as the result identifier, and an operation instruction is generated.

[0070] 8) Push the result identifier into stack #1 to generate an assignment instruction; the process goes to step 6);

[0071] 9) Parse the next valid character unit, is it an operator? If yes, the process goes to step 10); if not, the process goes to step 14);

[0072] 10) Does the new operator have a lower priority than the top operator in stack 2? If yes, the process goes to step 11); if no, the process goes to step 12);

[0073] 11) Pop two data items from stack #1 and one operator from stack #2. The original positions of the two data items in the stack are used as identifiers of operand 1 and operand 2. The top position of the current stack is defined as the result identifier, and an operation instruction is generated.

[0074] 12) Push the result identifier into stack #1 and generate an assignment instruction;

[0075] 13) Push the new operator onto stack 2; the process goes to step 2);

[0076] 14) Set the expression parsing failure flag to TRUE;

[0077] 15) End the parsing process.

[0078] Please refer to Figure 5 Taking the expression "X = A + B * C" as an example, the stack push and pop processes in the above steps are as follows: Figure 5 As shown in the figure, the data value at the top of stack 1# is the calculation result of the expression.

[0079] Accordingly, please refer to Figure 6 A second aspect of an embodiment of the present invention provides an expression parsing system based on a virtual stack, wherein a first virtual stack stores expression data items, and a second virtual stack stores expression operators, including:

[0080] Acquisition module 1, which is used to obtain the number of expression data items and the number of expression operators;

[0081] A first control module 2, which is used to output an expression format error flag when the number of expression data items is greater than the number of expression operators and the next character unit is a data item, or when the number of expression data items is less than or equal to the number of expression operators and the next character unit is an operator;

[0082] A second control module 3, which is used for pushing the operator into the second virtual stack according to the priority of the operator in the next character unit when the number of expression data items is greater than the number of expression operators and the next character unit is an operator;

[0083] The third control module 4 is used for pushing the data item into the first virtual stack when the number of expression data items is less than or equal to the number of expression operators and the next character unit is a data item, and ending the parsing expression when the second virtual stack is empty.

[0084] For further information, please refer to Figure 7 , the second control module 3 includes:

[0085] A first control unit 31, configured to push the operator in the next character unit into the second virtual stack when the priority of the operator in the next character unit is lower than the priority of the top operator in the second virtual stack;

[0086] The second control unit 32 is used to pop two data items from the first virtual stack and one operator from the second virtual stack when the priority of the operator in the next character unit is higher than the priority of the top operator of the second virtual stack, generate a three-address code instruction, push the operation result of the three-address code instruction into the first virtual stack and push the operator in the next character unit into the second virtual stack.

[0087] Furthermore, the third control module 4 is also used to pop two data items from the first virtual stack and one operator from the second virtual stack when the second virtual stack is not empty, generate a three-address code instruction, and push the operation result of the three-address code instruction into the first virtual stack.

[0088] Furthermore, the three-address code instruction includes: an operator, a first operation object, a second operation object and an operation result.

[0089] For further information, please refer to Figure 8 , the third control module 4 also includes:

[0090] A first judging unit 41, which is used to judge whether the character parsing is finished;

[0091] A third control unit 42, which is used to execute the step of ending the parsing expression when the second virtual stack is empty when the character parsing ends;

[0092] The third control unit 42 is further used to execute the step of obtaining the number of expression data items and the number of expression operators when the character parsing is not completed.

[0093] The embodiment of the present invention aims to protect an expression parsing method and system based on a virtual stack, which has the following effects:

[0094] By using a virtual stack to parse expressions, complex expressions are decomposed into multiple simple calculation expressions, and then the simple calculation expressions are converted into a three-address code instruction set that can be efficiently executed by the embedded protection device, thereby reducing the CPU resource occupancy of the embedded protection device and improving the operation sensitivity.

[0095] It should be understood that the above specific embodiments of the present invention are only used to illustrate or explain the principles of the present invention, and do not constitute a limitation of the present invention. Therefore, any modifications, equivalent substitutions, improvements, etc. made without departing from the spirit and scope of the present invention should be included in the protection scope of the present invention. In addition, the appended claims of the present invention are intended to cover all changes and modifications that fall within the scope and boundaries of the appended claims, or the equivalent forms of such scope and boundaries.

Claims

1. An expression parsing method based on a virtual stack, characterized in that: The first virtual stack stores expression data items, and the second virtual stack stores expression operators, comprising the following steps: Obtain the number of expression data items and the number of expression operators; When the number of the expression data items is greater than the number of the expression operators and the next character unit is the data item, or when the number of the expression data items is less than or equal to the number of the expression operators and the next character unit is the operator, an expression format error flag is output; When the number of expression data items is greater than the number of expression operators and the next character unit is the operator, pushing the operator into the second virtual stack according to the priority of the operator in the next character unit; When the number of expression data items is less than or equal to the number of expression operators and the next character unit is the data item, the data item is pushed into the first virtual stack, and parsing of the expression is terminated when the second virtual stack is empty.

2. The expression parsing method based on virtual stack according to claim 1, characterized in that: The step of pushing the operator into the second virtual stack according to the priority of the operator in the next character unit comprises: If the priority of the operator in the next character unit is lower than the priority of the operator at the top of the second virtual stack, pushing the operator in the next character unit into the second virtual stack; If the priority of the operator in the next character unit is higher than the priority of the operator at the top of the second virtual stack, two of the data items are popped from the first virtual stack and one of the operators is popped from the second virtual stack to generate a three-address code instruction, and the operation result of the three-address code instruction is pushed into the first virtual stack and the operator in the next character unit is pushed into the second virtual stack.

3. The expression parsing method based on virtual stack according to claim 1, characterized in that: After the parsing of the expression ends when the second virtual stack is empty, the method further includes: When the second virtual stack is not empty, the first virtual stack pops two of the data items and the second virtual stack pops one of the operators to generate a three-address code instruction, and the operation result of the three-address code instruction is pushed into the first virtual stack.

4. The expression parsing method based on virtual stack according to claim 2 or 3, characterized in that: The three-address code instruction includes: an operator, a first operation object, a second operation object and an operation result.

5. The expression parsing method based on virtual stack according to claim 1, characterized in that: After pushing the data item into the first virtual stack, the method further includes: Determine whether the character parsing is completed; If yes, executing the step of ending parsing the expression when the second virtual stack is empty; If not, execute the step of obtaining the number of expression data items and the number of expression operators.

6. An expression parsing system based on a virtual stack, characterized in that: The first virtual stack stores expression data items, and the second virtual stack stores expression operators, including: An acquisition module, which is used to acquire the number of expression data items and the number of expression operators; A first control module, which is used to output an expression format error flag when the number of expression data items is greater than the number of expression operators and the next character unit is the data item, or when the number of expression data items is less than or equal to the number of expression operators and the next character unit is the operator; A second control module, which is used for, when the number of the expression data items is greater than the number of the expression operators and the next character unit is the operator, pushing the operator into the second virtual stack according to the priority of the operator in the next character unit; The third control module is used to push the data item into the first virtual stack when the number of the expression data items is less than or equal to the number of the expression operators and the next character unit is the data item, and end parsing the expression when the second virtual stack is empty.

7. The expression parsing system based on virtual stack according to claim 6, characterized in that: The second control module includes: a first control unit, configured to push the operator in the next character unit into the second virtual stack when the priority of the operator in the next character unit is lower than the priority of the operator at the top of the second virtual stack; A second control unit is used to pop two of the data items from the first virtual stack and pop one of the operators from the second virtual stack when the priority of the operator in the next character unit is higher than the priority of the operator at the top of the second virtual stack, generate a three-address code instruction, push the operation result of the three-address code instruction into the first virtual stack and push the operator in the next character unit into the second virtual stack.

8. The expression parsing system based on virtual stack according to claim 6, characterized in that: The third control module is also used to, when the second virtual stack is not empty, pop two of the data items from the first virtual stack and pop one of the operators from the second virtual stack, generate a three-address code instruction, and push the operation result of the three-address code instruction into the first virtual stack.

9. The expression parsing system based on virtual stack according to claim 7 or 8, characterized in that: The three-address code instruction includes: an operator, a first operation object, a second operation object and an operation result.

10. The expression parsing system based on virtual stack according to claim 6, characterized in that: The third control module further includes: A first judging unit, used for judging whether the character parsing is finished; A third control unit, configured to execute a step of ending parsing of the expression when the second virtual stack is empty at the end of the character parsing; The third control unit is also used to execute the step of obtaining the number of expression data items and the number of expression operators when the character parsing is not completed.

Citation Information

Patent Citations

  • Expression calculation system based on encoding and decoding principle

    CN101944076A

  • Method and device for executing compiled PLC based on motion controller

    CN108303948A