A method, apparatus, and storage medium for recursively parsing and executing functions

By identifying and parsing recursive functions in ST structured text language, compiling and outputting and executing interpreted instruction sequences, and using frame stack data structure to manage recursive function calls, the problem that ST language does not support recursive function calls is solved, and the effective execution of complex algorithms is realized.

CN114594960BActive Publication Date: 2025-06-27NR ELECTRIC CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210213158.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-04
Publication Date
2025-06-27
Estimated Expiration
2042-03-04

AI Technical Summary

Technical Problem

The ST structured text language defined by the IEC61131-3 standard does not support recursive function calls and cannot meet the implementation needs of complex algorithms, especially in scenarios where online disturbance-free updates are required in the field of process control.

Method used

It provides a recursive function parsing execution method. By defining the instruction set used for function parsing, identifying recursive functions and non-recursive functions in ST structured text language, compiling the output interpreted instruction sequence, and obtaining the function instruction sequence through parser parsing, executing recursive function instructions and non-recursive function instructions in order, and using the frame stack data structure to manage the call stack of recursive functions.

Benefits of technology

It realizes the correct analysis and execution of recursive functions, expands the functions of structured text language, and meets the implementation needs of industrial control complex algorithms.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114594960B_ABST
    Figure CN114594960B_ABST
Patent Text Reader

Abstract

The present invention discloses a method, apparatus, and storage medium for recursively parsing and executing functions. The method includes: defining an instruction set for function parsing; identifying recursive functions and non-recursive functions in ST structured text language and outputting an interpreted instruction sequence; obtaining a function instruction sequence by parsing the interpreted instruction sequence through a parser, where the function instruction sequence includes recursive function instructions and non-recursive function instructions; executing the recursive function instructions and non-recursive function instructions in the order of the function instruction sequence; wherein, when executing the recursive function instructions, a call stack of a frame stack type data structure is established based on the data frame of the recursive function instructions; and the processing functions of the recursive function instructions are sequentially called from the recursive function instruction set based on the order of the call stack for execution until the call stack is empty. The present invention makes up for the deficiency that the structured text language of the IEC61131-3 standard does not support recursive calls, improves the flexibility of the program, and meets the usage requirements of complex algorithms.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to a method, device and storage medium for recursively parsing and executing functions, belonging to the technical field of instruction interpretation and execution. Background Art

[0002] The ST structured text language defined by the IEC61131-3 standard provides a large number of abstract statements in a highly compressed manner to describe the functions of the replication control system. It is especially developed for industrial control applications, but this language does not support recursive function calls and cannot meet the implementation requirements of some complex algorithms. In the process control field, in order to meet the requirements of online non-disruptive updates, a parsing and execution method is usually adopted, which needs to support the parsing and execution of recursive functions. Summary of the Invention

[0003] The purpose of the present invention is to overcome the deficiencies in the prior art and provide a method, device and storage medium for recursively parsing and executing functions, which support recursive function call execution and meet the application development requirements of complex algorithms.

[0004] To achieve the above object, the present invention is implemented by the following technical solutions:

[0005] In a first aspect, the present invention provides a method for recursively parsing and executing functions, including:

[0006] Defining an instruction set for function parsing, where the instruction set includes a recursive function instruction set and a non-recursive function instruction set;

[0007] Identifying recursive functions and non-recursive functions in the ST structured text language, and compiling and outputting an interpreted instruction sequence based on the recursive functions and non-recursive functions;

[0008] Obtaining a function instruction sequence by parsing the interpreted instruction sequence through a parser, where the function instruction sequence includes recursive function instructions and non-recursive function instructions;

[0009] Executing the recursive function instructions and non-recursive function instructions in the order of the function instruction sequence;

[0010] Wherein, when executing the recursive function instructions, a call stack of a frame stack type data structure is established based on the data frame of the recursive function instructions;

[0011] Sequentially calling the processing functions of the recursive function instructions from the recursive function instruction set based on the order of the call stack for execution until the call stack is empty.

