Unreachable Statement Recognition Method, C Language and Java Conversion Method and Device

Through lexical analysis and grammatical analysis combined with deep-first traversal, unreachable statements in C language are identified and annotated, which solves the shortcomings of existing tools in handling goto statements and code expansion rates, and realizes efficient conversion and executability from C language to Java language.

CN114237607BActive Publication Date: 2025-07-25SHANGHAI PUDONG DEVELOPMENT BANK
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111363168.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-17
Publication Date
2025-07-25
Estimated Expiration
2041-11-17

AI Technical Summary

Technical Problem

The existing C-to-Java language conversion tools have shortcomings in handling goto statements and code bloat rates, and it is difficult to effectively identify and process unreachable statements in C language, resulting in difficult to understand and execute errors in the converted Java code logic.

Method used

Lexical analysis and grammatical analysis methods are used to identify unreachable statements in C language programs through deep priority traversal and annotate them. A syntax tree is established to identify and process jumps, loops and branch keywords to avoid unreachable statements affecting the execution of Java code.

Benefits of technology

Effectively identify and annotate unreachable statements in C language to ensure that C language programs avoid execution errors when converting them to Java programs, and improve the comprehensibility of the code and the executability of the converted Java code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114237607B_ABST
    Figure CN114237607B_ABST
Patent Text Reader

Abstract

The present application relates to a method for identifying unreachable statements, a method and device for converting C language to JAVA. The method for identifying unreachable statements includes: obtaining a C language program to be processed; performing lexical analysis on the C language program to be processed to obtain words to be processed; performing syntax analysis on the words to be processed to build a syntax tree; traversing the syntax tree by means of depth-first traversal to identify unreachable statements in the syntax tree. The method for converting the C language program to a Java program includes: identifying unreachable statements in the C language program according to the method for identifying unreachable statements in the C language; converting the C language program to a Java program according to the unreachable statements. By using this method, unreachable statements in the C language can be identified.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of code conversion, and particularly to a method for identifying unreachable statements, a method and device for converting C language to JAVA language. Background Art

[0002] The automatic conversion of programming languages can shorten a large amount of development cycles and save software development costs in fields such as software transplantation and maintenance. However, there is relatively little research on the conversion from C language to Java language at home and abroad, and even less research on the syntax check of C language before C2J. In the translation system from Cobol to Java, the elimination of goto and perform compound statements is reliably achieved through algorithms.

[0003] Currently, the syntax check of C language is mainly realized by borrowing some tools through certain algorithms or rules, such as the C2J++ tool or the C2J tool of Novosoft Company.

[0004] However, there are many deficiencies in some existing syntax check schemes for C language before C2J. The C2J++ tool can convert C / C++ source code into a Java program that is semantically basically equivalent, but it cannot handle goto statements and cannot distinguish whether the * symbol is used as a multiplication operator or as an indirect operator of a pointer. The C2J tool of Novosoft Company solves many problems in the C2J++ tool, such as the goto statement. However, its disadvantage is that the code inflation rate is too large and the logic of the converted code is difficult to understand. This is mainly because it adds many complex conversion modes to simulate the behavior of the C source program, and it stores various data structures in a huge array, avoiding the type check and runtime security check of Java, and it is difficult to combine with mainstream Java. Summary of the Invention

[0005] Based on this, in view of the above technical problems, it is necessary to provide a method for identifying unreachable statements, a method and device for converting C language to JAVA language, which can identify unreachable statements in C language.

[0006] In a first aspect, the present application provides a method for identifying unreachable statements in C language, and the method for identifying unreachable statements in C language includes:

[0007] Obtain a C language program to be processed;

[0008] Perform lexical analysis on the C language program to be processed to obtain words to be processed;

[0009] Perform syntax analysis on the words to be processed to build a syntax tree;

[0010] Traverse the syntax tree by the depth - first traversal method to identify the unreachable statements in the syntax tree.

[0011] In one embodiment, the lexical analysis of the to - be - processed C language program to obtain the to - be - processed words includes:

[0012] Pre - process the to - be - processed C language program to delete useless symbols;

[0013] Identify the pre - processed to - be - processed C language program according to the keyword identification rule to obtain the to - be - processed words.

[0014] In one embodiment, the syntax analysis of the to - be - processed words to build a syntax tree includes:

[0015] Re - organize the to - be - processed words to obtain syntax units;

[0016] Connect the syntax units to obtain a syntax tree.

[0017] In one embodiment, before connecting the syntax units to obtain a syntax tree, it further includes:

[0018] Conduct a form check on the syntax units to correct the syntax units.

[0019] In one embodiment, the traversal of the syntax tree by the depth - first traversal method to identify the unreachable statements in the syntax tree includes:

[0020] Traverse the syntax tree by the depth - first traversal method to identify the unreachable statements in the syntax tree according to the keywords in the syntax tree.

[0021] In one embodiment, the traversal of the syntax tree by the depth - first traversal method to identify the unreachable statements in the syntax tree according to the keywords in the syntax tree includes at least one of the following:

[0022] Traverse the syntax tree by the depth - first traversal method to determine whether there are jump keywords in the syntax tree. If so, regard the statements after the subtree corresponding to the jump keyword as unreachable statements;

[0023] Traverse the syntax tree by the depth - first traversal method to determine whether there are loop keywords in the syntax tree. When there are loop keywords, the loop condition corresponding to the loop keyword always holds, and there are no jump keywords in the loop body corresponding to the loop keyword, regard the statements after the subtree corresponding to the loop keyword as unreachable statements;

[0024] Traverse the syntax tree by the depth - first traversal method to determine whether there are branch keywords in the syntax tree. When there are such branch keywords and there are jump keywords in each branch corresponding to the branch keywords, the statements after the subtree corresponding to the branch keywords are regarded as unreachable statements.

[0025] In one embodiment, after traversing the syntax tree by the depth - first traversal method to identify the unreachable statements in the syntax tree, it includes:

[0026] Annotate the unreachable statements.

[0027] In a second aspect, the present application provides a method for converting a C - language program into a Java program. The method for converting a C - language program into a Java program includes:

[0028] Identify the unreachable statements in the C - language program according to the above - mentioned method for identifying unreachable statements in C - language;

[0029] Convert the C - language program into a Java program according to the unreachable statements.

[0030] In a third aspect, the present application provides a device for identifying unreachable statements in C - language. The device for identifying unreachable statements in C - language includes:

[0031] A source - program acquisition module, configured to acquire the C - language program to be processed;

[0032] A lexical - analysis module, configured to perform lexical analysis on the C - language program to be processed to obtain the words to be processed;

[0033] A syntax - analysis module, configured to perform syntax analysis on the words to be processed to build a syntax tree;

[0034] A first - identification module, configured to traverse the syntax tree by the depth - first traversal method to identify the unreachable statements in the syntax tree.

[0035] In a fourth aspect, the present application provides a device for converting a C - language program into a Java program. The device for converting a C - language program into a Java program includes:

[0036] A second - identification module, configured to identify the unreachable statements in the C - language program according to the above - mentioned device for identifying unreachable statements in C - language;

[0037] A conversion module, configured to convert the C - language program into a Java program according to the unreachable statements.

[0038] Fifth aspect, the present application provides a computer device, including a memory and a processor, where the memory stores a computer program, and when the processor executes the computer program, the steps of the method described in any one of the above embodiments are implemented.

[0039] Sixth aspect, the present application provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the method described in any one of the above embodiments are implemented.

[0040] Seventh aspect, the present application provides a computer program product, including a computer program, and when the computer program is executed by a processor, the steps of the method described in any one of the above embodiments are implemented.

[0041] The above unreachable statement recognition method, C language and JAVA conversion method and device adopt lexical analysis and syntax analysis, traverse the syntax tree using the depth-first traversal method, find the unreachable statements therein, can perform good syntax parsing on the C language source program, obtain token symbols and semantic structures, and avoid the converted Java code from being unable to execute. Description of the Drawings

[0042] Figure 1 It is an application environment diagram of the unreachable statement recognition method in an embodiment;

[0043] Figure 2 It is a schematic flowchart of the unreachable statement recognition method in an embodiment;

[0044] Figure 3 It is a classification schematic diagram of unreachable statements in an embodiment;

[0045] Figure 4 It is a screenshot of the syntax tree visualization in an embodiment;

[0046] Figure 5 It is a schematic diagram of the annotation result in an embodiment;

[0047] Figure 6 It is an implementation flowchart of unreachable statements in an embodiment;

[0048] Figure 7 It is a schematic flowchart of the C language and JAVA conversion in an embodiment;

[0049] Figure 8 It is a structural block diagram of the unreachable statement recognition device in an embodiment;

[0050] Figure 9 It is a structural block diagram of the C language and JAVA conversion device in an embodiment;

[0051] Figure 10The internal structure diagram of a computer device in an embodiment. Detailed implementation manners

[0052] In order to make the objectives, technical solutions and advantages of the present application more clear and understandable, the present application will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0053] The unreachable statement recognition method and the C language to JAVA conversion method provided by the embodiments of the present application can be applied to, for example Figure 1 the application environment shown in the figure. Among them, the terminal 102 communicates with the server 104 through a network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or placed in the cloud or other network servers. The terminal 102 can send the C language program to be processed to the server 104, so that the server 104 performs lexical analysis on the C language program to be processed to obtain the words to be processed; performs syntactic analysis on the words to be processed to build a syntax tree; traverses the syntax tree by the depth-first traversal method to identify the unreachable statements in the syntax tree. In this way, by using lexical analysis and syntactic analysis, and using the depth-first traversal method to traverse the syntax tree to find the unreachable statements among them, the C language source program can be well syntactically parsed, obtaining word symbols and semantic structures, and avoiding the situation that the converted Java code cannot be executed.

