Methods, apparatus, equipment, media, and program products for cross-language translation of program code

By using nested dictionary structures, the cross-language translation method of program code can simultaneously complete syntax parsing, semantic aggregation, and type inference in a single scan, solving the problems of high time complexity and large memory consumption in existing technologies, and achieving efficient cross-language translation.

CN123114471APending Publication Date: 2026-11-17JOINT WARFARE COLLEGE NAT DEFENSE UNIV OF THE CHINESE PEOPLES LIBERATION ARMY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611207715.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-10
Publication Date
2026-11-17

AI Technical Summary

Technical Problem

Existing methods for cross-language translation of program code suffer from high time complexity, large memory consumption, and high expansion costs when performing type inference, especially when dealing with complex expressions.

Method used

By using a nested dictionary structure as the context state carrier, the nested dictionary structure is created and initialized on demand. The source code is scanned and parsed according to the order of text appearance, the nested sub-elements are processed recursively, semantic aggregation and type inference are performed in real time, and the target code fragment is generated, avoiding multiple traversals and global constraint solving.

Benefits of technology

It implements a lightweight pipelined compilation mechanism with low time complexity and low memory footprint, which can efficiently process complex expressions and reduce the complexity and memory requirements of the transpile.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123114471A_ABST
    Figure CN123114471A_ABST
Patent Text Reader

Abstract

This application provides a method, apparatus, device, medium, and program product for cross-language translation of program code. The method includes allocating and initializing a nested dictionary structure to describe the state context; scanning and parsing the source code in the order of text appearance; if the current syntax element contains nested sub-elements, creating and associating the current dictionary entry; then continuing to process the nested sub-elements; after all nested sub-elements have been processed, performing semantic aggregation based on the determined data type of each sub-element to obtain the data type corresponding to the current syntax element and writing it into the current dictionary entry; generating a target code fragment and writing it into the current dictionary entry; finally, returning to scanning and parsing until the source code scanning and parsing is complete. It simultaneously completes syntax parsing, semantic aggregation, type inference, and code generation in a single scan, effectively reducing the time complexity and memory consumption of the translation process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application belongs to the field of computer technology, and in particular relates to a method, apparatus, device, medium and program product for cross-language translation of program code. Background Technology

[0002] In the field of programming language translation, converting source code from a source language to a target language is a core function of compilers and translators, with type handling being a crucial factor determining the correctness and efficiency of the conversion. Constraint-based global analysis methods are currently a relatively mature type inference approach. This method establishes a system of type constraint equations by traversing the abstract syntax tree and then solves them using a unified algorithm. Specifically, it transforms type relationships in the source code into equality constraints, including constraints on the type of the rvalue during variable initialization and the value of the left expression, compatibility constraints on operand types in operator operations, and matching constraints on the types of actual and formal parameters during function calls. The solution process uses a unified algorithm to replace and normalize type variables, ultimately deriving the most general type.

[0003] In addition, data flow analysis-based methods, such as Rust's trait system and C#'s type inference, complete type checking through multiple scans or incremental analysis. Type information propagates along the control flow graph, and is iteratively solved by combining the symbol table and type dependency graph until a fixed point is reached.

[0004] Both of the above methods require complete type analysis before code generation. At the engineering implementation level, they both need to maintain complete data structures such as abstract syntax trees, symbol tables, and type dependency graphs, and employ a multi-pass scanning strategy. While theoretically complete, they face the following technical drawbacks in practical applications: Firstly, global analysis methods require multiple traversals of the source code, resulting in high time complexity. Secondly, maintaining multiple intermediate data structures such as abstract syntax trees, symbol tables, and type dependency graphs consumes significant memory resources. Thirdly, when dealing with complex expressions such as nested function calls, multidimensional array access, and mixed operators, complex constraint-solving algorithms and additional data structures are needed to maintain type dependencies. The explicit constraint propagation mechanism for type transitive dependencies between sub-expressions and parent expressions leads to large code volumes in the transpilation tool, making debugging difficult and expansion costs high.

[0005] Therefore, there is an urgent need for a new method for cross-language translation of program code to effectively solve the above problems. Summary of the Invention

[0006] To overcome the aforementioned deficiencies in the prior art, this application provides a method, apparatus, device, medium, and program product for cross-language translation of program code, to address the problem of reducing the complexity of the translator when converting between two statically typed languages.

[0007] In a first aspect, embodiments of this application provide a method for cross-language translation of program code. This method, executed by a processor, includes: allocating and initializing a nested dictionary structure for describing a state context, the nested dictionary structure including dictionary entries created according to the nesting hierarchy of syntactic elements; scanning and parsing the source code in the order of text appearance to obtain the current syntactic element; if the current syntactic element contains nested sub-elements, creating a current dictionary entry at the end of the nested dictionary structure, associating the current dictionary entry with the parent dictionary entry, and continuing to process the nested sub-elements; after all nested sub-elements have been processed, performing semantic aggregation based on the data type stored in the dictionary entries corresponding to each sub-element to determine the data type corresponding to the current syntactic element; writing the data type into the current dictionary entry corresponding to the current syntactic element; generating a target code fragment and writing the target code fragment into the current dictionary entry corresponding to the current syntactic element; and after the current syntactic element has been processed, returning to the step of scanning and parsing the source code in the order of text appearance, and continuing execution until the source code scanning and parsing is complete.

[0008] Secondly, embodiments of this application provide a cross-language translation device for program code. This device is configured in a processor and includes: a resource allocation module configured to allocate and initialize a nested dictionary structure for describing a state context, the nested dictionary structure including dictionary entries created according to the nesting level of syntactic elements; a parsing processing module configured to scan and parse the source code in the order of text appearance to obtain the current syntactic element; a nesting processing module configured to, if the current syntactic element contains nested sub-elements, create a current dictionary entry at the end of the nested dictionary structure, associate the current dictionary entry with the parent dictionary entry, and continue processing the nested sub-elements; after all nested sub-elements have been processed, perform semantic aggregation based on the data type stored in the dictionary entries corresponding to each sub-element to determine the data type corresponding to the current syntactic element; write the data type into the current dictionary entry corresponding to the current syntactic element; generate a target code fragment and write the target code fragment into the current dictionary entry corresponding to the current syntactic element; after the current syntactic element has been processed, return to the step of scanning and parsing the source code in the order of text appearance, and continue execution until the source code scanning and parsing is complete.

[0009] Thirdly, embodiments of this application provide an electronic device, which includes a processor and a memory communicatively connected to the processor; the memory stores computer-executable instructions; the processor executes the computer-executable instructions stored in the memory to implement the cross-language translation method of program code as described in the first aspect.

