CN110543297B - Method and apparatus for generating source code - Google Patents

Method and apparatus for generating source code Download PDF

Info

Publication number
CN110543297B
CN110543297B CN201810522867.1A CN201810522867A CN110543297B CN 110543297 B CN110543297 B CN 110543297B CN 201810522867 A CN201810522867 A CN 201810522867A CN 110543297 B CN110543297 B CN 110543297B
Authority
CN
China
Prior art keywords
syntax
node
tool
class
generating
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201810522867.1A
Other languages
Chinese (zh)
Other versions
CN110543297A (en
Inventor
刘艳
严康
季瑶
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information Technology Co Ltd
Original Assignee
Beijing Jingdong Century Trading Co Ltd
Beijing Jingdong Shangke Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Jingdong Century Trading Co Ltd, Beijing Jingdong Shangke Information Technology Co Ltd filed Critical Beijing Jingdong Century Trading Co Ltd
Priority to CN201810522867.1A priority Critical patent/CN110543297B/en
Publication of CN110543297A publication Critical patent/CN110543297A/en
Application granted granted Critical
Publication of CN110543297B publication Critical patent/CN110543297B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms

Abstract

The embodiment of the application discloses a method and a device for generating source codes. One embodiment of the method comprises: acquiring a source code to be converted, wherein the source code to be converted is compiled based on a first grammar, and the grammar comprises grammar elements; constructing a first abstract syntax tree of the source code to be generated, wherein the first abstract syntax tree comprises a first node corresponding to a first syntax element in the source code to be converted; constructing a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established corresponding relation table, wherein the corresponding relation table is used for representing the corresponding relation between a first syntax element of the first syntax and a second syntax element of a second syntax, and a second node included in the second abstract syntax tree corresponds to the second syntax element of the second syntax; and generating source codes based on the second syntax according to the second abstract syntax tree. The embodiment provides a new source code generation mode.

Description

Method and apparatus for generating source code
Technical Field
The embodiment of the application relates to the technical field of computers, in particular to a method and a device for generating source codes.
Background
With the development of internet technology, various application programs are developed vigorously. The update speed of the application program is very fast. Depending on the operating environment, multiple versions of the same functional application may need to be made.
Disclosure of Invention
The embodiment of the application provides a method and a device for generating source codes.
In a first aspect, an embodiment of the present application provides a method for generating source codes, where the method includes: acquiring a source code to be converted, wherein the source code to be converted is compiled based on a first grammar, and the grammar comprises grammar elements; constructing a first abstract syntax tree of the source code to be generated, wherein the first abstract syntax tree comprises a first node corresponding to a first syntax element in the source code to be converted; constructing a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established corresponding relation table, wherein the corresponding relation table is used for representing the corresponding relation between a first syntax element of the first syntax and a second syntax element of a second syntax, and a second node included in the second abstract syntax tree corresponds to the second syntax element of the second syntax; and generating source codes based on the second syntax according to the second abstract syntax tree.
In a second aspect, an embodiment of the present application provides an apparatus for generating source codes, where the apparatus includes: an obtaining unit configured to obtain a source code to be converted, wherein the source code to be converted is written based on a first syntax, and the syntax includes syntax elements; a first constructing unit configured to construct a first abstract syntax tree of the source code to be generated, wherein the first abstract syntax tree includes a first node corresponding to a first syntax element in the source code to be converted; a second construction unit, configured to construct a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established correspondence table, where the correspondence table is used to represent a correspondence between a first syntax element of the first syntax and a second syntax element of a second syntax, and a second node included in the second abstract syntax tree corresponds to the second syntax element of the second syntax; a generating unit configured to generate source codes based on the second syntax according to the second abstract syntax tree.
In a third aspect, an embodiment of the present application provides an electronic device, including: one or more processors; a storage device, on which one or more programs are stored, which, when executed by the one or more processors, cause the one or more processors to implement the method as described in any implementation manner of the first aspect.
In a fourth aspect, the present application provides a computer-readable medium, on which a computer program is stored, where the computer program, when executed by a processor, implements the method as described in any implementation manner of the first aspect.
According to the method and the device for generating the source code, the first abstract syntax tree of the source code to be generated is firstly constructed, the second abstract syntax tree corresponding to the first abstract syntax tree is then constructed according to the corresponding relation table, and the new source code is generated according to the second abstract syntax tree, so that a new method for generating the source code is provided.
Drawings
Other features, objects and advantages of the present application will become more apparent upon reading of the following detailed description of non-limiting embodiments thereof, made with reference to the accompanying drawings in which:
FIG. 1 is an exemplary system architecture diagram to which some embodiments of the present application may be applied;
FIG. 2 is a flow diagram of one embodiment of a method for generating source code according to the present application;
FIG. 3 is a schematic diagram of an application scenario of a method for generating source code according to the present application;
FIG. 4 is a diagram of a relationship between an exemplary first abstract syntax tree and a second abstract syntax tree;
FIG. 5 is a flow chart of an exemplary method of generating a correspondence table according to the present application;
FIG. 6A is a flow diagram of yet another embodiment of a method for generating source code according to the present application;
FIG. 6B is a flow chart according to an implementation of step 604 of the present application;
FIG. 7 is a block diagram illustrating one embodiment of an apparatus for generating source code according to the present application;
fig. 8 is a schematic structural diagram of a computer system suitable for implementing the terminal device or the server according to the embodiment of the present application.
Detailed Description
The present application will be described in further detail with reference to the following drawings and examples. It is to be understood that the specific embodiments described herein are merely illustrative of the relevant invention and not restrictive of the invention. It should be noted that, for convenience of description, only the portions related to the related invention are shown in the drawings.
It should be noted that the embodiments and features of the embodiments in the present application may be combined with each other without conflict. The present application will be described in detail below with reference to the embodiments with reference to the attached drawings.
Fig. 1 shows an exemplary system architecture 100 to which embodiments of the present method for generating source code or apparatus for generating source code may be applied.
As shown in fig. 1, the system architecture 100 may include terminal devices 101, 102, 103, a network 104, and a server 105. The network 104 serves as a medium for providing communication links between the terminal devices 101, 102, 103 and the server 105. Network 104 may include various connection types, such as wired, wireless communication links, or fiber optic cables, to name a few.
The user may use the terminal devices 101, 102, 103 to interact with the server 105 via the network 104 to receive or send messages or the like. Various communication client applications, such as software development applications, shopping applications, search applications, instant messaging tools, mailbox clients, social platform software, and the like, may be installed on the terminal devices 101, 102, 103.
The terminal apparatuses 101, 102, and 103 may be hardware or software. When the terminal devices 101, 102, and 103 are hardware, they may be various electronic devices with a display screen, including but not limited to smart phones, tablet computers, e-book readers, MP3 players (Moving Picture Experts Group Audio Layer III, mpeg Audio Layer 3), MP4 players (Moving Picture Experts Group Audio Layer IV, mpeg Audio Layer 4), laptop portable computers, desktop computers, and the like. When the terminal apparatuses 101, 102, 103 are software, they can be installed in the electronic apparatuses listed above. It may be implemented as multiple pieces of software or software modules, or as a single piece of software or software module. And is not particularly limited herein.
The server 105 may be a server providing various services, such as a background server providing support for software development type applications on the terminal devices 101, 102, 103. The background server may analyze and otherwise process data such as the request for constructing the abstract syntax tree, and feed back a processing result (e.g., the constructed abstract syntax tree) to the terminal device.
The server may be hardware or software. When the server is hardware, it may be implemented as a distributed server cluster formed by multiple servers, or may be implemented as a single server. When the server is software, it may be implemented as multiple pieces of software or software modules (e.g., to provide distributed services), or as a single piece of software or software module. And is not particularly limited herein.
It should be noted that the method for generating source codes provided by the embodiment of the present application may be executed by the server 105, and accordingly, the apparatus for generating source codes is generally disposed in the server 105. Optionally, the method for generating the source code provided in the embodiment of the present application may also be executed by a terminal device, and accordingly, the apparatus for generating the source code is generally disposed in the terminal device.
It should be noted that the method for generating the source code provided in the embodiment of the present application may also be performed by the server and the terminal device together, for example, the step of "building the first abstract syntax tree of the source code to be generated" may be performed by the server, and the remaining steps may be performed by the terminal device. This is not limited in this application.
It should be understood that the number of terminal devices, networks, and servers in fig. 1 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring to fig. 2, a flow 200 of one embodiment of a method for generating source code is shown. The embodiment is mainly exemplified by applying the method to an electronic device with certain computing capability, and the electronic device may be the server shown in fig. 1, or may be the terminal device shown in fig. 1. The method for generating the source code comprises the following steps:
step 201, obtaining a source code to be converted.
In this embodiment, an execution subject (e.g., a server shown in fig. 1) of the method for generating source codes may acquire the source codes to be converted.
In this embodiment, the source code to be converted is written based on the first syntax.
In this embodiment, the syntax may be a method of organizing the source code. It can be understood that for various types of software development frameworks, there are source code writing specifications corresponding to the various types of software development frameworks, and the set of specifications can be used as the grammar of the software development framework. The syntax includes syntax elements. Syntax elements may be used as constituent elements of the source code. It is colloquially understood that a syntax element defines what words can be written in the source code.
For example, the syntax elements may include, but are not limited to, at least one of: functions that may be called when writing source code, components that may be called, keywords that may be used, etc.
As another example, the syntax elements may include blanks, comments, strings, numbers, identifiers. Operators, etc.
Optionally, the syntax may also include an organization rule for syntax elements. It is colloquially understood that organizational rules in a grammar define how source code can be written.
In this embodiment, the first syntax may be a syntax on which the source code to be converted is based. The first syntax may be a source code writing syntax of various software development frameworks.
As an example, the first syntax may be a fact or a fact Native (RN) source authoring syntax. React is a JAVASCRIPT library used to construct the user interface. The fact Native is an open-source cross-platform mobile application development framework.
As an example, the first syntax may also be a source code writing syntax of the hosted application. It can be understood that the traditional application needs to be installed for use, and the use is inconvenient. Thus, a new form of application (which may also be referred to as a program) -hosted application has evolved. The user can log in and use the hosted application through the hosted application. The boarder application can run depending on the host application, and the user is more flexible and convenient to use. The functionality provided by the hosted application is independent of the functionality provided by the hosted application. The provider of the hosted application can provide a range of basic components that facilitate and limit the development of hosted applications by the hosted application developer.
Step 202, a first abstract syntax tree of a source code to be generated is constructed.
In this embodiment, an execution subject (e.g., the server shown in fig. 1) of the method for generating source code may construct the first abstract syntax tree of the source code to be generated.
In this embodiment, the Abstract Syntax Tree (AST) may be a Tree representation of the Abstract Syntax structure of the source code. The AST has the function of grasping the context of the program, so that the program can be conveniently interpreted by the subsequent links of the compiling process. AST is a set of models that developers tailor to a language, basically every structure in the language corresponds to an AST object. The abstract syntax tree may include nodes and connection relationships between the nodes.
In this embodiment, the abstract syntax tree of the source code to be generated is used as the first abstract syntax tree. The abstract syntax tree of the source code to be generated is preceded by a first abstract syntax tree, which is to be distinguished from a second abstract syntax tree hereinafter, and does not represent the source code to be converted and the second abstract syntax tree.
In this embodiment, the first abstract syntax tree of the source code to be generated may be constructed by various methods. The construction process is roughly as follows: the first step, word segmentation, the source code string is divided into grammar element arrays. And secondly, semantic analysis, namely analyzing the relation between the syntactic units on the basis of the word segmentation result.
As an example, a script parsing function of the browser may be invoked to generate an abstract syntax tree of the source code to be generated.
By way of example, the first abstract syntax tree may be constructed using various parsers (Parser). Parser, also known as an interpreter, typically converts text (strings) in a certain format into a certain data structure. Here, parser having the following functions can be selected: the program text is converted into an AST data structure.
In this embodiment, the first abstract syntax tree may include a first node corresponding to a first syntax element in the source code to be converted.
As an example, the component name in the source code to be converted may be taken as the first node. Here, the component name in the source code to be converted, and the first node are all referred to. The component name of the called component in the source code to be converted is defined by the syntax element in the first syntax. Writing the component name into the source code to be converted may be understood as the component name being the first syntax element in the source code to be converted. It will be appreciated that this component name can be used in the source code to be translated and in the first abstract syntax tree, because the first syntax provides the components of such component names based on the software development framework.
And 203, constructing a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established corresponding relation table.
In this embodiment, an execution subject (for example, a server shown in fig. 1) of the method for generating source code may construct a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established correspondence table.
In this embodiment, the correspondence table is used to represent a correspondence between a first syntax element of the first syntax and a second syntax element of the second syntax.
In this embodiment, the second node included in the second abstract syntax tree corresponds to a second syntax element of the second syntax.
In this embodiment, step 203 may be implemented by: for a first node in the first abstract syntax tree, a first syntax element corresponding to the first node is determined. In the correspondence table, a second syntax element corresponding to the first syntax element is determined. This second syntax element is taken as a second node on the second syntax tree.
In this embodiment, the second syntax is not a source code writing syntax of the same software development framework as the first syntax. That is, the second syntax may be a syntax of various software development frameworks, and may be different from the first syntax. For example, the second syntax may be the RN or hosted program source code writing syntax described above.
It should be noted that the correspondence table may be established in advance. Optionally, the correspondence table may be established in various ways. As an example, a staff may manually establish, through the electronic device, a correspondence between the first syntax element of the first syntax and the second syntax element of the second syntax, so as to obtain the correspondence table.
And step 204, generating source codes based on the second syntax according to the second abstract syntax tree.
In this embodiment, an execution subject (e.g., a server shown in fig. 1) of the method for generating source codes may generate source codes based on the second syntax according to the second abstract syntax tree.
In this embodiment, the source code based on the second syntax may be generated according to the second abstract syntax tree in various ways. By way of example, various open source or commercially available code generators may be utilized to generate source code based on the second syntax.
As an example, the following description takes the RN source code writing syntax or the boarder source code writing syntax as an example:
in a first case, the first syntax may be an RN source code writing syntax, and the second syntax may be a hosted program source code writing syntax; in this case, the source code to be converted may be RN source code developed based on the RN, and the source code generated in step 204 may be hosted program source code.
In a second case, the first syntax may be a hosted program source code writing syntax and the second syntax may be an RN source code writing syntax; in this case, the source code to be converted may be developed based on the development framework of the hosted program, and the source code generated in step 204 may be the RN source code.
In some embodiments, the source code based on the second syntax may be the source code of the hosted application.
It should be noted that there are many providers of hosted application development frameworks and the development frameworks for individual hosted applications are updated very quickly. It is difficult for software developers to quickly master the development modes of multiple hosted applications. The present application provides a method for converting various types of source codes into source codes of hosted applications, and the technical effects at least include:
first, the source code generation efficiency of hosted applications is improved, so that functions can be updated faster on each hosted application, and function on-line can be realized quickly.
Secondly, the technical requirements for the developers are reduced, and thus, the labor cost can be reduced.
With continued reference to fig. 3, fig. 3 is a schematic diagram of an application scenario of the method for generating source codes according to the present embodiment. In the application scenario of fig. 3:
first, a user may initiate a source code conversion request through the terminal 301, where the source code conversion request may include the source code 302 to be converted.
Thereafter, the server 303 may receive a source code conversion request from the terminal, thereby acquiring the source code to be converted.
The server may then build a first abstract syntax tree 304 of the source code to be generated.
Thereafter, the server may construct a second abstract syntax tree 306 corresponding to the first abstract syntax tree 303 according to a pre-established correspondence table 305.
The server may then generate translated source code 307 based on the second syntax according to the second abstract syntax 306.
Finally, the server may send the generated source code 307 to the terminal 301.
In the method provided by the above embodiment of the present application, the first abstract syntax tree of the source code to be generated is first constructed, then the second abstract syntax tree corresponding to the first abstract syntax tree is constructed according to the correspondence table, and then the new source code is generated according to the second abstract syntax tree, and the technical effects at least include:
first, a new method of generating source codes is provided.
Secondly, the second abstract syntax tree can be ensured to be quickly and accurately constructed from the first abstract syntax tree by utilizing the pre-established corresponding relation table. Thus, the conversion from one source code to another source code can be performed quickly and accurately.
Fig. 4 illustrates a relationship between an exemplary first abstract syntax tree and a second abstract syntax tree. It should be noted that the abstract syntax tree shown in fig. 4 is not necessarily a complete abstract syntax tree, and may be a partial abstract syntax tree.
In fig. 4, the first abstract syntax tree 401 includes 3 first nodes, i.e., a first node 4011, a first node 4012, and a first node 4013. Second abstract syntax tree 402 includes 5 second nodes, namely second node 4021, second node 4022, second node 4023, second node 4024, and second node 4025. The first node 4011 corresponds to the second node 4021, the first node 4012 corresponds to the second node 4022, and the first node 4013 corresponds to the second node 4023, the second node 4024, and the second node 4025.
Referring to fig. 5, a flow 500 of an exemplary generation method of a correspondence table according to the present application is shown.
The execution entity of the method for generating the correspondence table may be the same as or different from the execution entity of the method corresponding to fig. 2. The process 500 may include the following steps:
step 501, comparing a first tool set associated with a first grammar with a second tool set associated with a second grammar, and determining a target first tool.
In this embodiment, an execution subject (for example, a server shown in fig. 1) for the corresponding method may compare a first tool set associated with the first syntax with a second tool set associated with the second syntax, and determine a first target tool. Here, there is no second tool in the second target set that is functionally consistent with the target first tool. That is, if there is no second tool in the second set of tools that is functionally consistent with a certain first tool, this first tool may be determined as the target first tool.
In this embodiment, the grammar is associated with a set of tools, the set of tools including at least one of: a set of components, a set of styles, a set of lifecycle functions, and a set of events.
As examples, the target first tool may be a component, a style, a lifecycle function, or an event.
In this embodiment, the Component set may include at least one Component (Component). The components are packages for data and methods.
In this embodiment, the style set may include at least one style. Styles may be in a format where the styles are saved with meaningful names. For example, a style may be a format of a component that is saved with a meaningful name.
In this embodiment, the set of lifecycle functions can include at least one lifecycle function. The lifecycle functions can be used to control the actions of the components at various stages of the lifecycle.
In this embodiment, the event set may include at least one event. An event is an operation that can be recognized by a control. For example, pressing a ok button selects a radio button or a check box. Events can be bound to components.
And 502, constructing a class second tool consistent with the function of the target tool according to the second grammar and the second tool set, and generating a grammar element corresponding to the class second tool.
In this embodiment, an execution subject (e.g., the server shown in fig. 1) for the corresponding method may construct a second tool class consistent with the above-described target first tool function according to the second syntax and the second tool set.
By way of example, if the target first tool is a lifecycle function, then the lifecycle function and/or other tools may be selected from the second set of tools, the lifecycle function and/or other tools, to simulate the function as the lifecycle function of the target first tool. And combining the selected life cycle functions and/or other tools to serve as the class-two tools.
By way of example, the target first tool converts the target first tool into an object if the target first tool is a predefined event, and converts the converted object into a callable function. The callable function can then be treated as a class-second tool. The predefined event may be a predefined event. For example, in RN, events can be passed between parent and child components, while in the development framework of some hosted applications, events cannot be passed between components. In this case, the conversion may result in a callable function, which may be called by the subcomponent during execution, in the manner described above.
Step 503, generating first corresponding relation information and generating a corresponding relation table including the first relation information for the syntax elements corresponding to the target first tool and the class second tool respectively.
In the present embodiment, an execution subject (e.g., a server shown in fig. 1) for the corresponding method may generate first correspondence information for syntax elements corresponding to the target first tool and the structured class second tool, respectively, and generate a correspondence table including the first correspondence information.
Step 504, determining a first tool and a second tool with consistent functions from the first tool set and the second tool set.
In this embodiment, an execution subject (e.g., a server shown in fig. 1) for the corresponding method may determine a first tool and a second tool with consistent functions from the first tool set and the second tool set.
As an example, step 504 may be implemented by: a first tool of the first set of tools is associated with the functional specification and a second tool of the second set of tools is associated with the functional specification. Therefore, the functional descriptions can be compared to determine the first tool and the second tool which have the same functions.
As an example, step 504 may also be implemented by: acquiring comparison materials, wherein the comparison materials comprise a program calling a first tool and a program calling a second tool; by determining whether the functions implemented by the two programs are consistent, it is determined whether the functions of the first tool and the second tool are consistent.
And 505, generating second corresponding relation information and a corresponding relation table comprising the second corresponding relation aiming at the syntax elements corresponding to the first tool and the second tool with consistent functions respectively.
In this embodiment, an execution subject (for example, a server shown in fig. 1) for the corresponding method may generate second correspondence information and a correspondence table including the second correspondence information for syntax elements corresponding to the functionally consistent first tool and second tool, respectively.
Here, the second correspondence information may be added to the correspondence table generated in step 503 to generate a new correspondence table.
In the method for generating a correspondence table shown in this embodiment, the technical effects at least include:
first, a correspondence table is provided that can be used to characterize the correspondence between syntax elements of two syntaxes.
Second, in the case that the second tool set lacks tools relative to the first tool set, a second tool of the class consistent with the function of the target first tool is constructed, and the source code to be converted can be converted into the source code based on the second syntax. Therefore, the function of the source code to be generated can be ensured not to be lost after being converted into a new source code, and the integrity and the accuracy of the source code conversion are improved.
Thirdly, if the functions of the second tool in the second tool set are consistent with those of the first tool in the first tool set, the corresponding relation table is directly written in, and the establishing speed of the corresponding relation table can be improved.
With further reference to fig. 6A, a flow 600 of yet another embodiment of a method for generating source code is shown. The flow 600 of the method for generating source code comprises the following steps:
step 601, obtaining a source code to be converted.
In this embodiment, an execution subject (e.g., a server shown in fig. 1) of the method for generating source codes may acquire the source codes to be converted.
In this embodiment, the source code to be converted is written based on the first syntax.
In the present embodiment, the syntax includes a syntax element.
In the present embodiment, the syntax element type of the syntax element includes at least one of, but is not limited to: component class, style class, lifecycle function class, and event class.
Step 602, a first abstract syntax tree of a source code to be generated is constructed.
In this embodiment, an execution subject of the method for generating source code (e.g., the server shown in fig. 1) may construct a first abstract syntax tree of the source code to be generated.
In this embodiment, the first abstract syntax tree includes a first node corresponding to a first syntax element in the source code to be converted.
The specific operations of step 601 and step 602 in this embodiment are substantially the same as the operations of step 201 and step 202 in the embodiment shown in fig. 2, and are not described again here.
Step 603, traversing the first abstract syntax tree, and determining the node type of the first node according to the syntax element type of the first syntax element corresponding to the first node.
In this embodiment, an execution subject (e.g., the server shown in fig. 1) of the method for generating the source code may traverse the first abstract syntax tree, and determine the node type of the first node according to the syntax element type of the first syntax element corresponding to the first node.
In this embodiment, the node types include at least one of, but are not limited to: component class, style class, lifecycle function class, and event class.
And step 604, sequencing the node types, and generating second nodes corresponding to the first nodes of the node types in sequence.
In this embodiment, an execution subject (e.g., a server shown in fig. 1) of the method for generating source codes may sort the node types and sequentially generate the second nodes corresponding to the first nodes of the node types.
In some optional implementations of this embodiment, the node types may be ordered to obtain a sequential component class, a style class, a lifecycle function class, and an event class. Then, the sequentially generating second nodes corresponding to the first nodes corresponding to the nodes in step 604 may be implemented by using a process 604 shown in fig. 6B, where the process 604 may include:
step 6041, for the component class first node, according to the second syntax element corresponding to the corresponding component class first syntax element, a second node corresponding to the component class first node is generated.
Step 6042, for the style class first node, generate a second node corresponding to the style class first node according to the second syntax element corresponding to the style class first syntax element.
In some optional implementations of this embodiment, the specimen class first node is associated with the component class first node. Step 6042 may include: acquiring a key value pair of the sample from the first node of the sample class, and determining the acquired key value pair as a second node; and establishing an association relation between a second node corresponding to the first node of the component class associated with the first node of the style class and the determined second node.
Step 6043, for the first node of the life cycle function class, generate a second node corresponding to the first node of the life cycle function class according to the second syntax element corresponding to the corresponding first syntax element of the life cycle function class.
Step 6044, for the event type first node, according to the second syntax element corresponding to the corresponding event type first syntax element, generate a second node corresponding to the event type first node.
Step 605, generating a source code based on the second syntax according to the second syntax tree.
In this embodiment, an execution subject (e.g., the server shown in fig. 1) of the method for generating source code may generate source code based on the second syntax according to the second abstract syntax tree.
As can be seen from fig. 6A, compared with the corresponding embodiment of fig. 2, the flow 600 of the method for generating source codes in this embodiment highlights the step of constructing the second node by classification according to the different node types of the first node. Therefore, the technical effects of the solution described in this embodiment at least may include:
first, a new second abstract syntax tree generation method is provided, and thus, a new source code conversion method is provided.
Secondly, the first node of a certain node type is operated (the first node corresponds to and generates the second node), then the first node of another node type is operated, and the execution main body can concentrate on the conversion of one node for a period of time, so that the conversion efficiency is improved.
Third, the component class first node is operated first, and the backbone of the second abstract syntax tree (mainly the component class second node) can be established. On the basis of the existence of the backbone, other three types of first nodes are operated. Thus, the associated component class second node is quickly determined for the other three classes of second points generated.
With further reference to fig. 7, as an implementation of the method shown in the above figures, the present application provides an embodiment of an apparatus for generating source codes, where the embodiment of the apparatus corresponds to the embodiment of the method shown in fig. 2, and the apparatus may be applied to various electronic devices in particular.
As shown in fig. 7, the apparatus 700 for generating source codes of the present embodiment includes: an acquisition unit 701, a first construction unit 702, a second construction unit 703 and a generation unit 704. An obtaining unit configured to obtain a source code to be converted, wherein the source code to be converted is written based on a first syntax, and the syntax includes syntax elements; a first constructing unit configured to construct a first abstract syntax tree of the source code to be generated, wherein the first abstract syntax tree includes a first node corresponding to a first syntax element in the source code to be converted; a second construction unit, configured to construct a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established correspondence table, where the correspondence table is used to represent a correspondence between a first syntax element of the first syntax and a second syntax element of a second syntax, and a second node included in the second abstract syntax tree corresponds to the second syntax element of the second syntax; a generating unit configured to generate source codes based on the second syntax according to the second abstract syntax tree.
In this embodiment, specific processes of the obtaining unit 701, the first constructing unit 702, the second constructing unit 703 and the generating unit 704 of the apparatus 700 for generating a source code and technical effects brought by the specific processes may respectively refer to relevant descriptions of step 201, step 202, step 203 and step 204 in the corresponding embodiment of fig. 2, and are not described herein again.
In some optional implementations of the embodiment, the grammar is associated with a tool set, the grammar elements correspond to tools in the tool set, and the tool set includes at least one of: the method comprises the steps of collecting components, styles, life cycle functions and events; and the correspondence table is obtained by the following generation steps: comparing a first tool set associated with the first grammar with a second tool set associated with the second grammar to determine a target first tool, wherein the second tool set does not have a second tool which is consistent with the target first tool in function; constructing a second tool class consistent with the target first tool function according to the second grammar and the second tool set, and generating a grammar element corresponding to the second tool class; and generating first corresponding relation information aiming at the syntax elements respectively corresponding to the target first tool and the class second tool, and generating a corresponding relation table comprising the first corresponding relation information.
In some optional implementations of this embodiment, the generating further includes: determining a first tool and a second tool which have consistent functions from the first tool set and the second tool set; and generating second corresponding relation information aiming at the syntax elements corresponding to the first tool and the second tool with consistent functions respectively, and generating a corresponding relation table comprising the second corresponding relation information.
In some optional implementations of this embodiment, the constructing a second-class tool that is functionally consistent with the target first tool includes: in response to the target first tool being a predefined event, the target first tool is converted into an object, and the converted object is converted into a callable function.
In some optional implementations of the embodiment, the syntax element type of the syntax element includes at least one of: a component class, a style class, a lifecycle function class, and an event class; and the second building unit, further configured to: traversing the first abstract syntax tree, and determining the node type of the first node according to the syntax element type of the first syntax element corresponding to the first node, wherein the node type comprises at least one of the following items: a component class, a style class, a lifecycle function class, and an event class; and sequencing the node types, and generating second nodes corresponding to the first nodes of the node types in sequence.
In some alternative implementations of this embodiment, the generated source code is the source code of the hosted program.
In some optional implementations of this embodiment, the second building unit is further configured to: for the component class first node, generating a second node corresponding to the component class first node according to a second syntax element corresponding to the corresponding component class first syntax element; for the style class first node, generating a second node corresponding to the style class first node according to a second syntax element corresponding to the corresponding style class first syntax element; for the first node of the life cycle function class, generating a second node corresponding to the first node of the life cycle function class according to a second syntax element corresponding to the first syntax element of the corresponding life cycle function class; and for the event type first node, generating a second node corresponding to the event type first node according to a second syntax element corresponding to the corresponding event type first syntax element.
In some optional implementations of this embodiment, the style class first node is associated with the component class first node; and the second building unit, further configured to: acquiring a style key-value pair from a style class first node, and determining the acquired key-value pair as a second node; and establishing an association relation between a second node corresponding to the first node of the component class associated with the first node of the style class and the determined second node.
It should be noted that details of implementation and technical effects of each unit in the apparatus for generating a source code provided in the embodiment of the present application may refer to descriptions of other embodiments in the present application, and are not described herein again.
Referring now to FIG. 8, shown is a block diagram of a computer system 800 suitable for use in implementing a terminal device or server of an embodiment of the present application. The terminal device or the server shown in fig. 8 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present application.
As shown in fig. 8, a computer system 800 includes a Central Processing Unit (CPU)801 that can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 802 or a program loaded from a storage section 808 into a Random Access Memory (RAM) 803. In the RAM 803, various programs and data necessary for the operation of the system 800 are also stored. The CPU 801, ROM 802, and RAM 803 are connected to each other via a bus 804. An Input/Output (I/O) interface 805 is also connected to bus 804.
The following components are connected to the I/O interface 805: an input portion 806 including a keyboard, a mouse, and the like; an output section 807 including a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and a speaker; a storage portion 808 including a hard disk and the like; and a communication section 809 including a Network interface card such as a LAN (Local Area Network) card, a modem, or the like. The communication section 809 performs communication processing via a network such as the internet. A drive 810 is also connected to the I/O interface 805 as necessary. A removable medium 811 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 810 as necessary, so that a computer program read out therefrom is mounted on the storage section 808 as necessary.
In particular, according to an embodiment of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method illustrated in the flow chart. In such an embodiment, the computer program can be downloaded and installed from a network through the communication section 809 and/or installed from the removable medium 811. The computer program performs the above-described functions defined in the method of the present application when executed by the Central Processing Unit (CPU) 801. It should be noted that the computer readable medium described herein can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present application, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In this application, however, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
Computer program code for carrying out operations for aspects of the present application may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, Smalltalk, C + + or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or the connection may be made to an external computer (for example, through the Internet using an Internet service provider).
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present application. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The units described in the embodiments of the present application may be implemented by software or hardware. The described units may also be provided in a processor, and may be described as: a processor includes an acquisition unit, a first construction unit, a second construction unit, and a generation unit. The names of these units do not in some cases form a limitation on the unit itself, and for example, the acquiring unit may also be described as a "unit that acquires a source code to be generated".
As another aspect, the present application also provides a computer-readable medium, which may be contained in the apparatus described in the above embodiments; or may be present separately and not assembled into the device. The computer readable medium carries one or more programs which, when executed by the apparatus, cause the apparatus to: acquiring a source code to be converted, wherein the source code to be converted is compiled based on a first grammar, and the grammar comprises grammar elements; constructing a first abstract syntax tree of the source code to be generated, wherein the first abstract syntax tree comprises a first node corresponding to a first syntax element in the source code to be converted; constructing a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established corresponding relation table, wherein the corresponding relation table is used for representing the corresponding relation between a first syntax element of the first syntax and a second syntax element of a second syntax, and a second node included in the second abstract syntax tree corresponds to the second syntax element of the second syntax; and generating source codes based on the second syntax according to the second abstract syntax tree.
The above description is only a preferred embodiment of the application and is illustrative of the principles of the technology employed. It will be appreciated by those skilled in the art that the scope of the invention herein disclosed is not limited to the particular combination of features described above, but also encompasses other arrangements formed by any combination of the above features or their equivalents without departing from the spirit of the invention. For example, the above features may be replaced with (but not limited to) features having similar functions disclosed in the present application.

Claims (16)

1. A method for generating source code, comprising:
acquiring a source code to be converted, wherein the source code to be converted is compiled based on a first grammar, and the grammar comprises grammar elements;
constructing a first abstract syntax tree of the source code to be converted, wherein the first abstract syntax tree comprises a first node corresponding to a first syntax element in the source code to be converted;
constructing a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established corresponding relation table, wherein the corresponding relation table is used for representing the corresponding relation between a first syntax element of the first syntax and a second syntax element of a second syntax, and a second node included in the second abstract syntax tree corresponds to the second syntax element of the second syntax;
generating source codes based on the second syntax according to the second abstract syntax tree;
wherein the grammar is associated with a set of tools, the grammar elements correspond to tools in the set of tools, the set of tools including at least one of: the method comprises the steps of collecting components, styles, life cycle functions and events; and
the correspondence table is obtained by the following generation steps:
comparing a first tool set associated with the first grammar with a second tool set associated with the second grammar to determine a target first tool, wherein the second tool set does not have a second tool which is consistent with the target first tool in function;
constructing a second tool class consistent with the target first tool function according to the second grammar and the second tool set, and generating a grammar element corresponding to the second tool class;
and generating first corresponding relation information aiming at the syntax elements respectively corresponding to the target first tool and the class second tool, and generating a corresponding relation table comprising the first corresponding relation information.
2. The method of claim 1, wherein the constructing a second-class tool that is functionally consistent with the target first tool comprises:
in response to the target first tool being a predefined event, converting the target first tool into an object, and converting the converted object into a callable function.
3. The method of claim 1, the generating step further comprising:
determining a first tool and a second tool which have consistent functions from the first tool set and the second tool set;
and generating second corresponding relation information aiming at the syntax elements corresponding to the first tool and the second tool with consistent functions respectively, and generating a corresponding relation table comprising the second corresponding relation information.
4. The method of claim 1, wherein the generated source code is source code of a hosted program.
5. The method according to any of claims 1-4, wherein the syntax element type of the syntax element comprises at least one of: a component class, a style class, a lifecycle function class, and an event class; and
the constructing a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established corresponding relation table comprises the following steps:
traversing the first abstract syntax tree, and determining the node type of the first node according to the syntax element type of the first syntax element corresponding to the first node, wherein the node type comprises at least one of the following items: a component class, a style class, a lifecycle function class, and an event class;
and sequencing the node types, and generating second nodes corresponding to the first nodes of the node types in sequence.
6. The method of claim 5, wherein the ordering node types and sequentially generating second nodes corresponding to the first nodes of each node type comprises at least one of:
for the component class first node, generating a second node corresponding to the component class first node according to a second syntax element corresponding to the corresponding component class first syntax element;
for the style class first node, generating a second node corresponding to the style class first node according to a second syntax element corresponding to the corresponding style class first syntax element;
for the first node of the life cycle function class, generating a second node corresponding to the first node of the life cycle function class according to a second syntax element corresponding to the first syntax element of the corresponding life cycle function class;
and for the event type first node, generating a second node corresponding to the event type first node according to a second syntax element corresponding to the corresponding event type first syntax element.
7. The method of claim 6, wherein a style class first node is associated with a component class first node; and
the generating of the second node corresponding to the style class first node comprises:
acquiring a style key-value pair from a style class first node, and determining the acquired key-value pair as a second node;
and establishing an association relation between a second node corresponding to the first node of the component class associated with the first node of the style class and the determined second node.
8. An apparatus for generating source code, comprising:
an obtaining unit configured to obtain a source code to be converted, wherein the source code to be converted is written based on a first syntax, and the syntax includes syntax elements;
a first construction unit configured to construct a first abstract syntax tree of the source code to be converted, wherein the first abstract syntax tree includes a first node corresponding to a first syntax element in the source code to be converted;
a second construction unit, configured to construct a second abstract syntax tree corresponding to the first abstract syntax tree according to a pre-established correspondence table, where the correspondence table is used to represent a correspondence between a first syntax element of the first syntax and a second syntax element of a second syntax, and a second node included in the second abstract syntax tree corresponds to the second syntax element of the second syntax;
a generating unit configured to generate source codes based on the second syntax according to the second abstract syntax tree; wherein the grammar is associated with a set of tools, the grammar elements correspond to tools in the set of tools, the set of tools including at least one of: the method comprises the steps of collecting components, styles, life cycle functions and events; and
the correspondence table is obtained by the following generation steps:
comparing a first tool set associated with the first grammar with a second tool set associated with the second grammar to determine a target first tool, wherein the second tool set does not have a second tool which is consistent with the target first tool in function;
constructing a second tool class consistent with the target first tool function according to the second grammar and the second tool set, and generating a grammar element corresponding to the second tool class;
and generating first corresponding relation information aiming at the syntax elements respectively corresponding to the target first tool and the class second tool, and generating a corresponding relation table comprising the first corresponding relation information.
9. The apparatus of claim 8, wherein the constructing a second-like tool that is functionally consistent with the target first tool comprises:
in response to the target first tool being a predefined event, the target first tool is converted into an object, and the converted object is converted into a callable function.
10. The apparatus of claim 8, the generating step further comprising:
determining a first tool and a second tool which have consistent functions from the first tool set and the second tool set;
and generating second corresponding relation information aiming at the syntax elements corresponding to the first tool and the second tool with consistent functions respectively, and generating a corresponding relation table comprising the second corresponding relation information.
11. The apparatus of claim 8, wherein the generated source code is source code of a hosted program.
12. The apparatus according to any of claims 8-11, wherein the syntax element type of the syntax element comprises at least one of: a component class, a style class, a lifecycle function class, and an event class; and
the second building unit further configured to:
traversing the first abstract syntax tree, and determining the node type of the first node according to the syntax element type of the first syntax element corresponding to the first node, wherein the node type comprises at least one of the following items: a component class, a style class, a lifecycle function class, and an event class;
and sequencing the node types, and generating second nodes corresponding to the first nodes of the node types in sequence.
13. The apparatus of claim 12, wherein the second building unit is further configured to:
for the component class first node, generating a second node corresponding to the component class first node according to a second syntax element corresponding to the corresponding component class first syntax element;
for the style class first node, generating a second node corresponding to the style class first node according to a second syntax element corresponding to the corresponding style class first syntax element;
for the first node of the life cycle function class, generating a second node corresponding to the first node of the life cycle function class according to a second syntax element corresponding to the first syntax element of the corresponding life cycle function class;
and for the event type first node, generating a second node corresponding to the event type first node according to a second syntax element corresponding to the corresponding event type first syntax element.
14. The apparatus of claim 13, wherein a style class first node is associated with a component class first node; and
the second building unit further configured to:
acquiring a style key-value pair from a style class first node, and determining the acquired key-value pair as a second node;
and establishing an association relation between a second node corresponding to the first node of the component class associated with the first node of the style class and the determined second node.
15. An electronic device, comprising:
one or more processors;
a storage device having one or more programs stored thereon,
when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-7.
16. A computer-readable medium, on which a computer program is stored, wherein the program, when executed by a processor, implements the method of any one of claims 1-7.
CN201810522867.1A 2018-05-28 2018-05-28 Method and apparatus for generating source code Active CN110543297B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201810522867.1A CN110543297B (en) 2018-05-28 2018-05-28 Method and apparatus for generating source code

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201810522867.1A CN110543297B (en) 2018-05-28 2018-05-28 Method and apparatus for generating source code

Publications (2)

Publication Number Publication Date
CN110543297A CN110543297A (en) 2019-12-06
CN110543297B true CN110543297B (en) 2021-03-30

Family

ID=68700725

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201810522867.1A Active CN110543297B (en) 2018-05-28 2018-05-28 Method and apparatus for generating source code

Country Status (1)

Country Link
CN (1) CN110543297B (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112270176A (en) * 2020-11-09 2021-01-26 北京百度网讯科技有限公司 Method, apparatus, and computer storage medium for mode conversion in a deep learning framework

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105912381A (en) * 2016-04-27 2016-08-31 华中科技大学 Compile-time code security detection method based on rule base
CN106293677A (en) * 2015-06-08 2017-01-04 阿里巴巴集团控股有限公司 A kind of code conversion method and device
CN106843858A (en) * 2016-12-30 2017-06-13 南京大学 A kind of method that Spark codes are generated by conversion primitive
CN107423106A (en) * 2017-07-07 2017-12-01 北京小米移动软件有限公司 The method and apparatus for supporting more frame grammars

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US10140105B2 (en) * 2016-03-10 2018-11-27 Wowza Media Systems, LLC Converting source code
CN107977304B (en) * 2016-10-24 2021-05-25 北京京东尚科信息技术有限公司 System debugging method and device
CN106951305B (en) * 2017-03-24 2020-07-31 南京大学 Method for generating midCore script from QVT-R language based on model conversion

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106293677A (en) * 2015-06-08 2017-01-04 阿里巴巴集团控股有限公司 A kind of code conversion method and device
CN105912381A (en) * 2016-04-27 2016-08-31 华中科技大学 Compile-time code security detection method based on rule base
CN106843858A (en) * 2016-12-30 2017-06-13 南京大学 A kind of method that Spark codes are generated by conversion primitive
CN107423106A (en) * 2017-07-07 2017-12-01 北京小米移动软件有限公司 The method and apparatus for supporting more frame grammars

Also Published As

Publication number Publication date
CN110543297A (en) 2019-12-06

Similar Documents

Publication Publication Date Title
KR20180122019A (en) Tools and methods for real-time data flow programming languages
US10007596B2 (en) Generating test scripts through application integration
US9569288B2 (en) Application pattern discovery
CN110543297B (en) Method and apparatus for generating source code
US10592304B2 (en) Suggesting application programming interfaces based on feature and context analysis
CN111124541A (en) Configuration file generation method, device, equipment and medium
US10606729B2 (en) Estimating the number of coding styles by analyzing source code
CN107729001B (en) Expression processing method and device
US10574517B2 (en) Adding voice commands to invoke web services
CN110865889A (en) Method and apparatus for transferring events between components
US20160292067A1 (en) System and method for keyword based testing of custom components
US11099969B2 (en) Estimating the number of coding styles by analyzing source code
CN110647322B (en) List rendering method and device, electronic equipment and computer readable medium
US10846198B2 (en) Automatic navigation from log statement to code
CN110780874A (en) Method and apparatus for generating information
US10936810B2 (en) Token embedding based on target-context pairs
CN109857838B (en) Method and apparatus for generating information
CN109359194B (en) Method and apparatus for predicting information categories
US10437567B2 (en) System and method for use in physical design processes
CN113326188A (en) Browser testing method and device
Bartusevics et al. An approach for development of reusable function library for automation of continuous processes
CN113254340A (en) Test case generation method and device
CN112947912A (en) Method and device for generating code, electronic equipment and storage medium
CN112579151A (en) Method and device for generating model file
CN111125064A (en) Method and device for generating database mode definition statement

Legal Events

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