[0054] Among them, the terminal 102 can be but is not limited to various personal computers, laptop computers, smart phones, tablet computers, Internet of Things devices and portable wearable devices. The Internet of Things devices can be smart speakers, smart TVs, smart air conditioners, smart vehicle-mounted devices, etc. The portable wearable devices can be smart watches, smart bracelets, head-mounted devices, etc. The server 104 can be implemented by an independent server or a server cluster composed of multiple servers. In other embodiments, the above-mentioned unreachable statement recognition method can be only applied to the server, and no specific limitation is made here.

[0055] In one embodiment, as Figure 2 shown, a method for recognizing unreachable statements is provided. Taking the case where the method is applied to Figure 1 the server in the figure as an example, the method includes the following steps:

[0056] S202: Obtain the C language program to be processed.

[0057] Specifically, the C language program to be processed refers to the source program written in the C language. There may be a large number of unreachable statements in the C language code (such as goto statements, statements after return, etc.). These unreachable statements have no impact on the compilation and execution of the C language program, but will cause compilation errors after being translated into a Java program, hindering the execution of the program.

[0058] Among them, as shown in Figure 3 shown, Figure 3 is a classification schematic diagram of unreachable statements in an embodiment. In this embodiment, in the C language program, the reasons for these statements to be unreachable are mainly divided into three types: unreachable jump statements, unreachable while infinite loops, and unreachable branch selections. Unreachable jump statements are mainly caused by the continue / break / goto / return keywords. The combined use of continue, break, goto, and return will end the current section of the program prematurely in some scenarios, making the subsequent program never executed and in an unreachable state. Unreachable infinite loop statements are mainly caused by the while statement. If the judgment condition in the while is always true and the program cannot jump out, it will cause the subsequent statements to be unreachable. Unreachable branch selections are caused by if, else, and switch statements. In the C language program code, if each branch has a return type statement, the subsequent statement blocks of the branch will never be executed and are in an unreachable state.

[0059] S204: Perform lexical analysis on the C language program to be processed to obtain the words to be processed.

[0060] Specifically, lexical analysis is to extract different categories of key words in the C language program to be processed. The key words can be divided into five categories, specifically identifiers, reserved words, constants, operators, and delimiters, etc. In other embodiments, the key words may also include other categories.

[0061] Among them, the server can configure corresponding rules in the program. This rule is mainly to identify the corresponding keywords and perform different analysis and processing according to different keywords. Specifically, the server splits the C language program to be processed into individual characters and symbols, which are mainly summarized as five categories in the program: identifiers, reserved words, constants, operators, and delimiters.

[0062] S206: Perform syntactic analysis on the words to be processed to build a syntax tree.

[0063] Specifically, syntactic analysis is to recombine the words to be processed into sentences and program segments according to the rules defined in C.g4, and build a syntax tree based on the relationship between the sentences and program segments.

[0064] S208: Traverse the syntax tree using the depth - first traversal method to identify the unreachable statements in the syntax tree.

[0065] Specifically, the depth - first traversal method is a type of graph algorithm and is a traversal algorithm for graphs and trees. Briefly, its process is to go deep into each possible branch path until it can no longer go deeper, and each node can only be visited once.

[0066] The server, based on the knowledge of compilation principles, adopts the way of re - traversing the syntax tree parsing, uses the depth - first traversal method to traverse the syntax tree, finds the unreachable statements among them for annotation. It can perform good syntax parsing on the C - language source program, obtain token symbols and semantic structures, automatically annotate the unreachable statements, and avoid the situation that the converted Java code cannot be executed.

[0067] The above - mentioned unreachable statement recognition method uses lexical analysis and syntax analysis, traverses the syntax tree using the depth - first traversal method, finds the unreachable statements among them, can perform good syntax parsing on the C - language source program, obtain token symbols and semantic structures, and avoid the situation that the converted Java code cannot be executed.

[0068] In one of the embodiments, perform lexical analysis on the C - language program to be processed to obtain the words to be processed, including: pre - process the C - language program to be processed to delete useless symbols; identify the pre - processed C - language program to be processed according to the keyword recognition rule to obtain the words to be processed.

[0069] Specifically, extracting token symbols is the first step of syntax checking, also known as lexical analysis. In this stage, first, read the source program character by character from left to right, then pre - process the read content to remove some useless symbols such as line breaks, tab characters, etc., and finally scan the source program from beginning to end and decompose it into five types of tokens: identifiers, reserved words, constants, operators, and delimiters. Lexical analysis is oriented to individual characters, and its purpose is to form valid words from them. It is the basis for all subsequent work. If an error occurs in this step, for example, the left - shift symbol '<<' is split into '<' and '<', it will have an irreparable impact on the following text.

