CN111625224B - Code generation method, device, equipment and storage medium - Google Patents

Code generation method, device, equipment and storage medium Download PDF

Info

Publication number
CN111625224B
CN111625224B CN202010468473.XA CN202010468473A CN111625224B CN 111625224 B CN111625224 B CN 111625224B CN 202010468473 A CN202010468473 A CN 202010468473A CN 111625224 B CN111625224 B CN 111625224B
Authority
CN
China
Prior art keywords
language
code
business logic
tree
syntax tree
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010468473.XA
Other languages
Chinese (zh)
Other versions
CN111625224A (en
Inventor
焦煦然
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Baidu Netcom Science and Technology Co Ltd
Original Assignee
Beijing Baidu Netcom Science and Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Baidu Netcom Science and Technology Co Ltd filed Critical Beijing Baidu Netcom Science and Technology Co Ltd
Priority to CN202010468473.XA priority Critical patent/CN111625224B/en
Publication of CN111625224A publication Critical patent/CN111625224A/en
Application granted granted Critical
Publication of CN111625224B publication Critical patent/CN111625224B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/20Software design
    • G06F8/24Object-oriented
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/315Object-oriented languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/425Lexical analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis
    • G06F8/436Semantic checking
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/447Target code generation
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Computational Linguistics (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The application discloses a code generation method, a code generation device, code generation equipment and a code generation storage medium, relates to the technical field of cloud computing, and particularly relates to the technical field of software. The specific implementation scheme is that the code generation method comprises the following steps: writing codes of first business logic in a first language environment, wherein the codes of the first business logic are used for describing functions corresponding to the first business logic, the codes of the first business logic are written according to a description mode of a preset declarative Application Program Interface (API), and a first concrete grammar tree is obtained after writing is completed; converting the first concrete syntax tree into a first abstract syntax tree based on a predetermined syntax analysis algorithm; and converting the first abstract syntax tree into a second language code through an interpreter, wherein the second language code is used for executing the function corresponding to the first business logic. The embodiment of the application can realize dynamic description of the target code.

Description

Code generation method, device, equipment and storage medium
Technical Field
The present application relates to the field of cloud computing technologies, and in particular, to the field of software technologies, and more particularly, to a code generation method, device, apparatus, and storage medium. The application can be applied to the field of computers.
Background
Among various computer programming languages, the Go language has the advantages of simple grammar, high running speed, parallel processing and packaging, open source and the like, and can be used as a system programming language for carrying a huge central server such as a WEB server or a storage cluster and the like. For a high-performance distributed system, the Go language has higher development efficiency than most other languages, can provide mass parallel support, and has larger development advantages at a server side. At present, most of the common Go language code generation schemes are generated based on static files, the use and maintenance processes are complicated, the realization of service functions is negatively influenced, and the optimization and improvement are required.
Disclosure of Invention
The application provides a code generation method, a code generation device, code generation equipment and a storage medium.
According to a first aspect of the present application, there is provided a code generation method comprising:
writing a code of a first business logic according to a description mode of a preset declarative application program interface API in a first language environment, wherein the code of the first business logic is used for describing a function corresponding to the first business logic, and a first concrete grammar tree is obtained after writing is completed, and the first language comprises Python language;
converting the first concrete syntax tree into a first abstract syntax tree based on a predetermined syntax analysis algorithm;
and converting the first abstract syntax tree into second language codes through an interpreter, wherein the second language codes are used for executing functions corresponding to the first business logic, and the second language comprises a Go language.
According to a second aspect of the present application, there is provided a code generating apparatus comprising:
the code processing module is used for writing codes of first business logic according to a description mode of a preset declarative application program interface API in a first language environment, wherein the codes of the first business logic are used for describing functions corresponding to the first business logic, and a first specific grammar tree is obtained after writing is completed, and the first language comprises Python language;
the first conversion module is used for converting the first concrete grammar tree into a first abstract grammar tree based on a preset grammar analysis algorithm;
and the second conversion module is used for converting the first abstract syntax tree into second language codes through an interpreter, and the second language codes are used for executing functions corresponding to the first business logic, wherein the second language comprises a Go language.
According to a third aspect of the present application, there is provided an electronic device comprising:
at least one processor; and
a memory communicatively coupled to the at least one processor; wherein,
the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the code generation method as described above.
According to a fourth aspect of the present application, there is provided a non-transitory computer-readable storage medium storing computer instructions for causing a computer to execute the code generation method as above.
According to a fifth aspect of the present application, there is provided a computer program product comprising a computer program which, when executed by a processor, implements a method as described above.
According to the embodiment of the application, the second programming language such as Go language code can be generated based on the first programming language such as Python language code through the declarative application program interface API, so that the second language can be dynamically controlled through the first language, and the dynamic description of the logic of the second language code can be realized.
It should be understood that the description in this section is not intended to identify key or critical features of the embodiments of the application or to delineate the scope of the application. Other features of the present application will become apparent from the description that follows.
Drawings
The drawings are included to provide a better understanding of the present application and are not to be construed as limiting the application. Wherein:
FIG. 1 is a flow diagram of a code generation method according to one embodiment of the application;
FIG. 2 is a flow chart diagram of a code generation method according to another embodiment of the application;
FIG. 3 is a block diagram of a code generation apparatus according to one embodiment of the present application;
FIG. 4 is a block diagram of a code generating apparatus according to another embodiment of the present application;
fig. 5 is a block diagram of an electronic device for implementing a code generation method of an embodiment of the present application.
Detailed Description
Exemplary embodiments of the present application will now be described with reference to the accompanying drawings, in which various details of the embodiments of the present application are included to facilitate understanding, and are to be considered merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the application. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
Fig. 1 shows a flow chart of a code generation method according to an embodiment of the present application, where the method includes:
s101, writing codes of first business logic according to a description mode of a preset declarative application program interface API in a first language environment, wherein the codes of the first business logic are used for describing functions corresponding to the first business logic, and a first specific grammar tree is obtained after writing is completed, and the first language comprises Python language;
s102, converting the first concrete syntax tree into a first abstract syntax tree based on a preset syntax analysis algorithm;
s103, converting the first abstract syntax tree into a second language code through an interpreter, wherein the second language code is used for executing the function corresponding to the first business logic, and the second language comprises a Go language.
According to the code generation method provided by the embodiment of the application, the second programming language code can be generated based on the first programming language code through the declarative application program interface (Declarative Application Programming Interface, API), so that the second language can be dynamically controlled through the first language, and the dynamic description of the logic of the second language code can be realized.
When the code generation method provided by the embodiment of the application is used, the user writes or modifies the code in the first language environment, and the writing or modifying is in accordance with the description mode of the declarative API, and finally the written or modified code in the second language can be obtained, so that the user can write or modify the code in the second language through the first language, realize the dynamic editing of the target voice code, and facilitate the use and maintenance of the target voice code.
In some embodiments of the present application, the second language (or target language) may optionally include the Go language. Of course, in other embodiments of the present application, the second language may be other computer programming languages that meet the requirements of use, which are all applicable to the code generation process of the embodiments of the present application.
Regarding the application scenario of the embodiments of the present application, taking the Go language as an example of the target language, in some embodiments of the present application, optionally, the code for writing the first service logic in the first language environment may be service logic code on a server of the computer storage cluster, for example, for managing the operation of the computer cluster. More commonly, the code of the first business logic may also be a business logic code on a server of the network game, for example, for formulating migration policies for a large number of players between different servers.
In some embodiments of the present application, optionally, the first language includes Python, where Python is an object-oriented language, and the application field is wide. The embodiment of the application can write and modify the business logic codes in a Python environment, and the writing and modification are required to be performed by adopting a description mode of a pre-established API interface, so that the target codes can be obtained in the subsequent translation.
With respect to an API (or API interface), which is a convention for interfacing between different components of a software system, communication between computer software can be achieved through the API interface, and the API is mainly a set of definitions, programs, protocols, and the like. APIs belong to a middleware that provides applications and developers with the ability to access routines based on software or hardware without having to access the original code or understand the details of the internal operating mechanisms. The good API interface design can reduce the mutual dependence of all parts of the system and reduce the coupling degree among the constituent units, thereby achieving the purpose of improving the maintainability and the expandability of the system. In an embodiment of the present application, using a declarative API interface requires declaring the use of the present API as an interface at the beginning of programming.
In some embodiments of the present application, optionally, the description manner of the declarative API interface and the code description manner of the target language have a first mapping relationship, for example, the description manner of the corresponding API interface may be deduced from the description manner of the target language, and then the first mapping relationship between the target language and the API is a reversible relationship between the two. Thus, when the business logic is written in the first language, the description mode of the API is used, after the writing is completed, the corresponding business logic expressed as the target code can be obtained through forward translation, and the function of the business logic can be executed.
In some embodiments of the present application, optionally, the declarative API includes at least one of the following functions: keywords, descriptors, literal amounts, operators, brackets, control flows, built-in data types, notes. When the code is written or modified in the first language environment, the source code of the target language can be obtained by writing or modifying the code according to the API interface form provided by the embodiment of the application.
The following exemplifies a specific description of the API interface of an embodiment of the present application. Taking the example of programming in the Python language environment and the object code being the Go code, at least the following APIs can be used for processing, the following description is: API- > object code.
(1) Keyword (keywords)
.break()->break
.func()->func
.chan()->chan
.int()->int
.panic()->panic
The "keywords" in the API interface of the embodiment of the present application include keywords in Go language, reserved words, and built-in functions, which are not exhaustive herein.
(2) Descriptor (identifiers)
In various implementations, embodiments of the present application may provide the following two "descriptors":
.id(“a”)->a
.id_list(“a”,“b”,“c”)->a,b,c
(3) literal amount (literals)
.literal(“a”)->“a”
.literal(1)->1
.literal(0x1)->0x1
(4) Operator (operators)
.oper(“*”)->*
A more specific example is provided below:
.id(a).oper(“+”).literal(1)->a+1
(5) bracket (parentheses)
.block(<sub_procedure>)->{<sub_procedure>}
.parenthese(<sub_precedure>)->(<sub_procedure>)
Wherein < sub_procedure > is a code description.
(6) Control flow
.if(<condition>)->if<condition>
.for(<condition>)->for<condition>
.switch(<expression>)->switch<expression>
.select()->select
A more specific example is provided below:
.if(id(a).oper(“!=”).literal(“abc”)).block(break())->if a!=“abc”{break}
(7) built-in data type (data structure)
.map(<type1>,<type2>)->map[type1]type2
.slice(<type>)->[]<type>
(8) Comment (comments)
.inline_comment(“abc”)->//abc
.block_comment(“abc”)->/*abc*/
According to the embodiment of the application, based on the specific description mode of the API interface provided above, a user can describe the target Go program through the interface in a Python environment, and finally the Go program source code can be obtained. Therefore, the embodiment of the application belongs to a code generation scheme based on a meta programming idea, and can realize dynamic description of target code logic.
Specific implementations and processes of embodiments of the present application are described in detail below with respect to a number of specific embodiments.
Fig. 2 schematically shows a flowchart of a code generation method according to an embodiment of the present application, in which a solid line box represents a process performed, and a broken line box represents an object obtained after the process, and specifically includes the following in order:
s201, a user describes service logic corresponding to a target Go program in a Python environment based on an API interface to obtain an API-based Python code;
s202, performing lexical inspection on parameters when each function is called in the API by using a lexical inspector Lex scanner;
wherein, the description based on the API is equivalent to the concrete grammar tree CST corresponding to the target Go program, namely, a CST corresponding to the target program is described by a user through the API;
s203, converting the obtained CST by using a preset algorithm, such as a top-down grammar analysis algorithm LL top-down player based on a leftmost push-down algorithm, to obtain a corresponding abstract grammar tree AST; in the process of converting by using LL top-down player, grammar checking of codes can be carried out together;
s204, performing static semantic analysis on the obtained abstract syntax tree AST, for example, performing semantic inspection by using a code verification tool coupler to ensure that codes are accurate; it should be noted here that the semantic checking at this step is an optional step, that is, the following processing may be directly performed after the abstract syntax tree AST is obtained;
s205, traversing the abstract syntax tree AST with an Interpreter, thereby translating the AST into Go source code.
Through the steps, a user can generate the target Go voice program source code through reasonable use of an API (application program interface) in a Python environment, and can conveniently modify and update the generated Go code, so that dynamic description and control of Go code logic are realized.
Further, in order to more clearly and intuitively present the implementation manner of the embodiment of the present application, the following schematically illustrates a specific code generation process example according to the embodiment of the present application:
def generate_add_function(name,param1,param2):
p=codegen.func(“add”).parenthese(codegen.id_list(param1,
param2).int.block(codegen.return(id(param1).oper(“+”).id(param2))))
return p.parse().lint().render()
by calling the generate_add_function ("myAddFunc", "x", "y"), the string of the corresponding Go code can be returned as follows:
func myAddFunc(x,y int)int{return x+y}
it can be seen that the above example uses Python language and writes the object code logic according to the API interface provided by the embodiment of the present application, where the code is lexically checked when the API function is called, and the abstract syntax tree AST is semantically checked, and finally the corresponding Go source code of the code logic is returned, so as to implement the dynamic description of the object language code logic.
The specific arrangements and implementations of embodiments of the present application have been described above in terms of various embodiments. Corresponding to the processing method of at least one embodiment described above, an embodiment of the present application further provides a code generating apparatus 100, referring to fig. 3, including:
the code processing module 110 is configured to write, in a first language environment, a code of a first service logic according to a description manner of a preset declarative application program interface API, where the code of the first service logic is used to describe a function corresponding to the first service logic, and obtain a first specific syntax tree after writing is completed, where the first language includes Python language;
a first conversion module 120, configured to convert the first concrete syntax tree into a first abstract syntax tree based on a predetermined syntax analysis algorithm;
a second conversion module 130, configured to convert, by using an interpreter, the first abstract syntax tree into a second language code, where the second language code is used to execute a function corresponding to the first business logic, and the second language includes Go language.
Optionally, referring to fig. 4, the code generating apparatus 100 of the embodiment of the present application may further include:
the syntax checking module 140 is configured to perform syntax checking on the first concrete syntax tree in a process of converting the first concrete syntax tree into a first abstract syntax tree by the first conversion module 120.
Optionally, referring to fig. 4, the code generating apparatus 100 of the embodiment of the present application may further include:
the semantic analysis module 150 is configured to perform static semantic analysis on the first abstract syntax tree after the first conversion module 120 converts the first concrete syntax tree into the first abstract syntax tree.
Optionally, in the embodiment of the present application, a description manner of the declarative API has a first mapping relationship with a code description manner of the second language.
Optionally, in the embodiment of the present application, the first mapping relationship is a reversible relationship between a code description manner of the second language and a description manner of the declarative API.
Optionally, in an embodiment of the present application, the declarative API includes at least one of the following functions: keywords, descriptors, literal amounts, operators, brackets, control flows, built-in data types, notes.
By using the code generation scheme of the embodiment of the application, a user can dynamically control the target code logic through a Python program, and simultaneously has the functions of lexical examination, grammar examination and semantic examination of the code logic, thus the dynamic description of the target language code logic can be realized, and the universality is good.
The functions of each module in each device of the embodiments of the present application may refer to the processing correspondingly described in the foregoing method embodiments, which is not described herein again.
According to embodiments of the present application, the present application also provides an electronic device, a readable storage medium and a computer program product.
As shown in fig. 5, there is a block diagram of an electronic device of a code generation method according to an embodiment of the present application. Electronic devices are intended to represent various forms of digital computers, such as laptops, desktops, workstations, personal digital assistants, servers, blade servers, mainframes, and other appropriate computers. The electronic device may also represent various forms of mobile devices, such as personal digital processing, cellular telephones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions, are meant to be exemplary only, and are not meant to limit implementations of the applications described and/or claimed herein.
As shown in fig. 5, the electronic device includes: one or more processors 1001, memory 1002, and interfaces for connecting the components, including a high-speed interface and a low-speed interface. The various components are interconnected using different buses and may be mounted on a common motherboard or in other manners as desired. The processor may process instructions executing within the electronic device, including instructions stored in or on memory to display graphical information of a graphical user interface (Graphical User Interface, GUI) on an external input/output device, such as a display device coupled to the interface. In other embodiments, multiple processors and/or multiple buses may be used, if desired, along with multiple memories and multiple memories. Also, multiple electronic devices may be connected, each providing a portion of the necessary operations (e.g., as a server array, a set of blade servers, or a multiprocessor system). One processor 1001 is illustrated in fig. 5.
Memory 1002 is a non-transitory computer-readable storage medium provided by the present application. The memory stores instructions executable by the at least one processor to cause the at least one processor to perform the code generation method provided by the present application. The non-transitory computer-readable storage medium of the present application stores computer instructions for causing a computer to execute the code generation method provided by the present application.
The memory 1002 is used as a non-transitory computer readable storage medium for storing a non-transitory software program, a non-transitory computer executable program, and modules such as program instructions/modules (e.g., the code processing module 110, the first conversion module 120, and the second conversion module 130 shown in fig. 3) corresponding to the code generation method in the embodiment of the present application. The processor 1001 executes various functional applications of the server and data processing, that is, implements the code generation method in the above-described method embodiment, by running non-transitory software programs, instructions, and modules stored in the memory 1002.
Memory 1002 may include a storage program area that may store an operating system, at least one application program required for functionality, and a storage data area; the storage data area may store data created from the analysis of search results, the use of processing electronics, and the like. In addition, the memory 1002 may include high-speed random access memory, and may also include non-transitory memory, such as at least one magnetic disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory 1002 optionally includes memory remotely located relative to processor 1001, which may be connected to analysis processing electronics of the search results via a network. Examples of such networks include, but are not limited to, the internet, intranets, local area networks, mobile communication networks, and combinations thereof.
The electronic device corresponding to the code generation method of the embodiment of the application can further comprise: an input device 1003 and an output device 1004. The processor 1001, memory 1002, input device 1003, and output device 1004 may be connected by a bus or other means, for example by a bus connection in the fig. 5 embodiment of the present application.
The input device 1003 may receive input numeric or character information and generate key signal inputs related to user settings and function control of the search result analysis processing electronics, such as a touch screen, keypad, mouse, trackpad, touch pad, pointer stick, one or more mouse buttons, track ball, joystick, etc. input devices. The output means 1004 may include a display device, auxiliary lighting means (e.g., LEDs), tactile feedback means (e.g., vibration motors), and the like. The display device may include, but is not limited to, a liquid crystal display (Liquid Crystal Display, LCD), a light emitting diode (Light Emitting Diode, LED) display, and a plasma display. In some implementations, the display device may be a touch screen.
Various implementations of the systems and techniques described here can be implemented in digital electronic circuitry, integrated circuitry, application specific integrated circuits (Application Specific Integrated Circuits, ASIC), computer hardware, firmware, software, and/or combinations thereof. These various embodiments may include: implemented in one or more computer programs, the one or more computer programs may be executed and/or interpreted on a programmable system including at least one programmable processor, which may be a special purpose or general-purpose programmable processor, that may receive data and instructions from, and transmit data and instructions to, a storage system, at least one input device, and at least one output device.
These computing programs (also referred to as programs, software applications, or code) include machine instructions for a programmable processor, and may be implemented in a high-level procedural and/or object-oriented programming language, and/or in assembly/machine language. As used herein, the terms "machine-readable medium" and "computer-readable medium" refer to any computer program product, apparatus, and/or device (e.g., magnetic discs, optical disks, memory, programmable logic devices (programmable logic device, PLDs)) used to provide machine instructions and/or data to a programmable processor, including a machine-readable medium that receives machine instructions as a machine-readable signal. The term "machine-readable signal" refers to any signal used to provide machine instructions and/or data to a programmable processor.
To provide for interaction with a user, the systems and techniques described here can be implemented on a computer having: a display device (e.g., CRT (Cathode Ray Tube) or LCD (liquid crystal display) monitor) for displaying information to a user; and a keyboard and pointing device (e.g., a mouse or trackball) by which a user can provide input to the computer. Other kinds of devices may also be used to provide for interaction with a user; for example, feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form, including acoustic input, speech input, or tactile input.
The systems and techniques described here can be implemented in a computing system that includes a background component (e.g., as a data server), or that includes a middleware component (e.g., an application server), or that includes a front-end component (e.g., a user computer having a graphical user interface or a web browser through which a user can interact with an implementation of the systems and techniques described here), or any combination of such background, middleware, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: local area network (Local Area Network, LAN), wide area network (Wide Area Network, WAN) and the internet.
The computer system may include a client and a server. The client and server are typically remote from each other and typically interact through a communication network. The relationship of client and server arises by virtue of computer programs running on the respective computers and having a client-server relationship to each other.
According to the technical scheme provided by the embodiment of the application, a generation scheme of the target code such as the Go code based on the meta programming realized by using the Python is provided, a user can dynamically control the logic of the target code through the Python program, further, a lexical, grammatical and semantic checking function is provided, and the problems of inconvenience and lack of generality of the code generated through a static file are solved.
It should be appreciated that various forms of the flows shown above may be used to reorder, add, or delete steps. For example, the steps described in the present application may be performed in parallel, sequentially, or in a different order, provided that the desired results of the disclosed embodiments are achieved, and are not limited herein.
The above embodiments do not limit the scope of the present application. It will be apparent to those skilled in the art that various modifications, combinations, sub-combinations and alternatives are possible, depending on design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present application should be included in the scope of the present application.

Claims (8)

1. A code generation method, comprising:
writing a code of a first business logic according to a description mode of a preset declarative application program interface API in a first language environment, wherein the code of the first business logic is used for describing a function corresponding to the first business logic, and a first concrete grammar tree is obtained after writing is completed, and the first language comprises Python language;
converting the first concrete syntax tree into a first abstract syntax tree based on a predetermined syntax analysis algorithm;
converting the first abstract syntax tree into a second language code through an interpreter, wherein the second language code is used for executing functions corresponding to the first business logic, and the second language comprises a Go language;
the description mode of the declarative API and the code description mode of the second language have a first mapping relation, and the first mapping relation is a backward relation between the code description mode of the second language and the description mode of the declarative API;
the declarative API includes at least one of the following functions: keywords, descriptors, literal amounts, operators, brackets, control flows, built-in data types, notes.
2. The method of claim 1, in said converting the first concrete syntax tree into a first abstract syntax tree, the method further comprising:
and carrying out grammar checking on the first specific grammar tree.
3. The method of claim 1, after said converting the first concrete syntax tree to a first abstract syntax tree, the method further comprising:
and carrying out static semantic analysis on the first abstract syntax tree.
4. A code generating apparatus comprising:
the code processing module is used for writing codes of first business logic according to a description mode of a preset declarative application program interface API in a first language environment, wherein the codes of the first business logic are used for describing functions corresponding to the first business logic, and a first specific grammar tree is obtained after writing is completed, and the first language comprises Python language;
the first conversion module is used for converting the first concrete grammar tree into a first abstract grammar tree based on a preset grammar analysis algorithm;
the second conversion module is used for converting the first abstract syntax tree into second language codes through an interpreter, and the second language codes are used for executing functions corresponding to the first business logic, wherein the second language comprises a Go language;
the description mode of the declarative API and the code description mode of the second language have a first mapping relation, and the first mapping relation is a backward relation between the code description mode of the second language and the description mode of the declarative API;
wherein the declarative API includes at least one of the following functions: keywords, descriptors, literal amounts, operators, brackets, control flows, built-in data types, notes.
5. The apparatus of claim 4, further comprising:
and the grammar checking module is used for carrying out grammar checking on the first concrete grammar tree in the process of converting the first concrete grammar tree into the first abstract grammar tree by the first conversion module.
6. The apparatus of claim 4, further comprising:
the semantic analysis module is used for carrying out static semantic analysis on the first abstract syntax tree after the first conversion module converts the first concrete syntax tree into the first abstract syntax tree.
7. An electronic device, comprising:
at least one processor; and
a memory communicatively coupled to the at least one processor; wherein,
the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the method of any one of claims 1-3.
8. A non-transitory computer readable storage medium storing computer instructions for causing the computer to perform the method of any one of claims 1-3.
CN202010468473.XA 2020-05-28 2020-05-28 Code generation method, device, equipment and storage medium Active CN111625224B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010468473.XA CN111625224B (en) 2020-05-28 2020-05-28 Code generation method, device, equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010468473.XA CN111625224B (en) 2020-05-28 2020-05-28 Code generation method, device, equipment and storage medium

Publications (2)

Publication Number Publication Date
CN111625224A CN111625224A (en) 2020-09-04
CN111625224B true CN111625224B (en) 2023-11-24

Family

ID=72259197

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010468473.XA Active CN111625224B (en) 2020-05-28 2020-05-28 Code generation method, device, equipment and storage medium

Country Status (1)

Country Link
CN (1) CN111625224B (en)

Families Citing this family (12)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US11650856B2 (en) * 2020-06-26 2023-05-16 Red Hat Inc. Federated operator for edge computing network
CN114253520B (en) * 2020-09-21 2024-03-12 抖音视界有限公司 Interface code generation method and device
CN112270176B (en) 2020-11-09 2022-05-03 北京百度网讯科技有限公司 Method, apparatus, and computer storage medium for mode conversion in a deep learning framework
CN112486465A (en) * 2020-12-11 2021-03-12 北京字节跳动网络技术有限公司 Code updating method and device
CN112860356B (en) * 2021-02-10 2023-08-15 北京百度网讯科技有限公司 API call control method and device, electronic equipment and storage medium
CN113157255B (en) * 2021-03-22 2022-07-05 成都淞幸科技有限责任公司 Code generation method for syntax tree decoder
CN114816467B (en) * 2021-03-25 2023-04-28 北京百度网讯科技有限公司 Upgrade file generation method and device and electronic equipment
CN113312057B (en) * 2021-06-17 2023-10-03 上海小熊星教育科技有限公司 Code processing method, programming auxiliary method, medium and electronic equipment
CN113778440B (en) * 2021-08-18 2024-01-26 上海瑞家信息技术有限公司 Data processing method and device, electronic equipment and storage medium
CN114942782A (en) * 2021-09-24 2022-08-26 华为技术有限公司 Code migration method and device of model
CN114610317A (en) * 2022-03-16 2022-06-10 北京字节跳动网络技术有限公司 Code conversion method, device, equipment and storage medium
CN116501303B (en) * 2023-06-25 2023-11-14 北京长亭科技有限公司 Automatic API code generation method and device

Citations (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106371997A (en) * 2016-09-07 2017-02-01 网易(杭州)网络有限公司 Code checking method and device
CN106919434A (en) * 2017-03-22 2017-07-04 恒生电子股份有限公司 A kind of code generating method and device
CN108920133A (en) * 2018-06-14 2018-11-30 北京三快在线科技有限公司 Across Programming with Pascal Language method, apparatus, electronic equipment and storage medium
WO2018236384A1 (en) * 2017-06-22 2018-12-27 Archeo Futurus, Inc. Compiling and optimizing a computer code by minimizing a number of states in a finite machine corresponding to the computer code
CN110007926A (en) * 2019-04-17 2019-07-12 广东三维家信息科技有限公司 Language transfer method and device
CN110109675A (en) * 2019-04-30 2019-08-09 翟红鹰 Intelligent contract processing method, device and computer readable storage medium
CN110381135A (en) * 2019-07-18 2019-10-25 北京奇艺世纪科技有限公司 Interface creation method, service request method, device, computer equipment and medium
CN110716743A (en) * 2019-09-30 2020-01-21 上海一竹网络科技有限公司 Aggregation API development method and system suitable for multi-party collaborative development
CN111158665A (en) * 2019-12-27 2020-05-15 网易(杭州)网络有限公司 Code generation method and device, electronic equipment and storage medium

Family Cites Families (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20020161907A1 (en) * 2001-04-25 2002-10-31 Avery Moon Adaptive multi-protocol communications system
US10140105B2 (en) * 2016-03-10 2018-11-27 Wowza Media Systems, LLC Converting source code
RU2016137177A (en) * 2016-09-16 2018-03-19 Оракл Интернэйшнл Корпорейшн IMPROVED TRANSFORMATION OF SOURCE PROGRAMMING LANGUAGE CODE
RU2017110071A (en) * 2017-03-24 2018-09-24 Оракл Интернэйшнл Корпорейшн TRANSFORMING SOURCE PROGRAMMING LANGUAGE CODE
US10545730B2 (en) * 2018-05-07 2020-01-28 Red Hat, Inc. Automatically generating code for application programming interfaces

Patent Citations (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106371997A (en) * 2016-09-07 2017-02-01 网易(杭州)网络有限公司 Code checking method and device
CN106919434A (en) * 2017-03-22 2017-07-04 恒生电子股份有限公司 A kind of code generating method and device
WO2018236384A1 (en) * 2017-06-22 2018-12-27 Archeo Futurus, Inc. Compiling and optimizing a computer code by minimizing a number of states in a finite machine corresponding to the computer code
CN108920133A (en) * 2018-06-14 2018-11-30 北京三快在线科技有限公司 Across Programming with Pascal Language method, apparatus, electronic equipment and storage medium
CN110007926A (en) * 2019-04-17 2019-07-12 广东三维家信息科技有限公司 Language transfer method and device
CN110109675A (en) * 2019-04-30 2019-08-09 翟红鹰 Intelligent contract processing method, device and computer readable storage medium
CN110381135A (en) * 2019-07-18 2019-10-25 北京奇艺世纪科技有限公司 Interface creation method, service request method, device, computer equipment and medium
CN110716743A (en) * 2019-09-30 2020-01-21 上海一竹网络科技有限公司 Aggregation API development method and system suitable for multi-party collaborative development
CN111158665A (en) * 2019-12-27 2020-05-15 网易(杭州)网络有限公司 Code generation method and device, electronic equipment and storage medium

Non-Patent Citations (4)

* Cited by examiner, † Cited by third party
Title
C-Java自动程序转换系统的设计;严忠林 等;《计算机技术与发展》;第17卷(第2期);46-49, 134 *
Extensible intraprocedural flow analysis at the abstract syntax tree level;Emma Söderberg 等;《Science of Computer Programming》;第78卷(第10期);1809-1827 *
代码生成技术在管理信息系统开发中的研究与应用;董玉明;《中国优秀硕士学位论文全文数据库 信息科技辑》(第4期);I138-481 *
蒋立源等.《编译原理 第3版》.西北工业大学出版社,2005,367-368. *

Also Published As

Publication number Publication date
CN111625224A (en) 2020-09-04

Similar Documents

Publication Publication Date Title
CN111625224B (en) Code generation method, device, equipment and storage medium
EP3923160A1 (en) Method, apparatus, device and storage medium for training model
CN113010181B (en) Deployment method and device of operators in deep learning framework and electronic equipment
CN112069201A (en) Target data acquisition method and device
CN111881387B (en) Data processing method, device, equipment and medium for small program
US11586425B2 (en) Method for compilation optimization of hosted app, electronic device and readable storage medium
CN111752843A (en) Method, device, electronic equipment and readable storage medium for determining influence surface
JP2021152960A (en) Operator combining method, device, electric device, storage medium and computer program
US9886251B2 (en) Optimized compiling of a template function
JP2021099771A (en) Method for translation, device, electronic apparatus, readable storage medium, and computer program
US20210312240A1 (en) Header Model For Instance Segmentation, Instance Segmentation Model, Image Segmentation Method and Apparatus
WO2023169193A1 (en) Method and device for generating smart contract
CN111563253A (en) Intelligent contract operation method, device, equipment and storage medium
CN111639116B (en) Data access connection session protection method and device
CN112579096B (en) Method, device, equipment and medium for compiling and loading applet startup file
CN112379885B (en) Applet compiling method, device, equipment and readable storage medium
CN112270176B (en) Method, apparatus, and computer storage medium for mode conversion in a deep learning framework
US11941055B2 (en) Method and apparatus for graph computing, electronic device and storage medium
CN111831317B (en) Method and device for acquiring dependency relationship between services, electronic equipment and storage medium
EP3958183A1 (en) Deep learning model adaptation method and apparatus and electronic device
CN111880778A (en) Interface document generation method and device, electronic equipment and storage medium
CN111857825A (en) Instruction execution method and device, electronic equipment and computer-readable storage medium
CN115563183B (en) Query method, query device and program product
US20210288938A1 (en) Network Data Processing Method, Apparatus, Electronic Device, and Storage Medium
JP7393404B2 (en) Compiling method, compiling device, electronic device, storage medium and program

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant