System for converting rule into Python language

The Antlr4 tool automatically generates a language compiler, which solves the problem of low conversion efficiency of manual code writing by programmers in the existing technology and realizes efficient and automated conversion of rules into Python language.

CN120610752APending Publication Date: 2025-09-09XIAN SPACE STAR TECH IND GRP
View PDF 9 Cites 0 Cited by

Patent Information

Application Number
CN202510706498.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-29
Publication Date
2025-09-09

AI Technical Summary

Technical Problem

The existing technology requires programmers to manually write code for code conversion, resulting in heavy workload and low efficiency.

Method used

A rule conversion system based on the Antlr4 tool is used to automatically generate a language compiler by configuring lexical and grammatical rules, including an API module, an input parsing module, a grammatical analysis module, a grammatical parsing module, a rule conversion module, and a formatted output module to convert rules into Python language.

Benefits of technology

It greatly reduces the programming workload of programmers, improves code conversion efficiency, and supports conversion of multiple languages ​​and custom languages.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120610752A_ABST
    Figure CN120610752A_ABST
Patent Text Reader

Abstract

The invention belongs to the technical field of computer software products, and discloses a system for converting a rule into a Python language, which comprises an API (Application Program Interface) module and a conversion component, the API module is used for receiving a rule input by a user and forwarding the rule to the forwarding component; and the conversion component receives the rule input by the user and forwarded by the API module, and realizes conversion of the rule into the Python language. Based on an Antlr4 tool, lexical and grammatical analysis programs needed by a language compiler can be automatically generated only by configuring lexical and grammatical rules, the programming workload of programmers is greatly reduced, meanwhile, in combination with working requirements, self-defined lexical and grammar can be added, the whole conversion method is strengthened, and the conversion efficiency is improved. And mutual conversion of codes of various common languages and custom languages is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of computer software products, and in particular relates to a system for converting rules into Python language. Background Art

[0002] A prior art patent document, CN112711420A, discloses a "code conversion method and apparatus." The specific method is as follows: after obtaining the code to be processed, the code is parsed into an abstract syntax tree; chained value codes in the abstract syntax tree are determined, and existence check codes are added to the chained value codes; and the abstract syntax tree with the existence check codes is then converted into code for a specified script. This invention not only reduces the manual work of programmers, but also avoids errors caused by non-existent values ​​during the chained value process through existence check.

[0003] A patent document with prior art publication number CN116301873A discloses a "ST language to C language code conversion method and apparatus." The specific method is as follows: obtaining a first ST language code; formatting the first ST language code to obtain a second ST language code, wherein the formatting process determines whether to add space characters based on whether characters in the first ST language code are adjacent to at least one preset character; traversing the character string to be converted in the second ST language code and performing code conversion based on the type of the character string to be converted to obtain a C language code. The present invention solves the problem of ST language to C language code conversion by formatting the ST language code first and then performing code conversion to obtain a C language code.

[0004] The implementation methods of the above two applications require programmers to write codes to implement specific parsing and conversion work, which greatly increases the programming workload of programmers and also reduces work efficiency. Summary of the Invention

[0005] The purpose of the present invention is to provide a system for converting rules into Python language. The present invention is based on the Antlr4 tool. By only configuring the lexical and grammatical rules, the lexical and grammatical analysis programs required by the language compiler can be automatically generated, greatly reducing the programming workload of programmers. At the same time, according to work needs, customized lexical and grammatical rules can be added to strengthen the entire conversion method and realize the conversion between codes of multiple common languages ​​and customized languages.

[0006] The technical solution adopted by the present invention is a system for converting rules into Python language, including an API module and a conversion component;

[0007] The API module is used to receive the rules input by the user and forward them to the forwarding component;

[0008] The conversion component accepts user input rules forwarded from the API module and converts the rules into Python language;

[0009] The conversion component includes an input parsing module, a syntax analysis module, a syntax parsing module, a rule conversion module and a formatted output module;

[0010] The input parsing module is responsible for parsing the user input rules, parsing them line by line and sending them to the syntax analysis module;

[0011] The syntax analysis module defines syntax analysis rules through the Antlr4 tool, performs matching analysis on each line of the user input rule, divides it into independent syntax definition units, and outputs them to the syntax parsing module;

[0012] After the syntax analysis process, the syntax parsing module parses the input rule string into the elements of each rule according to the defined syntax analysis rules;

[0013] The rule conversion module converts each rule element into a corresponding Python language representation according to the rule elements parsed by the grammar parsing module;

[0014] The formatted output module outputs the converted Python language in layers.

[0015] Furthermore, the Restful interface standard is adopted to pass each rule that needs to be converted into the input parsing module, wherein one conversion rule is passed in as a whole string, or multiple conversion rules are passed in as a whole string, but the string rule must comply with the rule format definition.