[0070] In one of the embodiments, perform syntax analysis on the words to be processed to build a syntax tree, including: recombine the words to be processed to obtain syntax units; connect the syntax units to obtain a syntax tree.

[0071] In one of the embodiments, before connecting the syntax units to obtain a syntax tree, it further includes: perform form checking on the syntax units to correct the syntax units.

[0072] Building a syntax tree is the second step of syntax checking, also known as syntax analysis. In this step, the token string in the lexical analysis phase is serialized into syntax units such as phrases, sentences, and program segments. Preliminary checks are performed on errors such as missing semicolons, mismatched parentheses, and unpaired begin / end in the syntax units, and the resulting language structure is represented in the form of a tree. Taking the simplest C code as an example, the syntax tree after syntax analysis can be seen in Figure 4 as shown. Among them, Figure 4 the meanings of each unit are as follows: CompliationUnit: compilation unit; TranslationUnit: translation unit; ExternalDeclaration: external declaration; FunctionDefintion: function definition; DeclarationSpecifiers: declaration specifiers; typeSpecifier: type specifier, 1: type 1; Int: "int": the return value type of this function is int, corresponding to the int in front of main() in the left code; functionDeclarator: function declaration; Identifier: identifier, that is, the ( and ) after main are identifiers; CompoundStatement: compound statement, referring to the function body; LeftBrace, RightBrace: referring to { and} in the code; Blockitem: referring to the code block; Int: "int": referring to the int in "int a;" in the code; initDeclarator: initialization declaration; Identifier: definition, a: referring to the attribute a defined as int type in the code; Semi: identifier, ";" indicates the end of a piece of code. Figure 4 is a screenshot of the visualization of the syntax tree, which has nothing to do with the above syntax units. The syntax units are defined in the code rules.

[0073] In one embodiment, the syntax tree is traversed by the depth-first traversal method to identify unreachable statements in the syntax tree, including: traversing the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree according to the keywords in the syntax tree.

[0074] In one of the embodiments, the syntax tree is traversed by the depth-first traversal method to identify unreachable statements in the syntax tree according to the keywords in the syntax tree, including at least one of the following: traversing the syntax tree by the depth-first traversal method to determine whether there are jump keywords in the syntax tree. If so, the statements after the subtree corresponding to the jump keyword are taken as unreachable statements; traversing the syntax tree by the depth-first traversal method to determine whether there are loop keywords in the syntax tree. When there are loop keywords, the loop conditions corresponding to the loop keywords always hold, and there are no jump keywords in the loop body corresponding to the loop keywords, the statements after the subtree corresponding to the loop keyword are taken as unreachable statements; traversing the syntax tree by the depth-first traversal method to determine whether there are branch keywords in the syntax tree. When there are branch keywords and there are jump keywords in each branch corresponding to the branch keywords, the statements after the subtree corresponding to the branch keyword are taken as unreachable statements.

[0075] Specifically, in this embodiment, the first stage focuses on whether each word is legal, and the second stage is to combine the words obtained in lexical analysis according to the grammar rules. Here, the results generated in the first two steps are used to truly understand the function expressed by the source program. In this stage, the depth-first traversal (Depth First Serach) method is adopted to perform a depth-first traversal of the syntax tree branches.

[0076] Among them, the jump keywords may include continue, break, goto, and return, etc. The loop keywords may include while. The branch keywords may include if, else, etc.

[0077] The content within each pair of curly braces in a C language program is a statement block, which will be expanded one by one from left to right in the form of subtrees in the syntax tree. During the traversal of each subtree, if jump keywords such as continue / break / goto / return are found, the subsequent nodes in that subtree will no longer be traversed and are regarded as unreachable statements. If the left subtree of a certain subtree is a while, it indicates that this subtree and the while statement form a loop body. When traversing to the conditional expression of this loop body, if the expression always holds and no jump keyword break or return appears in this subtree, then all subsequent nodes of this syntax tree are considered to be in an unreachable state. Similarly, when encountering if / else and switch branch selection statement blocks during the traversal of the syntax tree, a flag is set to record whether the keyword return is encountered during the traversal of each subtree under this statement block. If there is a return in each subtree, then all subsequent nodes of this syntax tree are in an unreachable state. During the C2J process, the reachable statements are automatically translated from C language to Java language, while all unreachable statements are retained in the translated Java program in the form of comments until the traversal ends. Thus, this syntax checking method is completed.

[0078] In one embodiment, after traversing the syntax tree by the depth - first traversal method to identify the unreachable statements in the syntax tree, it includes: annotating the unreachable statements.

