CN115878120A - Code generation method, device and storage medium - Google Patents

Code generation method, device and storage medium Download PDF

Info

Publication number
CN115878120A
CN115878120A CN202111134619.8A CN202111134619A CN115878120A CN 115878120 A CN115878120 A CN 115878120A CN 202111134619 A CN202111134619 A CN 202111134619A CN 115878120 A CN115878120 A CN 115878120A
Authority
CN
China
Prior art keywords
code
target
statement
converted
node
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.)
Pending
Application number
CN202111134619.8A
Other languages
Chinese (zh)
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.)
Tencent Technology Shenzhen Co Ltd
Original Assignee
Tencent Technology Shenzhen 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 Tencent Technology Shenzhen Co Ltd filed Critical Tencent Technology Shenzhen Co Ltd
Priority to CN202111134619.8A priority Critical patent/CN115878120A/en
Publication of CN115878120A publication Critical patent/CN115878120A/en
Pending legal-status Critical Current

Links

Images

Abstract

The application discloses a code generation method, a device and a storage medium, which can be applied to various scenes such as cloud technology, artificial intelligence, intelligent transportation, internet of vehicles and the like, wherein the method comprises the following steps: analyzing the target pseudo code to obtain an analysis statement; the target pseudo code is a repair code for repairing the target application; determining a target sentence to be converted and a non-conversion sentence based on the analysis sentence; determining a target node corresponding to a target sentence to be converted based on preset mapping information; the target node is a node of a target grammatical structure; presetting mapping information for representing the mapping relation between the statement to be converted and the node; constructing a syntax tree based on the target node and the preset node; based on the syntax tree, executable code is generated. The pseudo code is converted and translated into the executable code, so that the same function is completed by using less code amount, and the coding efficiency and the readability of the code are improved.

Description

