Prompt word compiling and generating method and system for large language model
By constructing an Abstract Syntax Tree (AST) through PDL reduction and optimizing its structure, this technology solves the problem of low efficiency in existing technologies that rely on human experience for prompt words. It achieves high-quality, universal prompt word generation, which is applicable to multiple domains and multi-task scenarios.
Patent Information
- Application Number
- CN202511109784.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-08
- Publication Date
- 2025-10-28
- Estimated Expiration
- 2045-08-08
AI Technical Summary
In existing technologies, the construction of prompt words relies on human experience, which is inefficient, difficult to adapt to complex scenarios in multiple domains and tasks, lacks a general prompt word compilation framework, and cannot effectively express the user's high-level semantic intent and generate high-quality prompt words.
It adopts the Prompt Word Description Language (PDL) specification, constructs an Abstract Syntax Tree (AST) through lexical, syntactic, and semantic analysis, performs structural optimization, and generates high-quality prompt words. The compilation process includes lexical analysis, syntactic analysis, semantic verification, and structural optimization, realizing the automatic conversion from abstract semantic intent to structured prompt words.
It improves the accuracy, stability, and versatility of prompt word generation, reduces development costs, increases engineering efficiency, supports flexible adaptation to various large language models and task types, and is suitable for multiple application scenarios such as intelligent dialogue, document processing, and natural language programming.
Smart Images

