A method for generating and reviewing structured text code

By combining structured thinking chains and abstract syntax trees, the complexity of writing and reviewing ST code in industrial automation control systems has been solved. This has enabled efficient and interpretable automatic generation of ST code and semantic-level error review, thereby improving system development efficiency and reliability.

CN120743231BActive Publication Date: 2025-12-02CHINA JILIANG UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511254265.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-09-04
Publication Date
2025-12-02
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 grammatical standardization, deep nesting of logical structures, complex variable scope, and time-consuming and labor-intensive debugging and repair. It lacks intelligent assistance and semantic-level error warning mechanisms, and traditional static analysis tools are difficult to handle semantic errors across modules.

Method used

By combining Structured Thinking Chain (SCoT) with Abstract Syntax Tree (AST), the method generates an Abstract Syntax Tree with deep syntactic features through semantic parsing and structural decomposition of user requirements. Combined with the "code teacher-code learner" mechanism, it performs multiple rounds of automatic repair to achieve high-quality ST code automatic generation and semantic-level error review.

Benefits of technology

It enables automated generation of structured code from natural language or graphical descriptions, significantly reducing reliance on manual coding, improving error detection coverage and accuracy, supporting multi-round automatic iterative repair, and providing full-process structured output for easy engineering integration and audit tracking.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743231B_ABST
    Figure CN120743231B_ABST
Patent Text Reader

Abstract

This invention discloses a method for generating and reviewing structured text code. It receives user-inputted control task requirements, supporting input in natural language, structured forms, or graphical flowcharts. A requirement parsing module performs deep semantic analysis to identify control conditions, control actions, and constraints. The task is then transformed into multiple control units via an SCoT chain. An SCoT chain reader generates semantic vectors based on the task requirements, while an AST reader generates an AST representation based on grammatical rules, structure, and hierarchical relationships. A decoder combines the SCoT chain and AST features to predict the next grammatical rule and generate ST code. An interactive automatic review and repair mechanism verifies the syntax and logic of the ST code. If compilation errors occur, a teacher model analyzes the errors and provides repair suggestions, while a student model repairs the code according to the guidance until it meets the expected functionality and passes compilation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer technology, specifically relating to a method for generating and reviewing structured text code. Background Technology

[0002] In the field of industrial automation control, Structured Text (ST) language, as one of the main programming languages ​​defined by the IEC 61131-3 standard, is widely used in systems such as Programmable Logic Controllers (PLCs) and Distributed Control Systems (DCS). ST language possesses 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, writing and reviewing ST code faces several challenges, including high requirements for syntax standardization, deep nesting of logical structures, complex variable scopes, and time-consuming and labor-intensive debugging and repair processes.

[0003] Current ST code development relies on engineers manually designing control logic and completing the writing and debugging process, lacking intelligent assistance and semantic-level error warning mechanisms. This results in difficulty in ensuring code quality and low development efficiency. While traditional static analysis tools can identify some syntax and structural problems, they struggle to handle cross-module semantic errors, control path anomalies, or context dependency 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. Among them, the Chain-of-Thought (CoT) reasoning mechanism has been proven to improve the model's ability to handle complex logic problems, but its transfer to industrial ST languages ​​still faces obstacles such as "weak annotation of control structures" and "missing logical paths". Summary of the Invention

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

[0006] A method for generating and reviewing structured text code includes the following steps:

[0007] Step 1: Perform semantic parsing of user needs, extract task requirements, and perform structured decomposition to obtain the structured thought chain of the task;

[0008] Step 2: Generate vectors containing semantic information and extract natural language features through a structured thinking chain. Based on the structure and hierarchical relationship of the grammatical rules, generate abstract syntax tree features containing deep grammatical features through the vectors and rule sequences. Based on the features extracted by the structured thinking chain and the features extracted by 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 obtain the final code.

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

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

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

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

[0014] Step 1.3: Based on the logical relationship of the task and the control requirements, divide the control unit into different types. For each control unit, determine the input variables, output variables and dependencies.

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