[0010] Fourthly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the cross-language translation method of program code as described in the first aspect.

[0011] Fifthly, embodiments of this application provide a computer program product, which includes a computer program that, when executed by a processor, implements the cross-language translation method of program code as described in the first aspect.

[0012] The method, apparatus, device, medium, and program product for cross-language translation of program code provided in this application embodiment first allocates and initializes a nested dictionary structure to describe the state context. Then, the source code is scanned and parsed according to the order of text appearance to obtain the current syntax element. For the current syntax element containing nested sub-elements, a current dictionary entry is created at the end of the nested dictionary structure, and the current dictionary entry is associated with the parent dictionary entry to continue processing the nested sub-elements. After all nested sub-elements have been processed, semantic aggregation is performed according to the data type stored in the dictionary entry corresponding to each sub-element to determine the data type corresponding to the current syntax element. The data type is written into the corresponding current dictionary entry. A target code fragment is generated and written into the corresponding current dictionary entry. After the current syntax element has been processed, the step of scanning and parsing the source code according to the order of text appearance is returned, and execution continues until the source code scanning and parsing is completed. In the cross-language translation scenario, this application embodiment uses a nested dictionary structure created on demand as a context state carrier, and synchronously completes syntax parsing, semantic aggregation, type inference, and target code fragment generation during a single source code scan, realizing a lightweight pipeline compilation mechanism with low time complexity and low memory consumption. Attached Figure Description

[0013] Other features, objects, and advantages of this application will become more apparent from the following detailed description of non-limiting embodiments with reference to the accompanying drawings:

[0014] Figure 1 This is a schematic diagram of an application scenario provided by an embodiment of this application;

[0015] Figure 2 This is a flowchart illustrating a method for cross-language translation of program code provided in an embodiment of this application;

[0016] Figure 3 This is a flowchart illustrating another method for cross-language translation of program code provided in an embodiment of this application;

[0017] Figure 4This is a schematic diagram of the structure of a program code cross-language translation device 400 provided in an embodiment of this application;

[0018] Figure 5 This is a schematic diagram of the structure of an electronic device 500 provided in an embodiment of this application.

[0019] The same or similar reference numerals in the accompanying drawings represent the same or similar parts. Detailed Implementation

[0020] To better understand and explain this application, a further detailed description will be provided below with reference to the accompanying drawings. This application is not limited to these specific embodiments. Rather, any modifications or equivalent substitutions made to this application should be covered within the scope of the claims.

[0021] It should be noted that numerous specific details are provided in the following detailed embodiments. Those skilled in the art should understand that this application can be implemented without these specific details. In the various detailed embodiments given below, principles, structures, and components well known in the art are not described in detail in order to highlight the main points of this application.

[0022] In this application, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, without requiring or implying any actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed. The various embodiments in this application are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to mutually. For the structures, products, etc., disclosed in the embodiments, since they correspond to the disclosed parts, the descriptions are relatively simple; relevant details can be found in the method section.

[0023] Figure 1 This is a schematic diagram illustrating an application scenario provided in an embodiment of this application. For example... Figure 1As shown, this application scenario includes a user's terminal device 101 and a code conversion system 102. The code conversion system 102 can be deployed on a server or as a standalone service, receiving source code (such as SimScript files) uploaded by the terminal device 101. The code conversion system 102 first initializes a nested dictionary structure and a path pointer stack, then sequentially traverses the source code, scanning and parsing syntax elements while extending path pointers to create and associate corresponding dictionary entries. If there are nested sub-elements, they are recursively processed, and the type of the current element is inferred through semantic aggregation of sub-elements' types. If there is no nesting, the type is directly inferred from multiple sources and written into the dictionary entry. Simultaneously, target code fragments are generated based on the element nesting level and stored in the dictionary entry. After processing the syntax elements, the path pointers are immediately backtracked, continuously looping through the entire process of scanning, parsing, type inference, and code generation until the source code traversal and parsing are complete. The code conversion system 102 then returns the translated target code fragment to the terminal device 101.

[0024] The cross-language code translation method proposed in this application maintains dictionaries and pointers in real time when recursively processing nested structures, with no repeated scanning throughout the process. It parses, infers, and generates code simultaneously, eliminating the need to pre-build a complete syntax tree for secondary processing. This results in low memory pressure and prevents lag when processing extremely long files or massive modules. Furthermore, it reads the source code only once in textual order, without backtracking or rescanning, achieving a time complexity close to O(n).

[0025] The following is combined Figure 2 The present application describes the method for cross-language translation of program code provided in the embodiments. Figure 2 This is a flowchart illustrating a method for cross-language translation of program code provided in an embodiment of this application. For example... Figure 2 As shown, the method includes:

[0026] In step S21, a nested dictionary structure for describing the state context is allocated and initialized. This nested dictionary structure includes dictionary entries created according to the nesting level of the syntax elements.

[0027] In step S22, the source code is scanned and parsed in the order of text appearance to obtain the current syntax element.

[0028] In step S23, if the current syntax element contains nested child elements, a current dictionary entry is created at the end of the nested dictionary structure, and the current dictionary entry is associated with the parent dictionary entry. The nested child elements are then processed. After all nested child elements have been processed, semantic aggregation is performed based on the data type stored in the dictionary entry corresponding to each child element to determine the data type corresponding to the current syntax element. The data type is written into the current dictionary entry corresponding to the current syntax element. A target code fragment is generated and written into the current dictionary entry corresponding to the current syntax element. After the current syntax element has been processed, the process returns to the step of scanning and parsing the source code in the order of text appearance, and continues until the source code scanning and parsing is completed.

[0029] The following will provide a detailed explanation of the contents of steps S21 to S23.

[0030] In step S21, the nested dictionary structure includes dictionary entries created according to the nesting level of grammatical elements. The nested dictionary structure has a tree-like nesting architecture, with the root dictionary at the top and sub-dictionaries nested downwards at each level, without circular nesting. The structure can dynamically expand and contract, flexibly adding or deleting nested dictionary entries as grammatical elements are added or removed. The nesting depth of the nested dictionary is strictly equivalent to the nesting level of the expression syntax; the top-level dictionary corresponds to the outer grammatical level of the expression, and the progressively nested sub-dictionaries sequentially match the inner sub-expressions (i.e., the inner grammatical levels), with the structural topology completely consistent with the expression syntax tree shape.

[0031] The traversal rules of nested dictionary structures are adapted to the recursive parsing process of expressions, completing the semantic decomposition layer by layer according to the priority of expression operations and the nesting order. Taking the arithmetic expression (a+(b*(cd)))) as an example, a hierarchical nested dictionary model is constructed, with addition as the top-level structure, multiplication embedded in the addition operation field, and subtraction further nested within the multiplication operation. Relying on the multi-level nested dictionary form, the multi-level nested operation architecture of expressions is completely replicated. Compared with storage forms such as flat arrays, linked structures, and fixed structures, nested dictionary structures have many advantages in memory utilization for variable-depth and irregular expression data storage scenarios. Nested dictionaries only allocate storage space for actual existing syntax nodes, without the need to pre-define fixed memory intervals. Sub-expression nodes are created in real time as the expression structure is generated, and there is no idle reserved memory, effectively improving memory resource utilization. Relying on the built-in association attributes of the dictionary structure, redundant relation storage is eliminated. The hierarchical relationship embedded in the sub-dictionary directly represents the subordinate relationship of the operation unit, without the need to add auxiliary fields such as hierarchical numbers and parent-child indexes to record association information, reducing memory consumption caused by additional data.

[0032] Dictionary entries include, but are not limited to, data type fields, type conversion flag fields, child element attachment fields, target code snippet fields, and expected type fields. The data type field stores the data type determined by semantic aggregation or multi-source type inference. The type conversion flag field stores conversion instructions in case of type mismatch. The target code snippet field stores the generated target code snippet. The child element attachment field is a container field used to hold and associate nested sub-expressions and sub-syntax elements of the current syntax node; it is the core carrier field for implementing the hierarchical nested structure of dictionaries and the subordinate relationships of mapping expressions. The expected type field stores the type expectation value propagated from the parent dictionary entry.

[0033] The nesting level of syntax elements refers to the nested code scope level formed by nested boundary syntax elements in the source code. It is nested layer by layer from the outer layer to the inner layer, and there is a containment relationship between the levels. Variables, expressions and data types in different levels are independent of each other.

[0034] A path pointer stack is used to record the hierarchical access sequence from the root node of a nested dictionary structure to the dictionary entry corresponding to the currently processed syntax element. As an abstract data structure, the path pointer stack can be implemented in ways including, but not limited to, sequential list structures and linked list structures.

[0035] In step S22, following the text appearance order means starting from the beginning of the source code file, reading and parsing each character and syntax unit sequentially from top to bottom, from left to right, and from front to back, according to the natural writing order of the code in the text, without skipping, reversing the order, or reading randomly.

[0036] The code is read character by character in the order in which it appears in the source code text. First, lexical analysis is performed to divide the character stream into independent lexical units such as keywords, identifiers, operators, and constants. Then, based on the sequence of lexical units, syntax analysis is performed to identify syntax elements such as expressions, statements, code blocks, and nested structures, thereby obtaining the syntax elements to be processed and the contextual structure information.

[0037] Syntactic elements refer to the basic units that constitute program code. They are specific to a particular programming language and include, but are not limited to, identifiers, keywords, operators, constants, variables, and punctuation marks. Syntactic structures formed by combining these basic syntactic elements according to certain rules also fall under the category of syntactic elements, including, but not limited to, statements, expressions, declarations, and function definitions. Taking SimScript as an example, syntactic elements are the structured parsing results obtained after lexical and syntactic analysis of SimScript source code, including lexical-level syntactic elements and syntactic-level syntactic elements. Lexical-level syntactic elements are the smallest parsing units obtained through lexical analysis, including SimScript keywords, identifiers, variables, constants, operators, and separators. Syntactic-level syntactic elements are high-level syntactic structures obtained through syntactic analysis, including SimScript expressions, variable declarations, execution statements, conditional blocks, loop blocks, and nested structures.

[0038] In step S23, the source code is scanned sequentially according to textual order, and parsing is performed according to predetermined rules. If the current grammatical element contains nested sub-elements, a new dictionary entry is created at the end of the corresponding level of the nested dictionary structure. The new entry is then associated with the parent dictionary entry, and the nested sub-elements are processed recursively. After all nested sub-elements have been processed, semantic aggregation is performed based on the data type stored in each sub-element dictionary entry to determine the data type corresponding to the current grammatical element, and this type information is entered into the corresponding dictionary entry. Next, a matching target code fragment is generated and synchronously written into the corresponding dictionary entry. After all individual grammatical elements have been processed, the process returns to the sequential scanning and parsing phase, repeating the above operations until the entire source code parsing is completed.

[0039] Nested child elements can serve as the value or content of the current dictionary entry, or as a sub-level nested dictionary entry. For example, if a nested child element is a leaf node, it can be used as the content of the current dictionary entry. Leaf nodes can be simple syntax elements, such as identifiers or constants. When a nested child element is a compound syntax element or a syntax element with a nested structure, a sub-level nested dictionary entry can be created based on the current dictionary entry to manage the nested child elements.

[0040] In some embodiments, a stack structure can be used to ensure the correct nesting hierarchy. The state context also includes a path pointer stack. The path pointer stack is used to record the hierarchical access sequence from the root node of the nested dictionary structure to the dictionary entry corresponding to the currently processed syntax element. The method further includes extending the path pointer stack before creating the current dictionary entry at the end of the nested dictionary structure; when creating the current dictionary entry at the end of the nested dictionary structure, reading the tail pointer of the extended path pointer stack (which is used to locate the position of the current dictionary entry in the nested dictionary structure), writing the current dictionary entry to the corresponding position in the nested dictionary structure; and unwinding the path pointer stack after the current syntax element has been processed.

[0041] Nesting levels are managed using a path pointer stack, enabling adaptation between two storage formats for nested child elements. Before processing nested child elements, the path pointer stack is extended. When creating a dictionary entry, the stack tail pointer is read, and the position of the current dictionary entry within the nested dictionary structure is located based on the pointer stored at the stack tail. The current dictionary entry is then written to the corresponding position in the nested dictionary structure. If the nested child element is a leaf node (i.e., there is no new nesting level), the data of the leaf node is directly written as the field content to the corresponding field of the target dictionary entry. If the nested child element is an inner node (i.e., the child element includes new nesting levels), a new dictionary entry is created and mounted as a lower-level nested dictionary entry of the target dictionary entry for storage.

[0042] After processing a single syntax element, the path pointer stack is rolled back to restore the addressing state of the previous level. This process is repeated to complete the parsing and storage of all elements, accommodating both child elements as dictionary content and lower-level dictionary entries.

[0043] Before creating the dictionary entry corresponding to the current syntax element, the hierarchical access sequence (i.e., the path pointer stack) is extended by appending hierarchical key names to achieve a downward switch in hierarchy. When creating the dictionary entry, the end key name of the extended hierarchical access sequence is read to locate the target dictionary object of the current syntax level, and the current dictionary entry is written to the corresponding position in the target dictionary.