Figure 64DF7525-78C1-4236-AA33-6B637C2D7990 
Figure E972EAF9-6E70-4C9F-8B66-8ED881BFD0B2
Abstract
Description
Technical Field
[0001] This invention relates to the fields of artificial intelligence and natural language processing, and in particular to a method and system for compiling and generating prompt words for large language models. Background Technology
[0002] With the continuous development of natural language processing and artificial intelligence technologies, large-scale pre-trained language models (LLMs) have demonstrated significant capabilities in tasks such as text generation, dialogue question answering, information extraction, and code interpretation. To effectively control the output behavior of LLMs, developers need to design prompts as input guides. The structure, wording, and contextual organization of these prompts have a decisive impact on the quality of the model's output. Therefore, prompt construction and optimization have become a key technical step in the practical application of LLMs.
[0003] Currently, prompts primarily rely on developers manually writing them based on experience, and then optimizing them through multiple rounds of trial and error. Although tools such as prompt templates and prompt word libraries exist to assist in design, they still suffer from problems such as rigid structure, weak adaptability, and difficulty in covering complex scenarios. They struggle to meet the high demands for semantic integrity, format standardization, and logical consistency of prompts across multiple domains and tasks. Furthermore, some research attempts to introduce automated methods such as reinforcement learning and model self-optimization to improve prompt generation efficiency, but these often remain at the policy level, lacking a unified modeling and generation framework. In summary, existing technologies have not yet established a general prompt word compilation framework for large language models. They lack a complete technical path for the structured expression, semantic parsing, optimization and transformation of users' high-level semantic intent, including task objectives, contextual data, style requirements and output constraints, and the generation of LLM executable prompts. Summary of the Invention
[0004] To address the aforementioned problems, the present invention aims to provide a method and system for compiling and generating prompt words for large language models. This method automatically converts users' high-level semantic requirements into structured, executable, and multi-model-adaptable high-quality prompt words, thereby overcoming the shortcomings of existing technologies, such as reliance on experience in prompt word design, low efficiency, and poor transferability. The above-mentioned objective of the present invention is achieved through the following technical solutions: This invention provides a method for compiling and generating prompt words for large language models, including: Step S1: Respond to the compilation call request and receive the PDL specification containing the task target, context information, and constraint information as the compilation input text; Step S2: Perform lexical and syntactic analysis on the compiled input text, and construct the corresponding Abstract Syntax Tree (AST) as an intermediate representation using the context-free method; Step S3: Based on the preset semantic rule set and symbol table, perform semantic verification and structural correction on the AST. If the semantics are valid, perform structural optimization including context redundancy merging, constant folding and model adaptation structure rewriting to generate the optimized AST. Step S4: Traverse the optimized AST, match the prompt word templates in the prompt word generation rule base, map semantic nodes to structured text fragments, concatenate the structured text fragments to form the final prompt word text, and output the compilation result consistent with the PDL reduction semantics to drive the execution of large language model tasks.
[0005] Further, step S1 includes: Responding to the compilation call request, the compilation preparation process begins; It receives compiled input text written in a predefined PDL specification, which includes task objectives, context information, and constraint information. The constraint information includes the output language style, content format requirements, and optional target model-specific instructions.
[0006] Further, step S2 includes: The PDL specification is read character by character by a lexical analyzer. At the same time, the PDL specification is parsed into indivisible lexical units Token based on a predefined lexical rule set. Each Token is type-marked and content-identified. The Token includes semantic keywords used to divide the semantic structure, corresponding identifiers, and string values associated with the identifiers. The semantic keywords include TASK for task objectives, CONTEXT for context information, and CONSTRAINT for constraints. The identified tokens are arranged into a token sequence according to the logical order in the original PDL specification, which is then used as input to the parser. The parser performs structured matching on the token sequence based on context-free grammar, identifies grammatical structural units and their nesting relationships, and sequentially reduces the token sequence to grammatical nodes corresponding to grammatical rules during the structured matching process, thus constructing the corresponding AST. Each syntactic node in the AST is classified as a task node, context node, or constraint node, and retains the semantic mapping information in the PDL specification. The completed AST is then output as an intermediate representation for compilation.
[0007] Further, step S3 includes: Traverse the syntax nodes in the AST, extract the identifiers defined in the PDL specification and their associated attribute information, and construct a symbol table; the attribute information includes the name, type, scope and default value of the identifier; Based on the symbol table and combined with the preset semantic rule set, the semantic analyzer performs semantic verification on each syntactic node of the AST structure; the semantic verification includes identifier type matching verification, scope resolution verification, and constraint consistency verification. If the semantic verification passes, the AST is used as input to the structure optimizer for structural correction and optimization, and the optimized AST is output. If semantic verification fails, a semantic error message is generated and the compilation process is terminated.
[0008] Further, in step S3, the AST is used as input to the structure optimizer for structural correction and optimization, and the optimized AST is output, including: Identify context nodes with semantic redundancy or structural repetition in the AST and perform node merging according to the merging rules; For substructures in the AST that are expressed as constants, perform operations or substitutions in advance to complete constant folding; Based on the preset model adaptability rewriting rules, semantically equivalent replacements are performed on structures in the AST that are not conducive to language model parsing, and the optimized AST is output.
[0009] Further, step S4 includes: Traverse each grammar node in the optimized AST, identify the corresponding semantic type, and retrieve the prompt word template corresponding to the semantic type from the preset prompt word generation rule base; Map the field values in each syntax node to the corresponding structured text fragments according to the prompt word template format; Following the logical structure and node order of the AST, the complete prompt words are sequentially concatenated to construct them. After format preprocessing, the final prompt word text, which is consistent with the semantics of the PDL reduction, is output as the input for the large language model task.
[0010] Based on the same inventive concept, this invention also provides a system for compiling and generating prompt words for large language models, which performs the above-described method for compiling and generating prompt words for large language models, including: The user interface module is used to respond to compilation call requests and receive PDL specifications containing task objectives, context and constraint information as compilation input. The parsing module is used to perform lexical and syntactic analysis on PDL reduction and construct the corresponding abstract syntax tree (AST) as an intermediate representation based on the context-free method. The optimization module is used to perform semantic verification and structural correction on the AST based on the preset semantic rule set and symbol table. When the semantics are valid, it performs structural optimization including context redundancy merging, constant folding and model adaptation structure rewriting to generate the optimized AST. The generation module is used to traverse the optimized AST, match the prompt word templates in the generation rule base, and map semantic nodes into structured text fragments; The output module is used to concatenate structured text fragments into the final prompt word text, and outputs a compiled result consistent with the PDL reduction semantics, which is used to drive the execution of large language model tasks.
[0011] Furthermore, the PDL specification includes task objectives, contextual information, and constraint information; the constraint information includes the output language style, content format requirements, and optional target model-specific instructions.
[0012] Furthermore, the parsing module includes: The lexical analysis unit is used to read the PDL specification character by character. Based on a predefined lexical rule set, it parses the PDL specification into indivisible lexical units (Tokens). Each Token is type-marked and content-identified. Tokens include semantic keywords for dividing the semantic structure, corresponding identifiers, and string values associated with the identifiers. The semantic keywords include TASK (task objective), CONTEXT (context information), and CONSTRAINT (constraints). The identified Tokens are arranged into a Token sequence according to their logical order in the original PDL specification, which is used as input to the parser. The syntax analysis unit is used to perform structured matching of token sequences based on context-free grammars, identify grammatical structure units and their nesting relationships, and sequentially reduce the token sequence to the syntax nodes corresponding to the syntax rules during the structured matching process, constructing the corresponding AST. Each syntax node in the AST is classified as a task node, context node, or constraint node, and retains the semantic mapping information in the PDL reduction, and outputs the constructed AST as an intermediate representation of the compilation.
[0013] Furthermore, the optimization modules include: The symbol table construction unit is used to traverse the syntax nodes in the AST, extract the identifiers defined in the PDL specification and their associated attribute information, and construct the symbol table; the attribute information includes the name, type, scope and default value of the identifier; The semantic analysis unit is used to perform semantic verification on each syntactic node of the AST structure based on the symbol table and in combination with the preset semantic rule set. The semantic verification includes identifier type matching verification, scope resolution verification, and constraint consistency verification. When the semantic verification passes, the AST is used as input to the structure optimizer for structure correction and optimization, and the optimized AST is output. When the semantic verification fails, semantic error information is generated and the compilation process is terminated.
[0014] Compared with the prior art, the present invention has at least one of the following beneficial effects: This invention introduces a PDL-oriented compilation mechanism to achieve automated parsing, structural optimization, and high-quality prompt generation of input specifications before the generation of prompt words for large language models, thereby improving the development efficiency and output quality of prompt words. Therefore, compared with existing prompt word design methods that rely on manual writing, this invention has the following beneficial effects: (1) It significantly reduces the development and maintenance costs of prompt words, and significantly improves engineering efficiency and application deployment speed; (2) It improves the consistency, controllability, and logical accuracy of generated prompt words through semantic verification and structural optimization; (3) It achieves flexible adaptation to various large language models and task types through abstract syntax tree (AST) and rule-driven mechanisms, and has good versatility and scalability; (4) It can be widely used in various application scenarios such as intelligent dialogue, document processing, and natural language programming, and has certain commercial value and promotion prospects. Attached Figure Description
[0015] Figure 1 This is a flowchart illustrating the steps of the method for compiling and generating prompt words for a large language model according to the present invention. Figure 2 This is a flowchart illustrating the compilation process for generating prompt words based on the PDL specification in an embodiment of the present invention. Detailed Implementation
[0016] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0017] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the term “comprising” as used in this specification means the presence of the stated features, integers, steps, operations, elements, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof.
[0018] First Embodiment In existing technologies, the construction of prompts mainly relies on domain knowledge, human experience, and repeated debugging. Because LLMs are highly sensitive to factors such as word choice, structural arrangement, and contextual organization in prompt input, prompt design becomes a high-barrier, costly, inefficient, and difficult-to-scale task. Although some solutions attempt to introduce prompt templates or prompt dictionaries to reduce writing costs through preset structures or examples, the following limitations still exist: the construction and updating of prompt templates rely on human experience and lack intelligent optimization capabilities; while prompt dictionaries can provide retrieval and reduce repetitive work to some extent, how to automatically match task intent and adapt to context still requires human decision-making, relying on manual judgment and multiple rounds of experimentation without fundamentally solving the problem; the above methods are difficult to adapt to the prompt construction needs in complex cross-task, cross-domain, and multimodal contexts.
[0019] Furthermore, some cutting-edge research has begun to explore automated prompt optimization methods, such as using reinforcement learning or evolutionary algorithms to iteratively fine-tune prompts, or leveraging the reasoning capabilities of LLMs to progressively rewrite initial prompts. These methods mostly remain at the strategy level and fail to provide a systematic, engineered compilation framework. In addition, patent literature in certain specific fields has disclosed technical solutions for generating interactive dialogues using predefined scripts in conversational systems or intelligent customer service scenarios. However, these solutions are usually tightly coupled with specific business logic, and their design concepts and technical architectures lack universality. They cannot support the cross-domain prompt writing needs of general-purpose LLMs, and in particular, lack a unified framework for the structured abstraction, compilation, and optimization of different types of instructions, complex contextual information, and multi-level constraints.
[0020] Based on a profound understanding of the inherent limitations of manual, inefficient, and unscalable tooltips development, the inventors proposed an automatic tooltips generation method based on a formal compilation framework. This method utilizes a tooltips description language (PDL) to express user requirements and constructs a complete compilation process encompassing lexical analysis, syntax analysis, semantic verification, and structural optimization. This achieves automatic conversion from abstract semantic intent to structured, high-quality tooltips, improving the accuracy, stability, and versatility of tooltips generation and overcoming the limitations of traditional manual design methods in terms of efficiency and adaptability. By constructing a compiler-like automatic tooltips generation method, using PDL to describe user intent, and generating high-quality tooltips that can be input into an LLM through a structured compilation process, this method solves the problems of high manual costs, poor adaptability, and difficulty in quality control in traditional tooltips design, propelling tooltips engineering into a new stage of automation and scalability.
[0021] To achieve the above objectives, this invention discloses a method for compiling prompt words for large language models and its corresponding execution system. This method is independently completed by a single compilation entity, such as a compilation system deployed on a server or user device. Its core lies in borrowing and modifying the principles of traditional programming language compilers to construct a compilation process oriented towards natural language processing tasks. This process transforms abstract user input requirements into executable natural language instructions that have undergone structural optimization, semantic enhancement, and adaptation to specific large language models. The specific implementation is as follows: like Figure 1 , 2 As shown, this invention provides a method for compiling and generating prompt words for large language models, including: Step S1: Respond to the compilation call request and receive the PDL specification containing the task objective, context information, and constraint information as the compilation input text; wherein, step S1 includes: Responding to the compilation call request, the compilation preparation process begins; It receives compiled input text written in a predefined PDL specification, which includes task objectives, context information, and constraint information. The constraint information includes the output language style, content format requirements, and optional target model-specific instructions.
[0022] Specifically, the compilation process begins with receiving a PDL specification text submitted by the user. This text conforms to the predefined prompt-word description language syntax rules and uses a structured format to describe the various information required for the compilation task. In the PDL specification, the task objective section clarifies the core function or output purpose that the large language model should perform; the context information section provides background knowledge, input data, or dialogue history relevant to the task for the model to understand and reference; and the constraint information section includes requirements for the output language style (formal, concise, creative, etc.), content structure (JSON format, Markdown lists, code snippets, etc.), and the calling instructions of the adapted model (system prompts, role attributes, etc.). Before parsing the PDL text, the compilation system must complete basic environment initialization, specification validity verification, and necessary context preprocessing to prepare data for subsequent syntax and semantic analysis steps.
[0023] Step S2: Perform lexical and syntactic analysis on the compiled input text, and construct the corresponding Abstract Syntax Tree (AST) as an intermediate representation using the context-free method; wherein, step S2 includes: The PDL specification is read character by character by a lexical analyzer. At the same time, the PDL specification is parsed into indivisible lexical units Token based on a predefined lexical rule set. Each Token is type-marked and content-identified. The Token includes semantic keywords used to divide the semantic structure, corresponding identifiers, and string values associated with the identifiers. The semantic keywords include TASK for task objectives, CONTEXT for context information, and CONSTRAINT for constraints. The identified tokens are arranged into a token sequence according to the logical order in the original PDL specification, which is then used as input to the parser. The parser performs structured matching on the token sequence based on context-free grammar, identifies grammatical structural units and their nesting relationships, and sequentially reduces the token sequence to grammatical nodes corresponding to grammatical rules during the structured matching process, thus constructing the corresponding AST. Each syntactic node in the AST is classified as a task node, context node, or constraint node, and retains the semantic mapping information in the PDL specification. The completed AST is then output as an intermediate representation for compilation.
[0024] Specifically, the received PDL specification text is first scanned character by character by a lexical analyzer. Based on a predefined set of lexical rules, such as the token pattern defined by regular expressions, the text content is segmented into multiple lexical units (Tokens). Each Token includes: semantic keywords such as TASK, CONTEXT, CONSTRAINT, etc.; identifiers such as task names, variable names, etc.; and their corresponding string values such as task descriptions, input fragments, etc. The lexical analyzer is also responsible for attaching type labels to each Token and annotating its syntactic role, providing a foundation for subsequent syntax analysis.
[0025] Subsequently, the lexical analyzer (or scanner) performs structural reduction on the aforementioned token sequence based on a well-defined set of context-free grammar (CFG) rules. This operation includes shift-reduction processing of the token sequence, detection of valid grammatical structures, and parsing of nesting levels and logical branches. The final output is an abstract syntax tree (AST), which consists of several grammatical nodes, each corresponding to a higher-order semantic element, such as a "task objective description node," a "context injection node," or an "output constraint node." The AST structure fully preserves the semantic logic of the original PDL reduction, serving as an intermediate representation layer for subsequent semantic analysis and prompt generation optimization. Regarding the intermediate representation, each node represents a semantic unit, such as an instruction node, context node, or constraint node, and the tree structure clearly expresses the parent-child and sibling relationships between them.
[0026] Step S3: Based on the preset semantic rule set and symbol table, perform semantic verification and structural correction on the AST. If the semantics are valid, perform structural optimization, including context redundancy merging, constant folding, and model adaptation structure rewriting, to generate the optimized AST; wherein, step S3 includes: Traverse the syntax nodes in the AST, extract the identifiers defined in the PDL specification and their associated attribute information, and construct a symbol table; the attribute information includes the name, type, scope and default value of the identifier; Based on the symbol table and combined with the preset semantic rule set, the semantic analyzer performs semantic verification on each syntactic node of the AST structure; the semantic verification includes identifier type matching verification, scope resolution verification, and constraint consistency verification. If the semantic verification passes, the AST is used as input to the structure optimizer for structural correction and optimization, and the optimized AST is output. If semantic verification fails, a semantic error message is generated and the compilation process is terminated.
[0027] Further, in step S3, the AST is used as input to the structure optimizer for structural correction and optimization, and the optimized AST is output, including: Identify context nodes with semantic redundancy or structural repetition in the AST and perform node merging according to the merging rules; For substructures in the AST that are expressed as constants, perform operations or substitutions in advance to complete constant folding; Based on the preset model adaptability rewriting rules, semantically equivalent replacements are performed on structures in the AST that are not conducive to language model parsing, and the optimized AST is output.
[0028] Specifically, after the AST is constructed, the semantic analysis and structural optimization phase begins. First, the AST is traversed from top to bottom to identify all identifiers defined in the PDL specification and extract their names, types, scopes, default values, and other attribute information to construct a symbol table, which serves as the context for subsequent semantic verification. Then, the semantic analyzer performs semantic verification on each syntax node in the AST according to a pre-defined set of semantic rules. The verification process includes: ① type matching check, ensuring that each identifier assignment or reference conforms to its declared type; ② scope resolution verification, ensuring that all used identifiers are defined within their legal scopes; ③ constraint consistency check, ensuring that there are no logical conflicts between constraints such as output format, style, and structure. If a semantic error is found, the system will record the error information and terminate the compilation process. After all semantic checks pass, the structural optimizer starts to optimize and transform the AST. The optimization includes, but is not limited to, the following strategies: (1) Context redundancy merging: Identify repetitive or semantically equivalent context injection nodes in the AST, and merge nodes according to the merging rules to reduce redundant prompt content; (2) Constant folding: For static parsable substructures such as fixed input formats and constant variables, pre-calculate their results and replace them with specific values to simplify the subsequent generation process; (3) Model adaptability structure rewriting: Based on the predefined structure rewriting rule library, replace AST structures that are not conducive to the target LLM processing, such as excessively long nesting and inefficient prompt structures, with semantically equivalent but structurally superior versions to improve the parsability of prompt words and response stability. After optimization, output the structurally optimized AST as input for the subsequent generation stage.
[0029] Step S4: Traverse the optimized AST, match the prompt word templates in the prompt word generation rule base, map semantic nodes to structured text fragments, concatenate the structured text fragments to form the final prompt word text, and output a compiled result consistent with the PDL reduction semantics to drive the execution of large language model tasks. Step S4 includes: Traverse each grammar node in the optimized AST, identify the corresponding semantic type, and retrieve the prompt word template corresponding to the semantic type from the preset prompt word generation rule base; Map the field values in each syntax node to the corresponding structured text fragments according to the prompt word template format; Following the logical structure and node order of the AST, the complete prompt words are sequentially concatenated to construct them. After format preprocessing, the final prompt word text, which is consistent with the semantics of the PDL reduction, is output as the input for the large language model task.
[0030] Specifically, after structural optimization, the generator receives the optimized AST as input and performs node access and processing on the entire syntax tree according to a preset traversal strategy, such as depth-first traversal. Whenever the generator accesses a semantic node in the AST, the system calls the built-in prompt word generation rule library, selecting a matching generation template based on the node's type, attribute value, and semantic context. The generation template defines how to map structured semantic nodes to text fragments expressed in natural language. For example, if there is a node in the AST of type "task goal" with the value "summary generation," the generator will match a language template similar to "Please complete the summary task based on the following information:" and generate a prompt accordingly. Similarly, when a constraint node specifying an output format of "JSON" is detected, the system will generate a corresponding text prompt, such as "Please ensure your answer is a JSON object that strictly conforms to the RFC 8259 standard." During the text fragment concatenation stage, the generator not only connects the natural language expressions corresponding to the nodes, but also embeds specific start tags, structured example prompts such as Few-shot format, role-separating statements, and format control comments according to the format requirements of the target LLM. This ensures that the generated prompts are semantically complete, structurally clear, and pragmatically compatible with the input requirements of the target large language model. After formatting and content concatenation, all text fragments are integrated into a linearly structured prompt string, which is a high-quality prompt output that is semantically consistent with the PDL specification and can be directly used to drive the execution of LLM tasks.
[0031] Furthermore, building upon the existing prompt word compilation scheme, this embodiment can further support incremental and dynamic compilation mechanisms to meet the high-efficiency response requirements of scenarios with continuously evolving contexts, such as multi-turn dialogues. Specifically, while preserving the AST structure and symbol table of historical dialogues, when user input is added or modified, a complete recompilation process is not executed. Instead, only the affected AST child nodes are identified, and their local subtrees are updated, optimized, and regenerated, thereby achieving on-demand allocation and dynamic adaptation of compilation resources. This process is analogous to JIT compilation, significantly improving the real-time performance and computational efficiency of prompt word generation in complex dialogue flows while ensuring semantic consistency.
[0032] Second Embodiment Based on the same inventive concept, this invention also provides a system for compiling and generating prompt words for large language models, which performs the above-described method for compiling and generating prompt words for large language models, including: The user interface module is used to respond to compilation call requests and receive PDL specifications containing task objectives, context and constraint information as compilation input. The parsing module is used to perform lexical and syntactic analysis on PDL reduction and construct the corresponding abstract syntax tree (AST) as an intermediate representation based on the context-free method. The optimization module is used to perform semantic verification and structural correction on the AST based on the preset semantic rule set and symbol table. When the semantics are valid, it performs structural optimization including context redundancy merging, constant folding and model adaptation structure rewriting to generate the optimized AST. The generation module is used to traverse the optimized AST, match the prompt word templates in the generation rule base, and map semantic nodes into structured text fragments; The output module is used to concatenate structured text fragments into the final prompt word text, and outputs a compiled result consistent with the PDL reduction semantics, which is used to drive the execution of large language model tasks.
[0033] Furthermore, the PDL specification includes task objectives, contextual information, and constraint information; the constraint information includes the output language style, content format requirements, and optional target model-specific instructions.
[0034] Furthermore, the parsing module includes: The lexical analysis unit is used to read the PDL specification character by character. Based on a predefined lexical rule set, it parses the PDL specification into indivisible lexical units (Tokens). Each Token is type-marked and content-identified. Tokens include semantic keywords for dividing the semantic structure, corresponding identifiers, and string values associated with the identifiers. The semantic keywords include TASK (task objective), CONTEXT (context information), and CONSTRAINT (constraints). The identified Tokens are arranged into a Token sequence according to their logical order in the original PDL specification, which is used as input to the parser. The syntax analysis unit is used to perform structured matching of token sequences based on context-free grammars, identify grammatical structure units and their nesting relationships, and sequentially reduce the token sequence to the syntax nodes corresponding to the syntax rules during the structured matching process, constructing the corresponding AST. Each syntax node in the AST is classified as a task node, context node, or constraint node, and retains the semantic mapping information in the PDL reduction, and outputs the constructed AST as an intermediate representation of the compilation.
[0035] Furthermore, the optimization modules include: The symbol table construction unit is used to traverse the syntax nodes in the AST, extract the identifiers defined in the PDL specification and their associated attribute information, and construct the symbol table; the attribute information includes the name, type, scope and default value of the identifier; The semantic analysis unit is used to perform semantic verification on each syntactic node of the AST structure based on the symbol table and in combination with the preset semantic rule set. The semantic verification includes identifier type matching verification, scope resolution verification, and constraint consistency verification. When the semantic verification passes, the AST is used as input to the structure optimizer for structure correction and optimization, and the optimized AST is output. When the semantic verification fails, semantic error information is generated and the compilation process is terminated.
[0036] The above description is merely a preferred embodiment of the present invention. The scope of protection of the present invention is not limited to the above embodiment. All technical solutions based on the concept of the present invention are within the scope of protection of the present invention. It should be noted that for those skilled in the art, various improvements and modifications that do not depart from the principles of the present invention should also be considered within the scope of protection of the present invention.
[0037] It should be noted that the above embodiments can be freely combined as needed. The above description is only a preferred embodiment of the present invention. It should be pointed out that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A method for compiling and generating prompt words for large language models, characterized in that, include: Step S1: Respond to the compilation call request and receive the PDL specification containing the task target, context information, and constraint information as the compilation input text; Step S2: Perform lexical and syntactic analysis on the compiled input text, and construct the corresponding Abstract Syntax Tree (AST) as an intermediate representation using the context-free method; Step S3: Based on the preset semantic rule set and symbol table, perform semantic verification and structural correction on the AST. If the semantics are valid, perform structural optimization including context redundancy merging, constant folding and model adaptation structure rewriting to generate the optimized AST. Step S4: Traverse the optimized AST, match the prompt word templates in the prompt word generation rule base, map semantic nodes to structured text fragments, concatenate the structured text fragments to form the final prompt word text, and output the compilation result consistent with the PDL reduction semantics to drive the execution of the large language model task.
2. The prompt word compilation and generation method according to claim 1, characterized in that, Step S1 includes: In response to the compilation call request, proceed with the compilation preparation process; Receive the compiled input text written in the predefined PDL specification, which includes the task objective, context information, and constraint information; the constraint information includes the output language style, content format requirements, and optional target model-specific instructions.
3. The method for compiling and generating prompt words according to claim 2, characterized in that, Step S2 includes: The PDL specification is read character by character by a lexical analyzer. At the same time, the PDL specification is parsed into indivisible lexical units Token based on a predefined lexical rule set. Each Token is type-marked and content-identified. The Token includes semantic keywords for dividing the semantic structure, corresponding identifiers, and string values associated with the identifiers. The semantic keywords include TASK for the task objective, CONTEXT for the context information, and CONSTRAINT for the constraints. The identified tokens are arranged into a token sequence according to the logical order in the original PDL specification, and used as input to the parser; The parser performs structured matching on the token sequence based on the context-free grammar, identifies grammatical structural units and their nesting relationships, and sequentially reduces the token sequence to grammatical nodes corresponding to grammatical rules during the structured matching process, thus constructing the corresponding AST. Each of the syntax nodes in the AST is classified as a task node, context node, or constraint node, and retains the semantic mapping information in the PDL specification. The constructed AST is then output as an intermediate representation for compilation.
4. The prompt word compilation and generation method according to claim 3, characterized in that, Step S3 includes: Traverse the syntax nodes in the AST, extract the identifiers defined in the PDL specification and their associated attribute information, and construct a symbol table; the attribute information includes the name, type, scope and default value of the identifier; Based on the symbol table and in conjunction with the preset semantic rule set, the semantic analyzer performs semantic verification on each of the syntax nodes of the AST structure; the semantic verification includes type matching verification, scope resolution verification, and constraint consistency verification of the identifier. If the semantic verification passes, the AST is used as input to the structure optimizer for structural correction and optimization, and the optimized AST is output. If the semantic verification fails, a semantic error message is generated and the compilation process is terminated.
5. The prompt word compilation and generation method according to claim 4, characterized in that, In step S3, the AST is used as input to the structure optimizer for structural correction and optimization, and the optimized AST is output, including: Identify context nodes with semantic redundancy or structural repetition in the AST, and perform node merging according to the merging rules; For substructures in the AST that are expressed as constants, perform operations or replacements in advance to complete the constant folding; Based on the preset model adaptability rewriting rules, semantically equivalent replacements are performed on the structures in the AST that are not conducive to language model parsing, and the optimized AST is output.
6. The prompt word compilation and generation method according to claim 5, characterized in that, Step S4 includes: Traverse each of the syntax nodes in the optimized AST, identify the corresponding semantic type, and retrieve the prompt word template corresponding to the semantic type from the preset prompt word generation rule base; The field values in each of the syntax nodes are mapped to the corresponding structured text fragments according to the prompt word template format; The AST is sequentially assembled according to its logical structure and node order to construct a complete prompt word. After format preprocessing, the final prompt word text, which is semantically consistent with the PDL specification, is output as the input for the execution of the large language model task.
7. A system for compiling and generating prompt words for large language models, comprising executing the method for compiling and generating prompt words for large language models as described in any one of claims 1 to 6, characterized in that, include, The user interface module is used to respond to compilation call requests and receive PDL specifications containing task objectives, context and constraint information as compilation input. The parsing module is used to perform lexical and syntactic analysis on the PDL reduction and construct the corresponding abstract syntax tree (AST) as an intermediate representation based on the context-free method. The optimization module is used to perform semantic verification and structural correction on the AST based on a preset semantic rule set and symbol table. When the semantics are valid, structural optimization including context redundancy merging, constant folding and model adaptation structure rewriting is performed to generate the optimized AST. The generation module is used to traverse the optimized AST, match the prompt word templates in the generation rule base, and map semantic nodes into structured text fragments; The output module is used to concatenate the structured text fragments to form the final prompt word text, and output the compilation result that is consistent with the semantics of the PDL specification, which is used to drive the execution of the large language model task.
8. The prompt word compilation and generation system according to claim 7, characterized in that, The PDL specification includes the task objective, context information, and constraint information; the constraint information includes the output language style, content format requirements, and optional target model-specific instructions.
9. The prompt word compilation and generation system according to claim 8, characterized in that, The parsing module includes: The lexical analysis unit is used to read the PDL specification character by character and parse the PDL specification into indivisible lexical units (Tokens) based on a predefined lexical rule set. Each Token is then type-marked and content-identified. Each Token includes a semantic keyword for dividing the semantic structure, a corresponding identifier, and a string value associated with the identifier. The semantic keywords include the TASK representing the task objective, CONTEXT representing the context information, and CONSTRAINT representing the constraints. The identified Tokens are arranged into a Token sequence according to their logical order in the original PDL specification and used as input to the parser. The syntax analysis unit is used to perform structured matching on the token sequence based on the context-free grammar, identify the syntax structure units and their nesting relationships, and sequentially reduce the token sequence to the syntax nodes corresponding to the syntax rules during the structured matching process to construct the corresponding AST; each syntax node in the AST is classified as a task node, context node or constraint node, and retains the semantic mapping information in the PDL reduction, and outputs the constructed AST as an intermediate representation for compilation.
10. The prompt word compilation and generation system according to claim 8, characterized in that, The optimization module includes: The symbol table construction unit is used to traverse the syntax nodes in the AST, extract the identifiers defined in the PDL specification and their associated attribute information, and construct the symbol table; the attribute information includes the name, type, scope and default value of the identifier; The semantic analysis unit is used to perform semantic verification on each of the syntax nodes of the AST structure based on the symbol table and in combination with the preset semantic rule set. The semantic verification includes type matching verification, scope resolution verification, and constraint consistency verification of the identifier. When the semantic verification passes, the AST is used as input to the structure optimizer for structure correction and optimization, and the optimized AST is output. When the semantic verification fails, semantic error information is generated and the compilation process is terminated.
Citation Information
Patent Citations
Domain-specific language compiling method, device and equipment based on multiple computing frameworks
CN119536736A
Method and device for compressing cue words of large language model and medium
CN119940540A
A system and a method for automatic generation of smart contracts across blockchain platforms
EP4145761A1
Method and system for building compilable customized module
WO2025139892A1
Cited By
Automatic time sequence feature generation system and method for large model prompt project
CN121301463A
AI-based natural language demand analysis method and system
CN121479204A
New media intelligent channel switching method, system and device and medium
CN121561074A
Structured display method, system and equipment for reasoning process and storage medium
CN121579663A
Multi-model cue word optimization and feedback tuning system fusing context semantics
CN121636691A