CN114879964A - Code generation method and device - Google Patents

Code generation method and device Download PDF

Info

Publication number
CN114879964A
CN114879964A CN202210324919.0A CN202210324919A CN114879964A CN 114879964 A CN114879964 A CN 114879964A CN 202210324919 A CN202210324919 A CN 202210324919A CN 114879964 A CN114879964 A CN 114879964A
Authority
CN
China
Prior art keywords
code
character string
target
template
belongs
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
CN202210324919.0A
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.)
Hangzhou Hikvision Digital Technology Co Ltd
Original Assignee
Hangzhou Hikvision Digital 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 Hangzhou Hikvision Digital Technology Co Ltd filed Critical Hangzhou Hikvision Digital Technology Co Ltd
Priority to CN202210324919.0A priority Critical patent/CN114879964A/en
Publication of CN114879964A publication Critical patent/CN114879964A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • 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
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis

Landscapes

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

Abstract

The embodiment of the invention provides a code generation method and a code generation device, which are applied to the technical field of data processing. The method comprises the following steps: acquiring a template code aiming at a target programming language; the target programming language is a programming language to which a code to be generated belongs; carrying out character string recognition on the template code to obtain a plurality of character strings and the character string category to which each character string belongs; performing code conversion on each character string in the template code based on a conversion strategy corresponding to the character string category to which each character string in the plurality of character strings belongs to obtain an intermediate code; calling a target interpreter to run an intermediate code so as to obtain a target code output by the target interpreter; wherein, the target interpreter is an interpreter of a writing language to which the executable intermediate code belongs. According to the scheme, the complexity of the code generation scheme construction can be simplified, and the code generation applicability is improved.

Description