[0016] Each control unit is mapped to its corresponding control logic relationship, and a structure diagram of the control task is constructed. Nodes represent control units, and edges represent the logical relationships between control units. Each control unit is mapped to a corresponding structured thought chain. This ensures that the control intent of each unit can be clearly expressed through the path of "condition recognition - state judgment - action mapping". A corresponding structured thought chain is generated for each control unit, transforming the condition recognition, state judgment, and action mapping of the task into a structured reasoning chain, ultimately forming a complete and structured task description, providing a foundation 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 sequential structure class, condition judgment class, and loop structure class. For the condition judgment class, the condition expression is taken as input and the action to be executed is taken 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] Further, step 2 includes the following steps:

[0019] Step 2.1: The structured thought chain is tokenized into a series of tokens, and then transformed into a vector representation through character embedding and fully connected layers. Natural language features are extracted from the vectors through self-attention mechanism, gating mechanism and convolutional layer.

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

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

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

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

[0024] Further, in step 2.3, the nodes in the path from the root node to the target extended node are represented as vectors, and the vectors are converted into query node representations. Based on the features extracted by the abstract syntax tree, the 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 in order to predict the next syntax rule.

[0025] Furthermore, in step 2.3, based on the features required for prediction, the probability of the next grammar 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, grammar rules are generated, and the use of the softmax function or the pointer network for prediction is controlled by the gating mechanism.

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

[0027] Step 3.1: Compile the code, verify whether the code conforms to the standard, and perform a syntax check. If the compilation is successful, output the code; otherwise, feed the code back to the teacher model.

[0028] Step 3.2: The teacher model analyzes the cause of the error, identifies code problems, and provides repair guidance. The teacher model will use a structured reasoning path, combined with the task's conditions, states, and action mappings, to propose suggestions for code modification.

[0029] Step 3.3: The student model repairs the code based on 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 then the final code is obtained.

[0031] Furthermore, in step 3.4, the generation of enhanced control task logic involves selecting a subset from the relevant knowledge set obtained through 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 along with 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 compilation fails and the maximum number of iterations has not yet been reached, the code is further optimized through the interaction between the student model and the teacher model, based on the teacher model's repair suggestions, until compilation succeeds or the maximum number of iterations is reached, at which point the final code that meets the control task requirements is output.

[0032] The advantages and beneficial effects of this invention are as follows:

[0033] This invention supports the automatic generation of structured task-driven ST code. By introducing Structured Thinking Chain (SCoT), industrial control tasks can be directly mapped from natural language or graphical descriptions to structured code generation paths, achieving automated generation of high-quality task-driven ST control code and significantly reducing reliance on human coding experience. This invention integrates AST structural analysis to achieve dual review of syntax and control logic. Through Abstract Syntax Tree (AST) comparison of structural constraints and control logic verification of the generated code, it can simultaneously identify syntax errors and semantic inconsistencies, improving the coverage and accuracy of error detection and breaking through the limitations of traditional static syntax checking tools. This invention introduces a "code teacher-code learner" mechanism to simulate an expert collaborative repair process. Through the design of an interactive dual-agent repair mechanism, the teacher... The model generates a repair chain, and the learner model executes the repair, simulating the review process of human experts and performing multiple rounds of automatic correction to ensure that the repair process is interpretable, traceable, and automatically closed-loop. This invention supports multi-round automatic iterative repair without human intervention. The repair strategy driven by the repair chain has 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 automatic repair capabilities and system stability. This invention provides a full-process structured output, which facilitates engineering integration and audit tracing. It can automatically generate structured descriptions including control logic structure diagrams, variable mapping tables, function block call summaries, repair logs, and version difference documents, providing support for subsequent project archiving, operation and maintenance handover, and industrial security audits, enhancing maintainability and manageability. Attached Figure Description

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

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

[0036] Figure 3 This is a schematic diagram of the interactive review chain for dual-agent collaboration in an embodiment of the present invention. Detailed Implementation