[0016] Furthermore, the syntax analyzer of the syntax analysis module includes a lexical analyzer and a syntax analyzer; the lexical analyzer analyzes keywords and various identifiers; and the syntax analyzer constructs a syntax analysis tree based on the result of the lexical analysis.

[0017] Furthermore, the syntax analysis rules are specifically as follows:

[0018] (1) A file definition consists of one or more rules;

[0019] (2) Each rule contains a statement block;

[0020] (3) Each statement block contains multiple statements, mainly including variable definition statements, variable assignment statements, IF statements, function call statements, comparison statements and comments.

[0021] Furthermore, when the rule is a ruledcl_stat rule, the ruledcl_stat rule is divided into the following syntax definition units:

[0022] ruledcl_stat rule:

[0023] ruledcl_stat:'defrule'RULE_NAMEblock;

[0024] Variable definition statement:

[0025] type NAME('='expr)?

[0026] Among them, type is the supported type definition; expr is the expression;

[0027] Variable assignment statement:

[0028] expr augassign expr;

[0029] Among them, augassign is the assignment operator;

[0030] Comparison statements:

[0031] expr comp_op expr;

[0032] Among them, comp_op is the comparison operator definition;

[0033] IF statement:

[0034] ('IF'|'if')(expr|or_stat)+('THEN'|'then');

[0035] ELSE statement:

[0036] 'ELSE'|'else'.

[0037] Furthermore, when the lexical analyzer encounters a string containing the keyword defrule, it prioritizes matching the ruledcl_stat rule. At this point, the input line is split into a RULE_NAME element and a block element according to the grammatical definition of the ruledcl_stat rule.

[0038] Furthermore, the conversion logic of the ruledcl_stat rule is as follows: the syntax parsing module has parsed the string of the ruledcl_stat rule into RULE_NAME elements and block elements, and converted the RULE_NAME element as the function name into the string of def RULE_NAME(paras):, which is the function definition in the Python language. The conversion logic of other rules is the same as that of the ruledcl_stat rule.

[0039] Furthermore, a global Hash table is used to store the nodes of each grammar parse tree. The specific steps are:

[0040] When a leaf node is encountered, it is directly output and the node's Text is placed in the Hash table. Otherwise, its child node information is queried from the Hash table through the node and then placed in the Hash table.

[0041] Furthermore, the formatted output module outputs the converted Python language in layers in the following specific steps:

[0042] When formatting the output, the converted Python language is obtained according to the rules, and then the judgment is made: if it is a rule definition statement, the starting value of the output space of the Python language is set to 4. When encountering other statements, it is output by line, and 4 spaces are output first, and then the converted Python language is output; if an IF statement is encountered, the number of spaces is increased by 4 spaces on the original basis, that is, 8 spaces are output before the statement after IF is output. When an ELSE or IF end statement is encountered, the space is reduced by 4 spaces and restored to the original statement. The Stack stack is used here to control the number of spaces by popping and pushing.

[0043] The beneficial effects of the present invention are:

[0044] The present invention is based on the Antlr4 tool. By only configuring the lexical and grammatical rules, it can automatically generate the lexical and grammatical analysis programs required by the language compiler, greatly reducing the programming workload of programmers. At the same time, according to work needs, customized lexical and grammatical rules can be added to strengthen the entire conversion method and realize the conversion between codes of multiple common languages ​​and customized languages. BRIEF DESCRIPTION OF THE DRAWINGS

[0045] Figure 1 It is a schematic diagram of the overall structure of the present invention. DETAILED DESCRIPTION

[0046] In order to make the objectives, technical solutions and advantages of the present invention clearer and more understandable, the technical solutions of the present invention will be further described in detail below with reference to the accompanying drawings and embodiments.

[0047] like Figure 1 As shown, a system for converting rules into Python language includes an API module and a conversion component.

[0048] The rules described in the present invention refer to user-defined lexical and grammatical rules.

[0049] 1. API Module

[0050] The API module is used to receive rules input by users and forward them to the forwarding component.

[0051] The API (Application Program Interface) module primarily provides a unified external interface, formatted entirely in JSON. Using the RESTful interface standard, each rule requiring conversion is passed to the input parsing module. A conversion rule can be passed as a single string, or multiple conversion rules can be passed as a single string. However, the string must conform to the rule format definition.

[0052] 2. Conversion Components

[0053] The conversion component is the core of the entire system and is implemented using the Antlr4 tool. The conversion component accepts user-entered rules forwarded from the API module and converts the rules into Python language.

[0054] The conversion component includes an input parsing module, a syntax analysis module, a syntax parsing module, a rule conversion module and a formatted output module.