Code generation method, device and storage medium
Technical Field
The present application relates to the field of computer technologies, and in particular, to a code generation method and apparatus, and a storage medium.
Background
In the hot patch coding of the prior art, a plurality of functions need to be memorized, such as a replaceMethod, a callobjcInstance method, a callobjcClassMethod and the like, and the meaning of each parameter in the function needs to be memorized, so that coding errors are easy to occur; the number of codes is large, and the coding efficiency is low;
therefore, it is necessary to provide a code generation method, apparatus and storage medium to improve the generation efficiency of executable code.
Disclosure of Invention
The application provides a code generation method, a code generation device and a storage medium, which can improve the generation efficiency of executable codes.
In one aspect, the present application provides a code generation method, where the method includes:
analyzing the target pseudo code to obtain an analysis statement; the target pseudo code is a repair code for repairing a target application;
determining a target sentence to be converted and a non-conversion sentence based on the analysis sentence;
determining a target node corresponding to the target sentence to be converted based on preset mapping information; the target node is a node of a target grammatical structure; the preset mapping information represents the mapping relation between the statement to be converted and the node;
constructing a syntax tree based on the target node and a preset node; the target node represents the target sentence to be converted, and the preset node represents the non-conversion sentence; the target node is used for storing the attribute information of the target statement to be converted;
based on the syntax tree, executable code is generated.
Another aspect provides a code generation apparatus, the apparatus comprising:
the analysis statement determining module is used for analyzing the target pseudo code to obtain an analysis statement; the target pseudo code is a repair code for repairing a target application;
the target sentence to be converted determining module is used for determining a target sentence to be converted and a non-conversion sentence based on the analysis sentence;
the target node determining module is used for determining a target node corresponding to the target sentence to be converted based on preset mapping information; the target node is a node of a target grammatical structure; the preset mapping information represents the mapping relation between the statement to be converted and the node;
the syntax tree construction module is used for constructing a syntax tree based on the target node and a preset node; the target node represents the target sentence to be converted, and the preset node represents the non-conversion sentence; the target node is used for storing the attribute information of the target statement to be converted;
and the code generation module is used for generating executable codes based on the syntax tree.
Another aspect provides a code generation apparatus, which includes a processor and a memory, where at least one instruction or at least one program is stored in the memory, and the at least one instruction or the at least one program is loaded by the processor and executed to implement the code generation method as described above.
Another aspect provides a computer storage medium storing at least one instruction or at least one program, which is loaded and executed by a processor to implement the code generation method as described above.
Another aspect provides a computer program product comprising computer instructions stored in a computer readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes to implement the code generation method as described above.
The code generation method, the device and the storage medium have the following technical effects:
the method comprises the steps of resolving a target pseudo code to obtain a resolved statement; determining a target sentence to be converted and a non-conversion sentence based on the analysis sentence; therefore, in the process of converting the pseudo code into the executable code, the pseudo code is firstly analyzed into different statements, and then the statements are divided into two types, wherein one type is the statement to be converted which needs to be converted, and the other type is the statement which does not need to be converted; according to the preset mapping relation, a target node of a target grammatical structure corresponding to the target sentence to be converted is determined, and the grammatical structure of the sentence to be converted is simplified; constructing a syntax tree according to the target node, thereby generating an executable code; in the code conversion process, only part of sentences need to be converted, so that the syntax tree structure constructed based on the nodes is simple, the executable code can be quickly generated, the pseudo code is converted and translated to be changed into the executable code, the same function is completed by using less code quantity, and the coding efficiency and the readability of the code are improved.
Drawings
In order to more clearly illustrate the technical solutions and advantages of the embodiments of the present application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative efforts.
FIG. 1 is a schematic diagram of a code generation system provided by an embodiment of the present application;
fig. 2 is a schematic flowchart of a code generation method provided in an embodiment of the present application;
FIG. 3 is a flowchart illustrating a method for generating a code parser according to an embodiment of the present application;
FIG. 4 is a flowchart illustrating a method for generating executable code according to an embodiment of the present application;
FIG. 5 is a flowchart illustrating a method for executing the executable code according to an embodiment of the present application;
FIG. 6 is a diagram illustrating the syntax of pseudo code corresponding to an item in the prior art;
FIG. 7 is a diagram illustrating a pseudo code corresponding to an item according to an embodiment of the present disclosure;
FIG. 8 is a diagram illustrating the syntax of pseudo code corresponding to another item in the prior art;
FIG. 9 is a diagram illustrating a pseudo code corresponding to another item according to an embodiment of the present disclosure;
FIG. 10 is a diagram illustrating a syntax structure of a sentence to be converted according to an embodiment of the present application;
FIG. 11 is a diagram illustrating a syntax structure of a node in a syntax tree according to an embodiment of the present application;
FIG. 12 is a diagram illustrating a to-be-converted statement in pseudo code according to an embodiment of the present application;
FIG. 13 is a flowchart illustrating a method for building a code parser according to an embodiment of the present application;
FIG. 14 is a flowchart illustrating a method for generating executable code according to an embodiment of the present application;
FIG. 15 is a flowchart illustrating a method for converting pseudo code into executable code according to an embodiment of the present application;
FIG. 16 is a diagram illustrating a to-be-converted statement and a non-converted statement in target pseudo code according to an embodiment of the present application;
FIG. 17 is a diagram illustrating parsing of a function call according to an embodiment of the present application;
FIG. 18 is a diagram illustrating a syntax parsing of a pseudo code according to an embodiment of the present application;
fig. 19 is a flowchart illustrating a method for calling a _ c function according to an embodiment of the present application;
FIG. 20 is a schematic illustration of transcoding provided by an embodiment of the present application;
FIG. 21 is a schematic diagram of pseudo-code converted executable code corresponding to FIG. 16 according to an embodiment of the present application;
FIG. 22 is a schematic diagram of executable code provided by the prior art;
FIG. 23 is a schematic diagram of executable code provided by an embodiment of the present application;
FIG. 24 is a code quantity comparison chart of the two codes of FIGS. 22-23;
fig. 25 is a schematic structural diagram of a code generation apparatus provided in an embodiment of the present application;
fig. 26 is a schematic structural diagram of a server according to an embodiment of the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
It should be noted that the terms "first," "second," and the like in the description and claims of this application and in the accompanying drawings are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used is interchangeable under appropriate circumstances such that the embodiments of the application described herein are capable of operation in sequences other than those illustrated or described herein. Moreover, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or server that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
The terms referred to in this application are defined as follows:
one Patch: an iOS platform hot patch project;
thermal restoration: when the application program is on line, problems occur, and urgent patching repair is needed, namely hot repair.
Hot patching: script code for hot fix is called a hot patch.
Code conversion: from the simplified JavaScript pseudo code to the executable JavaScript code.
JavaScript ("JS") is a lightweight, interpreted or just-in-time programming language with function precedence.
Objective-C, usually written as ObjC or OC and less often Objective C or Obj-C, is an object-oriented programming language that extends C.
Referring to fig. 1, fig. 1 is a schematic diagram of a code generation system provided in an embodiment of the present application, and as shown in fig. 1, the code generation system may include at least a server 01 and a client 02.
Specifically, in this embodiment of the application, the server 01 may include an independently operating server, or a distributed server, or a server cluster including a plurality of servers, and may also be a cloud server that provides basic cloud computing services such as a cloud service, a cloud database, cloud computing, a cloud function, cloud storage, a Network service, cloud communication, a middleware service, a domain name service, a security service, a CDN (Content Delivery Network), and a big data and artificial intelligence platform. The server 01 may comprise a network communication unit, a processor and a memory, etc. In particular, the server 01 may be used to convert pseudo code into executable code.
Specifically, in this embodiment, the client 02 may include an entity device such as a smart phone, a desktop computer, a tablet computer, a notebook computer, a digital assistant, a smart wearable device, a smart speaker, a vehicle-mounted terminal, and a smart television, and may also include software running in the entity device, for example, a web page provided by some service providers to a user, and applications provided by the service providers to the user. In particular, the client 02 may be used to execute executable code.
A code generation method of the present application is described below, and fig. 2 is a flowchart of a code generation method provided in an embodiment of the present application, and the present specification provides the method operation steps as described in the embodiment or the flowchart, but may include more or less operation steps based on conventional or non-inventive labor. The order of steps recited in the embodiments is merely one manner of performing the steps in a multitude of sequences, and does not represent a unique order of performance. In practice, the system or server product may be implemented in a sequential or parallel manner (e.g., parallel processor or multi-threaded environment) according to the embodiments or methods shown in the figures. Specifically, as shown in fig. 2, the method may include:
s201: analyzing the target pseudo code to obtain an analysis statement; the target pseudo code is a repair code for repairing the target application.
In the embodiment of the present application, the pseudo code (Pseudocode) is an informal language similar to the english language for describing the structure diagram of the module. The purpose of the pseudo-code is to make the described algorithm easily implementable in any programming language (Pascal, C, java, etc.). The pseudo code cannot be directly executed, for example, javaScript pseudo code is used for realizing the calling of Objective-C code, so code conversion is needed for normal operation. The target pseudo code needs to be converted into executable code.
In the embodiment of the application, a problem may occur in the running process of the application program, the application program needs to be subjected to hot repair, and the repair code of the target application in the hot repair is hot patch code.
In an embodiment of the present application, the method further includes:
constructing the target pseudo code based on the point call grammar facing the object; the structure of the object-oriented point call syntax includes class names, methods and arguments.
In the embodiment of the application, the structure of the object-oriented point call grammar comprises class names, methods and parameters, and a pseudo code constructed based on the grammar is simpler; the pseudo code is edited by adopting the object-oriented point call syntax, so that the expressions of function call and attribute call are simplified; the pseudo codes of the application all adopt standard JavaScript syntax; for the same item, the syntax of the prior art pseudo code is shown in fig. 6, and the syntax of the pseudo code of the present application is shown in fig. 7; in another project, the syntax of the pseudo code of the prior art is shown in fig. 8, and the syntax of the pseudo code of the present application is shown in fig. 9; obviously, compared with the prior art, the syntax of writing the pseudo code is simpler.
In this embodiment of the present application, the parsing the target pseudo code to obtain a parsing statement includes:
analyzing the target pseudo code based on a code analyzer to obtain an analysis statement;
in an embodiment of the present application, as shown in fig. 3, the method for generating a code parser includes:
s301: analyzing a preset lexical sequence based on a lexical analysis tool to generate a lexical analysis code;
in the embodiment of the present application, the lexical analysis tool may be a flex tool, and flex is a tool for generating a lexical analyzer, which may generate C language codes matching corresponding character strings by using regular expressions.
S303: analyzing the sentence to be converted based on a syntactic analysis tool to generate a syntactic analysis code; the sentence to be converted is generated based on the preset lexical sequence;
in an embodiment of the present application, the parsing tool may be a bison tool, which is a generic parser generator that converts annotated context-free grammars into parsers that use a table of parsers.
In this embodiment of the present application, the parsing the to-be-converted statement based on a parsing tool to generate a parsing code includes:
determining a target grammar structure of the sentence to be converted;
and analyzing the sentence to be converted according to the target grammatical structure based on the grammatical analysis tool to generate the grammatical analysis code.
In the embodiment of the application, a simplified syntax structure corresponding to each statement to be converted can be preset, so that the executable code after conversion is simplified.
S305: and compiling the lexical analysis code and the syntactic analysis code based on a compiler to generate a code parser.
In embodiments of the present application, the code parser may include a lexical analyzer and a syntax analyzer. The lexical analyzer is constructed based on the lexical analysis codes, and the syntactic analyzer is constructed based on the syntactic analysis codes.
In a specific embodiment, as shown in fig. 13, the method for constructing the code parser includes:
s1301: inputting the user-defined lexical method into a flex tool to obtain a lexical analysis code, namely constructing a lexical analyzer;
s1303: inputting the custom grammar into a bison tool to obtain a grammar analysis code, namely constructing a grammar analyzer;
in the embodiment of the present application, flex and bison may be replaced with antlr, which has a larger volume than flex and bison. The antlr (speaker for Language Recognition) is an open source parser which can automatically generate a syntax tree based on an input and display the syntax tree in a visualized manner.
S1305: and inputting the lexical analysis code and the syntactic analysis code into the GCC to construct a code analyzer.
Among them, GCC (GNU Compiler Collection, GNU Compiler suite) is a programming language interpreter developed by GNU. GNU compiler suites include C, C + +, objective-C, fortran, java, ada, and Go language front ends, as well as libraries for these languages.
In the embodiment of the present application, as shown in fig. 14, the pseudo code and the syntax node structure definition are input to a code parser, and are converted into executable code.
In the embodiment of the present application, a flowchart of converting pseudo code into executable code is shown in fig. 15, and includes the following steps:
s1501: inputting the patch pseudo code into a lexical analyzer and outputting a lexical unit;
s1503: inputting the lexical unit into a syntax analyzer to obtain a sentence to be converted and a non-converted sentence; the statement to be converted comprises class definition, method definition, attribute call, method call and the like; the character strings and other sentences are non-conversion sentences;
s1505: creating a corresponding node according to each statement to be converted; wherein, the attribute calling and the method calling correspond to the same node 'CallNode'; character strings and other sentences correspond to the same node "StringNode";
s1507: constructing a grammar chain (namely a grammar tree) according to the plurality of created nodes;
s1509: and generating executable code according to the grammar chain.
In this embodiment of the present application, the analyzing the target pseudo code to obtain an analysis statement includes:
analyzing the target pseudo code based on a lexical analyzer to obtain a word sequence;
and analyzing the word sequence based on a syntactic analyzer to obtain an analyzed sentence.
In the embodiment of the application, the syntax tree can be quickly created through the syntax structure corresponding to each node, so that the executable code can be quickly generated.
S203: and determining a target sentence to be converted and a non-conversion sentence based on the analysis sentences.
In the embodiment of the application, in the hot repair process of the target application, only certain statements, such as class definitions, method calls, attribute calls and the like, need to be concerned with the hot patch codes; such sentences may be determined as sentences to be converted, and sentences other than these sentences may be determined as non-converted sentences.
In the embodiment of the present application, the target to-be-converted statement may include, but is not limited to, a class definition, a method definition, an attribute call, a method call, a string content, and the like.
In a specific embodiment, as shown in fig. 16, fig. 16 includes class definitions, method definitions, attribute calls, and statement to be converted by method call (the statement marked as focus in fig. 16), and the statement marked as focus in fig. 16 refers to a statement that does not need to be converted; in the analysis process, judging whether the method definition and the method calling have parameters or not; part of statements to be converted contain hidden parameters; in the process of constructing the syntax tree, hidden parameters need to be replaced by nodes.
In the prior art, a pseudo code can be converted into an executable code by simple regular character string replacement, but errors are easy to occur in the method for converting the content of the character string; and the conversion syntax is complex; according to the method, the patch pseudo code is analyzed and the syntax tree is established through lexical analysis and syntax analysis, the executable code is output after semantic analysis is further performed, and therefore the pseudo code can be extremely strong in analysis and customization capacity, which cannot be achieved by regular replacement of character strings. However, lexical analysis and syntactic analysis are performed on all pseudo codes, complexity and workload are large, and the method is equivalent to the realization of the first half of a JavaScript compiler. For hot-patch code, only certain statements and scenarios are of interest, such as: class definition, method invocation, attribute invocation, and wait for translation statements. The method and the device divide the statement after the pseudo code is divided into two parts, and only the statement to be converted can be converted, so that the workload of code conversion is reduced, and the code conversion efficiency is improved.
S205: determining a target node corresponding to the target sentence to be converted based on preset mapping information; the target node is a node of a target grammatical structure; the preset mapping information represents the mapping relation between the statement to be converted and the node.
In the embodiment of the application, the grammar structure of each statement to be converted can be predetermined, namely, grammar definition is carried out on each statement to be converted, and then the mapping relation between the statement to be converted and the nodes in the grammar tree is constructed; therefore, the grammar structure of the statement to be converted is simplified, and the conversion rate of the code is improved; in a specific embodiment, the syntax definition is as shown in fig. 10, where the syntax structure of each statement to be converted is defined, the statement to be converted in the diagram includes statements such as attribute call, function parameter definition, and each statement is correspondingly provided with a fixed syntax structure; for example, the syntax structure of the property call includes three parts, i.e., id.id, property _ call.id, and function _ call.id, and the ID is an identifier; in a particular embodiment, the attribute call may include: this.
In the embodiment of the present application, after matching the syntax structure of the attribute call, a corresponding node in the syntax tree may be constructed. In a specific embodiment, the structure of the nodes in the syntax tree may be as shown in fig. 11 according to the correspondence of the sentence to be converted, where the sentence to be converted corresponds to a parent node, and the word sequence of the sentence to be converted, which is decomposed according to the syntax structure, corresponds to a plurality of child nodes; for example, a class node includes child nodes such as class names, method definitions, and the like.
S207: constructing a syntax tree based on the target node and a preset node; the target node represents the target sentence to be converted, and the preset node represents the non-conversion sentence; the target node is used for storing the attribute information of the target statement to be converted.
In the embodiment of the present application, the attribute information of the target sentence to be converted may represent a syntactic structure of the target sentence to be converted. For example, id.id, property _ call.id, and function _ call.id in the syntax structure of the property call are all the property information of the target statement to be converted.
In the embodiment of the present application, a case of parsing a function call is shown in fig. 17, and a function call statement may be recognized and a syntax node may be constructed, so as to finally form a syntax tree.
In the embodiment of the present application, as shown in fig. 18, for a sentence (non-conversion sentence) that does not need attention, a sentence (state) unit in the parser can be used to ensure that a lexical unit that is input by the lexical parser and cannot be processed can be processed at this point, so that a parsing error can be prevented from being triggered. Note that the longest integrity match principle needs to be followed in the state to ensure that the parser can take precedence. Set, for example, for the statement self; when matching is carried out, matching is carried out according to self.set instead of self, so that syntax analysis errors are avoided; the accuracy of grammar parsing is improved.
S209: based on the syntax tree, executable code is generated.
In an embodiment of the application, as shown in fig. 4, before the generating the executable code based on the syntax tree, the method further includes:
s2081: constructing a method calling function of each node in the syntax tree, wherein the method calling function is used for determining a calling method of each node in the syntax tree;
s2083: and generating an executable code based on the statement represented by each node in the syntax tree and the method calling function.
In the embodiment of the present application, as shown in fig. 21, fig. 21 is an executable code after pseudo code conversion corresponding to fig. 16.
In an embodiment of the present application, as shown in fig. 5, the executable code includes at least two executable statements, and after the executable code is generated based on the syntax tree, the method further includes:
s2011: determining a current executable statement and a corresponding current calling method in the executable code;
s2013: acquiring a method calling function in the executable code;
in an embodiment of the present application, a method call function may be used to find a currently called method.
S2015: judging whether the current calling method exists in the first language object or not based on the method calling function;
s2017: and if so, calling the current calling method to execute the executable code.
In an embodiment of the present application, if the current calling method does not exist in the first language object, the method further includes:
s2019: and judging whether the current calling method exists in the second language object or not based on the method calling function.
S20111: and if so, calling the current calling method to execute the executable code.
In the embodiment of the application, in the execution process of the executable code, the calling method corresponding to the executable code can be obtained through the method calling function.
In an embodiment of the present application, if the current calling method does not exist in the second language object, the method further includes:
s20113: and determining the executable code as abnormal code.
In the embodiment of the application, if the method calling function cannot acquire the corresponding calling method, it is indicated that the executable code is an abnormal code, that is, an error occurs in the code in the conversion process, and therefore troubleshooting is required.
In the embodiment of the application, in the hot patch, both JavaScript codes and Objective-C pseudo codes exist, and a code parser cannot distinguish the two codes, so that correct calling can be achieved only by distinguishing the codes in the runtime, and therefore, all method calls need to be converted into calling a special "\u C" method in a code converter, and the method is mainly responsible for distinguishing JavaScript/Objective-C methods and completing correct calling of the two different methods. As shown in fig. 19, in the code conversion phase, all method calls are converted into that the same _ c method is called first, in the _ c method, the method to be called is first found in the JS object, if the method can be found, the JS method is indicated, if the method can not be found, the OC layer is switched to, if the method can not be found, the OC object is searched, the execution is performed, and if the method can not be found, the exception is thrown. The transcoding process simply inserts the _ c function. This allows code to be called during execution, and in one embodiment, the code conversion scheme is shown in FIG. 20. setTitle (), self, this object, does not exist at all, and this object may not have setTitle method at all, or this is an OC method. Therefore, the execution function of the script is also realized.
In an embodiment of the present application, the current calling method includes a parameter calling method and an argument calling method, and the calling the current calling method to execute the executable code includes:
determining the form parameter of the current executable statement based on the form parameter calling method;
determining the actual parameters of the current executable statement based on the actual parameter calling method;
and executing the executable code based on the form factor and the argument of the current executable statement.
In the embodiment of the present application, executable codes of the prior art and the present application are shown in fig. 22 to 23 for the same item, and fig. 24 is a code amount comparison graph of the two; based on fig. 22-24, it can be seen that the amount of code converted by the method of the present application is significantly greater than that of the prior art.
According to the method and the device, common functions do not need to be recorded in the code conversion process, and a user does not need to know the existence of the functions, so that the method and the device completely conform to the grammar habit of JavaScript. The coding grammar is completely based on the modern object-oriented programming language, so that the convenience and readability of writing are improved; and the amount of code is reduced by at least 40%.
According to the technical scheme provided by the embodiment of the application, the target pseudo code is analyzed to obtain an analysis statement; determining a target sentence to be converted and a non-conversion sentence based on the analysis sentence; therefore, in the process of converting the pseudo code into the executable code, the pseudo code is firstly analyzed into different statements, and then the statements are divided into two types, wherein one type is the statement to be converted which needs to be converted, and the other type is the statement which does not need to be converted; according to the preset mapping relation, a target node of a target grammatical structure corresponding to the target sentence to be converted is determined, and the grammatical structure of the sentence to be converted is simplified; constructing a syntax tree according to the target node, thereby generating an executable code; in the code conversion process, only part of sentences need to be converted, so that the syntax tree structure constructed based on the nodes is simple, the executable code can be quickly generated, the pseudo code is converted and translated to be changed into the executable code, the same function is completed by using less code quantity, and the coding efficiency and the readability of the code are improved.
An embodiment of the present application further provides a code generation apparatus, as shown in fig. 25, the apparatus includes:
an analysis statement determination module 2510, configured to analyze the target pseudo code to obtain an analysis statement; the target pseudo code is a repair code for repairing the target application;
a target sentence to be converted determining module 2520, configured to determine a target sentence to be converted and a non-conversion sentence based on the parsing sentence;
a target node determining module 2530, configured to determine, based on preset mapping information, a target node corresponding to the target to-be-converted statement; the target node is a node of a target grammatical structure; the preset mapping information represents the mapping relation between the statement to be converted and the node;
a syntax tree construction module 2540, configured to construct a syntax tree based on the target node and a preset node; the target node represents the target sentence to be converted, and the preset node represents the non-conversion sentence; the target node is used for storing the attribute information of the target statement to be converted;
a code generation module 2550, configured to generate executable code based on the syntax tree.
In some embodiments, the parsing statement determination module includes:
and the analysis statement determining unit is used for analyzing the target pseudo code based on the code analyzer to obtain an analysis statement.
In some embodiments, the apparatus may further include:
the lexical analysis code generation module is used for analyzing a preset lexical sequence based on a lexical analysis tool to generate a lexical analysis code;
the grammar analysis code generation module is used for analyzing the statement to be converted based on a grammar analysis tool to generate grammar analysis codes; the sentence to be converted is generated based on the preset lexical sequence;
and a code parser generation module for compiling the lexical analysis code and the syntactic analysis code based on a compiler to generate the code parser.
In some embodiments, the parsing code generation module may include:
a target grammar structure determining unit, configured to determine a target grammar structure of the sentence to be converted;
and the syntactic analysis code generating unit is used for analyzing the sentence to be converted according to the target syntactic structure based on the syntactic analysis tool to generate the syntactic analysis code.
In some embodiments, the apparatus may further include:
a method call function construction module, configured to construct a method call function of each node in the syntax tree, where the method call function is used to determine a call method of each node in the syntax tree;
and the executable code generating module is used for generating executable codes based on the statements represented by the nodes in the syntax tree and the method calling function.
In some embodiments, the executable code includes at least two executable statements, and the apparatus may further include:
the information determining module is used for determining a current executable statement and a corresponding current calling method in the executable code;
a method call function obtaining module, configured to obtain a method call function in the executable code;
a first judging module, configured to judge whether the current calling method exists in the first language object based on the method calling function;
and the first execution module is used for calling the current calling method to execute the executable code if the current calling method exists in the first language object.
In some embodiments, if the current calling method does not exist in the first language object, the apparatus may further include:
a second judging module, configured to judge whether the current calling method exists in a second language object based on the method calling function;
and the second execution module is used for calling the current calling method to execute the executable code if the current calling method exists in the second language object.
In some embodiments, the current calling method includes a parameter calling method and an argument calling method, and the first execution module or the second execution module may include:
a parameter determining unit, configured to determine a parameter of the current executable statement based on the parameter calling method;
an argument determining unit configured to determine an argument of the current executable statement based on the argument calling method;
and the code execution unit is used for executing the executable code based on the parameters and the arguments of the current executable statement.
In some embodiments, the apparatus may further include:
the object pseudo code construction module is used for constructing the object pseudo code based on the object-oriented point call grammar; the structure of the object-oriented point call syntax includes class names, methods and arguments.
The device and method embodiments in the device embodiment described are based on the same inventive concept.
An embodiment of the present application provides a code generation apparatus, which includes a processor and a memory, where at least one instruction or at least one program is stored in the memory, and the at least one instruction or the at least one program is loaded by the processor and executed to implement the code generation method provided in the foregoing method embodiment.
Embodiments of the present application further provide a computer storage medium, which may be disposed in a terminal to store at least one instruction or at least one program for implementing a code generation method in the method embodiments, where the at least one instruction or the at least one program is loaded and executed by the processor to implement the code generation method provided in the method embodiments.
Embodiments of the present application also provide a computer program product or computer program comprising computer instructions stored in a computer readable storage medium. The processor of the computer device reads the computer instructions from the computer readable storage medium, and the processor executes the computer instructions to make the computer device execute to realize the code generation method provided by the method embodiment.
Alternatively, in an embodiment of the present application, the storage medium may be located in at least one network server of a plurality of network servers of a computer network. Optionally, in this embodiment, the storage medium may include, but is not limited to: a U-disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a removable hard disk, a magnetic or optical disk, and other various media capable of storing program codes.
The memory according to the embodiments of the present application may be used to store software programs and modules, and the processor may execute various functional applications and data processing by operating the software programs and modules stored in the memory. The memory can mainly comprise a program storage area and a data storage area, wherein the program storage area can store an operating system, application programs needed by functions and the like; the storage data area may store data created according to use of the apparatus, and the like. Further, the memory may include high speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device. Accordingly, the memory may also include a memory controller to provide the processor access to the memory.
The code generation method provided by the embodiment of the application can be executed in a mobile terminal, a computer terminal, a server or a similar computing device. Taking the example of the code generation method running on a server, fig. 26 is a hardware structure block diagram of the server according to the code generation method provided in the embodiment of the present application. As shown in fig. 26, the server 2600 may have a relatively large difference due to different configurations or performances, and may include one or more Central Processing Units (CPUs) 2610 (the CPU 2610 may include, but is not limited to, a Processing device such as a microprocessor MCU or a programmable logic device FPGA), a memory 2630 for storing data, and one or more storage media 2620 (e.g., one or more mass storage devices) for storing applications 2623 or data 2622. Memory 2630 and storage media 2620 may be transient or persistent storage, among other things. The program stored in the storage medium 2620 may include one or more modules, each of which may include a series of instruction operations in a server. Still further, a central processor 2610 may be provided in communication with storage medium 2620 to perform a sequence of instruction operations on storage medium 2620 on server 2600. The server 2600 may also include one or more power supplies 2660, one or more wired or wireless network interfaces 2650, one or more input-output interfaces 2640, and/or one or more operating systems 2621, such as Windows Server, mac OS XTM, unixTM, linuxTM, freeBSDTM, etc.
The input/output interface 2640 may be used to receive or transmit data via a network. Specific examples of such networks may include wireless networks provided by the communications provider of server 2600. In one example, the input/output Interface 2640 includes a Network adapter (NIC) that can be connected to other Network devices through a base station to communicate with the internet. In one example, the input/output interface 2640 may be a Radio Frequency (RF) module, which is used for communicating with the internet in a wireless manner.
It will be understood by those skilled in the art that the structure shown in fig. 26 is only an example, and does not limit the structure of the electronic device. For example, server 2600 may also include more or fewer components than shown in fig. 26, or have a different configuration than shown in fig. 26.
As can be seen from the embodiments of the code generation method, apparatus, device, or storage medium provided in the present application, the present application parses a target pseudo code to obtain a parsing statement; determining a target sentence to be converted and a non-conversion sentence based on the analysis sentence; therefore, in the process of converting the pseudo code into the executable code, the pseudo code is firstly analyzed into different sentences, and then the sentences are divided into two types, wherein one type is the sentences to be converted which need to be converted, and the other type is the sentences which do not need to be converted; according to the preset mapping relation, a target node of a target grammatical structure corresponding to the target sentence to be converted is determined, and the grammatical structure of the sentence to be converted is simplified; constructing a syntax tree according to the target node, thereby generating an executable code; in the code conversion process, only part of sentences need to be converted, so that the syntax tree structure constructed based on the nodes is simple, the executable code can be quickly generated, the pseudo code is converted and translated to be changed into the executable code, the same function is completed by using less code amount, and the coding efficiency and the readability of the code are improved.
It should be noted that: the sequence of the embodiments of the present application is only for description, and does not represent the advantages and disadvantages of the embodiments. And specific embodiments thereof have been described above. Other embodiments are within the scope of the following claims. In some cases, the actions or steps recited in the claims may be performed in a different order than in the embodiments and still achieve desirable results. In addition, the processes depicted in the accompanying figures do not necessarily require the particular order shown, or sequential order, to achieve desirable results. In some embodiments, multitasking and parallel processing may also be possible or may be advantageous.
The embodiments in the present specification are described in a progressive manner, and the same and similar parts among the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, as for the apparatus, device, and storage medium embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and reference may be made to some descriptions of the method embodiments for relevant points.
It will be understood by those skilled in the art that all or part of the steps for implementing the above embodiments may be implemented by hardware, or may be implemented by a program instructing relevant hardware, where the program may be stored in a computer storage medium, and the above storage medium may be a read-only memory, a magnetic disk, an optical disk, or the like.
The above description is only exemplary of the present application and should not be taken as limiting the present application, as any modification, equivalent replacement, or improvement made within the spirit and principle of the present application should be included in the protection scope of the present application.