[0012] Optionally, the instruction set is a custom virtual machine instruction set. The non-recursive function instruction set includes memory operation instructions, arithmetic operation instructions, logical operation instructions, bit operation instructions, comparison operation instructions, flow control instructions, and non-recursive function call instructions. The recursive function instruction set includes new data frame instructions, delete data frame instructions, data push stack instructions, data pop stack instructions, formal parameter passing and assignment instructions, and recursive function call instructions.

[0013] Optionally, the recognition of recursive functions and non-recursive functions in the ST structured text language includes:

[0014] Perform lexical analysis, syntax analysis, and semantic analysis on the ST structured text language to generate a function semantic tree.

[0015] Traverse its child nodes in depth-first order based on the function semantic tree, mark the first node that returns to the function semantic tree with child nodes as a recursive function, and mark the first node that returns to the function semantic tree without child nodes as a non-recursive function.

[0016] Optionally, the compiled output interpreted instruction sequence includes:

[0017] Add 1 output variable to the explicitly declared variable area F.data in the allocated data area to record the return value ret of the recursive function.

[0018] Add instructions for passing formal parameters in the preamble part to copy the initial source data area of the recursive function and non-recursive function to the local data area.

[0019] Compile the data in the local data area and output non-recursive function statement instructions or recursive function call instructions in sequence to generate an interpreted instruction sequence.

[0020] Among them, during the compilation process, it is judged whether the recursive call of the recursive function is encountered. If so, output a recursive function call instruction; if not, output a non-recursive function statement instruction.

[0021] Optionally, the function instruction sequence is managed by a linked list, a frame of data of the non-recursive function instruction is managed by a vector array, and the data frame of the recursive function instruction is managed by a stack.

[0022] Optionally, the execution of calling the processing function of the recursive function instruction from the recursive function instruction set includes:

[0023] Cache the current recursive function instruction subscript.

[0024] Set the current recursive function instruction subscript to 0.

[0025] Obtain the newly allocated data frame of the recursive function instruction according to the recursive call of the recursive function.

[0026] Call the exec function according to the data frame to execute the processing function of the recursive function instruction;

[0027] Restore the current subscript of the recursive function instruction.

[0028] In a second aspect, the present invention provides a recursive function parsing and execution device, the device includes:

[0029] An instruction set definition module, configured to define an instruction set for function parsing, the instruction set includes a recursive function instruction set and a non-recursive function instruction set;

[0030] An interpreted instruction sequence acquisition module, configured to identify recursive functions and non-recursive functions in the ST structured text language, and compile and output an interpreted instruction sequence based on the recursive functions and non-recursive functions;

[0031] A function instruction sequence acquisition module, configured to parse the interpreted instruction sequence through a parser to obtain a function instruction sequence, the function instruction sequence includes recursive function instructions and non-recursive function instructions;

[0032] A function instruction sequence execution module, configured to execute recursive function instructions and non-recursive function instructions in the order of the function instruction sequence;

[0033] Wherein, when executing a recursive function instruction, a call stack of a frame stack type data structure is established based on the data frame of the recursive function instruction;

[0034] Call the processing function of the recursive function instruction from the recursive function instruction set in sequence based on the order of the call stack for execution until the call stack is empty.

[0035] In a third aspect, the present invention provides a recursive function parsing and execution device, including a processor and a storage medium;

[0036] The storage medium is used to store instructions;

[0037] The processor is configured to operate according to the instructions to execute the steps of the above method.

[0038] In a fourth aspect, the present invention provides a computer-readable storage medium, on which a computer program is stored, characterized in that when the program is executed by a processor, the steps of the above method are implemented.

[0039] Compared with the prior art, the beneficial effects achieved by the present invention:

[0040] The present invention provides a method, apparatus, and storage medium for recursively parsing and executing functions. By identifying recursive functions, when a recursive function is called in an upper-layer program, a non-recursive function call instruction is used to pass data formal parameters. When the recursive function calls itself internally, a recursive function call instruction is used to create a frame stack type data structure. By performing push and pop operations to cache the data areas of several calls and restore the context before and after the call, the correct parsing and execution of the recursive function are achieved, effectively expanding the functions of the structured text language and meeting the implementation requirements of complex industrial control algorithms. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] Figure 1 is a flowchart of a method for recursively parsing and executing functions provided by an embodiment of the present invention;