[0044] Suppose that {A(B)} is a nested hierarchy. Initially, the nested dictionary structure is empty, the path pointer stack points to the outermost root dictionary, and the level is assumed to be 0. When the syntax element A is scanned, the path pointer stack is extended, and the pointer moves down one level from the root dictionary, preparing to enter the inner level of A. At the end position pointed to by the pointer, a current dictionary entry corresponding to A is created and attached to the root dictionary. Next, when the syntax element B is scanned, the path pointer stack is extended, and the pointer moves down one level from the current dictionary entry corresponding to A, preparing to enter the inner level of B. At the end position pointed to by the pointer, a current dictionary entry corresponding to B is created and attached to the current dictionary entry corresponding to A, thus forming a nested dictionary structure.

[0045] After processing the current syntax element, the extended path pointer stack is rolled back. The rollback operation is relative to the extension operation. Rollback is achieved by removing the key corresponding to the current syntax element from the hierarchical access sequence. Then, the source code is returned to scan and parse the next syntax element.

[0046] This application embodiment achieves precise management of nested paths by appending and removing hierarchical key names, stably supports dual storage formats where inner child elements can be used as dictionary entry content or as the next level dictionary entry, and is fully compatible with nested dictionary construction, semantic aggregation, and code generation processes.

[0047] In some embodiments, a dictionary entry includes a child element mounting field and an expected type field. The sequence in the hierarchical access sequence, excluding the last element, is the outer path. The outer path is used to access the dictionary entry corresponding to the outer syntax element of the current syntax element. The current dictionary entry is associated with the parent dictionary entry, including mounting the current dictionary entry under the child element mounting field of the parent dictionary entry; accessing the parent dictionary entry (i.e., the parent dictionary entry) corresponding to the current dictionary entry through the extended path pointer stack; if the parent dictionary entry has a pre-written type expected value for the current syntax element, then the type expected value is written into the expected type field of the current dictionary entry; or, if the parent dictionary entry has not pre-written a type expected value for the current syntax element, then the expected type field of the current dictionary entry is kept empty.

[0048] This application embodiment pre-writes the type expectation value into the parent dictionary entry before or simultaneously with the creation of the child dictionary entry. The child dictionary entry can directly read this value when it is processed later. This makes the processing of the syntactic elements of the parent-child relationship not require strict synchronization or callback, and avoids global lookup, thus improving the efficiency of hierarchical access.

[0049] When parsing the current syntax element, if it includes nested child elements, each child element is processed recursively. After all child elements have been processed, semantic aggregation is performed based on the data type stored in the dictionary entries corresponding to each child element. The data type stored in each dictionary entry is determined during the recursive processing. Semantic aggregation can be understood as collecting the data type information of the child elements based on the semantic role (function call / array access / operation / assignment) of the current syntax element, and deriving the data type of the current syntax element according to the fixed rules corresponding to that role.

[0050] By performing semantic aggregation corresponding to different semantic roles, type deduction of arithmetic, logical, and relational expressions can be achieved, completing type determination and legality verification of assignment statements; based on the type aggregation of elements within an array, the overall type of the array can be deduced; combined with actual parameter information, the return type of function calls can be deduced, and type resolution of array subscripts and object member access can be supported. Furthermore, semantic aggregation can also perform semantic compliance judgment on control statements such as branches and loops, and the resulting type results can serve as an important basis for target code generation, assisting in the static semantic error detection of the program, thereby ensuring the standardization and effectiveness of the code's semantic logic.

[0051] The cross-language code translation method provided in this application, in cross-language translation scenarios, uses a nested dictionary structure created on demand as a context state carrier. During a single source code scan, it simultaneously completes syntax parsing, semantic aggregation, type inference, and target code fragment generation, achieving a lightweight pipeline compilation mechanism with low time complexity and low memory consumption. It processes the source code sequentially in a single pass, recursively processing sub-elements. After processing all sub-elements, it immediately performs semantic aggregation in the parent level to determine the corresponding type and generates the target code fragment, writing it into the corresponding dictionary entry. This effectively solves the problem of time complexity increasing linearly with the number of passes in cross-language translation due to multiple traversals. Furthermore, type inference is completed on-the-spot through local sub-element type aggregation, eliminating the need to construct a global constraint graph. This solves the problem of high memory consumption and space complexity expanding with code size caused by global constraint solving in cross-language translation.

[0052] In some embodiments, before all nested sub-elements have been processed, processing of nested sub-elements continues. This includes, if the current syntax element still contains sub-elements with undetermined data types, and those sub-elements do not contain nested sub-elements, invoking a multi-source type inference method to determine the data type corresponding to the sub-element; and writing the data type into the current dictionary entry corresponding to the sub-element. If the current syntax element still contains sub-elements with undetermined data types, and those sub-elements still contain nested sub-elements, the process continues by creating a current dictionary entry at the end of the nested dictionary structure, associating the current dictionary entry with the parent dictionary entry, and then using that sub-element as the new current syntax element to continue processing the nested sub-elements of the sub-elements until all sub-elements within the current syntax element have their corresponding data types determined.

[0053] This application embodiment achieves the type determination of all child elements within the same processing cycle before parent aggregation by completing child nodes first and then aggregating parent nodes. This does not rely on additional post-processing traversal or global constraint solving stages. This allows the translator to complete bottom-up type deduction while scanning in text order, without interrupting the scanning process or maintaining a queue of pending processing. It truly realizes a lightweight pipeline architecture that scans, deduces, and generates code simultaneously, significantly reducing the time complexity of cross-language translation.

[0054] In some embodiments, the dictionary entry includes a data type field and an expected type field. Before generating the target code snippet, the method further includes reading and comparing the expected type field and data type field contained in the dictionary entry corresponding to the current syntax layer to obtain a comparison result; if the comparison result indicates different values, a type conversion flag is written in the type conversion flag field of the dictionary entry; or, if the comparison result indicates the same value, the type conversion flag field of the dictionary entry is kept as a preset null value.

[0055] In some embodiments, the type conversion flag field includes at least one of the following: a preset null value, a forced type conversion flag, and a function conversion flag.

[0056] After determining the data type of each child element, first read the type conversion flag field from its corresponding dictionary entry. If the field is a default empty value, it means that the expected type is consistent with the actual data type, and the target code can be generated directly according to the actual data type; if the field has been written with the type conversion flag, it means that there is a difference between the expected type and the actual data type, and the corresponding type conversion operation needs to be inserted when generating the target code.

[0057] When a type conversion marker exists, an explicit type conversion instruction is embedded in the target code segment based on the source and target types indicated by the marker. For example, if the actual type is integer and the expected type is floating-point, then integer-to-floating-point conversion code is generated.