[0037] The specific embodiments of the present invention will be described in detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are for illustration and explanation only and are not intended to limit the present invention.

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

[0039] Step 1: After the user inputs their control task requirements, the requirement analysis module performs deep semantic analysis to automatically identify key control conditions, control actions, and constraints within the task. Through contextual understanding and semantic reasoning, the requirement analysis module accurately extracts the task requirements and transforms them into a structured thought process. For example... Figure 2 As shown, the specific steps include the following:

[0040] Step 1.1: Receive User Input and Parse Requirements. Receive user input regarding control task requirements, supporting various input formats such as natural language, structured forms, or graphical process descriptions. Select the appropriate parsing method based on the input type. For example, for natural language input, use natural language processing techniques; for structured input or graphical processes, use conversion algorithms to transform them into standardized text format for subsequent processing. The input requirements will be 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 identify and extract the key semantics in the task using Structured CoT (SCoT) chain technology. SCoT chains decompose 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 condition judgments, variable settings, and execution operations in the task and transform them into structured reasoning paths. Specifically, the reasoning process of an SCoT chain can be represented as follows:

[0042]

[0043] in, This indicates the condition recognition part. This indicates the state judgment part. This represents the action mapping part. In this way, the system can clearly extract the core objectives and execution steps of the control task.

[0044] Step 1.3: Task Decomposition and Control Unit Construction. The control conditions and actions extracted in Step 1.2 will be decomposed into multiple basic control units, including sequential structures, conditional branches, and loop structures. Based on the logical relationships and control requirements of the tasks, the system classifies the tasks into different types of control units. 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 tasks are clear and can be correctly executed in subsequent generation processes.