[0042] Figure 2 is a schematic diagram of the principle of recursive function instruction output provided by an embodiment of the present invention;

[0043] Figure 3 is an example diagram of a recursive function data frame stack provided by an embodiment of the present invention;

[0044] Figure 4 is a schematic diagram of the principle of recursive function parsing implementation provided by an embodiment of the present invention;

[0045] Figure 5 is an example diagram of recursive function parsing verification provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0046] The present invention will be further described below with reference to the accompanying drawings. The following embodiments are only used to more clearly illustrate the technical solutions of the present invention and should not be used to limit the protection scope of the present invention.

[0047] Embodiment 1:

[0048] As Figure 1 shown, the present invention provides a method for recursively parsing and executing functions, including:

[0049] 1. Define an instruction set for function parsing, where the instruction set includes a recursive function instruction set and a non-recursive function instruction set;

[0050] 2. Identify recursive functions and non-recursive functions in ST structured text language and compile and output an interpreted instruction sequence based on the recursive functions and non-recursive functions;

[0051] 3. Parse the interpreted instruction sequence through a parser to obtain a function instruction sequence, where the function instruction sequence includes recursive function instructions and non-recursive function instructions;

[0052] 4. Execute the recursive function instructions and non-recursive function instructions in the order of the function instruction sequence;

[0053] Among them, when executing a recursive function instruction, a call stack of a frame stack type data structure is established based on the data frame of the recursive function instruction;

[0054] The processing functions of the recursive function instructions are sequentially called from the recursive function instruction set based on the order of the call stack for execution until the call stack is empty.

[0055] (1) The instruction set is a custom virtual machine instruction set. The non-recursive function instruction set includes memory operation instructions, arithmetic operation instructions, logical operation instructions, bit operation instructions, comparison operation instructions, flow control instructions, and non-recursive function call instructions; the recursive function instruction set includes new data frame instructions, delete data frame instructions, data push stack instructions, data pop stack instructions, formal parameter passing and assignment instructions, and recursive function call instructions.

[0056] (2) All recursive function instructions operate based on a private data area, and additional instructions are added for formal parameter assignment and function return value processing in the prologue and epilogue phases respectively.

[0057] The instruction mnemonics for recursive processing are shown in Table 1:

[0058] Table 1:

[0059]

[0060] (3) Identifying recursive functions in ST structured text language includes:

[0061] Performing lexical analysis (based on the flex tool), syntax analysis (based on the bison tool), and semantic analysis on the ST structured text language containing recursive functions to generate a function semantic tree;

[0062] Traversing the child nodes of the function semantic tree in depth-first order. If backtracking returns to the first node of the function semantic tree, set the recursive function flag.

[0063] (4) Compiling and outputting the recursive function instruction sequence includes:

[0064] Allocating a data area and adding 1 output variable to the explicitly declared variable area F.data to record the return value ret of the recursive function;

[0065] Adding instructions for formal parameter passing in the prologue part to copy the initial source data area of the function to the local data area;

[0066] During the compilation process, determine whether a recursive call is encountered. If so, output recursive function instructions; if not, output non-recursive function instructions;

[0067] The recursive function instruction sequence and the non-recursive function instruction sequence are obtained according to the output recursive function instructions and non-recursive function instructions respectively.

[0068] As Figure 2 shown, the compilation output of the recursive function and the non-recursive function instruction sequence includes:

[0069] a) Allocate a data area and add 1 output variable in the explicitly declared variable area F.data to record the function return value ret;

[0070] b) Add instructions for passing formal parameters in the prologue part to copy the initial source data area of the function to the local data area;

[0071] c) Output non-recursive function instructions;

[0072] d) When encountering a recursive call, output recursive function instructions;

[0073] e) Output non-recursive function instructions.

[0074] (5) As Figures 3 - 4 shown, the frame stack type data structure uses a linked list to manage the recursive function instruction sequence, a vector array to manage one frame of data, and a stack to manage the data frame sequence of recursive function calls.

