Structured text code generation and review method

By combining the methods of structured thinking chain and abstract syntax tree, the low efficiency and error identification problems of ST code writing and review in industrial automation control systems are solved, efficient intelligent code generation and repair are achieved, and the development efficiency and reliability of the system are improved.

CN120743231AActive Publication Date: 2025-10-03CHINA JILIANG UNIV

Patent Information

Application Number
CN202511254265.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-04
Publication Date
2025-10-03
Estimated Expiration
2045-09-04

AI Technical Summary

Technical Problem

In existing industrial automation control systems, the writing and review of structured text (ST) code faces problems such as high syntax standardization, deep logical structure nesting, complex variable scope, and time-consuming and labor-intensive debugging and repair. Traditional static analysis tools also have difficulty handling cross-module semantic errors, control path anomalies, or context dependency errors.

Method used

By combining the Structured Thinking Chain (SCoT) with the Abstract Syntax Tree (AST), we perform semantic analysis and structural decomposition of user requirements to generate an abstract syntax tree with deep grammatical features. This is combined with a large language model for code generation and review, and a "code teacher-code learner" mechanism is introduced for multiple rounds of automatic repair, enabling intelligent code generation and semantic-level error review.

Benefits of technology

It achieves high-quality automatic generation of ST code, significantly improving development efficiency and system stability. It can identify and fix syntax and semantic errors, provide structured output, and facilitate engineering integration and audit tracking.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743231A_ABST
    Figure CN120743231A_ABST
Patent Text Reader

Abstract

The invention discloses a structured text code generation and review method, which comprises the following steps of: receiving a control task demand input by a user, supporting natural language, structured form or graphical process input, performing deep semantic analysis by utilizing a demand analysis module, identifying control conditions, control actions and constraint factors, and generating and reviewing a structured text code. The task is converted into a plurality of control units through the SCoT chain; the SCoT chain reader generates a semantic vector according to a task demand, the AST reader generates AST representation through a grammar rule structure and a hierarchical relationship, and the decoder predicts a next grammar rule and generates an ST code in combination with the SCoT chain and AST features; and verifying grammar and logic of the ST code through an interactive automatic review and repair mechanism, if compiling errors occur, analyzing errors by the teacher model and providing repair suggestions, and performing code repair by the student model according to guidance until the code conforms to an expected function and passes compiling.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of computer technology, and in particular relates to a structured text code generation and review method. Background Art

[0002] In the field of industrial automation control, Structured Text (ST), as one of the primary programming languages ​​defined by the IEC61131-3 standard, is widely used in systems such as programmable logic controllers (PLCs) and distributed control systems (DCSs). ST boasts expressive capabilities similar to high-level languages, making it suitable for describing and implementing complex control logic. However, with the increasing complexity of industrial systems, the writing and review of ST code faces multiple challenges, including strict syntax requirements, deeply nested logical structures, complex variable scopes, and time-consuming and labor-intensive debugging and repair processes.

[0003] Existing ST code development relies on engineers manually designing control logic, writing, and debugging it. This lacks intelligent assistance and semantic-level error warning mechanisms, leading to difficulties in ensuring code quality and low development efficiency. While traditional static analysis tools can identify some syntactic and structural issues, they struggle to address cross-module semantic errors, control path anomalies, or context-dependent errors.

[0004] In recent years, with the widespread application of Large Language Models (LLMs) in code generation and repair, intelligent programming has gradually become an important direction for solving industrial programming problems. The Chain-of-Thought (CoT) reasoning mechanism has been proven to improve the model's ability to handle complex logical problems. However, its migration to industrial ST languages ​​still faces obstacles such as "weak control structure annotation" and "missing logical paths." Summary of the Invention

[0005] To address the shortcomings of existing technologies, achieve high-quality, interpretable ST code generation and semantic-level error review for complex control tasks, and improve the efficiency, reliability, and standardization of industrial control system development, the present invention adopts the following technical solutions:

[0006] A structured text code generation and review method comprises the following steps:

[0007] Step 1: Perform semantic analysis on user needs, extract task requirements and perform structural decomposition to obtain a structured thinking chain for the task;

[0008] Step 2: Generate a vector containing semantic information and extract natural language features through structured thought chains. Based on the structure and hierarchical relationship of grammatical rules, generate an abstract syntax tree feature containing deep grammatical features through the vector and rule sequence. Based on the features extracted by the structured thought chain and the abstract syntax tree, combine the query task node and the abstract syntax tree to predict the next grammatical rule and generate the corresponding code.

[0009] Step 3: Review and fix the code to get the final code.

[0010] Furthermore, the step 1 includes the following steps:

[0011] Step 1.1: Analyze user needs and break them down into a set of task modules;

[0012] Step 1.2: Identify and extract key semantics in the task through structured thought chains;

[0013] The structured thinking chain decomposes the task into multiple independent control units by reasoning and mapping the conditions, states and actions of the control task;