[0058] Before generating the target code snippet, the expected type field and data type field of the corresponding dictionary entry in the current syntax layer are read and compared, and a type conversion flag is set based on the comparison result. When the two types are inconsistent, a conversion flag is written to the type conversion flag field to indicate that automatic type conversion needs to be performed; when the two are consistent, the flag field is left empty. In this way, static type matching verification can be completed during the semantic analysis stage, identifying type mismatch errors in advance; at the same time, the type conversion requirements are clearly marked, providing a direct basis for target code generation, so that the code generation logic does not need to repeatedly perform type checks, improving processing efficiency. It effectively solves the semantic problem of mismatch between actual data type and expected type in the context, supports safe implicit type conversion, decouples type checking and code generation, and improves the stability, standardization, and maintainability of the entire parsing and compilation process.

[0059] Figure 3 This is a flowchart illustrating a method for cross-language translation of program code provided in an embodiment of this application. For example... Figure 3 As shown, the method may include the following steps:

[0060] In step S31, a nested dictionary structure for describing the state context is allocated and initialized. This nested dictionary structure includes dictionary entries created according to the nesting level of the syntax elements.

[0061] In step S32, the source code is scanned and parsed according to the order in which the text appears to obtain the current syntax element.

[0062] In step S33, if the current syntax element contains nested child elements, a current dictionary entry is created at the end of the nested dictionary structure, and the current dictionary entry is associated with the parent dictionary entry. The nested child elements are then processed. After all nested child elements have been processed, semantic aggregation is performed based on the data type stored in the dictionary entry corresponding to each child element to determine the data type corresponding to the current syntax element. The data type is written into the current dictionary entry corresponding to the current syntax element. A target code fragment is generated and written into the current dictionary entry corresponding to the current syntax element. After the current syntax element has been processed, the process returns to the step of scanning and parsing the source code in the order of text appearance, and continues until the source code scanning and parsing is completed.

[0063] In step S34, if the current syntax element does not contain nested child elements, a current dictionary entry is created at the end of the nested dictionary structure, and the current dictionary entry is associated with the parent dictionary entry; multi-source type inference is performed to determine the data type corresponding to the current syntax element; the data type is written into the current dictionary entry corresponding to the current syntax element; a target code fragment is generated and written into the current dictionary entry corresponding to the current syntax element; after the current syntax element is processed, the process returns to the step of scanning and parsing the source code in the order of text appearance, and continues until the source code is scanned and parsed.

[0064] In the above steps, syntax elements are distinguished into those containing nested child elements and those without, and then processed separately using different strategies. This allows for precise matching of the inherent logical differences in type inference and code generation for different structures. For compound structures containing nested child elements (such as expressions, statement blocks, and function calls), their data type cannot be directly determined at the current level and must be inferred through semantic aggregation after all child elements have been processed. The split-processing follows a strict flow: "create dictionary entries → associate parent elements → recursively process child elements → semantic aggregation after all child elements are completed → write type → generate target code." This ensures that when generating the target code fragment for the current compound structure, the types and code fragments of all child elements are ready. Type compatibility checks and code concatenation can be performed directly based on the determined child element types, avoiding errors in the preceding generation or incomplete intermediate representations due to missing type information. This achieves reliable bottom-up inference in a single scan.

[0065] For elements without nested child elements, such as identifiers, literals, constants, and simple variable accesses, their types can be directly determined at the current level through multi-source type inference (combining symbol tables, literal suffixes, contextual expectations, etc.), without creating additional nested levels or waiting for child elements. Offloading processing allows it to directly enter the immediate flow of "creating dictionary entries → associating with parent elements → multi-source inference → writing types → generating target code," effectively avoiding the memory space required to maintain redundant nested dictionary levels and recursive stack frames. This significantly reduces the memory footprint and processing latency of the dictionary structure, enabling the pipeline to maintain high throughput when processing a large number of simple syntax elements.

[0066] Whether a syntax element contains nested child elements or not, it creates dictionary entries and associates them with the parent entry to store the deduced data type and the generated target code fragment in a unified nested dictionary structure. This effectively avoids the introduction of multiple intermediate representations or heterogeneous symbol tables due to structural differences.

[0067] During the scanning and parsing phase, structural features of the current syntax element can be extracted to determine whether it contains nested child elements. For the current syntax element containing child elements, the data types of multiple child elements are semantically aggregated to obtain the deduced data type, which is then written into the dictionary entry corresponding to the child element. For the current syntax element not containing nested child elements, the deduced data type can be inferred from the multi-source type.

[0068] In some embodiments, the method of generating the target code snippet is related to whether the current syntax element contains nested child elements. If the current syntax element contains nested child elements, generating the target code snippet includes reading the code snippets of the dictionary entries corresponding to each child element according to the syntax role of the current syntax element, concatenating and assembling them according to the concatenation template corresponding to the syntax role, and using the concatenated and assembled result as the target code snippet corresponding to the current syntax element. The syntax roles include function calls, array access, arithmetic operations, and assignment statements, and the concatenation templates include function call templates, array subscript templates, operator concatenation templates, and assignment concatenation templates, respectively. For example, for a function call element, the function name code snippet and the parameter code snippet are concatenated according to the call syntax. For an array access element, the array base code snippet and the index code snippet are concatenated according to the index syntax. For an arithmetic operation element, the left operand code snippet, the operator, and the right operand code snippet are concatenated according to the operation syntax. For an assignment statement element, the left value code snippet and the right value code snippet are concatenated according to the assignment syntax.

[0069] For syntax elements that include nested child elements, the data type of the nested child elements is determined by multi-source type inference during the processing of nested child elements. A code snippet is then generated based on the data type and type conversion marker field. If the current syntax element does not contain nested child elements (i.e., a leaf node), a code snippet is generated based on the data type and type conversion marker field after multi-source type inference. The process involves reading the current syntax element's own identification information and combining it with the type conversion marker field to generate the target code snippet. This includes generating the target code snippet based on its own identification information and the type conversion marker field. Specifically: when the type conversion marker field is a preset null value, the element's own identification information is directly used as the target code snippet; or, when the type conversion marker field is a forced type conversion marker, the forced type conversion marker is parsed to obtain the target data type, and the target data type and its own identification information are concatenated according to the forced type conversion expression format to generate the target code snippet; or, when the type conversion marker field is a function conversion marker, the function conversion marker is parsed to obtain the target data type, and the target data type and its own identification information are concatenated according to the function conversion expression format to generate the target code snippet.