[0075] (6) As Figure 4 shown, the instruction sequence adopts a sequential execution method. When executing a recursive function type instruction, the corresponding instruction sequence is obtained according to the recursive function type number and a frame stack type data structure is created, and the recursive function is executed in the following order:

[0076] a) Cache the current recursive function instruction subscript;

[0077] b) Set the current recursive function instruction subscript to 0;

[0078] c) Obtain a newly allocated data frame;

[0079] d) Call exec to execute the recursive function;

[0080] e) Restore the current recursive function instruction subscript.

[0081] (7) When a recursive function is called in the program of the program organization unit POU, the data area first used by the recursive function is allocated in the data area of the program organization unit POU. The instruction stream output steps at the place where the recursive function is called in the program organization unit POU are as follows:

[0082] a) Output the instruction of the formal parameter passing statement for the recursive function call;

[0083] b) Output the non-recursive function call instruction fcall;

[0084] c) Output the processing instruction for the return value of the recursive function.

[0085] Based on the above embodiments, as Figure 5 shown, taking the typical Factorial function as an example, the corresponding ST source code, the data area after compilation, the data frame, the decompiled instruction sequence, and the execution result are respectively shown. The result shows that the recursive function is correctly executed and the correct calculated value is returned.

[0086] Embodiment 2:

[0087] The embodiment of the present invention provides a recursive function parsing and execution device, which includes:

[0088] An instruction set definition module, which is used to define an instruction set applicable to recursive function parsing;

[0089] An instruction sequence acquisition module, which is used to identify the recursive function in the ST structured text language and compile and output the recursive function instruction sequence;

[0090] An instruction sequence processing module, which is used to parse the recursive function instruction sequence through a parser to obtain recursive function instructions, and establish a call stack of a frame stack type data structure according to the recursive function instructions;

[0091] An instruction sequence execution module, which is used to sequentially call the processing functions of the corresponding instruction set based on the call stack to complete the execution of the recursive function until the call stack is empty.

[0092] Embodiment 3:

[0093] The present invention provides a recursive function parsing and execution device, including a processor and a storage medium;

[0094] The storage medium is used to store instructions;

[0095] The processor is used to operate according to the instructions to execute the steps of the method provided in Embodiment 1.

[0096] Embodiment 4:

[0097] The present invention provides a computer-readable storage medium, on which a computer program is stored, and when the program is executed by a processor, the steps of the method provided in Embodiment 1 are implemented.

[0098] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a system, or a computer program product. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.

[0099] The present application is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to the embodiments of the present application. It should be understood that each flow and / or block in the flowchart and / or block diagram, as well as the combination of flows and / or blocks in the flowchart and / or block diagram, can be realized by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate means for realizing the functions specified in Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.

[0100] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory generate a manufactured article including instruction means, and the instruction means realizes the functions specified in Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.

[0101] These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for realizing the functions specified in Figure 1 one flow or multiple flows and / or blocks Figure 1 one block or multiple blocks.

[0102] The above description is only the preferred embodiment of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the technical principle of the present invention, several improvements and modifications can be made, and these improvements and modifications should also be regarded as the protection scope of the present invention.

Claims

1. A method for recursively parsing and executing a function, characterized in that, Comprising: Defining an instruction set for function parsing, the instruction set including a recursive function instruction set and a non-recursive function instruction set; Identifying recursive functions and non-recursive functions in ST structured text language and compiling and outputting an interpreted instruction sequence based on the recursive functions and non-recursive functions; Obtaining a function instruction sequence by parsing the interpreted instruction sequence through a parser, the function instruction sequence including recursive function instructions and non-recursive function instructions; Executing the recursive function instructions and non-recursive function instructions in the order of the function instruction sequence; Wherein, when executing the recursive function instructions, a call stack of a frame stack type data structure is established based on the data frame of the recursive function instructions; Successively calling the processing functions of the recursive function instructions from the recursive function instruction set based on the order of the call stack for execution until the call stack is empty; Wherein, the compiling and outputting the interpreted instruction sequence based on the recursive functions and non-recursive functions: Adding 1 output variable to the explicitly declared variable area F.data in the data allocation area for recording the return value ret of the recursive function; Adding instructions for parameter passing in the preamble part to copy the initial source data area of the recursive function and non-recursive function to the local data area; Compiling the data in the local data area to sequentially output non-recursive function statement instructions or recursive function call instructions and generating an interpreted instruction sequence; Wherein, during the compilation process, it is judged whether a recursive call of a recursive function is encountered. If so, a recursive function call instruction is output. If not, a non-recursive function statement instruction is output.