[0014] Step 1.3: Divide the control units into different types based on the logical relationships and control requirements of the tasks. For each control unit, determine the input variables, output variables, and dependencies.

[0015] Step 1.4: Construct a control task structure diagram and structured thinking chain mapping;

[0016] Map each control unit with its corresponding control logic relationship, and construct a structural diagram of the control task. Use nodes to represent control units and edges to represent the logical relationship between control units. Map each control unit with the corresponding structured thinking chain; ensure that the control intention of each unit can be clearly expressed through the "condition identification-state judgment-action mapping" path; generate a corresponding structured thinking chain for each control unit, and convert the condition identification, state judgment and action mapping of the task into a structured reasoning chain, and finally form a complete and structured task description, which provides a basis for subsequent ST code generation and verification.

[0017] Furthermore, in step 1.3, each control unit corresponds to a specific operation in the task, including a sequential structure class, a conditional judgment class, and a loop structure class. For the conditional judgment class, the conditional expression is used as input and the executed action is used as output; for the loop structure class, the control unit is constructed according to the loop condition, the number of iterations, and the operation type.

[0018] Furthermore, the step 2 includes the following steps:

[0019] Step 2.1: Tokenize the structured thought chain into a series of tokens, then convert them into vector representations through character embedding and fully connected layers. Natural language features are extracted from the vectors through self-attention mechanisms, gating mechanisms, and convolutional layers.

[0020] Step 2.2: Extract information from the structured rule sequence and capture the hierarchical relationship between grammar rules. Combine the representation of each rule node with the representation of its parent node to capture the deep dependency relationship in the tree structure.

[0021] Step 2.3: Use the non-terminal node to be expanded as the query node, combine the query path with the abstract syntax tree features, and apply the multi-head attention mechanism to extract structural and semantic information to predict the next grammatical rule or identifier and generate the corresponding code;

[0022] Furthermore, in step 2.1, each word unit is decomposed into characters, and the word unit's embedding vector is represented by character embedding through character vectors and character weight matrices; the position information of the character is encoded through position embedding; the embedding vector and position embedding are used to learn nonlinear features through a multi-head attention mechanism; the character vector based on the linear transformation, the character weight matrix and the head weight matrix learned by the multi-head attention mechanism are further combined with the character embedding information through a gating mechanism, and then the local features around the word unit are further extracted through a convolution operation;

[0023] Furthermore, in the step 2.2, a self-attention layer is constructed, and feature extraction is performed by adding deep embedding information to the rule embedding and the position embedding, and the extracted features are fused with the content encoding rules through a gating mechanism. The fused information is combined with the natural language description, and feature extraction is performed through natural language attention; based on the abstract syntax tree, the parent node features of the node are represented by the adjacency matrix and the features of the node, and the feature information of the current node and its ancestor node are combined through the tree convolution layer.

[0024] Furthermore, in step 2.3, the nodes in the path from the root node to the target expansion node are represented as vectors, and the vectors are converted into query node representations. Based on the features extracted from the abstract syntax tree, features are extracted according to the query nodes through the abstract syntax tree attention layer. Based on the extracted features, the input natural language description is integrated through the natural language attention mechanism to extract the features required for prediction, so as to predict the next grammar rule.

[0025] Furthermore, in step 2.3, based on the features required for the prediction, the probability of the next grammatical rule is predicted by the softmax function. At the same time, a pointer network based on the attention mechanism is introduced. When the pointer mechanism is enabled, the grammatical rules are generated, and the use of softmax function prediction or pointer network prediction is controlled by the gating mechanism.

[0026] Furthermore, the step 3 includes the following steps:

[0027] Step 3.1: Compile the code, verify whether the code meets the standards, and perform syntax checking. If the compilation is successful, the code is output; otherwise, the code is fed back to the teacher model;

[0028] Step 3.2: The teacher model analyzes the cause of the error, identifies the code problem, and provides repair guidance. The teacher model will propose suggestions for modifying the code through the reasoning path of the structured thinking chain, combined with the condition, state, and action mapping of the task.

[0029] Step 3.3: The student model repairs the code according to the teacher model's suggestions and recompiles the repaired code. If the compilation is successful, the code is output; otherwise, the code is fed back to the teacher model.

[0030] Step 3.4: Interactive review and repair based on the teacher model and student model until the repair and verification conditions are met and the final code is obtained.

[0031] Furthermore, in step 3.4, the generation of the enhanced control task logic is to select a subset from the relevant knowledge set obtained from its path planning and retrieval enhancement based on the retrieval source to generate a preliminary control task graph for the enhanced control task; the control requirements and execution operations of the task are to obtain the task requirements and corresponding code examples from the example set; based on the enhanced control task logic and the control requirements and execution operations of the task, combined with the queried control task requirements, the corresponding code is predicted. If the compilation is not successful and the maximum number of iterations has not been reached, the code is continued to be optimized according to the repair suggestions of the teacher model through the interaction between the student model and the teacher model, until the compilation is successful or the maximum number of iterations is reached, and the final code that meets the control task requirements is output.