Code generation method and device
Technical Field
The present invention relates to the field of data processing technologies, and in particular, to a code generation method and apparatus.
Background
In the existing code generation technology, a template engine is often needed, specifically, a developed template engine is used to replace a marked character string in a written template code, so as to render the code content in batch.
In order to implement the rendering function of complex logic codes such as loops, condition judgments, functions, macros and the like, in the related art, the template syntax of the template engine is often limited, so that the code generation technology based on the template engine only supports the generation of codes of a single programming language, and needs to implement corresponding code functions with more complex logic, and the applicability is poor.
Disclosure of Invention
The embodiment of the invention aims to provide a code generation method and a code generation device so as to simplify the complexity of the construction of a code generation scheme and improve the applicability of code generation. The specific technical scheme is as follows:
in a first aspect, an embodiment of the present invention provides a code generation method, where the method includes:
acquiring a template code aiming at a target programming language; the target programming language is a programming language to which a code to be generated belongs;
carrying out character string recognition on the template code to obtain a plurality of character strings and the character string category to which each character string belongs;
performing code conversion on each character string in the template code based on a conversion strategy corresponding to the character string category to which each character string in the plurality of character strings belongs to obtain an intermediate code;
calling a target interpreter to run the intermediate code to obtain a target code output by the target interpreter; wherein the target interpreter is an interpreter capable of executing a writing language to which the intermediate code belongs.
Optionally, the performing character string recognition on the template code to obtain a plurality of character strings and a character string category to which each character string belongs includes:
identifying at least one set of start-stop identifiers in the template code; wherein each set of start-stop identifiers comprises a pair of start identifiers and end identifiers;
taking the character strings positioned between each group of start-stop identifiers as first character strings, and determining the appointed category to which each first character string belongs based on the corresponding relation between the preset start-stop identifiers and the appointed categories; the specified type is a character string type except for a common type in all character string types, and the encoding language of the character string belonging to the common type is the same as that of the code required to be generated;
and taking character strings which do not belong to any group of start-stop identifiers as second character strings, and determining the character string category to which each second character string belongs as the common category.
Optionally, the identifying at least one set of start-stop identifiers in the template code includes:
determining whether each line of code text in the template code contains at least one pair of a start identifier and an end identifier; if so, the determined pair of start and stop designators is determined as a set of start and stop designators.
Optionally, the determining whether the line of code text includes at least one pair of a start identifier and an end identifier includes:
searching the line of code text for a start identifier and an end identifier;
and if at least one pair of start identifier and end identifier is searched, determining that the line code text contains at least one pair of start identifier and end identifier, and otherwise, determining that the line code text does not contain at least one pair of start identifier and end identifier.
Optionally, the method further includes:
if the number of the searched start designators does not match the number of the searched end designators, information indicating an error is output.
Optionally, the performing code conversion on each character string in the template code based on the conversion policy corresponding to the character string category to which each character string in the plurality of character strings belongs includes:
for each of the plurality of character strings, transcoding the character string in a manner comprising:
reading the character string from the template code;
if the character string category to which the character string belongs is a grammar category, compiling the character string into a command code according to a compiling rule corresponding to the grammar category to which the character string belongs, and writing the command code into a target code file; the target code file is used for recording a code to be generated;
if the character string type to which the character string belongs is a change type, writing the character string as a variable into the target code file;
and if the character string type of the character string belongs to the common type, writing the character string into the target code file as a code text.
Optionally, the writing the character string into a command code according to a writing rule corresponding to a syntax category to which the character string belongs includes:
if the grammar type is a type irrelevant to the code block identification, the character string is used as a command code;
if the grammar type is the type related to the code block identification, according to the code block identification operation corresponding to the grammar type to which the character string belongs, executing the specified operation aiming at the code block identification at the specified position of the character string to obtain a command code; wherein the specified position comprises a position before or after the character string, and the specified operation comprises adding a code block identifier or deleting the code block identifier.
Optionally, the target interpreter executes the intermediate code, including:
reading the configuration values of all replaceable variables in the pre-configured intermediate code;
and replacing each replaceable variable in the intermediate code by using each read configuration value, and running the replaced intermediate code.
Optionally, the method further includes:
establishing a code correspondence between first code texts in the template codes and second code texts in the intermediate codes, wherein the second code texts corresponding to each first code text are: code text generated based on the first code text;
if the target interpreter has an error in operation, determining a second code text which causes the target interpreter to have an error in operation;
determining a first code text corresponding to the determined second code text as an error code text based on the code correspondence;
and generating information for prompting the error of the error code text.
In a second aspect, an embodiment of the present invention provides a code generation apparatus, where the apparatus includes:
the code acquisition module is used for acquiring a template code aiming at the target programming language; the target programming language is a programming language to which a code to be generated belongs;
the character string recognition module is used for carrying out character string recognition on the template code to obtain a plurality of character strings and the character string category to which each character string belongs;
the code conversion module is used for carrying out code conversion on each character string in the template code based on a conversion strategy corresponding to the character string category to which each character string in the plurality of character strings belongs to obtain an intermediate code;
the code running module is used for calling a target interpreter to run the intermediate code so as to obtain a target code output by the target interpreter; wherein the target interpreter is an interpreter capable of executing a writing language to which the intermediate code belongs.
Optionally, the character string recognition module is configured to include:
an identifier identification submodule for identifying at least one set of start-stop identifiers in the template code; wherein each set of start-stop identifiers comprises a pair of start identifiers and end identifiers;
the first determining submodule is used for taking the character strings positioned between each group of start-stop identifiers as first character strings and determining the appointed category to which each first character string belongs based on the corresponding relation between the preset start-stop identifiers and the appointed categories; the specified type is a character string type except for a common type in all character string types, and the encoding language of the character string belonging to the common type is the same as that of the code required to be generated;
and the second determining submodule is used for taking the character strings which do not belong to any group of start-stop identifiers as second character strings and determining the character string category to which each second character string belongs as the common category.
Optionally, the identifier identification submodule is specifically configured to determine, for each line of code text in the template code, whether the line of code text includes at least one pair of a start identifier and an end identifier; if so, the determined pair of start and stop designators is determined as a set of start and stop designators.
Optionally, the identifier identification submodule is specifically configured to search the line of code text for a start identifier and a stop identifier; and if at least one pair of start identifier and end identifier is searched, determining that the line code text contains at least one pair of start identifier and end identifier, and otherwise, determining that the line code text does not contain at least one pair of start identifier and end identifier.
Optionally, the apparatus further comprises:
and the first information output module is used for outputting information indicating errors if the number of the searched start identifiers is not matched with the number of the searched end identifiers.
Optionally, the code conversion module is specifically configured to, for each of the character strings in the plurality of character strings, code convert the character string in the following manner: reading the character string from the template code; if the character string category to which the character string belongs is a grammar category, compiling the character string into a command code according to a compiling rule corresponding to the grammar category to which the character string belongs, and writing the command code into a target code file; the target code file is used for recording a code to be generated; if the character string type to which the character string belongs is a change type, writing the character string as a variable into the target code file; and if the character string type of the character string belongs to the common type, writing the character string into the target code file as a code text.
Optionally, the code conversion module is specifically configured to, if the syntax type is a type unrelated to the code block identifier, use the character string as a command code; if the grammar type is the type related to the code block identification, according to the code block identification operation corresponding to the grammar type to which the character string belongs, executing the specified operation aiming at the code block identification at the specified position of the character string to obtain a command code; wherein the specified position comprises a position before or after the character string, and the specified operation comprises adding a code block identifier or deleting the code block identifier.
Optionally, the target interpreter includes: the code running module is used for reading the configuration values of all replaceable variables in the pre-configured intermediate code; and replacing each replaceable variable in the intermediate code by using each read configuration value, and running the replaced intermediate code.
Optionally, the apparatus further comprises:
a second information output module, configured to establish a code correspondence between first code texts in the template codes and second code texts in the intermediate codes, where the second code text corresponding to each first code text is: a code text generated based on the first code text; if the target interpreter has an error in operation, determining a second code text which causes the target interpreter to have an error in operation; determining a first code text corresponding to the determined second code text as an error code text based on the code correspondence; and generating information for prompting the error of the error code text.
In a third aspect, an embodiment of the present invention provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, where the processor and the communication interface complete communication between the memory and the processor through the communication bus;
a memory for storing a computer program;
a processor for implementing the method steps of any of the first aspect when executing a program stored in the memory.
In a fourth aspect, the present invention provides a computer-readable storage medium, in which a computer program is stored, and the computer program, when executed by a processor, implements the method steps of any one of the first aspect.
The embodiment of the invention has the following beneficial effects:
in the code generation method provided by the embodiment of the invention, a template code for a target programming language can be acquired, character string recognition is performed on the template code to obtain a plurality of character strings and a character string category to which each character string belongs, and then code conversion is performed on each character string in the template code to obtain an intermediate code based on a conversion strategy corresponding to the character string category to which each character string belongs in the plurality of character strings; and calling the target interpreter to run the intermediate code so as to obtain the target code output by the target interpreter. Because the template code can be converted into the intermediate code after the template code aiming at the target programming language is obtained, and then the target interpreter aiming at the programming language of the intermediate code is called to run the intermediate code to obtain the target code, compared with the related technology, the method does not need to realize the complex logic codes such as circulation, condition judgment, functions, macros and the like any more, obtains the template codes aiming at different programming languages, and then the interpreter can generate different programming language codes, thereby simplifying the complexity of the construction of the code generation scheme and improving the applicability of code generation.
Of course, not all of the advantages described above need to be achieved at the same time in the practice of any one product or method of the invention.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art that other embodiments can be obtained by using the drawings without creative efforts.
FIG. 1 is a flow chart of a code generation method provided by an embodiment of the invention;
FIG. 2 is a diagram illustrating a transcoding process according to an embodiment of the present invention;
FIG. 3 is another flow chart of a code generation method according to an embodiment of the present invention;
FIG. 4 is a flowchart of a code generation method according to an embodiment of the present invention;
FIG. 5 is a block diagram of a code generation method provided by an embodiment of the present invention;
fig. 6 is a schematic structural diagram of a code generation apparatus according to an embodiment of the present invention;
fig. 7 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, 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 invention.
A code generation technique is a technique for generating a code. For example, when images need to be rendered in different manners, if a set of code needs to be developed for each manner of rendering images, the efficiency is low. Through the code generation technology, developers only need to develop one set of codes, and the codes aiming at each rendering image mode can be obtained.
In the related art, code generation is mostly implemented by means of a template engine, wherein the template engine accepts a tokenized character string and generates a rendered character string with values instead of tokens as output. When the codes need to be generated, a developer can write template codes first, and then the template codes are processed by using a template engine so as to render target codes.
In order to realize the rendering functions of complex logic codes such as loops, condition judgment, functions, macros and the like, the development cost of the template engine is high, and in the related technology, the template grammar of the template engine is often limited, so that the code generation technology based on the template engine only supports the generation of codes of a single programming language, such as an HTML language, and the applicability of code generation is poor.
In order to solve technical problems in the related art, embodiments of the present invention provide a code generation method and apparatus. It is emphasized that embodiments of the present invention can be applied to a template engine.
The code generation method provided by the embodiment of the invention can comprise the following steps:
acquiring a template code to acquire the template code aiming at the target programming language; the target programming language is a programming language to which a code to be generated belongs;
carrying out character string recognition on the template code to obtain a plurality of character strings and the character string category to which each character string belongs;
performing code conversion on each character string in the template code based on a conversion strategy corresponding to the character string category to which each character string in the plurality of character strings belongs to obtain an intermediate code;
calling a target interpreter to run an intermediate code so as to obtain a target code output by the target interpreter; wherein, the target interpreter is an interpreter of a writing language to which the executable intermediate code belongs.
The code generation method of the embodiment of the invention can convert the template code into the intermediate code after acquiring the template code aiming at the target programming language, and then call the target interpreter aiming at the programming language of the intermediate code to run the intermediate code to obtain the target code.
Furthermore, any interpreter can be used as a tool support, any lexical and grammatical analysis module is not required to be constructed in the whole scheme, the generation of complex logic codes can be realized only by depending on the self capability of the interpreter, and the generation capability is realized for any kind of codes. Meanwhile, the realization of the interpreter is not limited by the scheme, a special grammar does not need to be specially constructed, and a user can construct any universal template engine according to the required language and the interpreter, so that the complexity of constructing the code generation scheme is simplified, and the development cost of the template engine adopting the scheme is reduced.
A code generation method according to an embodiment of the present invention is described below with reference to the drawings.
As shown in fig. 1, a code generation method provided in an embodiment of the present invention may include the following steps:
s101, acquiring a template code aiming at a target programming language; the target programming language is a programming language to which a code to be generated belongs;
the template code may be a pre-written code for describing the rules for code generation and the code text to be utilized for generating the code. And the template code for the target programming language is: the code text which is contained and is required to be utilized for generating the code is the code text written by utilizing the target programming language. Optionally, the template code includes a character string of a grammar category and a character string of a general category, where the character string of the grammar category is used to describe rules for code generation, and the character string of the general category is a code text that needs to be utilized to generate the code.
Illustratively, one template code is:
line 1: {% for i in range ({% num%% }):% }
Line 2: if (0 ═ strcmp (ld- > type, "{%% layer _ name _ list [ i ]% }))
Line 3: {
Line 4: hr ═ CUSTOM _ {% layer _ name _ list [ i ]. upper ()% } _ GetModelMemsize (superparams, param _ blobs, ld, mem _ tab);
line 5: }
Line 6: {% end% }
Where lines 1 and 6 are used to describe the rules of code generation and lines 2 through 5 are the code text that needs to be utilized to generate the code.
S102, carrying out character string recognition on the template code to obtain a plurality of character strings and a character string category to which each character string belongs;
wherein, the character string categories include but are not limited to: a grammar class, a replacement class, and a normal class. The character strings of the grammar category are used for describing the processing logic of code generation, the character strings of the replacement category are replaceable variables in the template codes, and the character strings of the common type are code texts which are required to be utilized when the codes are generated in the template codes.
After the template code for the target programming language is obtained, character string recognition may be performed on the template code to obtain a plurality of character strings and a character string category to which each character string belongs. Illustratively, in the above template code example, for i in range (num) is a character string of a syntax category, if (0) is strcmp (ld- > type, "is a character string of a normal category, and layer _ name _ list [ i ] is a character string of an alternative category.
Optionally, after performing character string recognition on the template code, a character string information table of the template code may be established, in which each character string in the template code, the character string category of each character string, and the position of each character string in the template code, such as the several rows, several character strings, etc., are recorded, so as to facilitate subsequent code conversion on the template code.
S103, carrying out code conversion on each character string in the template code based on a conversion strategy corresponding to the character string category to which each character string in the plurality of character strings belongs to obtain an intermediate code;
the intermediate code may be a code of a high-level programming language such as Python language. In order to obtain the intermediate code, each character string in the template code needs to be transcoded.
Different character string types in the template code correspond to different conversion strategies, wherein the corresponding relation between the character string types and the conversion strategies can be preset. Optionally, after obtaining each character string in the template code, for each character string in the plurality of character strings, a conversion policy corresponding to the character string category of the character string may be determined, and then the character string may be converted according to the conversion policy.
The character strings of different character string types need to be converted by different conversion strategies, and at this time, the character strings can be transcoded by adopting the following modes for each character string, including the steps A1-A4:
step A1: reading the character string from the template code;
when the intermediate code needs to be generated, the character string can be read from the template code. Optionally, the reading may be performed from left to right according to the sequence of the code line where the character string in the template code is located, and of course, the reading may also be performed sequentially according to other agreed sequences. When character string recognition is performed on the template code, a character string table of the template code is established, and reading the character string from the template code can be understood as reading each character string from the character string table corresponding to the template code.
For each character string, after the character string is read from the template code, the character string category of the character string can be further determined, so as to prepare for the subsequent conversion of the character string.
Step A2: if the character string category to which the character string belongs is a grammar category, compiling the character string into a command code according to a compiling rule corresponding to the grammar category to which the character string belongs, and writing the command code into a target code file; the target code file is used for recording a code to be generated;
wherein a grammar category may include a plurality of grammar subcategories, the subcategories of different programming languages may be different, for example, for Python language, a grammar subcategory may include at least one of:
1: CMD _ TAB _ PLUS: the commands requiring the addition of a tab to the next line in Python mainly include 'class instruction', 'def instruction', 'for instruction', 'if instruction', 'try instruction', 'while instruction', and 'with instruction'.
Wherein, tab is a start-stop identifier in Python language. When at least one of class, def, for, if, try, while and with is included in the character string belonging to the grammar class, the sub-class of the character string is CMD _ TAB _ PLUS.
2: CMD _ TAB _ MINUS: and a self-defined code block end command 'end' is used for matching with the TE _ CMD _ TAB _ PLUS command, and a code block between CMD _ TAB _ PLUS and CMD _ TAB _ MINUS is a logic code block.
3: CMD _ TAB _ MINUS _ PLUS: the commands for Python to reduce one tab compared to the previous row and require the next row to increase one tab mainly include 'elif instruction', 'else instruction', 'except instruction', and 'finally instruction'. When at least one of the elif, else, except and finally is included in the character string belonging to the grammar category, the sub-category of the character string is CMD _ TAB _ MINUS _ PLUS.
4: CMD: the instructions without changing the alignment tab in Python mainly include 'assert instruction', 'break instruction', 'connect instruction', 'del instruction', 'from instruction', 'global instruction', 'import instruction', 'lambda instruction', 'normal instruction', 'pass instruction', 'rain instruction', and 'return instruction'.
When at least one of alert, break, continue, del, from, global, import, lambda, nonlocal, pass, rain, and return is included in the string belonging to the grammar class, then the sub-class of the string is CMD.
5: CUSTOM _ CMD: and the custom command is used for generating a specific custom code.
6: CMD _ ASSIGNMENT: and evaluating the expression. And the command belongs to a conventional command with CMD.
Wherein different grammar subcategories require different writing rules.
And if the grammar type is a type irrelevant to the code block identification, the character string is used as a command code. Wherein, the code block identification refers to identification used for dividing the code block in the programming language, for example, for C language, the code block identification includes "{" and "}", and for Python language, the code block identification can be marked with tab.
For the Python language, CMD, CUSTOM _ CMD, and CMD _ associated belong to a category unrelated to the code block identifier, and the conversion is performed by directly using character strings corresponding to CMD, CUSTOM _ CMD, and CMD _ associated as command codes.
And if the grammar type is the type related to the code block identification, according to the code block identification operation corresponding to the grammar type to which the character string belongs, executing the specified operation aiming at the code block identification at the specified position of the character string to obtain the command code.
Wherein the designated position comprises a position before or after the character string, and the designating operation comprises adding a code block identifier or deleting the code block identifier. For example, if the designated position is a character string and the designated operation is adding the code block identifier, then the code block identifier is added at a position after the character string, and for the Python language, the tab operation is added.
The designated positions and designated operations corresponding to different grammar subcategories may be different, and therefore, the designated positions and designated operations may be determined based on the specific grammar subcategories. For example, in Python language, CMD _ TAB _ PLUS, CMD _ TAB _ MINUS, and CMD _ TAB _ MINUS _ PLUS belong to the category associated with code block identification.
For the CMD _ TAB _ PLUS subclass, a TAB operation is added, that is, after the string code corresponding to the subclass is written into the target code file, 1 TAB mark is added.
For the CMD _ TAB _ minor _ PLUS sub-category, 1 TAB mark is reduced before the character string code corresponding to the sub-category is written into the target code file, and 1 TAB mark is increased finally after the character string code corresponding to the sub-category is written into the target code file.
For the CMD _ TAB _ MINUS sub-category, a TAB flag needs to be reduced in the object code file.
Step A3: if the character string category to which the character string belongs is a change category, writing the character string as a replaceable variable into a target code file;
if the character string type to which the character string belongs is a change type, the character string can be written into the target code file as a replaceable variable. The replaceable variable may be replaced with a configuration value during subsequent execution of the intermediate code.
For example, in the template code, if the character string layer _ name _ list [ i ] is a character string of a change type, the character string layer _ name _ list [ i ] is written into the target code file as a variable.
Step A4: and if the character string type of the character string belongs to the common type, writing the character string into a target code file as a code text.
If the character string type to which the character string belongs is a change type, the character string may be directly used as a code text.
Optionally, in order to describe the transcoding process more clearly, as shown in fig. 2, a schematic diagram of the transcoding process in the code generation method is provided for the embodiment of the present invention.
When the character string type of the character string is a common type, directly writing the character string as a code text into a target code file;
when the character string category of the character string is a change category, writing the character string as a replaceable variable into a target code file;
when the character string type of the character string is a CUSTOM _ CMD subcategory, writing the character string into a target code file as a self-defined command code;
when the character string type of the character string is CMD or CMD _ ASSIGNMENT subcategory, writing the character string as a command code into a target code file;
and when the character string type of the character string is CMD _ TAB _ PLUS subcategory, writing the character string as a command code into a target code file, and adding a Tab identifier and a grammar identifier + 1.
When the character string type of the character string is CMD _ TAB _ MINUS _ PLUS subcategory, a Tab mark is deleted firstly, then the character string is used as a command code and written into a target code file, and finally a Tab mark is added.
When the character string type of the character string is CMD _ TAB _ MINUS sub-type, a Tab mark is deleted firstly, and a grammar mark is marked as-1.
Since the command code of the CMD _ TAB _ PLUS sub-category and the command code of the CMD _ TAB _ MINUS sub-category always exist in pairs, when only a single command code of the CMD _ TAB _ PLUS sub-category or a command code of the CMD _ TAB _ MINUS sub-category is contained in the codes, an error will be reported when the codes are running, and therefore, whether a single command code of the CMD _ TAB _ PLUS sub-category or a command code of the CMD _ TAB _ MINUS sub-category exists is determined by adding a syntax flag. After the conversion of each character string in the template code is completed, if the syntax identifier is not 0, it indicates that the intermediate code in the target code file has a single command code of the CMD _ TAB _ PLUS sub-category or a command code of the CMD _ T AB _ MINUS sub-category, and at this time, it may be determined that the intermediate code has a problem, and an error prompt may be reported.
S104, calling the target interpreter to run the intermediate code so as to obtain the target code output by the target interpreter; wherein, the target interpreter is an interpreter of a writing language to which the executable intermediate code belongs.
When the intermediate code is Python language, the target interpreter is one capable of executing Python language.
Optionally, the target interpreter may read configuration values of the replaceable variables in the pre-configured intermediate code, replace the replaceable variables in the intermediate code with the read configuration values, and run the replaced intermediate code.
Illustratively, the template code 1 is taken as an example for explanation, and each replaceable variable in the intermediate code comprises: num and layer _ name _ list [ i ], the template code parameters may be: num is 2 and layer _ name _ list [ "ttt1", "ttt2" ].
When the target interpreter needs to execute the central code, it can read num of 2 and layer _ name _ list of [ "ttt1", "ttt2" ] from the template parameters, then replace num in the intermediate code with 2, replace the value of layer _ name _ list [ i ] with ttt1 in the first loop, and replace the value of layer _ name _ list [ i ] with ttt2 in the second loop.
The object code 1 can thus be obtained:
line 1: if (0 ═ strcmp (ld- > type, "ttt1"))
Line 2: {
Line 3: hr — CUSTOM _ TTT1 — getmodememsize (superparams, param _ blobs, ld, mem _ tab);
line 4: }
And, object code 2:
line 1: if (0 ═ strcmp (ld- > type, "ttt2"))
Line 2: {
Line 3: hr — CUSTOM _ TTT2 — getmodememsize (superparams, param _ blobs, ld, mem _ tab);
line 4: }
According to the code generation method, after the template code aiming at the target programming language is obtained, the template code can be converted into the intermediate code, the target interpreter aiming at the programming language of the intermediate code is called, the intermediate code is operated, and the target code is obtained.
On the basis of the embodiment shown in fig. 1, as shown in fig. 3, an embodiment of the present invention further provides another code generation method, where the performing character string recognition on the template code to obtain a plurality of character strings and a character string category to which each character string belongs may include steps S301 to S303:
s301, identifying at least one group of start-stop identifiers in the template codes; wherein each set of start-stop identifiers comprises a pair of start identifiers and end identifiers;
the start-stop flag may be predetermined, such as "{%" and "% }". There may be a plurality of start-stop identifiers, with different start-stop identifiers identifying different character string categories. Illustratively, the start-stop designators include: "{%" and "% }", "{%", and "% }". "{%" and "% }" are used to identify syntax categories, and "{%" and "% }" are used to identify alteration categories.
In one implementation, the code text may be processed line by line to determine whether each line of code text contains a set of start-stop identifiers. At this time, it may be determined whether at least one pair of a start identifier and an end identifier is included in each line of code text in the template code. If at least one pair of start and end identifiers is included, the determined pair of start and end identifiers is determined as a set of start and end identifiers.
The line code text is determined to contain at least one pair of start identifier and end identifier, the line code text can be determined to contain at least one pair of start identifier and end identifier by determining the search of the start identifier and the end identifier, namely, the line code text is searched for the start identifier and the end identifier, if at least one pair of start identifier and end identifier is searched, the line code text is determined to contain at least one pair of start identifier and end identifier, otherwise, the line code text is determined not to contain at least one pair of start identifier and end identifier.
The above-mentioned searching for the start identifier and the end identifier for the line of code text may be performed by first constructing a regular expression of the start-end identifier, and then performing a search for the marking pattern by using a regular processing library. If the pattern does not exist, null is returned. And if the mode exists, counting the starting identifier and the ending identifier again, wherein if the mode exists, the number of the marks is increased by 1 when the mode is the starting identifier, and the number of the marks is decreased by 1 when the mode is the ending identifier. After the statistics are over, the pattern search may be performed again until all bytes in the row are searched. Optionally, finally, whether the start identifier and the end identifier are matched is judged according to whether the number of the marks is 0, if so, the positions of the start identifier and the end identifier are returned, otherwise, the exception that the marks are not matched is thrown out.
S302, using the character strings between each group of start-stop identifiers as first character strings, and determining the appointed category to which each first character string belongs based on the corresponding relation between the preset start-stop identifiers and the appointed categories;
the designated type is a character string type except for the common type in each character string type, and the encoding language of the character string belonging to the common type is the same as that of the code required to be generated. The designated category includes a grammar category and a change category.
As can be seen from the foregoing, the start-stop identifier and the character string type may be in a one-to-one correspondence relationship, different start-stop identifiers are used for character strings of different character string types, and after the first character string is determined, the syntax type corresponding to the start-stop identifier corresponding to the first character string may be used as the designated type to which the first character string belongs.
S303, using the character strings which do not belong to any group of start-stop identifiers as second character strings, and determining the character string category to which each second character string belongs as a common category.
In this step, a character string that does not belong to any group of start-stop identifiers may be used as a second character string, and the character string class to which each second character string belongs may be determined to be a common class.
The code generation method of the embodiment of the invention can simplify the complexity of the construction of the code generation scheme, improve the applicability of code generation and reduce the development cost of the template engine adopting the scheme. Furthermore, character string recognition is carried out on the template codes through the start-stop identifier, and a realization basis is provided for improving the applicability of code generation and reducing the development cost of the template engine adopting the scheme.
In one embodiment, based on the embodiment shown in fig. 3, if the number of searched start markers does not match the number of searched end markers, information indicating an error is output.
If the number of the searched start identifiers does not match the number of the searched end identifiers, it indicates that there is a single start identifier or end identifier, and a pair of start-end identifiers cannot be determined, and it indicates that there is an error in the template code, and at this time, information indicating the error may be output.
Optionally, in an embodiment, in order to improve the accuracy of code generation, a code correspondence between a first code text in the template code and a second code text in the intermediate code may be established.
Wherein, the second code text corresponding to each first code text is: a code text generated based on the first code text. The first code text is any code text in the template code. The code correspondence may be a row-column correspondence between the template code and the intermediate code. When the intermediate code compiling and the intermediate code executing are executed, the error information and the error position of the intermediate code can be obtained according to the error returned by the capturing interpreter, and then the error information and the error position of the template code can be obtained through the corresponding relation.
And if the target interpreter runs in a fault, determining the second code text which causes the target interpreter to run in the fault. When the target interpreter runs in error, error information of the target interpreter running in error can be obtained, and the second code text when the target interpreter runs in error is further caused from the error information. Further, the first code text corresponding to the determined second code text may be determined as the error code text based on the code correspondence relationship, and information indicating an error of the error code text may be generated.
As shown in fig. 4, a flowchart of a code generation method according to an embodiment of the present invention is provided. The C language code template is a template code containing C language codes. After the C language code template is obtained, the C language code may be subjected to mark segmentation, that is, a character string in the template code and a character string type of each character string are identified, so as to obtain an identifier list in which each character string obtained by segmentation and a character string type of the character string are recorded, and an intermediate code structure is further performed, so as to obtain an intermediate code. And compiling and executing the intermediate code by calling a target interpreter, wherein if the intermediate byte code is obtained first, the intermediate byte code is executed, and the target C language is obtained. In the whole template influence processing process, if errors occur in the processes of mark segmentation, intermediate code construction, intermediate code compiling and execution, abnormal information can be output.
As shown in fig. 5, a framework diagram of a code generation method according to an embodiment of the present invention is provided. After the code template and the template parameters are input into the template engine, the template engine can convert the code template into an intermediate code, and then call an interpreter to run the intermediate code, so as to obtain the target code.
As shown in fig. 6, an embodiment of the present invention further provides a code generation apparatus, where the apparatus includes:
a code obtaining module 601, configured to obtain a template code for a target programming language; the target programming language is a programming language to which a code to be generated belongs;
a character string recognition module 602, configured to perform character string recognition on the template code to obtain a plurality of character strings and a character string category to which each character string belongs;
the code conversion module 603 is configured to perform code conversion on each character string in the template code based on a conversion policy corresponding to a character string category to which each character string in the plurality of character strings belongs, so as to obtain an intermediate code;
a code running module 604, configured to invoke the target interpreter to run the intermediate code, so as to obtain a target code output by the target interpreter; wherein, the target interpreter is an interpreter of a writing language to which the executable intermediate code belongs.
Optionally, the character string recognition module is configured to include:
an identifier identification submodule for identifying at least one set of start-stop identifiers in the template code; wherein each set of start-stop identifiers comprises a pair of start identifiers and end identifiers;
the first determining submodule is used for taking the character strings positioned between each group of start-stop identifiers as first character strings and determining the appointed category to which each first character string belongs based on the corresponding relation between the preset start-stop identifiers and the appointed categories; the designated type is a character string type except for a common type in all character string types, and the encoding language of the character string belonging to the common type is the same as that of the code required to be generated;
and the second determining submodule is used for taking the character strings which do not belong to any group of start-stop identifiers as second character strings and determining the character string category to which each second character string belongs as a common category.
Optionally, the identifier identification submodule is specifically configured to determine, for each line of code text in the template code, whether the line of code text includes at least one pair of a start identifier and an end identifier; if so, the determined pair of start and stop designators is determined as a set of start and stop designators.
Optionally, the identifier identification sub-module is specifically configured to search the line of code text for a start identifier and a stop identifier; and if at least one pair of start identifier and end identifier is searched, determining that the line code text contains at least one pair of start identifier and end identifier, and otherwise, determining that the line code text does not contain at least one pair of start identifier and end identifier.
Optionally, the apparatus further comprises:
and the first information output module is used for outputting information indicating errors if the number of the searched start identifiers is not matched with the number of the searched end identifiers.
Optionally, the code conversion module is specifically configured to perform code conversion on each of the character strings in the plurality of character strings in the following manner: reading the character string from the template code; if the character string category to which the character string belongs is a grammar category, compiling the character string into a command code according to a compiling rule corresponding to the grammar category to which the character string belongs, and writing the command code into a target code file; the target code file is used for recording a code to be generated; if the character string category to which the character string belongs is a change category, writing the character string as a replaceable variable into a target code file; and if the character string type of the character string belongs to the common type, writing the character string into a target code file as a code text.
Optionally, the code conversion module is specifically configured to, if the syntax type is a type unrelated to the code block identifier, use the character string as a command code; if the grammar type is the type related to the code block identification, according to the code block identification operation corresponding to the grammar type to which the character string belongs, executing the specified operation aiming at the code block identification at the specified position of the character string to obtain a command code; wherein the designated position comprises a position before or after the character string, and the designating operation comprises adding a code block identifier or deleting the code block identifier.
Optionally, the apparatus further comprises:
the second information output module is used for establishing a code corresponding relation between the first code text in the template code and the second code text in the intermediate code, wherein the second code text corresponding to each first code text is as follows: code text generated based on the first code text; if the target interpreter has an error in operation, determining a second code text which causes the target interpreter to have an error in operation; determining a first code text corresponding to the determined second code text as an error code text based on the code correspondence; and generating information for prompting the error of the error code text.
According to the code generation device, after the template code aiming at the target programming language is obtained, the template code can be converted into the intermediate code, the target interpreter aiming at the programming language of the intermediate code is called, and the intermediate code is operated to obtain the target code. Moreover, by means of the interpreter, the rendering function of complex logic codes such as circulation, condition judgment, functions and macros can be avoided, and the development cost of the template engine adopting the scheme is reduced.
An embodiment of the present invention further provides an electronic device, as shown in fig. 7, including a processor 701, a communication interface 702, a memory 703 and a communication bus 704, where the processor 701, the communication interface 702, and the memory 703 complete mutual communication through the communication bus 704,
a memory 703 for storing a computer program;
the processor 701 is configured to implement the steps of the code generation method provided in the foregoing embodiments when executing the program stored in the memory 703.
The communication bus mentioned in the electronic device may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The communication bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is shown, but this does not mean that there is only one bus or one type of bus.
The communication interface is used for communication between the electronic equipment and other equipment.
The Memory may include a Random Access Memory (RAM) or a Non-Volatile Memory (NVM), such as at least one disk Memory. Optionally, the memory may also be at least one memory device located remotely from the processor.
The Processor may be a general-purpose Processor, including a Central Processing Unit (CPU), a Network Processor (NP), and the like; but also Digital Signal Processors (DSPs), Application Specific Integrated Circuits (ASICs), Field Programmable Gate Arrays (FPGAs) or other Programmable logic devices, discrete Gate or transistor logic devices, discrete hardware components.
In yet another embodiment provided by the present invention, a computer-readable storage medium is further provided, in which a computer program is stored, and the computer program realizes the steps of any one of the code generation methods described above when executed by a processor.
In yet another embodiment, a computer program product containing instructions is also provided, which when run on a computer causes the computer to perform any of the code generation methods of the above embodiments.
In the above embodiments, the implementation may be wholly or partially realized by software, hardware, firmware, or any combination thereof. When implemented in software, may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When loaded and executed on a computer, cause the processes or functions described in accordance with the embodiments of the invention to occur, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a network of computers, or other programmable device. The computer instructions may be stored in a computer readable storage medium or transmitted from one computer readable storage medium to another, for example, from one website site, computer, server, or data center to another website site, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, Digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device, such as a server, a data center, etc., that incorporates one or more of the available media. The usable medium may be a magnetic medium (e.g., floppy Disk, hard Disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., Solid State Disk (SSD)), among others.
It should be noted that, in this document, relational terms such as first and second, and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in the process, method, article, or apparatus that comprises the element.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the apparatus, the electronic device, the computer-readable storage medium, and the computer program product embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and for the relevant points, reference may be made to the partial description of the method embodiments.
The above description is only for the preferred embodiment of the present invention, and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention shall fall within the protection scope of the present invention.

Claims (10)

1. A method of code generation, the method comprising:
acquiring a template code aiming at a target programming language; the target programming language is a programming language to which a code to be generated belongs;
carrying out character string recognition on the template code to obtain a plurality of character strings and the character string category to which each character string belongs;
performing code conversion on each character string in the template code based on a conversion strategy corresponding to the character string category to which each character string in the plurality of character strings belongs to obtain an intermediate code;
calling a target interpreter to run the intermediate code to obtain a target code output by the target interpreter; wherein the target interpreter is an interpreter capable of executing a writing language to which the intermediate code belongs.
2. The method according to claim 1, wherein the performing character string recognition on the template code to obtain a plurality of character strings and a character string category to which each character string belongs comprises:
identifying at least one set of start-stop identifiers in the template code; wherein each set of start-stop identifiers comprises a pair of start identifiers and end identifiers;
taking the character strings positioned between each group of start-stop identifiers as first character strings, and determining the appointed category to which each first character string belongs based on the corresponding relation between the preset start-stop identifiers and the appointed categories; the specified type is a character string type except for a common type in all character string types, and the encoding language of the character string belonging to the common type is the same as that of the code required to be generated;
and taking character strings which do not belong to any group of start-stop identifiers as second character strings, and determining the character string category to which each second character string belongs as the common category.
3. The method of claim 2, wherein the identifying at least one set of start-stop identifiers in the template code comprises:
determining whether each line of code text in the template code contains at least one pair of a start identifier and an end identifier; if so, the determined pair of start and stop designators is determined as a set of start and stop designators.
4. The method of claim 3, wherein determining whether the line of code text contains at least one pair of a start identifier and an end identifier comprises:
searching the line of code text for a start identifier and an end identifier;
and if at least one pair of start identifier and end identifier is searched, determining that the line code text contains at least one pair of start identifier and end identifier, and otherwise, determining that the line code text does not contain at least one pair of start identifier and end identifier.
5. The method of claim 4, further comprising:
if the number of the searched start designators does not match the number of the searched end designators, information indicating an error is output.
6. The method according to claim 1, wherein the transcoding each character string in the template code based on the conversion policy corresponding to the character string category to which each character string in the plurality of character strings belongs comprises:
for each of the plurality of character strings, transcoding the character string in a manner comprising:
reading the character string from the template code;
if the character string category to which the character string belongs is a grammar category, compiling the character string into a command code according to a compiling rule corresponding to the grammar category to which the character string belongs, and writing the command code into a target code file; the target code file is used for recording a code to be generated;
if the character string type to which the character string belongs is a change type, writing the character string as a variable into the target code file;
and if the character string type of the character string belongs to the common type, writing the character string into the target code file as a code text.
7. The method according to claim 6, wherein said writing the character string as a command code according to a writing rule corresponding to a grammar class to which the character string belongs comprises:
if the grammar type is a type irrelevant to the code block identification, the character string is used as a command code;
if the grammar type is the type related to the code block identification, according to the code block identification operation corresponding to the grammar type to which the character string belongs, executing the specified operation aiming at the code block identification at the specified position of the character string to obtain a command code; wherein the specified position comprises a position before or after the character string, and the specified operation comprises adding a code block identifier or deleting the code block identifier.
8. The method of claim 1, wherein the target interpreter executes the intermediate code, comprising:
reading the configuration values of the replaceable variables in the pre-configured intermediate code;
and replacing each replaceable variable in the intermediate code by using each read configuration value, and running the replaced intermediate code.
9. The method of claim 1, further comprising:
establishing a code correspondence between first code texts in the template codes and second code texts in the intermediate codes, wherein the second code texts corresponding to each first code text are: code text generated based on the first code text;
if the target interpreter has an error in operation, determining a second code text which causes the target interpreter to have an error in operation;
determining a first code text corresponding to the determined second code text as an error code text based on the code correspondence;
and generating information for prompting the error of the error code text.
10. An apparatus for generating code, the apparatus comprising:
the code acquisition module is used for acquiring a template code aiming at the target programming language; the target programming language is a programming language to which a code to be generated belongs;
the character string recognition module is used for carrying out character string recognition on the template code to obtain a plurality of character strings and the character string category to which each character string belongs;
the code conversion module is used for carrying out code conversion on each character string in the template code based on a conversion strategy corresponding to the character string category to which each character string in the plurality of character strings belongs to obtain an intermediate code;
the code running module is used for calling a target interpreter to run the intermediate code so as to obtain a target code output by the target interpreter; wherein the target interpreter is an interpreter capable of executing a writing language to which the intermediate code belongs.
CN202210324919.0A 2022-03-30 2022-03-30 Code generation method and device Pending CN114879964A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210324919.0A CN114879964A (en) 2022-03-30 2022-03-30 Code generation method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210324919.0A CN114879964A (en) 2022-03-30 2022-03-30 Code generation method and device

Publications (1)

Publication Number Publication Date
CN114879964A true CN114879964A (en) 2022-08-09

Family

ID=82669416

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210324919.0A Pending CN114879964A (en) 2022-03-30 2022-03-30 Code generation method and device

Country Status (1)

Country Link
CN (1) CN114879964A (en)

Similar Documents

Publication Publication Date Title
US7788640B2 (en) Using code analysis to generate documentation
CN107644286B (en) Workflow processing method and device
US8875110B2 (en) Code inspection executing system for performing a code inspection of ABAP source codes
CN110502227B (en) Code complement method and device, storage medium and electronic equipment
US11579856B2 (en) Multi-chip compatible compiling method and device
CN111382070B (en) Compatibility testing method and device, storage medium and computer equipment
KR101554424B1 (en) Method and apparatus for auto generation of test case
CN109189688B (en) Test case script generation method and device and electronic equipment
KR20180129623A (en) Apparatus for statically analyzing assembly code including assoxiated multi files
CN108459846B (en) Software cloud customization method and platform
CN113778852A (en) Code analysis method based on regular expression
CN111309301B (en) Program language conversion method, device and conversion equipment
US20180321932A1 (en) Method, device and related system for dynamically repairing application
CN111966578A (en) Automatic evaluation method for android compatibility defect repair effect
US10613854B2 (en) Systems and methods for automated renumbering, reformatting, and re-referencing of branching statements or go-to instruction line referencing within modified code using a dual-pass approach that includes operations using predicted new line numbers, error detection/correction, and alignment correction processing
CN116245076A (en) Automatic construction method of DRC test version gallery, DRC method, DRC system and readable storage medium
CN114879964A (en) Code generation method and device
CN113805861B (en) Code generation method based on machine learning, code editing system and storage medium
KR101534493B1 (en) Source code security weakness detection apparatus and method based on structure conversion
JP2012173745A (en) Database analysis device and database analysis program
CN114610320B (en) LLVM (LLVM) -based variable type information restoration and comparison method and system
CN114816425B (en) Method and system for converting an automation language program into a LUA language program
CN118153491A (en) Chip design code processing method and device and computer equipment
CN112860544B (en) Code detection method, device, equipment and computer readable storage medium
CN114756217B (en) Plug-in based script generation system

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