[0070] For syntax elements that include nested child elements, the code snippets corresponding to the dictionary entries of each child element are concatenated and assembled according to the concatenation template corresponding to the syntax role to obtain the target code snippet. In some embodiments, the data type corresponding to the current syntax element can be converted to the target data type according to the type conversion flag before generating the target code snippet to avoid precision loss.

[0071] This application's embodiment reads the generated code fragments from the dictionary entries of each child element at the current processing level, assembles them according to the splicing template corresponding to the grammatical role to obtain the target code fragment of the current grammatical element. This eliminates the need to wait for the entire source code scan to complete before generating the code fragments, and also avoids backtracking to released child element AST nodes. It achieves bottom-up, layer-by-layer assembly of code fragments in a single scan, ensuring the pipeline's real-time output capability. Furthermore, for grammatical elements without nested child elements, their target code fragments are directly generated based on their own identifier information and type conversion flag fields. This avoids querying the parent's expected type upwards or searching for type mapping rules downwards during code generation, completely cohesively integrating type conversion strategy parsing and code generation within a single dictionary entry. This significantly reduces the processing latency of leaf nodes, allowing the pipeline to remain efficient when processing a large number of simple grammatical elements. Compared to traditional multi-stage compilation architectures, it significantly reduces memory usage.

[0072] In some embodiments, before the fallback path pointer stack, the method further includes: determining whether the current syntax element is a statement-level root element; if so, appending the generated target code fragment to the data buffer; otherwise, continuing to store the generated target code fragment in the dictionary entry corresponding to the current syntax element. Here, the data buffer is pre-allocated during the initialization phase, and a statement-level root element refers to a syntax unit in the source code that has complete syntax boundaries, can exist independently, and does not depend on the context of an outer expression.

[0073] In some embodiments, dictionary entries include data type fields. Semantic aggregation is performed based on the data types stored in the dictionary entries corresponding to each child element, including: when the current syntax element is determined to be an arithmetic operation element, reading the data types stored in the data type fields of the dictionary entries of the left and right child elements, and performing arithmetic type promotion rules based on operator characteristics and data types to determine the data type of the current syntax element; or, when the current syntax element is determined to be a function call element, directly reading the return type of the called function child element as the data type corresponding to the current syntax element; or, when the current syntax element is determined to be an array access element, reading the data types stored in the data type fields of the dictionary entries corresponding to the array child elements, removing pointer levels or array dimensions to obtain the data type as the data type corresponding to the current syntax element; or, when the current syntax element is determined to be an assignment statement element, reading the declared type stored in the data type field of the dictionary entry corresponding to the left child element as the data type corresponding to the current syntax element; or, when semantic aggregation fails, using a preset default type as the data type corresponding to the current syntax element.

[0074] Taking the nested function call `sum(product(a, b), c)` as an example, when scanning and parsing `sum(product(a, b), c)`, it is determined that it contains nested child elements. A dictionary entry corresponding to `sum` is created at the end of the nested dictionary structure and attached to the child element attachment field of the parent dictionary entry. Since there are still unprocessed child elements, semantic aggregation is not performed yet; instead, the child elements are processed more deeply. `product(a, b)` is processed, a dictionary entry corresponding to `product` is created, and attached to the child element attachment field of the dictionary entry corresponding to `sum`. The leaf nodes `a` and `b` are then processed, assuming they are determined to be of type `int` through multi-source type inference. After `a` and `b` are fully processed, the dictionary entry corresponding to `product` is returned to. The dictionary entries of the two child elements (both of type `int`) are read, and semantic aggregation is performed according to the semantic rules of `product` (such as type inference for multiplication operations or function signatures). This determines that the data type of `product(a, b)` is `int`, generates the target code snippet `"int product(a, b)"`, and writes it to the `product` dictionary entry.

[0075] Then, process 'c': create a dictionary entry for 'c' and attach it to the dictionary entry corresponding to 'sum'. This element has no nested child elements, so its data type is determined to be float directly through multi-source type inference. Generate the target code snippet "float c" and write it into the dictionary entry corresponding to 'c'.

[0076] At this point, after all child elements of `sum` have been processed, we return to the dictionary entry corresponding to `sum` to perform semantic aggregation, reading each child entry under the field attached to the child element:

[0077] The `product` sub-entry has an data type of `int` and the target code snippet is "int product(a,b)".

[0078] The `c` sub-entry has a data type of `float` and the target code snippet is "float c".

[0079] Based on the semantic rules of `sum` (such as function signature (int, float) -> float, or type promotion rules for addition), aggregation deduction is performed by combining the type information of the two arguments to determine that the return data type of the current `sum` function call is `float`. The aggregated `float` type is written into the data type field of the `sum` dictionary entry. Finally, based on the syntactic role of `sum` (function call), the target code fragments in each child element dictionary entry are read, concatenated and assembled according to the function call template to obtain the target code fragment, which is then used as the target code fragment corresponding to `sum` and written into its corresponding dictionary entry.

[0080] In some embodiments, performing multi-source type inference includes: querying the type information of the current syntax element itself in a preset priority order as the data type corresponding to the current syntax element, wherein the preset priority order is as follows: the explicit declared type of the current syntax element itself, the return type attribute of the called function object itself, the definition prefix keyword mapping type of the current syntax element, the type expectation value stored in the dictionary entry corresponding to the previous syntax element, and the preset default type.

[0081] The type information of the currently processed syntax element is queried according to a preset priority order. This can include: First priority: When the currently processed syntax element is an explicit variable or constant definition statement, its explicit declaration type attribute is read; Second priority: When the currently processed syntax element is an explicit function definition statement, the return type attribute of the called function object is queried and read; Third priority: When the currently processed syntax element is a variable definition statement with a prefix, the data type mapped according to the prefix is ​​queried and read; Fourth priority: If the dictionary entry corresponding to the currently processed syntax element already stores the type expectation value propagated from the parent dictionary entry, that type expectation value is used as the data type of the currently processed syntax element; If none of the above priorities are met, a preset default type is used. For example, taking C language as an example...

[0082] int loop_cnt = 0; / Reading int → Hitting the first priority, extracting the explicitly declared type int

[0083] `int get_status(void);` This reads an `int`, indicating a second priority level; it retrieves the function's return type as `int`, where `void` is the parameter type.

[0084] nCount = 0; / Reading nCount → hits the third priority level, the prefix n corresponds to the data type int.

[0085] Furthermore, it should be noted that although the operations of the method of this application are described in a specific order in the accompanying drawings, this does not require or imply that these operations must be performed in that specific order, or that all the operations shown must be performed to achieve the desired result. On the contrary, the steps depicted in the flowchart can be executed in a different order. Additionally or alternatively, certain steps may be omitted, multiple steps may be combined into one step, and / or one step may be broken down into multiple steps.