[0032] The advantages and beneficial effects of the present invention are:

[0033] The present invention supports structured task-driven ST code automatic generation. By introducing structured thinking chain (SCoT), industrial control tasks can be directly mapped from natural language or graphical descriptions to structured code generation paths, realizing task-driven high-quality ST control code automatic generation, significantly reducing dependence on manual writing experience; the present invention integrates AST structural analysis to realize dual review of grammar and control logic, and performs structural constraint comparison and control logic verification on the generated code through the abstract syntax tree (AST), which can simultaneously identify grammatical errors and semantic inconsistencies, improve the coverage and accuracy of error detection, and break through the capabilities of traditional static grammar checking tools; the present invention introduces the "code teacher-code learner" mechanism to simulate the expert collaborative repair process, and by designing an interactive dual-agent repair mechanism, the teacher The model generates a repair chain, and the learner model executes the repair, simulating the review ideas of human experts and performing multiple rounds of automatic corrections to ensure that the repair process is explainable, traceable, and automatically closed-loop; the present invention supports multiple rounds of automatic iterative repair without human intervention, and drives the repair strategy through the repair chain with self-feedback and self-learning capabilities. It intelligently adjusts the repair plan through structured error reports and historical repair paths until the repair is completed or the termination conditions are met, significantly improving the automatic repair capability and system stability; the present invention provides full-process structured output to facilitate engineering integration and audit tracking, and can automatically generate structured descriptions including control logic structure diagrams, variable mapping tables, function block call summaries, repair logs, and version difference documents, to provide support for subsequent project archiving, operation and maintenance handovers, and industrial safety audits, thereby enhancing maintainability and manageability. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] Figure 1 is a flow chart of a method in an embodiment of the present invention.

[0035] Figure 2 It is a structural diagram of ST code generation based on SCoT-AST collaboration in an embodiment of the present invention.

[0036] Figure 3 It is a structural diagram of the interactive review chain of dual-agent collaboration in an embodiment of the present invention. DETAILED DESCRIPTION

[0037] The following describes the specific embodiments of the present invention in detail with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are only used to illustrate and explain the present invention and are not intended to limit the present invention.

[0038] like Figure 1As shown, a structured text code generation and review method is shown. The Structured Chain of Thought (SCoT) is combined with the Abstract Syntax Tree (AST). By structurally decomposing the control task, the large language model is guided to gradually generate ST code that complies with the IEC 61131-3 standard, and the AST is used for syntax and semantic review. The "code teacher-code learner" interaction mechanism is adopted to generate a repair chain (Chain-of-Repair) based on compiler feedback to guide the model to perform multiple rounds of automatic repair. In the process, constraint strategies such as control block mapping, variable binding, and function block calling are introduced to ensure clear code logic, structural compliance, and semantic consistency, and structured documents are output to assist debugging and auditing. Finally, semantic structure-driven and intelligent agent collaboration utilizes code structure perception capabilities and dual-agent interactive repair chains to achieve full-process intelligent processing of industrial control codes from requirement analysis, logic generation to error review and automatic repair. The method specifically includes the following steps:

[0039] Step 1: After the user inputs the control task requirements, the requirements analysis module performs deep semantic analysis to automatically identify the key control conditions, control actions and constraints in the task. The requirements analysis module accurately extracts the task requirements through context understanding and semantic reasoning, and converts them into a structured thinking chain. Figure 2 As shown, the specific steps include:

[0040] Step 1.1: Receive user input and parse the requirements. Control task requirements are received from the user in various input formats, including natural language, structured forms, and graphical process descriptions. An appropriate parsing method is selected based on the input type. For example, natural language input is processed using natural language processing techniques; structured input or graphical processes are converted into a standardized text format using a conversion algorithm for subsequent processing. The input requirements are broken down into recognizable task modules, facilitating further analysis and processing in subsequent steps.

[0041] Step 1.2: Analyze the key semantics and task objectives of the control task. After the requirements analysis is completed, the system will use the structured thinking chain (SCoT chain) technology to identify and extract the key semantics of the task. The SCoT chain decomposes the task into multiple independent control units by reasoning and mapping the conditions, states, and actions of the control task. For example, the system will identify elements such as conditional judgments, variable settings, and execution operations in the task and convert them into structured reasoning paths. Specifically, the reasoning process of the SCoT chain can be expressed as:

[0042]

[0043] in, Represents the condition recognition part, Indicates the status judgment part, Represents the action mapping part. In this way, the system can clearly extract the core goals and execution steps of the control task.

[0044] Step 1.3: Task decomposition and control unit construction. The control conditions and control actions extracted in Step 1.2 are broken down into multiple basic control units, including sequential structures, conditional branches, loop structures, and more. The system divides tasks into different types of control units based on their logical relationships and control requirements. For each control unit, the system determines the input variables, output variables, and their dependencies to ensure that the execution order and logical structure of the task are clear and can be correctly executed during the subsequent generation process.