[0055] 1. Input parsing module

[0056] The input parsing module is responsible for parsing the user input rules, parsing them line by line, and sending them to the syntax analysis module.

[0057] 2. Syntax analysis module

[0058] The syntax analysis module defines syntax analysis rules through the Antlr4 tool, matches and analyzes each line of statements input by the user, divides them into independent syntax definition units, and outputs them to the syntax parsing module.

[0059] Antlr4 (Another Tool for Language Recognition) is an open-source parser generation tool. It supports multiple target languages, including C++, CSharp, Go, Java, PHP, Python, and Swift. It not only generates parsing code based on grammar rules but also provides visitor and listener patterns, allowing developers to write their own logic based on the generated code.

[0060] The parser of the syntax analysis module consists of two parts: the lexer and the parser. The lexer mainly analyzes keywords and various identifiers, such as INT, ID, etc. The parser mainly constructs a syntax analysis tree based on the results of the lexical analysis.

[0061] The specific syntax analysis rules are:

[0062] (1) A file definition consists of one or more rules;

[0063] (2) Each rule contains a block;

[0064] (3) Each statement block contains multiple statements, mainly including variable definition statements, assignment statements, IF statements, function call statements, comparison statements, comments, etc.

[0065] In a specific embodiment, the ruledcl_stat rule is divided into the following syntax definition units:

[0066] ruledcl_stat rule:

[0067] ruledcl_stat:'defrule'RULE_NAMEblock;

[0068] Variable definition statement:

[0069] type NAME('='expr)?

[0070] Among them, type is the supported type definition, which can be INT, String, Float, etc.; expr is the expression;

[0071] Variable assignment statement:

[0072] expr augassign expr;

[0073] Among them, augassign is the assignment operator;

[0074] Comparison statements:

[0075] expr comp_op expr;

[0076] Among them, comp_op is the comparison operator definition, which can be >, <, >=, <=, etc.

[0077] IF statement:

[0078] ('IF'|'if')(expr|or_stat)+('THEN'|'then');

[0079] ELSE statement:

[0080] 'ELSE'|'else'.

[0081] The above are some core rules defined in the system of the present invention, among which the IF and ELSE statements are composed of two rules, mainly for distinguishing the levels during the subsequent conversion.

[0082] 3. Syntax parsing module

[0083] The syntax parsing module is also implemented using the Antlr4 tool. After syntax analysis, the syntax parsing module parses the input rule string into the elements of each rule according to the defined syntax analysis rules.

[0084] Take the ruledcl_stat rule as an example: When the lexical parser encounters a string containing the keyword defrule, it prioritizes matching the ruledcl_stat rule. At this point, the input line is split into the RULE_NAME element and the block element according to the grammar definition of the ruledcl_stat rule.

[0085] 4. Rule conversion module

[0086] The rule conversion module converts each rule element into the corresponding Python language representation according to the rule elements parsed by the grammar parsing module.

[0087] The rule conversion module primarily uses the Listen method in the Antlr4 tool to access the parse tree and implement the conversion logic for each rule. Taking the ruledcl_stat rule as an example, the parsing module has parsed the string of the ruledcl_stat rule into two elements: RULE_NAME and block. The RULE_NAME element can be used as a function name, converting it into a string like def RULE_NAME(paras): , which is a Python function definition. The same implementation strategy is used for other rules.

[0088] In the system of the present invention, since there are multiple rules overlapping, a global Hash table is used to store the nodes of each grammar parsing tree. The specific steps are:

[0089] When a leaf node is encountered, it is directly output and the node's Text is placed in the Hash table. Otherwise, its child node information is queried from the Hash table through the node and then placed in the Hash table.

[0090] 5. Formatted output module

[0091] The formatted output module outputs the converted Python language in layers.

[0092] When formatting the output, the converted Python language is obtained according to the rules, and then the judgment is made: if it is a rule definition statement, the starting value of the output space of the Python language is set to 4. When encountering other statements, it is output by line, and 4 spaces are output first, and then the converted Python language is output; if an IF statement is encountered, the number of spaces is increased by 4 spaces on the original basis, that is, 8 spaces are output before the statement after IF is output. When an ELSE or IF end statement is encountered, the space is reduced by 4 spaces and restored to the original statement. The Stack stack is used here to control the number of spaces by popping and pushing.

[0093] The rule conversion service is developed using the SpringBoot microservices framework, supporting cloud platform deployment and automated operations and maintenance. It converts rules into Python through a unified RESTful interface. The conversion component is broken down into functional modules: input parsing, syntax analysis, parsing, rule conversion, and formatted output. These modules are built on the Listen interface of the AntLR4 tool, making it highly scalable. Furthermore, by implementing different language conversion plugins, it can support conversion to languages ​​other than Python.