[0086] This application also provides a program code cross-language translation device. Figure 4 This is a schematic diagram of the structure of a program code cross-language translation device 400 provided in an embodiment of this application. Figure 4 As shown, the device is configured in a processor, and the device 400 includes:

[0087] Resource allocation module 401 is configured to allocate and initialize a nested dictionary structure for describing the state context, the nested dictionary structure including corresponding dictionary entries created according to the nesting level of syntactic elements;

[0088] The parsing module 402 is configured to scan and parse the source code in the order of text appearance to obtain the current syntax element;

[0089] The nesting processing module 403 is configured to, if the current syntax element contains nested child elements, create a current dictionary entry at the end of the nested dictionary structure, associate the current dictionary entry with the parent dictionary entry, and continue processing the nested child elements; after all nested child elements have been processed, perform semantic aggregation based on the data type stored in the dictionary entries corresponding to each child element to determine the data type corresponding to the current syntax element; write the data type into the current dictionary entry corresponding to the current syntax element; generate a target code fragment and write the target code fragment into the current dictionary entry corresponding to the current syntax element; after the current syntax element has been processed, return to the step of scanning and parsing the source code in the order of text appearance, and continue execution until the source code scanning and parsing is completed.

[0090] The nested processing module 403 can also be implemented as multiple sub-units, such as a dictionary creation sub-unit, configured to create a current dictionary entry at the end of the nested dictionary structure if the current syntax element contains nested child elements, associate the current dictionary entry with the parent dictionary entry, and continue processing the nested child elements; a type inference and writing sub-unit, configured to perform semantic aggregation based on the data type stored in the dictionary entries corresponding to each child element after all nested child elements have been processed, to determine the data type corresponding to the current syntax element; and write the data type into the current dictionary entry corresponding to the current syntax element; a code snippet generation sub-unit, configured to generate a target code snippet and write the target code snippet into the current dictionary entry corresponding to the current syntax element; and a parsing processing sub-unit, further configured to return to the step of scanning and parsing the source code in the order of text appearance after the current syntax element has been processed, and continue execution until the source code scanning and parsing is complete.

[0091] It should be noted that the specific implementation principle and effect of the cross-language translation device 400 for program code provided in this application embodiment can be found in the relevant description and effect of the foregoing embodiment, and will not be elaborated further here.

[0092] This application also provides an electronic device for implementing the cross-language translation method of program code provided in this application. Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 5 As shown, the electronic device 500 includes a processor 501 and a memory 502 communicatively connected to the processor 501; wherein the memory 502 stores computer-executable instructions; the processor 501 executes the computer-executable instructions stored in the memory 502 to implement the method of any of the embodiments described above. The processor 501 and the memory 502 can be connected via a bus.

[0093] This application also provides a computer-readable storage medium storing computer-executable instructions that, when executed by a processor, are used to implement the methods in any of the embodiments described above.

[0094] Computer-readable media can be any available medium accessible by a computer device, and includes volatile and non-volatile media, removable and non-removable media implemented in any way and with any technique to store information such as computer-readable instructions, data structures, program modules or other data. Computer-readable media includes, but is not limited to, RAM, ROM, EEPROM, flash memory or other memory technologies, CD-ROM, digital versatile disc (DVD) or other optical storage, cassette tape, magnetic tape, disk storage or other magnetic storage devices, or any other medium that can be used to store the required information and is accessible by a computing device. Any combination of the above should also be included within the scope of computer-readable media.

[0095] This application also provides a computer program product, which includes a computer program that, when executed by a processor, implements the method as described in any of the preceding embodiments.

[0096] The software logic portion of the program code cross-language translation method provided in this application embodiment can be implemented using a programmable logic device or as a computer program product that enables a computer to execute the demonstrated method. The computer program product includes a computer-readable storage medium containing computer program logic or code portions for implementing the various steps of the aforementioned software logic portion. The computer-readable storage medium can be an internal medium installed in a computer or a removable medium removable from the computer body (e.g., a hot-swappable storage device). Internal media includes, but is not limited to, rewritable non-volatile memory, such as RAM, ROM, and hard disks. Removable media includes, but is not limited to: optical storage media (e.g., CD-ROM and DVD), magneto-optical storage media (e.g., MO), magnetic storage media (e.g., magnetic tape or portable hard disks), media with built-in rewritable non-volatile memory (e.g., memory cards), and media with built-in ROM (e.g., ROM cartridges).

[0097] Those skilled in the art will understand that any computer system with suitable programming means is capable of executing the steps of the methods of this application contained in a computer program product. Although most of the specific embodiments described in this specification focus on software programs, alternative embodiments that implement the methods provided in this application in hardware are also within the scope of protection claimed in this application.

[0098] Furthermore, the word "comprising" does not exclude other components, units, or steps, and the singular does not exclude the plural. Multiple components, units, or devices recited in the claims may also be implemented by a single component, unit, or device through software or hardware.

[0099] Finally, it should be noted that the foregoing description is merely a specific implementation method provided by this application to illustrate the technical solution of this application, and is not intended to limit it. The scope of protection of this application is not limited thereto. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments within the scope of the technology disclosed in this application, or make equivalent substitutions for some of the technical features. Such modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application, and should all be covered within the scope of protection of this application.

Claims

1. A method for cross-language translation of program code, characterized in that, This method is executed by the processor and includes: Allocate and initialize a nested dictionary structure for describing the state context, the nested dictionary structure comprising corresponding dictionary entries created according to the nesting level of syntactic elements; The source code is scanned and parsed in the order of text appearance to obtain the current syntax element; If the current syntax element contains nested child elements, create the current dictionary entry at the end of the nested dictionary structure, associate the current dictionary entry with the parent dictionary entry, and continue processing the nested child elements; After all nested sub-elements have been processed, semantic aggregation is performed based on the data types stored in the dictionary entries corresponding to each sub-element to determine the data type corresponding to the current syntax element; the data type is written into the current dictionary entry corresponding to the current syntax element; a target code fragment is generated and written into the current dictionary entry corresponding to the current syntax element; after the current syntax element has been processed, the process returns to the step of scanning and parsing the source code in the order of text appearance, and continues until the source code scanning and parsing is completed.

2. The method according to claim 1, characterized in that, The state context also includes a path pointer stack, which is used to record the hierarchical access sequence from the root node of the nested dictionary structure to the dictionary entry corresponding to the currently processed syntax element. The method further includes: The path pointer stack is extended before the current dictionary entry is created at the end of the nested dictionary structure; When creating the current dictionary entry at the end of the nested dictionary structure, the tail pointer of the extended path pointer stack is read. The tail pointer is used to locate the position of the current dictionary entry in the nested dictionary structure, and the current dictionary entry is written to the corresponding position in the nested dictionary structure. After the current syntax element has been processed, the path pointer stack is rolled back.