Claims (11)

1. A method of code generation, the method comprising:
analyzing the target pseudo code to obtain an analysis statement; the target pseudo code is a repair code for repairing a target application;
determining a target sentence to be converted and a non-conversion sentence based on the analysis sentence;
determining a target node corresponding to the target sentence to be converted based on preset mapping information; the target node is a node of a target grammatical structure; the preset mapping information represents the mapping relation between the statement to be converted and the node;
constructing a syntax tree based on the target node and a preset node; the target node represents the target sentence to be converted, and the preset node represents the non-conversion sentence; the target node is used for storing the attribute information of the target statement to be converted;
based on the syntax tree, executable code is generated.
2. The method of claim 1, wherein parsing the target pseudocode to obtain a parsed statement comprises:
analyzing the target pseudo code based on a code analyzer to obtain an analysis statement;
the generation method of the code parser comprises the following steps:
analyzing a preset lexical sequence based on a lexical analysis tool to generate a lexical analysis code;
analyzing the statement to be converted based on a syntactic analysis tool to generate a syntactic analysis code; the sentence to be converted is generated based on the preset lexical sequence;
compiling the lexical analysis code and the syntactic analysis code based on a compiler to generate the code parser.
3. The method of claim 2, wherein the parsing the to-be-converted statement based on the parsing tool to generate the parsing code comprises:
determining a target grammar structure of the statement to be converted;
and analyzing the statement to be converted according to the target grammatical structure based on the grammatical analysis tool to generate the grammatical analysis code.
4. The method of claim 1, wherein prior to generating executable code based on the syntax tree, the method further comprises:
constructing a method calling function of each node in the syntax tree, wherein the method calling function is used for determining a calling method of each node in the syntax tree;
and generating executable codes based on the statements characterized by the nodes in the syntax tree and the method calling functions.
5. The method of any of claims 1-4, wherein the executable code comprises at least two executable statements, and wherein after generating the executable code based on the syntax tree, the method further comprises:
determining a current executable statement and a corresponding current calling method in the executable code;
acquiring a method calling function in the executable code;
judging whether the current calling method exists in the first language object or not based on the method calling function;
and if so, calling the current calling method to execute the executable code.
6. The method of claim 5, wherein if the currently called method is not present in the first language object, the method further comprises:
judging whether the current calling method exists in a second language object or not based on the method calling function;
and if so, calling the current calling method to execute the executable code.
7. The method of claim 6, wherein the current calling method comprises a argument calling method and an argument calling method, and wherein said calling the current calling method to execute the executable code comprises:
determining the form parameter of the current executable statement based on the form parameter calling method;
determining the parameters of the current executable statement based on the parameter calling method;
executing the executable code based on the arguments and arguments of the current executable statement.
8. The method according to any one of claims 1-4, further comprising:
constructing the target pseudo code based on an object-oriented point call grammar; the structure of the object-oriented point call syntax includes class names, methods and arguments.
9. An apparatus for generating code, the apparatus comprising:
the analysis statement determination module is used for analyzing the target pseudo code to obtain an analysis statement; the target pseudo code is a repair code for repairing a target application;
the statement determining module is used for determining a target statement to be converted and a non-conversion statement based on the analysis statement;
the target node determining module is used for determining a target node corresponding to the target sentence to be converted based on preset mapping information; the target node is a node of a target grammatical structure; the preset mapping information represents the mapping relation between the statement to be converted and the node;
the syntax tree construction module is used for constructing a syntax tree based on the target node and a preset node; the target node represents the target sentence to be converted, and the preset node represents the non-conversion sentence; the target node is used for storing the attribute information of the target statement to be converted;
and the code generation module is used for generating executable codes based on the syntax tree.
10. A computer storage medium having stored therein at least one instruction or at least one program, the at least one instruction or the at least one program being loaded and executed by a processor to implement the code generation method of any one of claims 1 to 8.
11. A computer program product comprising computer instructions, wherein the computer instructions, when executed by a processor, implement the code generation method of any of claims 1-8.
CN202111134619.8A 2021-09-27 2021-09-27 Code generation method, device and storage medium Pending CN115878120A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111134619.8A CN115878120A (en) 2021-09-27 2021-09-27 Code generation method, device and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111134619.8A CN115878120A (en) 2021-09-27 2021-09-27 Code generation method, device and storage medium