[0079] Specifically, it can be seen Figure 5 as shown, where the unreachable statements are annotated in ways such as using double slashes. The common annotations in Java are generally / / or / ** / .

[0080] Specifically, see Figure 6 as shown Figure 6 which is the implementation flowchart of the unreachable statements in one embodiment. In this embodiment, first, lexical symbols are extracted, then a syntax tree is built, and finally, the semantic structure is obtained to find and annotate the unreachable statements.

[0081] In one embodiment, as Figure 7 shown, a C - language to JAVA conversion method is provided. Taking the case where this method is applied to Figure 1 a server as an example for illustration, it includes the following steps:

[0082] S702: Identify the unreachable statements in the C - language program according to the unreachable statement recognition method in any of the above embodiments for the C language.

[0083] S704: Convert the C - language program to a Java program according to the unreachable statements.

[0084] In this embodiment, the syntax check of C language is implemented before C2J conversion, and unreachable statements are automatically found and commented, without the need to manually sort out the program execution flow. At the same time, it can well perform the conversion processing of C language statements such as goto, return, and break, avoiding errors during the compilation and execution of the converted Java program.

[0085] The above C language and JAVA conversion method adopts lexical analysis and syntax analysis, uses the depth-first traversal method to traverse the syntax tree, finds the unreachable statements among them, can well parse the C language source program, obtains token symbols and semantic structures, and avoids the inability to execute the converted Java code.

[0086] It should be understood that although the steps in the flowcharts involved in the above embodiments are shown in sequence according to the arrows, these steps do not necessarily need to be executed in the order indicated by the arrows. Unless otherwise clearly stated in this article, the execution of these steps has no strict order limit, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above embodiments may include multiple steps or multiple stages. These steps or stages do not necessarily need to be executed at the same time, but can be executed at different times. The execution order of these steps or stages does not necessarily need to be sequential, but can be executed alternately or alternately with at least a part of other steps or steps or stages in other steps.

[0087] Based on the same inventive concept, the embodiments of the present application also provide an unreachable statement recognition device and a C language and JAVA conversion device for implementing the above-mentioned unreachable statement recognition method and C language and JAVA conversion method. The implementation solutions provided by this device to solve problems are similar to the implementation solutions described in the above methods. Therefore, the specific limitations in one or more embodiments of the unreachable statement recognition device and C language and JAVA conversion device provided below can refer to the limitations on the unreachable statement recognition method and C language and JAVA conversion method in the above text, and will not be repeated here.

[0088] In one embodiment, as Figure 8 shown, an unreachable statement recognition device is provided, including: a source program acquisition module 801, a lexical analysis module 802, a syntax analysis module 803, and a first recognition module 804, where:

[0089] The source program acquisition module 801 is used to acquire the C language program to be processed;

[0090] The lexical analysis module 802 is used to perform lexical analysis on the C language program to be processed to obtain the tokens to be processed;

[0091] A syntax analysis module 803 for performing syntax analysis on the words to be processed to build a syntax tree;

[0092] A first recognition module 804 for traversing the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree.

[0093] In one embodiment, the above lexical analysis module 802 may include:

[0094] A first preprocessing unit for preprocessing the C language program to be processed to delete useless symbols;

[0095] A lexical analysis unit for identifying the preprocessed C language program to be processed according to the keyword recognition rule to obtain the words to be processed.

[0096] In one embodiment, the above syntax analysis module 803 may include:

[0097] A recombination unit for recombining the words to be processed to obtain syntax units;

[0098] A syntax analysis unit for connecting the syntax units to obtain a syntax tree.

[0099] In one embodiment, the above syntax analysis module 803 may further include:

[0100] A second preprocessing unit for performing a form check on the syntax units to correct the syntax units.

[0101] In one embodiment, the above first recognition module 804 is further configured to traverse the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree according to the keywords in the syntax tree.

[0102] In one embodiment, the above first recognition module 804 is further configured to perform at least one of the following: traversing the syntax tree by the depth-first traversal method to determine whether there is a jump keyword in the syntax tree, and if so, taking the statements after the subtree corresponding to the jump keyword as unreachable statements; traversing the syntax tree by the depth-first traversal method to determine whether there is a loop keyword in the syntax tree, and when there is a loop keyword, the loop condition corresponding to the loop keyword always holds, and there is no jump keyword in the loop body corresponding to the loop keyword, taking the statements after the subtree corresponding to the loop keyword as unreachable statements; traversing the syntax tree by the depth-first traversal method to determine whether there is a branch keyword in the syntax tree, and when there is a branch keyword and there are jump keywords in all branches corresponding to the branch keyword, taking the statements after the subtree corresponding to the branch keyword as unreachable statements.

[0103] In one embodiment, the above device further includes:

[0104] A comment module for commenting on unreachable statements.

[0105] In one embodiment, as Figure 9 shown, a C language to JAVA conversion device is provided, including: a second recognition module 901 and a conversion module 902, where:

[0106] The second recognition module 901 is used to recognize unreachable statements from a C language program according to the unreachable statement recognition device in any of the above embodiments in the C language;

[0107] The conversion module 902 is used to convert the C language program into a Java program according to the unreachable statements.

[0108] Each module in the above unreachable statement recognition device and C language to JAVA conversion device can be implemented in whole or in part by software, hardware, and their combination. Each of the above modules can be embedded in the processor of the computer device in hardware form or be independent of it, or can be stored in the memory of the computer device in software form, so that the processor can call and execute the operations corresponding to each of the above modules.

[0109] In one embodiment, a computer device is provided. The computer device can be a server, and its internal structure diagram can be as Figure 10 shown. The computer device includes a processor, a memory, and a network interface connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program, and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, it implements an unreachable statement recognition method and a C language to JAVA conversion method.

[0110] Those skilled in the art can understand that Figure 10 the structure shown in

[0111] In one embodiment, a computer device is provided, including a memory and a processor. A computer program is stored in the memory. When the processor executes the computer program, the following steps are implemented: obtaining a C language program to be processed; performing lexical analysis on the C language program to be processed to obtain words to be processed; performing syntax analysis on the words to be processed to build a syntax tree; traversing the syntax tree by means of depth-first traversal to identify unreachable statements in the syntax tree.

[0112] In one embodiment, the performing lexical analysis on the C language program to be processed to obtain words to be processed, which is implemented when the processor executes the computer program, includes: preprocessing the C language program to be processed to delete useless symbols; identifying the preprocessed C language program to be processed according to keyword recognition rules to obtain words to be processed.

[0113] In one embodiment, the performing syntax analysis on the words to be processed to build a syntax tree, which is implemented when the processor executes the computer program, includes: reorganizing the words to be processed to obtain syntax units; connecting the syntax units to obtain a syntax tree.

[0114] In one embodiment, before connecting the syntax units to obtain a syntax tree, which is implemented when the processor executes the computer program, it further includes: performing form checking on the syntax units to correct the syntax units.

[0115] In one embodiment, the traversing the syntax tree by means of depth-first traversal to identify unreachable statements in the syntax tree, which is implemented when the processor executes the computer program, includes: traversing the syntax tree by means of depth-first traversal to identify unreachable statements in the syntax tree according to keywords in the syntax tree.

[0116] In one embodiment, the traversing the syntax tree by means of depth-first traversal to identify unreachable statements in the syntax tree according to keywords in the syntax tree, which is implemented when the processor executes the computer program, includes at least one of the following: traversing the syntax tree by means of depth-first traversal to determine whether there is a jump keyword in the syntax tree. If so, the statements after the subtree corresponding to the jump keyword are used as unreachable statements; traversing the syntax tree by means of depth-first traversal to determine whether there is a loop keyword in the syntax tree. When there is a loop keyword, the loop condition corresponding to the loop keyword always holds, and there is no jump keyword in the loop body corresponding to the loop keyword, the statements after the subtree corresponding to the loop keyword are used as unreachable statements; traversing the syntax tree by means of depth-first traversal to determine whether there is a branch keyword in the syntax tree. When there is a branch keyword and there are jump keywords in all branches corresponding to the branch keyword, the statements after the subtree corresponding to the branch keyword are used as unreachable statements.

[0117] In one embodiment, after traversing the syntax tree by the depth-first traversal method to identify unreachable statements when the processor executes a computer program, it includes: annotating the unreachable statements.

[0118] In one embodiment, a computer device is provided, including a memory and a processor. A computer program is stored in the memory. When the processor executes the computer program, the following steps are implemented: identifying unreachable statements from a C language program according to the unreachable statement identification method in the C language of any of the above embodiments; converting the C language program into a Java program according to the unreachable statements.

[0119] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the following steps are implemented: obtaining a C language program to be processed; performing lexical analysis on the C language program to be processed to obtain words to be processed; performing syntax analysis on the words to be processed to build a syntax tree; traversing the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree.

[0120] In one embodiment, when the computer program is executed by the processor, performing lexical analysis on the C language program to be processed to obtain words to be processed includes: preprocessing the C language program to be processed to delete useless symbols; identifying the preprocessed C language program to be processed according to the keyword identification rule to obtain words to be processed.

[0121] In one embodiment, when the computer program is executed by the processor, performing syntax analysis on the words to be processed to build a syntax tree includes: reorganizing the words to be processed to obtain syntax units; connecting the syntax units to obtain a syntax tree.