3. The method according to claim 2, characterized in that, Dictionary entries include child element mounting fields and expected type fields. The sequence in the hierarchical access sequence, excluding the last element, is the outer path. This outer path is used to access the dictionary entry corresponding to the outer syntax element of the current syntax element. Therefore, the current dictionary entry is associated with the parent dictionary entry, including: Mount the current dictionary entry under the mounting field of the child element of the parent dictionary entry; Access the parent dictionary entry corresponding to the current dictionary entry through the outer path of the extended path pointer stack; if the parent dictionary entry has a pre-written type expectation value for the current syntax element, then write the type expectation value into the expected type field of the current dictionary entry; or, if the parent dictionary entry has not pre-written a type expectation value for the current syntax element, then keep the expected type field of the current dictionary entry storing a preset null value.

4. The method according to claim 1, characterized in that, Further processing of nested child elements includes: If there are child elements with undetermined data types in the current syntax element, and the child element does not have nested child elements, perform multi-source type inference to determine the data type corresponding to the child element; write the data type to the current dictionary entry corresponding to the child element. If there are still child elements with undetermined data types in the current syntax element, and these child elements still have nested child elements, continue to create the current dictionary entry at the end of the nested dictionary structure, associate the current dictionary entry with the parent dictionary entry, and then use this child element as the new current syntax element to continue processing the nested child elements of the child element until all child elements in the current syntax element have determined their corresponding data types.

5. The method according to claim 4, characterized in that, Perform multi-source type inference, including: The type information of the current syntax element itself is queried according to a preset priority order, which is used as the data type corresponding to the current syntax element. The preset priority order is as follows: the explicit declared type of the current syntax element itself, the return type attribute of the called function object itself, the type mapped by the definition prefix keyword of the current syntax element, the type expectation value stored in the dictionary entry corresponding to the previous syntax element, and the preset default type.

6. The method according to claim 1, characterized in that, Dictionary entries include data type fields. Semantic aggregation is performed based on the data type stored in the dictionary entry corresponding to each child element, including: When determining that the current syntax element is an arithmetic operation element, the data types stored in the data type fields of the dictionary entries of the left and right child elements are read. Arithmetic type promotion rules are then applied based on the operator characteristics and data type to determine the data type of the current syntax element; or... When it is determined that the current syntax element is a function call element, the return type of the called function's sub-element is directly read as the data type corresponding to the current syntax element; or, When it is determined that the current syntax element is an array access element, the data type stored in the data type field of the dictionary entry corresponding to the array sub-element is read, and the data type obtained after removing the pointer hierarchy or array dimension is used as the data type corresponding to the current syntax element; or... When it is determined that the current syntax element is an assignment statement element, the declared type stored in the data type field of the dictionary entry corresponding to the left child element is read as the data type corresponding to the current syntax element; or... When semantic aggregation fails, the default type is used as the data type of the current syntax element.

7. The method according to claim 1, characterized in that, Generate target code snippets, including: Based on the grammatical role corresponding to the current grammatical element, the code snippets of the dictionary entries corresponding to each sub-element are read, and the snippets are assembled according to the concatenation template corresponding to the grammatical role. The result of the concatenation is used as the target code snippet corresponding to the current grammatical element. The grammatical roles include function calls, array access, arithmetic operations, and assignment statements. The concatenation templates include function call templates, array subscript templates, operator concatenation templates, and assignment concatenation templates, respectively.

8. The method according to claim 1, characterized in that, The dictionary entries include data type fields and expected type fields. Before generating the target code snippet, the method also includes: Read and compare the expected type field and data type field contained in the dictionary entry corresponding to the current syntax layer to obtain the comparison result; if the comparison result is different, write the type conversion flag in the type conversion flag field of the dictionary entry; or, if the comparison result is the same, keep the type conversion flag field of the dictionary entry as a preset null value.

9. The method according to claim 8, characterized in that, If the type conversion marker field includes at least one of the following: a default null value, a forced type conversion marker, and a function conversion marker, then the target code snippet generated includes: When the type conversion marker field is a pre-defined null value, the current syntax element's own identifier information is used directly as the target code snippet; or, When the type conversion marker field is a forced type conversion marker, the forced type conversion marker is parsed to obtain the target data type. Based on the target data type and its own identifier information, strings are concatenated according to the forced type conversion expression format to generate the target code snippet; or... When the type conversion marker field is a function conversion marker, the function conversion marker is parsed to obtain the target data type. Based on the target data type and its own identification information, the strings are concatenated according to the function conversion expression format to generate the target code fragment.

10. The method according to claim 1, characterized in that, The method also includes: If the current syntax element does not contain nested child elements, create the current dictionary entry at the end of the nested dictionary structure and associate the current dictionary entry with the parent dictionary entry; perform multi-source type inference to determine the data type corresponding to the current syntax element; write the data type into the current dictionary entry corresponding to the current syntax element; generate a target code snippet and write the target code snippet into the current dictionary entry corresponding to the current syntax element; after the current syntax element is processed, return to the step of scanning and parsing the source code in the order of text appearance, and continue execution until the source code scanning and parsing is completed.

11. A program code cross-language translation device, characterized in that, The device is configured in the processor and includes: The resource allocation module is configured to allocate and initialize a nested dictionary structure for describing the state context, the nested dictionary structure including corresponding dictionary entries created according to the nesting level of syntactic elements; The parsing module is configured to scan and parse the source code in the order in which the text appears to obtain the current syntax element; The nested processing module is configured to, if the current syntax element contains nested child elements, create a current dictionary entry at the end of the nested dictionary structure, associate the current dictionary entry with the parent dictionary entry, and continue processing the nested child elements; after all the nested child elements have been processed, perform semantic aggregation based on the data type stored in the dictionary entries corresponding to each child element to determine the data type corresponding to the current syntax element; write the data type into the current dictionary entry corresponding to the current syntax element; generate a target code snippet and write the target code snippet into the current dictionary entry corresponding to the current syntax element; after the current syntax element has been processed, return to the step of scanning and parsing the source code in the order of text appearance, and continue execution until the source code scanning and parsing is completed.

12. An electronic device, characterized in that, The electronic device includes a processor and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the program code cross-language translation method as described in any one of claims 1 to 10.

13. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the cross-language translation method of program code as described in any one of claims 1 to 10.

14. A computer program product, characterized in that, It includes a computer program that, when executed by a processor, implements the cross-language translation method of program code as described in any one of claims 1 to 10.