2. The recursive function parsing and execution method according to claim 1, characterized in that The instruction set is a custom virtual machine instruction set. The non-recursive function instruction set includes memory operation instructions, arithmetic operation instructions, logical operation instructions, bit operation instructions, comparison operation instructions, flow control instructions and non-recursive function call instructions; the recursive function instruction set includes new data frame instructions, delete data frame instructions, data push instructions, data pop instructions, parameter passing and assignment instructions and recursive function call instructions.

3. A method for parsing and executing a recursive function according to claim 1, characterized in that, The identifying recursive functions and non-recursive functions in the ST structured text language includes: Performing lexical analysis, syntax analysis and semantic analysis on the ST structured text language to generate a function semantic tree; Traversing its child nodes in depth-first order based on the function semantic tree, marking the first node that returns to the function semantic tree with child nodes as a recursive function, and marking the first node that returns to the function semantic tree without child nodes as a non-recursive function.

4. A method for parsing and executing a recursive function according to claim 1, characterized in that, The function instruction sequence is managed by a linked list, one frame of data of the non-recursive function instructions is managed by a vector array, and the data frame of the recursive function instructions is managed by a stack.

5. A recursive function parsing and execution method according to claim 1, characterized in that The calling the processing functions of the recursive function instructions from the recursive function instruction set for execution includes: Caching the current recursive function instruction subscript; Setting the current recursive function instruction subscript to 0; Obtaining a newly allocated data frame of the recursive function instruction according to the recursive call of the recursive function; Calling the exec function according to the data frame to execute the processing function of the recursive function instruction; Restoring the current recursive function instruction subscript.

6. A recursive function parsing and execution device, characterized in that The apparatus includes: An instruction set definition module for defining an instruction set for function parsing, the instruction set including a recursive function instruction set and a non-recursive function instruction set; An interpreted instruction sequence acquisition module, configured to identify recursive functions and non-recursive functions in ST structured text language, and compile and output an interpreted instruction sequence based on the recursive functions and non-recursive functions; A function instruction sequence acquisition module, configured to obtain a function instruction sequence by parsing the interpreted instruction sequence through a parser, where the function instruction sequence includes recursive function instructions and non-recursive function instructions; A function instruction sequence execution module, configured to execute the recursive function instructions and non-recursive function instructions in the order of the function instruction sequence; wherein, when executing the recursive function instructions, a call stack of a frame stack type data structure is established based on the data frame of the recursive function instructions; The processing functions of the recursive function instructions are sequentially called from the recursive function instruction set based on the order of the call stack for execution until the call stack is empty; wherein, the interpreted instruction sequence is compiled and output based on the recursive functions and non-recursive functions: One output variable is added to the explicitly declared variable area F.data in the data allocation area to record the return value ret of the recursive function; Instructions for passing formal parameters are added in the prologue part to copy the initial source data areas of the recursive functions and non-recursive functions to the local data area; The data in the local data area is compiled to sequentially output non-recursive function statement instructions or recursive function call instructions, and an interpreted instruction sequence is generated; wherein, during the compilation process, it is judged whether a recursive call of a recursive function is encountered. If so, a recursive function call instruction is output. If not, a non-recursive function statement instruction is output.

7. A recursive function parsing and execution device, characterized in that Including a processor and a storage medium; The storage medium is used to store instructions; The processor is configured to operate according to the instructions to execute the steps of the method according to any one of claims 1-5.

8. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, the steps of the method according to any one of claims 1-5 are implemented.

Citation Information

Patent Citations

  • Rapid execution method and system for parser

    CN107015840A

  • Method for compiling program organizing unit

    CN108804105A