[0122] In one embodiment, before connecting the syntax units to obtain a syntax tree when the computer program is executed by the processor, it further includes: performing a form check on the syntax units to correct the syntax units.

[0123] In one embodiment, when the computer program is executed by the processor, traversing the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree includes: traversing the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree according to the keywords in the syntax tree.

[0124] In one embodiment, when the computer program is executed by a processor, traversing the syntax tree by depth-first traversal to identify unreachable statements in the syntax tree includes at least one of the following: traversing the syntax tree by depth-first traversal to determine whether there is a jump keyword in the syntax tree. If so, the statements after the subtree corresponding to the jump keyword are regarded as unreachable statements; traversing the syntax tree by depth-first traversal to determine whether there is a loop keyword in the syntax tree. When there is a loop keyword, the loop condition corresponding to the loop keyword always holds, and there is no jump keyword in the loop body corresponding to the loop keyword, the statements after the subtree corresponding to the loop keyword are regarded as unreachable statements; traversing the syntax tree by depth-first traversal to determine whether there is a branch keyword in the syntax tree. When there is a branch keyword and there are jump keywords in all branches corresponding to the branch keyword, the statements after the subtree corresponding to the branch keyword are regarded as unreachable statements.

[0125] In one embodiment, after traversing the syntax tree by depth-first traversal to identify unreachable statements when the computer program is executed by a processor, it includes: annotating the unreachable statements.

[0126] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the following steps are implemented: identifying unreachable statements in a C language program according to the method for identifying unreachable statements in C language in any of the above embodiments; converting the C language program into a Java program according to the unreachable statements.

[0127] In one embodiment, a computer program product is provided, including a computer program. When the computer program is executed by a processor, the following steps are implemented: obtaining a C language program to be processed; performing lexical analysis on the C language program to be processed to obtain words to be processed; performing syntax analysis on the words to be processed to build a syntax tree; traversing the syntax tree by depth-first traversal to identify unreachable statements in the syntax tree.

[0128] In one embodiment, when the computer program is executed by a processor, performing lexical analysis on the C language program to be processed to obtain words to be processed includes: preprocessing the C language program to be processed to delete useless symbols; identifying the preprocessed C language program to be processed according to the keyword identification rule to obtain words to be processed.

[0129] In one embodiment, when the computer program is executed by a processor, performing syntax analysis on the words to be processed to build a syntax tree includes: reorganizing the words to be processed to obtain syntax units; connecting the syntax units to obtain a syntax tree.

[0130] In one embodiment, before connecting syntax units to obtain a syntax tree when the computer program is executed by a processor, it further includes: performing a formal check on the syntax units to correct the syntax units.

[0131] In one embodiment, when the computer program is executed by a processor, traversing the syntax tree by the depth - first traversal method to identify unreachable statements in the syntax tree includes: traversing the syntax tree by the depth - first traversal method to identify unreachable statements in the syntax tree according to keywords in the syntax tree.

[0132] In one embodiment, when the computer program is executed by a processor, traversing the syntax tree by the depth - first traversal method to identify unreachable statements in the syntax tree according to keywords in the syntax tree includes at least one of the following: traversing the syntax tree by the depth - first traversal method to determine whether there is a jump keyword in the syntax tree. If so, the statements after the subtree corresponding to the jump keyword are regarded as unreachable statements; traversing the syntax tree by the depth - first traversal method to determine whether there is a loop keyword in the syntax tree. When there is a loop keyword, the loop condition corresponding to the loop keyword always holds, and there is no jump keyword in the loop body corresponding to the loop keyword, the statements after the subtree corresponding to the loop keyword are regarded as unreachable statements; traversing the syntax tree by the depth - first traversal method to determine whether there is a branch keyword in the syntax tree. When there is a branch keyword and there are jump keywords in all branches corresponding to the branch keyword, the statements after the subtree corresponding to the branch keyword are regarded as unreachable statements.

[0133] In one embodiment, after traversing the syntax tree by the depth - first traversal method to identify unreachable statements in the syntax tree when the computer program is executed by a processor, it includes: annotating the unreachable statements.

[0134] In one embodiment, a computer program product is provided, including a computer program. When the computer program is executed by a processor, the following steps are implemented: identifying unreachable statements in a C - language program according to the method for identifying unreachable statements in C - language in any of the above embodiments; converting the C - language program into a Java program according to the unreachable statements.

[0135] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above methods. Among them, any reference to a memory, database, or other medium used in the embodiments provided in the present application can include at least one of non-volatile and volatile memories. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The databases involved in the embodiments provided in the present application can include at least one of relational databases and non-relational databases. Non-relational databases can include distributed databases based on blockchain, etc., and are not limited thereto. The processors involved in the embodiments provided in the present application can be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, data processing logics based on quantum computing, etc., and are not limited thereto.

[0136] The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity of description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered to be within the scope described in this specification.