[0094] The contents not described in detail in this specification belong to the prior art known to those skilled in the art.

Claims

1. A system for converting rules into Python language, characterized in that: Includes API modules and conversion components; The API module is used to receive the rules input by the user and forward them to the forwarding component; The conversion component accepts user input rules forwarded from the API module and converts the rules into Python language; The conversion component includes an input parsing module, a syntax analysis module, a syntax parsing module, a rule conversion module and a formatted output module; The input parsing module is responsible for parsing the user input rules, parsing them line by line and sending them to the syntax analysis module; The syntax analysis module defines syntax analysis rules through the Antlr4 tool, performs matching analysis on each line of the user input rule, divides it into independent syntax definition units, and outputs them to the syntax parsing module; After the syntax analysis process, the syntax parsing module parses the input rule string into the elements of each rule according to the defined syntax analysis rules; The rule conversion module converts each rule element into a corresponding Python language representation according to the rule elements parsed by the grammar parsing module; The formatted output module outputs the converted Python language in layers.

2. The system for converting rules into Python language according to claim 1, characterized in that: Using the Restful interface standard, each rule that needs to be converted is passed into the input parsing module. One conversion rule is passed in as a whole string, or multiple conversion rules are passed in as a whole string, but the string rule must comply with the rule format definition.

3. The system for converting rules into Python language according to claim 1, characterized in that: The syntax analyzer of the syntax analysis module includes a lexical analyzer and a syntax analyzer; the lexical analyzer analyzes keywords and various identifiers; the syntax analyzer constructs a syntax analysis tree based on the results of the lexical analysis.

4. The system for converting rules into Python language according to claim 1, characterized in that: The grammatical analysis rules are specifically: (1) A file definition consists of one or more rules; (2) Each rule contains a statement block; (3) Each statement block contains multiple statements, mainly including variable definition statements, variable assignment statements, IF statements, function call statements, comparison statements and comments.

5. The system for converting rules into Python language according to claim 4, characterized in that: When the rule is a ruledcl_stat rule, it is divided into the following syntax definition units: ruledcl_stat rule: ruledcl_stat:'defrule'RULE_NAMEblock; Variable definition statement: type NAME('='expr)? Among them, type is the supported type definition; expr is the expression; Variable assignment statement: expr augassign expr; Among them, augassign is the assignment operator; Comparison statements: expr comp_op expr; Among them, comp_op is the comparison operator definition; IF statement: ('IF'|'if')(expr|or_stat)+('THEN'|'then'); ELSE statement: 'ELSE'|'else'.

6. The system for converting rules into Python language according to claim 5, characterized in that: When the lexical analyzer encounters a string containing the keyword defrule, it prioritizes matching the ruledcl_stat rule. At this point, the input line is split into the RULE_NAME element and the block element according to the grammatical definition of the ruledcl_stat rule.

7. The system for converting rules into Python language according to claim 6, characterized in that: The conversion logic for the ruledcl_stat rule is as follows: The syntax parsing module parses the ruledcl_stat rule string into RULE_NAME and block elements. The RULE_NAME element, as the function name, is converted into a string called defRULE_NAME(paras):, which is a Python function definition. The conversion logic for other rules is the same as that for the ruledcl_stat rule.

8. The system for converting rules into Python language according to claim 1, characterized in that: A global Hash table is used to store the nodes of each grammar parse tree. The specific steps are: When a leaf node is encountered, it is directly output and the node's Text is placed in the Hash table. Otherwise, its child node information is queried from the Hash table through the node and then placed in the Hash table.

9. The system for converting rules into Python language according to claim 1, characterized in that: The specific steps of the formatted output module to output the converted Python language according to the hierarchy are as follows: When formatting the output, the converted Python language is obtained according to the rules, and then the judgment is made: if it is a rule definition statement, the starting value of the output space of the Python language is set to 4. When encountering other statements, it is output by line, and 4 spaces are output first, and then the converted Python language is output; if an IF statement is encountered, the number of spaces is increased by 4 spaces on the original basis, that is, 8 spaces are output before the statement after IF is output. When an ELSE or IF end statement is encountered, the space is reduced by 4 spaces and restored to the original statement. The Stack stack is used here to control the number of spaces by popping and pushing.

Citation Information

Patent Citations

  • Code conversion method and device

    CN112711420A

  • ST language to C language code conversion method and device

    CN116301873A

  • Computer language transformation system and transformation method from C language to MSVL (Modeling, Simulation and Verification Language)

    CN103150200A

  • Computer language conversion system and conversion method thereof

    CN114661303A

  • Large-scale code data feature extraction method and system

    CN117113347A