Sublimation compiler grammar analysis method based on boron platform, storage medium and electronic equipment
By using the bisheng compiler's syntax analysis method on the Kunpeng platform, using context-independent grammar to build an analysis tree and combining the random walk algorithm, the problem of inaccurate identification of the legitimacy of the token stream is solved, and more accurate syntax analysis and error correction are achieved.
Patent Information
- Application Number
- CN202411207548.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-30
- Publication Date
- 2025-02-25
AI Technical Summary
In the prior art, the syntax analyzer is inaccurate in identifying or modifying the legality of the token stream, resulting in a few illegal source programs still returning error information after modification.
The Bisheng compiler syntax analysis method based on the Kunpeng platform is adopted to build an analysis tree through context-independent grammar, identify whether the source program is legal, and use a random walk algorithm to collect error path information to guide modification.
By building an analysis tree and applying a random walk algorithm, the accurate identification of the source program syntax structure and the accurate collection of error paths are achieved, and the accuracy and reliability of syntax analysis are improved.
Smart Images

Figure CN119512553A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of compiler syntax analysis, and in particular to a Bisheng compiler syntax analysis method, storage medium and electronic device based on a Kunpeng platform. Background Art
[0002] Kunpeng microarchitecture supports hardware and software collaborative prefetching, supports basic continuous data access and regular step data access through hardware prefetching, and combines the software prefetching optimization of the Bisheng compiler to cover nested loops, indirect memory access and other more complex memory access scenarios, and realizes prefetching support for most memory access features. The Bisheng compiler is a high-performance compiler for the Kunpeng hardware platform, providing deeply optimized compilation technology, enhanced multi-core parallelization, automatic vectorization, etc., greatly improving instruction and data throughput.
[0003] Among them, syntax analysis is the second important stage of the compiler front end. The task of the syntax analyzer used for syntax analysis is to read the token stream generated by the lexical analysis stage and determine whether the token stream meets the syntax rules of the programming language. If the syntax analyzer confirms that the token stream is illegal, it returns an error message to guide the programmer to modify the source program; if the syntax analyzer confirms that the token stream conforms to the syntax rules of the corresponding language, it will build a suitable data structure representation for the program for use in subsequent stages of compilation. However, a small number of illegal source programs still return error messages after modification, and the reasons include inaccurate identification of the legality of the token stream or inaccurate modification.
[0004] Therefore, there is an urgent need for a Bisheng compiler syntax analysis method, storage medium and electronic device based on the Kunpeng platform to solve the problem of inaccurate identification or modification of the legitimacy of the token stream by the syntax analyzer. Summary of the invention
[0005] In view of the deficiencies in the prior art, the present invention provides a Bisheng compiler syntax analysis method, storage medium and electronic device based on the Kunpeng platform to solve the problem of inaccurate recognition or modification of the legitimacy of the token stream by the syntax analyzer.
[0006] To achieve the above object, the present invention adopts the following technical solutions: A syntax analysis method of the Bisheng compiler based on the Kunpeng platform, characterized in that it includes the following steps: The compiler constructs a parse tree of the token stream of the source program according to the rules of the context-free grammar, and uses the parse tree to identify whether the source program is legal. If a complete parse tree can be successfully constructed, it means that the source program is syntactically correct. If a complete parse tree cannot be successfully constructed, the syntax analyzer confirms that the source program is illegal and returns an error message to guide the programmer to modify the source program.
[0007] To optimize the above technical solutions, the specific measures taken also include: Furthermore, the compiler constructs a parse tree of the token stream of the source program according to the rules of the context-free grammar, comprising the following steps: The compiler first performs lexical analysis, splits the character stream of the source program into a series of tokens, deletes useless characters in the source program, and performs lexical checking to eliminate lexical errors found; Based on the lexical analysis, the compiler enters the syntax analysis stage. The syntax analyzer uses the rules of context-free grammar to try to build a parse tree. Each node of the parse tree represents a grammar symbol, and the connection between the nodes represents the relationship between the productions.
[0008] Furthermore, the process of identifying whether the source program is legal by analyzing the tree, if a complete analysis tree can be successfully constructed, it means that the source program is grammatically correct, and if a complete analysis tree cannot be successfully constructed, the syntax analyzer confirms that the source program is illegal, includes the following steps: The parser starts from the start symbol of the context-free grammar and repeatedly replaces non-terminal symbols with terminals or more complex non-terminal sequences according to the rules of the production until the entire input string is completely covered; if a complete analysis tree can be successfully constructed in this process, and all leaf nodes in the tree are terminals, then the source program is grammatically correct; If the parser finds an unmatched production rule or fails to completely cover the input string while trying to build a parse tree, it means that there is a syntax error in the source program. At this time, the parser will report the error and provide information about the location and type of the possible error.
[0009] Furthermore, the error information is returned to guide the programmer to modify the source program, including the following steps: when modifying, the incomplete analysis tree is converted into a structured image, a random walk algorithm is applied to the structured image, and error path information is collected through the random walk algorithm and fed back to the syntax analyzer or programmer for modification.
[0010] Furthermore, the converting of the incomplete analysis tree into a structured picture comprises the following steps: obtaining each node in the analysis tree according to the incomplete analysis tree, and drawing a structured picture using software that supports tree structure drawing.
[0011] Furthermore, the random walk algorithm is applied to the structured image, and error path information is collected by the random walk algorithm and fed back to the parser or programmer for modification, including the following steps: Import a structured image and apply a random walk algorithm on the structured image, starting from an initial error point, randomly selecting an edge to traverse until a termination condition is reached, where the initial error point includes the first error detected during the analysis tree construction process, and the termination condition includes a known error pattern, traversing a preset number of steps, or returning to the initial point; During the random walk, the traversed paths and error points encountered are recorded. These paths may represent a series of decisions or steps that lead to the current error state. By analyzing these paths and error points, potential error patterns, common causes of errors, or grammar rules that need to be improved can be identified. The collected error path information is fed back to the parser or programmer to optimize the error handling strategy, adjust the grammar rules, or develop a more robust error recovery mechanism.
[0012] Furthermore, if a complete analysis tree can be successfully constructed, it means that the source program is grammatically correct, and the following steps are also included: efficiently training GAN through SeqGAN policy gradient to generate structured sequence data for the legal source program.
[0013] Furthermore, a computer-readable storage medium stores a computer program, characterized in that: the computer program enables a computer to execute a Bi Sheng compiler syntax analysis method based on the Kunpeng platform as described above.
[0014] Furthermore, an electronic device is characterized in that it includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, it implements a Bisheng compiler syntax analysis method based on the Kunpeng platform as described above.
[0015] The beneficial effects of the present invention are: The present invention constructs a parse tree through a context-free grammar, and identifies whether a source program is legal through the parse tree. The context-free grammar can be used to accurately describe the grammatical structure of the source program, and provide a strict grammar checking basis for the compiler; by constructing the parse tree, the compiler can verify layer by layer whether the grammatical structure of the source program conforms to the grammar rules, and the position of each node in the tree and its relationship with the child node clearly indicate the nesting and combination relationship between the grammatical units, thereby ensuring the accuracy and reliability of the identification. At the same time, the context-free grammar has a high degree of flexibility and scalability, and can adapt to these changes by modifying or extending the grammar rules to meet different application requirements.
[0016] The present invention combines the construction of the above-mentioned analysis tree to apply the random walk algorithm to traverse it, so as to accurately collect error path information and feed it back to the syntax analyzer or programmer, so as to enable more accurate modification; the analysis tree is converted into a structured picture, so that the structure and hierarchical relationship of the analysis tree are clear at a glance, which is convenient for applying the random walk algorithm to intuitively display the direction and distribution of the error path on the structured picture, making the error analysis more intuitive and accurate; the random walk algorithm explores the nodes and paths in the graph by simulating the random walk process, and can efficiently traverse the analysis tree and collect error path information; the random walk algorithm has a global search capability, which can cover complex or hidden error paths that may be ignored by the analysis tree, thereby ensuring the collection effect of the error path information and ensuring the accuracy of the information that can be modified by the syntax analyzer or programmer. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 This is a flowchart of a syntax analysis method of the Bisheng compiler based on the Kunpeng platform proposed by the present invention; Figure 2 The present invention provides a flowchart for modifying error information returned in a syntax analysis method of the Bisheng compiler based on the Kunpeng platform. DETAILED DESCRIPTION
[0018] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0019] As attached Figure 1 As shown, a syntax analysis method of the Bisheng compiler based on the Kunpeng platform includes the following steps: The compiler constructs a parse tree of the token stream of the source program according to the rules of the context-free grammar, and uses the parse tree to identify whether the source program is legal. If a complete parse tree can be successfully constructed, it means that the source program is syntactically correct. If a complete parse tree cannot be successfully constructed, the syntax analyzer confirms that the source program is illegal and returns an error message to guide the programmer to modify the source program.
[0020] The present invention builds a parse tree by context-free grammar, and identifies whether a source program is legal by the parse tree, can utilize context-free grammar to accurately describe the grammatical structure of the source program, and provides a strict grammar checking basis for the compiler. By building the parse tree, the compiler can verify layer by layer whether the grammatical structure of the source program conforms to the grammar rules, and the position of each node in the tree and its relationship with the child nodes clearly represent the nesting and combination relationship between the grammatical units, thereby ensuring the accuracy and reliability of identification. Meanwhile, the context-free grammar has a high degree of flexibility and extensibility, and can adapt to these changes by modifying or extending the grammar rules to meet different application requirements.
[0021] In a specific embodiment, the compiler constructs a parse tree of a token stream of a source program according to the rules of a context-free grammar, including the following steps: The compiler first performs lexical analysis to split the character stream of the source program into a series of tokens, which are usually keywords, identifiers, constants, operators, etc. in the programming language, and deletes useless characters in the source program, such as spaces, tabs, newlines, etc., and performs lexical checking to eliminate lexical errors found, such as illegal characters; based on the lexical analysis, the compiler enters the syntax analysis stage, and the syntax analyzer uses the rules of context-free grammar to try to build a parse tree. Each node of the parse tree represents a grammar symbol, which includes terminal symbols or non-terminal symbols, and the connection between nodes represents the relationship between productions.
[0022] In a specific embodiment, the above-mentioned identification of whether the source program is legal by using the analysis tree, if a complete analysis tree can be successfully constructed, it means that the source program is grammatically correct, if a complete analysis tree cannot be successfully constructed, then the syntax analyzer confirms that the source program is illegal, includes the following steps: The parser starts from the start symbol of the context-free grammar, and according to the rules of productions, repeatedly replaces non-terminal symbols with terminals or more complex sequences of non-terminal symbols until the entire input string is completely covered; if a complete analysis tree can be successfully constructed in this process, and all leaf nodes in the tree are terminals, then the source program is grammatically correct; if the parser finds unmatched productions or fails to completely cover the input string while trying to build a analysis tree, then the source program has a grammatical error, in which case the parser will report an error and provide information about the location and type of the possible error.
[0023] In the process of constructing the analysis tree of the present invention, the compiler can timely discover and report syntax errors. Since the analysis tree intuitively shows the relationship between syntax units, the compiler can provide more detailed and specific error information to help developers quickly locate problems.
[0024] As attached Figure 2 As shown, in a specific embodiment, the above-mentioned return error information is used to guide the programmer to modify the source program, including the following steps: when modifying, the incomplete analysis tree is converted into a structured image, and a random walk algorithm is applied to the structured image. The error path information is collected through the random walk algorithm and fed back to the syntax analyzer or the programmer for modification.
[0025] The present invention combines the construction of the above-mentioned analysis tree to apply the random walk algorithm to traverse it, so as to accurately collect error path information and feed it back to the grammar analyzer or programmer, so as to make more accurate modifications. At the same time, the analysis tree is converted into a structured picture, so that the structure and hierarchical relationship of the analysis tree are clear at a glance, which is convenient for applying the random walk algorithm to intuitively display the direction and distribution of the error path on the structured picture, making the error analysis more intuitive and accurate; the random walk algorithm explores the nodes and paths in the graph by simulating the random walk process, and can efficiently traverse the analysis tree and collect error path information; the random walk algorithm has a global search capability, which can cover complex or hidden error paths that may be ignored by the analysis tree, thereby ensuring the collection effect of the error path information and ensuring the accuracy of the information that can be modified by the grammar analyzer or programmer.
[0026] Among them, the above-mentioned conversion of an incomplete analysis tree into a structured image includes the following steps: according to the incomplete analysis tree, each node in the analysis tree is obtained, including a root node, a child node and a leaf node, and a structured image is drawn using software that supports tree structure drawing, such as XMind, MindMeister, MindNode, etc.
[0027] The above-mentioned application of the random walk algorithm on the structured image, collecting error path information through the random walk algorithm and feeding it back to the grammar analyzer or programmer for modification, includes the following steps: importing the structured image, and applying the random walk algorithm on the structured image, starting from a certain initial error point, randomly selecting an edge to traverse until a certain termination condition is reached, wherein the initial error point includes the error first detected during the analysis tree construction process, and the termination condition includes a known error mode, traversing a preset number of steps or returning to the initial point; in the random walk process, recording the traversed paths and encountered error points, these paths may represent a series of decisions or steps leading to the current error state, and by analyzing these paths and error points, identifying potential error modes, common error causes or grammar rules that need to be improved; The collected error path information is fed back to the parser or programmer to optimize the error handling strategy, adjust the grammar rules, or develop a more robust error recovery mechanism.
[0028] Among them, in a specific embodiment, if the above-mentioned can successfully construct a complete analysis tree, it means that the source program is grammatically correct, and also includes the following steps: for the legal source program, the SeqGAN policy gradient is used to efficiently train the GAN to generate structured sequence data, which can be used for automated programming or code completion.
[0029] The present invention constructs a parse tree through a context-free grammar, and identifies whether a source program is legal through the parse tree. The context-free grammar can be used to accurately describe the grammatical structure of the source program, and provide a strict grammar checking basis for the compiler; by constructing the parse tree, the compiler can verify layer by layer whether the grammatical structure of the source program conforms to the grammar rules, and the position of each node in the tree and its relationship with the child node clearly indicate the nesting and combination relationship between the grammatical units, thereby ensuring the accuracy and reliability of the identification. At the same time, the context-free grammar has a high degree of flexibility and scalability, and can adapt to these changes by modifying or extending the grammar rules to meet different application requirements.
[0030] At the same time, the present invention combines the construction of the above-mentioned analysis tree to apply the random walk algorithm to traverse it, so as to accurately collect error path information and feed it back to the syntax analyzer or programmer, so as to enable more accurate modification; the analysis tree is converted into a structured picture, so that the structure and hierarchical relationship of the analysis tree are clear at a glance, which is convenient for applying the random walk algorithm to intuitively display the direction and distribution of the error path on the structured picture, making the error analysis more intuitive and accurate; the random walk algorithm explores the nodes and paths in the graph by simulating the random walk process, and can efficiently traverse the analysis tree and collect error path information; the random walk algorithm has a global search capability, which can cover complex or hidden error paths that may be ignored by the analysis tree, thereby ensuring the collection effect of the error path information and ensuring the accuracy of the information available for modification by the syntax analyzer or programmer.
[0031] In another embodiment, the present invention proposes a computer-readable storage medium storing a computer program, wherein the computer program enables a computer to execute a Bisheng compiler syntax analysis method based on the Kunpeng platform as described above.
[0032] In another embodiment, the present invention proposes an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements a Bisheng compiler syntax analysis method based on the Kunpeng platform as described above.
[0033] In the embodiments disclosed in the present application, the computer storage medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or apparatus. The computer storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or equipment, or any suitable combination of the foregoing. More specific examples of computer storage media may include electrical connections based on one or more lines, portable computer disks, hard disks, random access memories (RAM), read-only memories (ROM), erasable programmable read-only memories (EPROM or flash memory), optical fibers, portable compact disk read-only memories (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0034] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed in this application can be implemented in electronic hardware or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0035] The above are only preferred embodiments of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements and modifications without departing from the principle of the present invention should be regarded as the protection scope of the present invention.
Claims
1. A syntax analysis method of Bisheng compiler based on Kunpeng platform, characterized in that: The steps include: The compiler constructs a parse tree of the token stream of the source program according to the rules of the context-free grammar, and uses the parse tree to identify whether the source program is legal. If a complete parse tree can be successfully constructed, it means that the source program is syntactically correct. If a complete parse tree cannot be successfully constructed, the syntax analyzer confirms that the source program is illegal and returns an error message to guide the programmer to modify the source program.
2. According to a method for parsing syntax of a Bisheng compiler based on a Kunpeng platform according to claim 1, it is characterized in that: The compiler constructs a parse tree of the token stream of the source program according to the rules of the context-free grammar, including the following steps: The compiler first performs lexical analysis, splits the character stream of the source program into a series of tokens, deletes useless characters in the source program, and performs lexical checking to eliminate lexical errors found; Based on the lexical analysis, the compiler enters the syntax analysis stage. The syntax analyzer uses the rules of context-free grammar to try to build a parse tree. Each node of the parse tree represents a grammar symbol, and the connection between the nodes represents the relationship between the productions.
3. According to a method for parsing syntax of a Bisheng compiler based on a Kunpeng platform according to claim 1, it is characterized in that: The process of identifying whether the source program is legal by analyzing the tree, if a complete analysis tree can be successfully constructed, it means that the source program is grammatically correct, if a complete analysis tree cannot be successfully constructed, the syntax analyzer confirms that the source program is illegal, includes the following steps: The parser starts from the start symbol of the context-free grammar and repeatedly replaces non-terminal symbols with terminals or more complex non-terminal sequences according to the rules of the production until the entire input string is completely covered; if a complete analysis tree can be successfully constructed in this process, and all leaf nodes in the tree are terminals, then the source program is grammatically correct; If the parser finds an unmatched production rule or fails to completely cover the input string while trying to build a parse tree, it means that there is a syntax error in the source program. At this time, the parser will report the error and provide information about the location and type of the possible error.
4. According to a method for parsing syntax of a Bisheng compiler based on a Kunpeng platform according to claim 1, it is characterized in that: The error information is returned to guide the programmer to modify the source program, including the following steps: when modifying, the incomplete analysis tree is converted into a structured image, a random walk algorithm is applied to the structured image, and the error path information is collected by the random walk algorithm and fed back to the syntax analyzer or the programmer for modification.
5. According to a method for parsing syntax of a Bisheng compiler based on a Kunpeng platform according to claim 4, it is characterized in that: The method of converting the incomplete analysis tree into a structured picture comprises the following steps: obtaining each node in the analysis tree according to the incomplete analysis tree, and drawing a structured picture using software that supports tree structure drawing.
6. According to a method for parsing syntax of a Bisheng compiler based on a Kunpeng platform according to claim 4, it is characterized in that: The random walk algorithm is applied on the structured image, and error path information is collected by the random walk algorithm and fed back to the parser or programmer for modification, including the following steps: Import a structured image and apply a random walk algorithm on the structured image, starting from an initial error point, randomly selecting an edge to traverse until a termination condition is reached, where the initial error point includes the first error detected during the analysis tree construction process, and the termination condition includes a known error pattern, traversing a preset number of steps, or returning to the initial point; During the random walk, the traversed paths and error points encountered are recorded. These paths may represent a series of decisions or steps that lead to the current error state. By analyzing these paths and error points, potential error patterns, common causes of errors, or grammar rules that need to be improved can be identified. The collected error path information is fed back to the parser or programmer to optimize the error handling strategy, adjust the grammar rules, or develop a more robust error recovery mechanism.
7. The syntax analysis method of the Bisheng compiler based on the Kunpeng platform according to claim 1 is characterized in that: If a complete analysis tree can be successfully constructed, it means that the source program is grammatically correct, and the following steps are also included: the legal source program is efficiently trained by SeqGAN strategy gradient GAN to generate structured sequence data.
8. A computer-readable storage medium storing a computer program, characterized in that: The computer program enables the computer to execute a Bisheng compiler syntax analysis method based on the Kunpeng platform as described in any one of claims 1-7.
9. An electronic device, characterized in that: include: A memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, a Bisheng compiler syntax analysis method based on the Kunpeng platform as described in any one of claims 1 to 7 is implemented.