[0045] Specifically, after parsing the key semantics and task objectives of the control task in step 1.2, this information is further broken down into multiple basic control units. Each control unit corresponds to a specific operation in the task, mainly including sequential structures, conditional judgments, loop structures and other types. Based on the logical relationship and dependency conditions of the control task, the task is broken down into multiple basic units to ensure that each unit can be executed independently and can be smoothly connected with other units. The input variables, output variables and execution conditions of each control unit are clarified to ensure that the order of task execution and the conditional judgment logic are clear. For conditional judgment units, the system takes the conditional expression as input and the executed action as output; for loop structure units, the system constructs the control unit based on the loop conditions, number of iterations and operation type. Control unit Including condition judgment, state judgment and action mapping, the control unit can be expressed as:

[0046]

[0047] in, is the input variable, For conditional judgment, To perform actions based on conditions, is the output variable.

[0048] Step 1.4: Construct the control task structure diagram and SCoT chain mapping. Based on the control units disassembled in step 1.3, construct the control task structure diagram. This structure diagram represents each control unit through nodes, and the edges represent the logical relationships between control units. The construction of the control task structure diagram ensures that the task execution process is clear and logical. Then, the system generates a corresponding SCoT chain for each control unit, converting the task's condition identification, state judgment, and action mapping into a structured reasoning chain, ultimately forming a complete and structured task description, providing a basis for subsequent ST code generation and verification.

[0049] Specifically, based on the control units generated in step 1.3, each control unit is mapped with its corresponding control logic relationship, and a structural diagram of the control task is constructed. The diagram represents each control unit through a node, and the edges represent the logical relationship between the control units. The control task structure diagram shows the overall process of task execution, ensuring that each control unit is executed correctly according to the predetermined order or conditions. Then, the system maps each control unit with the corresponding structured thinking chain (SCoT chain) to ensure that the control intention of each unit can be clearly expressed through the "condition identification-state judgment-action mapping" path. Specifically, for the i-th control unit , its SCoT chain mapping can be expressed as:

[0050]

[0051] in, Indicates the control unit, The corresponding SCoT chain includes the control unit's conditional judgment, state judgment, and action mapping. Through SCoT chain reasoning, the control unit's execution conditions, state, and actions are clarified, providing a clear control logic structure for subsequent ST code generation. Ultimately, the control task's structure diagram and SCoT chain together provide a clear framework for code generation and verification of the entire task.

[0052] Step 2: The structured thought chain reader receives the structured thought chain required by the task as input, and after tokenization, embedding representation and feature extraction, generates vectors containing semantic information, which are passed to subsequent modules. Then, the AST reader receives these vectors and rule sequences, and based on the structure and hierarchical relationship of the grammatical rules, generates an AST representation containing deep grammatical features through embedding, position and depth encoding, combined with the self-attention mechanism and tree convolution layer. Finally, the decoder uses the features from the thought chain reader and the AST reader to combine the query node and the AST features, and after processing through the multi-head attention mechanism and the fully connected layer, it finally predicts the next grammatical rule and generates the corresponding code. Figure 2 As shown, the specific steps include:

[0053] Step 2.1: The input structured thought chain enters the thought chain reader and is first tokenized into a series of tokens. It is then converted into vector representations through character embedding and fully connected layers. These vectors are further subjected to feature extraction through self-attention mechanisms, gating mechanisms, and convolutional layers.

[0054] Specifically, it is first tokenized into a series of tokens, where , represents the length of the input. Next, each token is decomposed into characters, where is the number of characters in the token. All tokens and characters are represented as real-valued vectors through embedding and .

[0055] Through a fully connected layer, the token is represented by character embedding:

[0056]

[0057] in, is the weight matrix, and the character sequence is padded to a predefined maximum length. After the fully connected layer, layer normalization is applied. The resulting vector is then fed into the NL reader and combined with the word embedding through a gating sublayer.