[0137] The above-described embodiments only represent several implementation manners of the present application. The description is relatively specific and detailed, but it should not be construed as a limitation on the patent scope of the present application. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can still be made, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the appended claims.

Claims

1. A method for identifying unreachable statements in the C language, characterized in that, The method for identifying unreachable statements in C language includes: Obtain the C language program to be processed; Perform lexical analysis on the C language program to be processed to obtain the words to be processed; Perform syntactic analysis on the words to be processed to build a syntax tree; Traverse the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree, including: traversing the syntax tree by the depth-first traversal method to determine whether there is a loop keyword while in the left subtree of the syntax tree. When there is the loop keyword, the loop condition corresponding to the loop keyword always holds, and there are no jump keywords break and return in the loop body corresponding to the loop keyword, the statements after the subtree corresponding to the loop keyword are regarded as unreachable statements; Traverse the syntax tree by the depth-first traversal method to determine whether there are branch keywords if, else, and switch in the syntax tree. When there are the branch keywords and there are jump keywords in each branch corresponding to the branch keywords, the statements after the subtree corresponding to the branch keywords are regarded as unreachable statements.

2. The method for identifying unreachable statements in C language according to claim 1, wherein The performing lexical analysis on the C language program to be processed to obtain the words to be processed includes: Perform preprocessing on the C language program to be processed to delete useless symbols; Identify the preprocessed C language program to be processed according to the keyword identification rules to obtain the words to be processed.

3. The method for identifying unreachable statements in the C language according to claim 1, characterized in that, The performing syntactic analysis on the words to be processed to build a syntax tree includes: Recombine the words to be processed to obtain syntactic units; Connect the syntactic units to obtain a syntax tree.

4. The method for identifying unreachable statements in the C language according to claim 3, characterized in that Before connecting the syntactic units to obtain a syntax tree, it further includes: Perform form checking on the syntactic units to correct the syntactic units.

5. The method for identifying unreachable statements in the C language according to any one of claims 1 to 4, characterized in that, The traversing the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree includes: Traverse the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree according to the keywords in the syntax tree.

6. The method for identifying unreachable statements in C language according to claim 5, characterized in that The traversing the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree according to the keywords in the syntax tree includes at least one of the following: Traverse the syntax tree by the depth-first traversal method to determine whether there is a jump keyword in the syntax tree. If so, the statements after the subtree corresponding to the jump keyword are regarded as unreachable statements.

7. The method for identifying unreachable statements in the C language according to any one of claims 1 to 4, characterized in that, After traversing the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree, it includes: Annotate the unreachable statements.

8. A method for converting a C language program into a Java program, characterized in that, The method for converting a C language program into a Java program includes: Identify unreachable statements in the C language program according to the method for identifying unreachable statements in C language according to any one of claims 1 to 7; Convert the C language program into a Java program according to the unreachable statements.

9. An unreachable statement recognition device in the C language, characterized in that, The device for identifying unreachable statements in C language includes: A source program acquisition module, configured to obtain a C language program to be processed; A lexical analysis module for performing lexical analysis on the to-be-processed C language program to obtain to-be-processed words; A syntax analysis module for performing syntax analysis on the to-be-processed words to build a syntax tree; A first recognition module for traversing the syntax tree by the depth-first traversal method to identify unreachable statements in the syntax tree, including: traversing the syntax tree by the depth-first traversal method to determine whether there is a loop keyword while in the left subtree of the syntax tree. When there is the loop keyword, the loop condition corresponding to the loop keyword always holds, and there are no jump keywords break and return in the loop body corresponding to the loop keyword, the statements after the subtree corresponding to the loop keyword are regarded as unreachable statements; Traversing the syntax tree by the depth-first traversal method to determine whether there are branch keywords if, else, and switch in the syntax tree. When there are the branch keywords and there are jump keywords in each branch corresponding to the branch keywords, the statements after the subtree corresponding to the branch keywords are regarded as unreachable statements.

10. An apparatus for converting a C language program into a Java program, characterized in that, The device for converting the C language program into a Java program includes: A second recognition module for recognizing unreachable statements in the C language program according to the unreachable statement recognition device in the C language described in claim 9; A conversion module for converting the C language program into a Java program according to the unreachable statements.

11. A computer device, comprising a memory and a processor, the memory storing a computer program, characterized in that, When the processor executes the computer program, the steps of the method described in any one of claims 1 to 7 or 8 are implemented.

12. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, the steps of the method described in any one of claims 1 to 7 or 8 are implemented.

13. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by the processor, the steps of the method described in any one of claims 1 to 7 or 8 are implemented.

Citation Information

Patent Citations

  • Code transformation

    CN101770363A

  • Data processing method and device for converting C into Java and equipment

    CN113608748A