[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, and loop structures. Based on the logical relationships and dependencies of the control task, the task is decomposed into multiple basic units, ensuring that each unit can be executed independently and smoothly connected with other units. The input variables, output variables, and execution conditions of each control unit are clearly defined to ensure that the execution order and conditional judgment logic of the task are clear. For conditional judgment units, the system uses 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 condition, iteration count, and operation type. Control Unit Including condition judgment, state judgment, and action mapping, the control unit can be represented as:

[0046]

[0047] in, For input variables, For conditional judgment, For operations performed based on conditions, For output variables.

[0048] Step 1.4: Construct the control task structure diagram and SCoT chain mapping. Based on the control units decomposed in Step 1.3, construct the control task structure diagram. This structure diagram uses nodes to represent each control unit and edges to represent the logical relationships between control units. The construction of the control task structure diagram ensures that the task execution flow is clear and logical. Then, the system generates a corresponding SCoT chain for each control unit, transforming 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 foundation for subsequent ST code generation and verification.

[0049] Specifically, based on the control units generated in step 1.3, each control unit is mapped to its corresponding control logic relationship, and a control task structure diagram is constructed. This diagram uses nodes to represent each control unit and edges to represent the logical relationships between control units. The control task structure diagram illustrates the overall flow of task execution, ensuring that each control unit executes correctly according to a predetermined sequence or conditions. Next, the system maps each control unit to its corresponding Structured CoT chain, ensuring that the control intent of each unit can be clearly expressed through the path of "condition recognition—state judgment—action mapping." Specifically, for the i-th control unit... Its SCoT chain mapping can be represented as:

[0050]

[0051] in, Indicates the control unit. The corresponding SCoT chain contains the condition judgments, state judgments, and action mappings for the control unit. Through reasoning via the SCoT chain, the execution conditions, states, and actions of the control unit are clarified, thus providing a clear control logic structure for subsequent ST code generation. Ultimately, the structure diagram of the control task and the SCoT chain together provide a clear framework for the 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. After tokenization, embedding representation, and feature extraction, it generates vectors containing semantic information, which are then passed to subsequent modules. Next, the AST reader receives these vectors and the rule sequence. Based on the structure and hierarchical relationships of the grammatical rules, it generates an AST representation containing deep grammatical features through embedding, positional, and deep encoding, combined with a self-attention mechanism and tree convolutional layers. Finally, the decoder uses features from the thought chain reader and the AST reader, combines the query node and AST features, processes them through a multi-head attention mechanism and fully connected layers, and ultimately predicts the next grammatical rule, generating the corresponding code. Figure 2 As shown, the specific steps include the following:

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

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

[0055] The token is represented by character embeddings through a fully connected layer:

[0056]

[0057] in, This is a weight matrix, and the character sequence is padded to a predefined maximum length. After the fully connected layer, the application layer normalizes the data. The resulting vector is then fed into the NL reader and combined with the word embeddings through a gated sublayer.

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

[0059] The self-attention sublayer follows the Transformer architecture and uses a multi-head attention mechanism to capture long dependency information. For a series of input tokens... Represent them as embedding vectors And it is represented by a lookup table.

[0060] To encode the positional information of words, positional embedding is also used. The calculation of the first... The word in the first The position embedding within a Transformer block is calculated using the following formula:

[0061]

[0062] in, Representing vectors In position Position embedding, It refers to the number of dimensions.

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

[0064]

[0065]

[0066] in: This indicates the number of attention heads in a multi-head attention mechanism. They represent the first The query, key, and value weight matrix of each attention head. Indicates the first A set of weight matrices for each attention head. This represents the output projection matrix, used to map the concatenation results of all attention heads back to the original feature dimensions;

[0067] The attention layer for each head is calculated using the following formula:

[0068]

[0069] in, This represents the length of each feature vector. , and Calculate using the following formulas respectively:

[0070]

[0071] in, , and These are the 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 that block.

[0072] After calculating features through self-attention, information from character embeddings is further combined. This is achieved through a softmax-based gating mechanism. For the... Each word is used to calculate the control vector through linear transformation. It comes from The softmax weights of character embeddings Through from Obtained by performing a linear transformation. The softmax weights output by the Transformer. Through from It is obtained by performing a linear transformation. Then, the calculation formula for the gating mechanism is as follows:

[0073]

[0074] They are used for weighted Transformer layer features. and character embedding features These features are respectively composed of and Obtained through linear transformation. The output of the gating mechanism is... ,in Indicates having elements A block matrix.

[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 formula for calculating the convolution of a layer is:

[0076]

[0077] in, These are convolution weights. Indicates the window size. Specifically, Indicates the output of the gating mechanism Separable convolutions are used in these layers because they have fewer parameters, making them easier to train. Zero padding is added for the first and last words. The GELU activation function is used between these layers.

[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 multi-layered encoding structures such as embedding representations, deep embeddings, and positional embeddings. Through self-attention mechanisms and tree convolutional layers, the AST reader combines the representation of each rule node with the representation of its parent node (ancestor), thereby capturing deep dependencies in the tree structure.

[0079] Specifically, the AST reader consists of multiple blocks (total) It consists of blocks. Each block contains four sub-layers: self-attention, gating mechanism, NL attention, and tree convolution. Except for the tree convolution layer, each sub-layer is connected by residual connections, and layer normalization is performed after each sub-layer.

[0080] To capture the features of the AST, a Transformer-like self-attention layer was constructed. The input consists of the sum of the regular embedding, the positional embedding, and the depth embedding, i.e.:

[0081]

[0082] The self-attention sublayer uses the same mechanism as in the formula to extract features from the AST input, but adds deep embedding. Enter into the input:

[0083]

[0084] This invention aims to represent the rules of content encoding. It is combined with features extracted from the self-attention layer. Therefore, a gating mechanism is used to compute the output after feature fusion:

[0085]

[0086] in, These are the merged features. The rules for content encoding are represented in this way. Combined with features extracted from the self-attention layer.

[0087] In the decoding step, attention needs to be paid to 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 follows:

[0088]

[0089] To combine the information of a node with that of its ancestors, the AST is viewed as a graph, and an adjacency matrix is ​​used. To represent a directed graph. If the node It is a node The parent node, then Assume that the features of all nodes are represented as follows: The characteristics of their parent nodes can be obtained by multiplying them by the adjacency matrix:

[0090]

[0091] Tree convolutional layers combine the feature information of the current node with that of its ancestor nodes. The tree convolutional window applied to the current subtree is as follows:

[0092]

[0093] in, These are the weights of the convolutional layer. This indicates the size of the convolution window. In the experiment, the convolution window size... Two convolutional layers were added to the last layer of the AST reader. Each layer uses the GELU activation function.

[0094]

[0095] Step 2.3: The goal of the decoder 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 extended non-terminal nodes as query nodes, combines the query path with AST features, and applies a multi-head attention mechanism to extract structural and semantic information. Finally, it predicts the next grammar rule or identifier using a softmax function or pointer network to generate the corresponding code.

[0096] Specifically, the decoder is the final component of the system; it 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 (total...). Each sublayer consists of several blocks, and each block contains multiple sublayers. Each sublayer is surrounded by residual connections and then normalized.

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

[0098]

[0099] in, It is a weight matrix. It is an embedded representation of 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 then based on the query node... Feature extraction. Queries in this layer. Calculated by the query node, while the key ( ) and value ( ) by code characteristics Calculation. 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 characteristics Calculation, key ( ) and value ( Features described by the input Calculation. The formula is:

[0103]

[0104] Finally, the features needed for prediction are extracted through a set of two fully connected layers. The first fully connected layer uses the GELU activation function to improve non-linear representation capabilities. The output features are:

[0105]

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

[0107] During training, the next grammar rule is predicted using the softmax function of the last layer of features in the decoder. The rule is selected from all possible candidate rules. The formula is as follows:

[0108]

[0109] Furthermore, 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 syntax rules are as follows: ,in These are nonterminal symbols to be expanded. It is a terminal symbol.

[0110] The choice between using softmax rule prediction and pointer networks is determined by another gating mechanism. The decision is made through calculation of the final features of the decoder. The overall predicted probability of the next grammar rule is:

[0111]

[0112] in, This represents the probability of a pointer network. The formula for calculating a pointer network is:

[0113]

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

[0115] By maximizing the negative log-likelihood loss, the model can optimize and compare with the reference procedure, thereby improving prediction accuracy.

[0116] Step 3: Interactive Automated Review and Repair. The compiler first verifies whether the ST code conforms to industry standards such as IEC 61131-3 and checks its syntax correctness. If compilation is successful, the system outputs the final ST code; if compilation fails, the system generates an error report and sends it 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 is then adjusted by the student model according to the teacher model's guidance, including modifying the code structure, variable definitions, or logical conditions. The repaired code is then submitted to the compilation system again for compilation. This process continues for multiple rounds until the code compiles successfully and meets all verification requirements. Figure 3 As shown, the specific steps include the following:

[0117] Step 3.1: Preliminary Compilation and Syntax Verification. The preliminary ST code generated in Step 2 will first be input into the code review system for compilation. The compiler verifies whether the code conforms to industrial control standards such as IEC 61131-3 and performs a syntax check. The system will check for syntax errors, missing syntax elements, or incomplete control structures. If compilation is successful, the system will output the final ST code; if compilation fails, the compiler will generate a detailed error report and provide feedback to the teacher model.

[0118] Step 3.2: Teacher Model Analysis of Errors and Repair Suggestions. Upon receiving a compilation error report, the teacher model analyzes the cause of the error, identifies potential problems in the code, and provides detailed repair guidance. This guidance may include adjusting code structure, correcting 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 suggestions for the repair process, ensuring that the repaired code meets expected functionality and standard requirements.

[0119] Specifically, upon receiving a compilation error report, the teacher model first parses the error type and its location. By comparing it with the previous control task logic, the model identifies possible causes of the error, such as syntax problems, logical conflicts, or incomplete control structures. The teacher model uses inference 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 propose code modification schemes using a Structured CoT chain reasoning path, combined with the task's conditions, states, and action mappings. For example, if the condition judgment is incorrect, 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 suggestions can be expressed by the following formula:

[0120]

[0121] in, Here are the suggested fixes for the i-th error. This is a description of a compilation error. The structured thought chain corresponding to the incorrect position, This provides the control logic context for the task. Repair suggestions will be specific, focusing on syntax adjustments, logic corrections, or function block calls, helping the student model execute accurate code fixes. The teacher model's analysis and repair suggestions provide a clear direction for subsequent code corrections, ensuring that each error can achieve the desired effect through accurate reasoning and repair.

[0122] Step 3.3: Student Model Performs Repair and Recompilation. Following the guidance of the teacher model, the student model will automatically perform code repair. The student model will adjust the code structure, update variable definitions, or modify logical judgments based on the repair suggestions. The repaired ST code will be submitted to the compilation system for recompilation. If compilation succeeds, the system outputs the final ST code; if errors persist, the process will be repeated, and error reports will be 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 based on these suggestions. The repair process includes adjusting the code structure, modifying variable definitions, and improving conditional statements and loop structures. The student model translates the teacher model's repair guidance into actual code modifications, ensuring that the repaired code meets the expected logical and syntactic requirements. Specifically, the student model reads the modification suggestions provided by the teacher model and, combined with the specific requirements of the control task, locates and adjusts the erroneous parts of the code. For example, for conditional statement errors, the student model modifies the conditional expression; for syntax errors, the model corrects them according to standard syntax. The repaired code is then resubmitted to the compilation system for compilation verification. If compilation succeeds, the repair process is complete; if errors persist, the repaired code is fed back to the teacher model for further correction. The repair process can be represented as:

[0124]

[0125] in, For the repaired ST code, This is the original ST code. This is the i-th repair suggestion. This forms the logical context for the corresponding control task. This process continues after each compilation round until the code compiles successfully and meets all syntax and logic verification requirements. Through this interactive repair mechanism, the system can continuously optimize the code, ensuring that the final generated ST code can accurately perform the expected function.

[0126] Step 3.4: Iterative Optimization and Final Code Output. This interactive review and repair process will continue for multiple rounds until the ST code compiles successfully and meets all syntax and logic verification requirements. Each time compilation fails, the teacher model provides new repair guidance, and the student model performs repair and optimization again to ensure that the final generated ST code accurately achieves the expected functionality and conforms to industry standards. Finally, the repaired and verified ST code will be 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 it. This process continues for multiple rounds until either of the following conditions is met: 1) the generated ST code passes compilation and logic verification; 2) the preset maximum number of iterations is reached. Each time 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 function and conform to industry standards. The specific process of step 3.4 is as follows: The generated enhanced control task logic is... ,in This represents the generated preliminary control task diagram. This represents the relevant knowledge set obtained through path planning and retrieval enhancement, for the ... There are several search sources, from which you can select the top results. The items constitute a subset ,Right now The task's control requirements and execution operations are as follows: ,in Indicates the requirements of the task. For relevant code examples, The final selected case set, in which each example case is determined by the task requirements. With the corresponding ST code implementation Composition. (Through input) and The Large Language Model (LLM) generates the initial code S, namely:

[0128]

[0129] Here, Q represents the control task requirements queried by the user. If the generated ST code fails to compile in any iteration and the maximum number of iterations has not yet been reached, the system will continue to optimize the code based on the teacher model's repair suggestions until the stopping condition is met, and finally output 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 finally 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, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions 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 method for generating and reviewing structured text code, characterized in that... Includes the following steps: Step 1: Perform semantic parsing of user needs, extract task requirements, and perform structured decomposition to obtain the structured thought chain of the task; Step 2: Generate vectors containing semantic information and extract natural language features through a structured thought chain. Using the vectors and rule sequences, and based on the structure and hierarchical relationship of grammatical rules, generate an abstract syntax tree feature containing deep grammatical features. Based on the features extracted from the structured thought chain and the features extracted from 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; specifically including the following steps: Step 2.1: The structured thought chain is tokenized into a series of word units, then converted into a vector representation, and natural language features are extracted from the vectors; Step 2.2: Extract information from the structured rule sequence and capture the hierarchical relationships between grammatical rules. Combine the representation of each rule node with the representation of its parent node to capture deep dependencies in the tree structure. Step 2.3: Take the non-terminal nodes to be expanded as query task nodes, combine the query path with the features of the abstract syntax tree, and apply a multi-head attention mechanism to extract structural and semantic information to predict the next syntax rule or identifier and generate the corresponding code. Step 3: Review and fix the code to obtain the final code.

2. The method for generating and reviewing structured text code according to claim 1, characterized in that: Step 1 includes the following steps: Step 1.1: Analyze user requirements and break them down into a set of task modules; Step 1.2: Identify and extract key semantics from the task through structured thinking chains; The structured thinking chain decomposes the control task into multiple independent control units by reasoning and mapping the conditions, states and actions of the control task. Step 1.3: Based on the logical relationship of the task and the control requirements, divide the control unit into different types. For each control unit, determine the input variables, output variables and dependencies. Step 1.4: Construct a control task structure diagram and a structured thought chain mapping; Each control unit is mapped to its corresponding control logic relationship, and a structure diagram of the control task is constructed. Nodes represent control units, and edges represent the logical relationships between control units. Each control unit is mapped to a corresponding structured thought chain. A corresponding structured thought chain is generated for each control unit, transforming the task's condition identification, state judgment, and action mapping into a structured reasoning chain.

3. The method for generating and reviewing structured text code according to claim 2, characterized in that: In step 1.3, each control unit corresponds to a specific operation in the task, including sequential structure class, condition judgment class, and loop structure class. For the condition judgment class, the condition expression is taken as input and the action to be executed is taken 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 method for generating and reviewing structured text code according to claim 1, characterized in that: In step 2.1, each word is decomposed into characters, and the embedding vector of the word is represented by character embedding through character vectors and character weight matrices; the positional information of the characters is encoded by positional embedding; and the embedding vectors and positional embeddings are used to learn nonlinear features through a multi-head attention mechanism. Based on the character vectors, character weight matrices, and head weight matrices learned through the multi-head attention mechanism, the information from character embedding is further combined through a gating mechanism, and then further extracted by convolutional operations to extract local features around the lexical units.

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

6. The method for generating and reviewing structured text code according to claim 4, characterized in that: In step 2.3, the nodes in the path from the root node to the target extended node are represented as vectors, and the vectors are converted into query task node representations. Based on the features extracted by the abstract syntax tree, the features are extracted according to the query task 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 in order to predict the next syntax rule.

7. The method for generating and reviewing structured text code according to claim 4, characterized in that: In step 2.3, based on the features required for prediction, the probability of the next grammar 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, grammar rules are generated, and the use of the softmax function or the pointer network for prediction is controlled by the gating mechanism.

8. The method for generating and reviewing structured text code according to claim 1, characterized in that: Step 3 includes the following steps: Step 3.1: Compile the code, verify whether the code conforms to the standard, and perform a syntax check. If the compilation is successful, output the code; otherwise, feed the code back to the teacher model. Step 3.2: The teacher model analyzes the cause of the error, identifies code problems, and provides repair guidance. The teacher model will use a structured reasoning path, combined with the task's conditions, states, and action mappings, to propose suggestions for code modification. Step 3.3: The student model repairs the code based on 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 then the final code is obtained.

9. The method for generating and reviewing structured text code according to claim 8, characterized in that: In step 3.4, the generation of enhanced control task logic is based on the retrieval source. From the relevant knowledge set obtained through path planning and retrieval enhancement, a subset is selected 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 along with corresponding code examples. Based on the enhanced control task logic and the control requirements and execution operations of the task, combined with the control task requirements queried, the corresponding code is predicted. If the compilation fails and the maximum number of iterations has not been reached, the code is further optimized through the interaction between the student model and the teacher model, based on the repair suggestions of the teacher model, until the compilation succeeds or the maximum number of iterations is reached, and the final code that meets the control task requirements is output.

Citation Information

Patent Citations

  • 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