[0058] The NL reader consists of a series of blocks (total Each block contains three different sub-layers: self-attention, gating mechanism, and word convolution, which are used to extract features. Residual connections are used between the two sub-layers, and layer normalization is performed.

[0059] Among them, the self-attention sublayer follows the Transformer architecture and uses a multi-head attention mechanism to capture long-term dependency information. , representing them as embedding vectors , and represented by a lookup table.

[0060] In order to encode the position information of the word, position embedding is also used. The word in The position embedding in a Transformer block is as follows:

[0061]

[0062] in, Represents a vector In position Position embedding, is the number of dimensions.

[0063] The Transformer block learns nonlinear features through a multi-head attention mechanism, thereby obtaining the matrix ,in is the output query matrix. The calculation formula of the multi-head layer is:

[0064]

[0065]

[0066] in: represents the number of attention heads in the multi-head attention mechanism, Respectively represent The query, key, and value weight matrices of the attention heads, Indicates the The set of weight matrices of the attention heads, Represents the output projection matrix, which is used to map the concatenated results of all attention heads back to the original feature dimension;

[0067] The attention layer of each head is calculated by the following formula:

[0068]

[0069] in, Indicates the length of each feature vector. 、 and Calculated by the following formulas:

[0070]

[0071] in, 、 and are model parameters. For the first block, the input is the vector sum of the lookup table embedding and the position embedding, i.e. ; For other blocks, the input is the output vector of the lower-level Transformer block and the position information corresponding to the block.

[0072] After calculating the features through self-attention, we further combine the information of character embedding. This is achieved through a softmax-based gating mechanism. words, and calculate the control vector through linear transformation , it comes from . Softmax weights of character embeddings It is through Obtained by linear transformation. The softmax weight of Transformer output It is through Then, the calculation formula of the gating mechanism is as follows:

[0073]

[0074] They are used to weight Transformer layer features and character embedding features , these features are respectively and The output of the gating mechanism is ,in Indicates that it has elements The block matrix of .

[0075] Finally, two convolutional layers are applied to the output of the gating mechanism , to extract local features around each token, where Indicates the number of convolutional layers. The convolution calculation formula of the layer is:

[0076]

[0077] in, is the convolution weight, represents the window size. In particular, Represents the output of the gating mechanism In these layers, separable convolutions are used because they have fewer parameters and are easier to train. Zero padding is added to the first and last words. Between these layers, the GELU activation function is used.

[0078] Step 2.2: The AST reader models the program structure based on the generated partial AST. It extracts information from the structured rule sequence and captures the hierarchical relationships between grammatical rules through a multi-layered encoding structure consisting of embedding representations, deep embeddings, and positional embeddings. Through a self-attention mechanism and tree convolutional layers, the AST reader combines the representation of each rule node with that of its parent node (ancestor), thereby capturing deep dependencies in the tree structure.

[0079] Specifically, the AST reader consists of multiple blocks (total The network consists of four blocks. Each block contains four sublayers: self-attention, gating mechanism, NL attention, and tree convolution. Except for the tree convolution layer, each sublayer uses residual connections, and layer normalization is performed after each sublayer.

[0080] In order to capture the characteristics of AST, a self-attention layer similar to Transformer is constructed. The input includes the sum of rule embedding, position embedding and depth embedding, that is:

[0081]

[0082] The self-attention sublayer uses the same mechanism as in the formula to extract features of the AST input, but adds a deep embedding To the input:

[0083]

[0084] The present invention hopes to express the rules of content encoding Combined with the features extracted by the self-attention layer. Therefore, a gating mechanism is used to calculate the output of feature fusion:

[0085]

[0086] in, is the fused feature. The content encoding rule is represented by Combined with the features extracted by the self-attention layer.

[0087] In the decoding step, we need to focus on the natural language description of the input. This is achieved through multi-head NL attention, similar to the attention mechanism of the Transformer decoder. The extracted features are represented as:

[0088]

[0089] To combine the information of a node with that of its ancestors, we treat the AST as a graph and use an adjacency matrix To represent a directed graph. If the node is a node The parent node of . Assume that the features of all nodes are represented as , the features of their parent nodes can be obtained by multiplying with the adjacency matrix:

[0090]

[0091] The tree convolution layer is used to combine the feature information of the current node and its ancestor nodes. The tree convolution window applied to the current subtree is:

[0092]

[0093] in, are the weights of the convolutional layer, Indicates the size of the convolution window. In the experiment, the convolution window size , and added two convolutional layers to the last layer of the AST reader. Each layer uses the GELU activation function:

[0094]

[0095] Step 2.3: The decoder's goal is to generate code for the target task based on features obtained from the structured thought chain reader and the AST reader. The decoder uses the expanded non-terminal nodes as query nodes, combines the query path with the AST features, and applies a multi-head attention mechanism to extract structural and semantic information. Finally, it predicts the next grammatical rule or identifier through a softmax function or pointer network and generates the corresponding code.

[0096] Specifically, the decoder is the final component in the system, which is responsible for combining the generated code information with the natural language description and predicting the next grammar rule. Similar to the AST reader, the decoder also uses a stack (a total of Each block contains multiple sub-layers. Residual connections are used around each sub-layer, followed by layer normalization.

[0097] The decoder takes the query node path as input and treats the non-terminal node to be expanded as the query. The query node is represented as a path from the root node to the target expansion node. The nodes in the path are represented as real-valued vectors. Then, a fully connected layer is applied to these vectors to convert them into query node representations. :

[0098]

[0099] in, is the weight matrix, is the embedding representation of the nodes in the path.

[0100] Next, two attention layers are applied to integrate the outputs from the AST reader and the NL reader. First, an AST attention layer is applied to the output of the AST reader and the NL reader is added based on the query node Extract features. The query in this layer is calculated by the query node, while the key ( ) and value ( ) by code features Calculate. The formula is:

[0101]

[0102] Next, features from the input description are integrated. This integration is achieved through the NL attention mechanism, in which the query By features Calculate, key ( ) and value ( ) Features described by the input Calculate. The formula is:

[0103]

[0104] Finally, a set of two fully connected layers is used to extract the features required for prediction. The first fully connected layer uses the GELU activation function to improve the nonlinear representation capability. The output features are:

[0105]

[0106] in, is the weight matrix of the fully connected layer.

[0107] During the training process, the next grammatical rule is predicted by the softmax function of the last layer of the decoder. It is selected from all possible candidate rules. The formula is as follows:

[0108]

[0109] In addition, a pointer network is introduced, which is essentially an attention mechanism that allows a token to be directly copied from a natural language description. When the pointer mechanism is enabled, the generated grammar rules are ,in is the nonterminal symbol to be expanded, Is the terminator.

[0110] Choose to use softmax rule prediction or pointer network by another gating mechanism Decision, this mechanism is calculated by the final feature of the decoder. The overall predicted probability of the next grammar rule is:

[0111]

[0112] in, represents the probability of the pointer network. The calculation formula of the pointer network is:

[0113]

[0114] In the reasoning process, the reasoning starts from the initial rule Start by expanding a special symbol to the root symbol. Recursive prediction terminates when each leaf node in the AST is predicted to be a terminal symbol. During prediction, a beam search of size 5 is used to select the optimal path. Invalid rules are excluded from the beam search.

[0115] By maximizing the negative log-likelihood loss, the model is able to optimize and compare against a reference program, thereby improving prediction accuracy.

[0116] Step 3: Interactive automatic review and repair. The compiler first verifies whether the ST code complies with industrial standards such as IEC 61131-3 and checks the grammatical correctness. If the compilation is successful, the system outputs the final ST code; if the compilation fails, the system generates an error report and feeds it back to the teacher model. The teacher model analyzes the errors, identifies the problems and provides specific modification suggestions to help the system fix the errors in the code. The repaired code will be adjusted by the student model according to the guidance of the teacher model, including modifying the code structure, variable definition or logical conditions. The repaired code is submitted to the compilation system again for compilation. This process will continue for multiple rounds until the code passes the compilation and meets all verification requirements. Figure 3 As shown, the specific steps include:

[0117] Step 3.1: Preliminary Compilation and Syntax Verification. The preliminary ST code generated in Step 2 is first compiled into the code review system. The compiler verifies compliance with industrial control standards such as IEC 61131-3 and performs a syntax check. The system checks for syntax errors, missing syntax elements, or unclosed control structures. If the compilation is successful, the system outputs the final ST code. If the compilation fails, the compiler generates a detailed error report and provides feedback to the teacher model.

[0118] Step 3.2: The Teacher Model Analyzes Errors and Provides Fix Recommendations. Upon receiving a compilation error report, the Teacher Model analyzes the cause of the error, identifies potential issues in the code, and provides detailed remediation guidance. This guidance may include adjusting code structure, revising variable definitions, optimizing logical conditions, or modifying control structures. By deeply understanding the control logic generated by the SCoT chain, the Teacher Model provides precise recommendations for remediation, ensuring that the repaired code meets expected functionality and standard requirements.

[0119] Specifically, after receiving the compilation error report, the teacher model will first parse the error type and specific location in the report. By comparing it with the previous control task logic, the model identifies that the error may be caused by syntax problems, logical conflicts, or incomplete control structures. The teacher model uses reasoning algorithms to analyze the root cause of the error and generates repair suggestions based on the requirements of the control task. Specifically, the teacher model will use the reasoning path of the structured thinking chain (SCoT chain) and combine the condition, state, and action mapping of the task to propose a solution to modify the code. For example, if the condition judgment is wrong, the model will suggest modifying the conditional expression; if the loop condition is incorrect, the model will recommend adjusting the loop boundary and termination condition. The teacher model's repair suggestion can be expressed by the following formula:

[0120]

[0121] in, is the repair suggestion for the i-th error, For compilation error description, The structured thinking chain corresponding to the wrong position, The teacher model provides the control logic context for the task. Repair suggestions are specific to syntax adjustments, logic corrections, or function block calls, helping the student model perform accurate code repairs. The teacher model's analysis and repair suggestions provide clear direction for subsequent code corrections, ensuring that each error is accurately reasoned and repaired to achieve the desired effect.

[0122] Step 3.3: The student model performs repairs and recompiles. Following the teacher model's guidance, the student model automatically performs code repairs. Based on the recommended repairs, the student model adjusts the code structure, updates variable definitions, or modifies logical decisions. The repaired ST code is submitted to the compilation system for recompilation. If the compilation passes, the system outputs the final ST code. If errors persist, the process repeats, and the error report is returned to the teacher model to generate new repair suggestions.

[0123] Specifically, after the teacher model provides repair suggestions, the student model repairs the generated ST code according to these suggestions. The repair process includes adjusting the code structure, modifying variable definitions, improving conditional judgments and loop structures, etc. The student model converts the teacher model's repair guidance into actual code modifications to ensure that the repaired code meets the expected logic and syntax requirements. Specifically, the student model locates and adjusts the erroneous parts of the code by reading the modification suggestions provided by the teacher model and combining them with the specific requirements of the control task. For example, for conditional judgment errors, the student model will modify the conditional expression; for syntax errors, the model will correct them according to the standard syntax. The repaired code will be resubmitted to the compilation system for compilation verification. If the compilation passes, the repair process is completed; if there are still errors, the repaired code will be fed back to the teacher model for further correction. The repair process can be expressed as:

[0124]

[0125] in, For the repaired ST code, is the original ST code, is the i-th repair suggestion, This process continues after each compilation round until the code compiles and meets all syntax and logic verification requirements. This interactive repair mechanism allows the system to continuously optimize the code, ensuring that the resulting ST code accurately executes the intended functionality.

[0126] Step 3.4: Loop Optimization and Final Code Output. This interactive review and repair process continues for multiple rounds until the ST code compiles and meets all syntax and logic verification requirements. Each time a compilation fails, the teacher model provides new repair guidance, and the student model performs further repairs and optimizations, ensuring that the resulting ST code accurately implements the intended functionality and complies with industry standards. Finally, the repaired and verified ST code is output as the final result.

[0127] Specifically, after the repair code generated in step 3.3 is modified by the student model, the system will recompile and verify. This process continues for multiple rounds until any of the following conditions are met: 1) the generated ST code passes compilation and logic verification; 2) the preset maximum number of iterations is reached. Each time the compilation fails, the teacher model provides new repair guidance, and the student model repairs and optimizes again to ensure that the final generated ST code can accurately perform the expected functions and meet industrial standards. The specific process of step 3.4 is as follows: The generated enhanced control task logic is ,in represents the generated preliminary control task graph, Represents the relevant knowledge set obtained through path planning and retrieval enhancement. Search source, you can select the top Subset ,Right now The control requirements and execution operations of the task are ,in Indicates the task requirements, For relevant code examples, This is the final selected case set, in which each example case is determined by the task requirements Implementation with the corresponding ST code By inputting and , the large language model (LLM) generates a preliminary code S, namely:

[0128]

[0129] Where Q is the control task requirement queried by the user. If the generated ST code fails to compile at any iteration and the maximum number of iterations has not yet been reached, the system will continue to optimize the code using the teacher model's repair suggestions until the stopping condition is met, ultimately outputting ST code that meets the control task requirements.

[0130] In addition, to ensure the efficiency and accuracy of the optimization process, the system combines historical error information with new repair suggestions after each failure to ensure that each round of optimization can gradually approach the ideal ST code and ultimately generate code that meets all syntax and logic verification requirements.

[0131] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some or all of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. A structured text code generation and review method, characterized in that The steps include: Step 1: Perform semantic analysis on user needs, extract task requirements and perform structural decomposition to obtain a structured thinking chain for the task; Step 2: Generate a vector containing semantic information and extract natural language features through structured thought chains. Based on the structure and hierarchical relationship of grammatical rules, generate an abstract syntax tree feature containing deep grammatical features through the vector and rule sequence. Based on the features extracted by the structured thought chain and the abstract syntax tree, combine the query task node and the abstract syntax tree to predict the next grammatical rule and generate the corresponding code. Step 3: Review and fix the code to get the final code.

2. The structured text code generation and review method according to claim 1, wherein: The step 1 comprises the following steps: Step 1.1: Analyze user needs and break them down into a set of task modules; Step 1.2: Identify and extract key semantics in the task through structured thought chains; The structured thinking chain decomposes the task into multiple independent control units by reasoning and mapping the conditions, states and actions of the control task; Step 1.3: Divide the control units into different types based on the logical relationships and control requirements of the tasks. For each control unit, determine the input variables, output variables, and dependencies. Step 1.4: Construct a control task structure diagram and structured thinking chain mapping; Map each control unit with its corresponding control logic relationship, and construct a structural diagram of the control task. Use nodes to represent control units and edges to represent the logical relationship between control units. Map each control unit with the corresponding structured thinking chain; generate a corresponding structured thinking chain for each control unit, and transform the task's condition identification, state judgment, and action mapping into a structured reasoning chain.

3. The structured text code generation and review method according to claim 2, characterized in that: In step 1.3, each control unit corresponds to a specific operation in the task, including a sequential structure class, a conditional judgment class, and a loop structure class. For the conditional judgment class, the conditional expression is used as input and the executed action is used as output; for the loop structure class, the control unit is constructed according to the loop condition, the number of iterations, and the operation type.

4. The structured text code generation and review method according to claim 1, wherein: The step 2 comprises the following steps: Step 2.1: Tokenize the structured thought chain into a series of tokens, then convert it into a vector representation and perform natural language feature extraction on the vector. Step 2.2: Extract information from the structured rule sequence and capture the hierarchical relationship between grammar rules. Combine the representation of each rule node with the representation of its parent node to capture the deep dependency relationship in the tree structure. Step 2.3: Use the non-terminal node to be expanded as the query node, combine the query path with the abstract syntax tree features, and apply the multi-head attention mechanism to extract structural and semantic information to predict the next grammatical rule or identifier and generate the corresponding code.

5. A structured text code generation and review method according to claim 4, characterized in that: In step 2.1, each word is decomposed into characters, and the word is represented by character embedding using character vectors and character weight matrices; the position information of the character is encoded using position embedding; and the embedding vector and position embedding are used to learn nonlinear features through a multi-head attention mechanism; The character vector based on linear transformation, the character weight matrix and the head weight matrix learned by the multi-head attention mechanism are further combined with the character embedding information through the gating mechanism, and then the local features around the word unit are further extracted through the convolution operation.

6. A structured text code generation and review method according to claim 5, characterized in that: In step 2.2, a self-attention layer is constructed to perform feature extraction by adding deep embedding information to the rule embedding and the position embedding. The extracted features are fused with the content encoding rules through a gating mechanism. The fused information is combined with the natural language description, and feature extraction is performed through natural language attention. Based on the abstract syntax tree, the parent node features of the node are represented by the adjacency matrix and the node features, and the feature information of the current node and its ancestor node are combined through the tree convolution layer.

7. The structured text code generation and review method according to claim 5, characterized in that: In step 2.3, the nodes in the path from the root node to the target expansion node are represented as vectors, and the vectors are converted into query node representations. Based on the features extracted from the abstract syntax tree, features are extracted according to the query nodes through the abstract syntax tree attention layer. Based on the extracted features, the natural language description of the input is integrated through the natural language attention mechanism to extract the features required for prediction, so as to predict the next grammar rule.

8. The structured text code generation and review method according to claim 5, wherein: In step 2.3, based on the features required for the prediction, the probability of the next grammatical rule is predicted by the softmax function. At the same time, a pointer network based on the attention mechanism is introduced. When the pointer mechanism is enabled, the grammatical rules are generated, and the use of softmax function prediction or pointer network prediction is controlled by the gating mechanism.

9. The structured text code generation and review method according to claim 1, wherein: The step 3 comprises the following steps: Step 3.1: Compile the code, verify whether the code meets the standards, and perform syntax checking. If the compilation is successful, the code is output; otherwise, the code is fed back to the teacher model; Step 3.2: The teacher model analyzes the cause of the error, identifies the code problem, and provides repair guidance. The teacher model will propose suggestions for modifying the code through the reasoning path of the structured thinking chain, combined with the condition, state, and action mapping of the task. Step 3.3: The student model repairs the code according to the teacher model's suggestions and recompiles the repaired code. If the compilation is successful, the code is output; otherwise, the code is fed back to the teacher model. Step 3.4: Interactive review and repair based on the teacher model and student model until the repair and verification conditions are met and the final code is obtained.

10. A structured text code generation and review method according to claim 9, characterized in that: In step 3.4, the enhanced control task logic is generated by selecting a subset from the relevant knowledge set obtained from the path planning and retrieval enhancement based on the retrieval source to generate a preliminary control task graph for the enhanced control task; the control requirements and execution operations of the task are obtained from the example set. Task requirements and corresponding code examples; Based on the enhanced control task logic and the control requirements and execution operations of the task, combined with the queried control task requirements, the corresponding code is predicted. If the compilation is not successful and the maximum number of iterations has not yet been reached, the code is further optimized according to the repair suggestions of the teacher model through the interaction between the student model and the teacher model until the compilation is successful or the maximum number of iterations is reached, and the final code that meets the control task requirements is output.

Citation Information

Patent Citations

  • LLVM-based ST language compiling method and system and compiler

    CN110825384A

  • ST language source code compiling method and device, computer equipment and medium

    CN111488154A

  • Extensible structured text generation method and device in PLC field, equipment and medium

    CN120085869A

  • Code automatic generation and optimization system based on multiple modes

    CN120276718A

  • LLVM-based ST language compiling method and system

    CN120447904A

Cited By

  • Self-adaptive fault detection method and system based on teacher-student coordination network

    CN121388493A

  • Code generation method and device, equipment and storage medium

    CN121477769A

  • Code generation method based on knowledge topology alignment and dynamic complexity reduction

    CN121979505A

  • A code generation method based on knowledge topology alignment and dynamic complexity reduction

    CN121979505B

  • Industrial control code generation method and device based on industrial knowledge graph

    CN122086374A