Publications (1)

Publication Number Publication Date
CN115878120A true CN115878120A (en) 2023-03-31

Family

ID=85762884

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111134619.8A Pending CN115878120A (en) 2021-09-27 2021-09-27 Code generation method, device and storage medium

Country Status (1)

Country Link
CN (1) CN115878120A (en)

Similar Documents

Publication Publication Date Title
US20070050707A1 (en) Enablement of multiple schema management and versioning for application-specific xml parsers
CN112104709A (en) Intelligent contract processing method, device, medium and electronic equipment
CN113515278A (en) Low code model processing method, system, electronic device and storage medium
CN115509514B (en) Front-end data simulation method, device, equipment and medium
CN108595334B (en) Method and device for calculating dynamic slices of Java program and readable storage medium
CN112379917A (en) Browser compatibility improving method, device, equipment and storage medium
CN112667287A (en) Method, device and equipment for creating rule file and computer readable storage medium
CN114327477A (en) Intelligent contract execution method and device, electronic device and storage medium
EP0520708B1 (en) Method and apparatus for converting high level form abstract syntaxes into an intermediate form
CN112130830A (en) Interface generation method and device and electronic equipment
CN111767096A (en) Interface document generation method, device, equipment and computer readable storage medium
CN114443041A (en) Method for parsing abstract syntax tree and computer program product
CN112269566B (en) Script generation processing method, device, equipment and system
CN112988163A (en) Intelligent programming language adaptation method and device, electronic equipment and medium
CN114840195B (en) Privatization method for iOS SDK static library
CN107885604B (en) Method and device for communication between heterogeneous systems, computer equipment and storage medium
CN115878120A (en) Code generation method, device and storage medium
US20070050706A1 (en) Method of xml transformation and presentation utilizing an application-specific parser
CN112579093B (en) Information pushing method and device and related equipment
CN115292058A (en) Service scene level service topology generation method and device and electronic equipment
CN114816420A (en) Data processing method and device
CN110489124B (en) Source code execution method, source code execution device, storage medium and computer equipment
CN113448852A (en) Test case obtaining method and device, electronic equipment and storage medium
CN112650502A (en) Batch processing task processing method and device, computer equipment and storage medium
CN110737431A (en) Software development method, development platform, terminal device and storage medium

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