Method, device and electronic device for generating syntax tree of code file

Generating a syntax tree through lexical parsing and lookup table analysis solves the problem that existing compilers cannot automatically analyze and correct writing errors in code files, and realizes efficient code inspection and correction.

CN111381826BActive Publication Date: 2025-03-25BEIJING QIHOOD TECHNOLOGY CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN201811638890.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2018-12-29
Publication Date
2025-03-25
Estimated Expiration
2038-12-29

AI Technical Summary

Technical Problem

Existing compilers cannot fully and thoroughly analyze and correct writing errors in code files, especially when the amount of code is high, causing program developers to spend a lot of time and effort on manual inspections.

Method used

A linear linked list is generated through lexical parsing, and analyses are performed based on the first lookup table and the second lookup table, the conflict type of lexical symbols is determined, and the conflict elimination process is performed, thereby generating a syntax tree of the code file.

Benefits of technology

It realizes automated code inspection and correction, improves inspection efficiency, and saves program developers time and energy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111381826B_ABST
    Figure CN111381826B_ABST
Patent Text Reader

Abstract

The present application relates to the field of computer software development, and discloses a method, device and electronic device for generating a syntax tree of a code file, wherein the method for generating a syntax tree of a code file includes, when receiving a code file to be parsed in a predetermined programming language, parsing each lexical symbol in the code file to be parsed through a lexical parsing module and generating a corresponding linear linked list; then, based on a first lookup table and a second lookup table, analyzing each lexical symbol in the linear linked list in turn, and when it is determined that any lexical symbol belongs to a predetermined conflict type, performing corresponding conflict elimination processing on any lexical symbol; then, based on the processing result of the conflict elimination processing, generating a syntax tree of the code file to be parsed. The method of the embodiment of the present application enables static analysis of the written code file through the syntax tree, thereby accurately and efficiently checking and correcting syntax errors, writing errors, etc. in the written code file.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer development technology, and in particular to a method, device and electronic device for generating a syntax tree of a code file. Background Art

[0002] In the current computer field, the compiler development technology corresponding to high-level programming languages ​​such as C / C++ / JAVA is becoming more and more mature. The application of high-level language compilers can realize the operation of converting programming languages ​​into machine languages. However, the current compilers cannot conduct a comprehensive and detailed analysis of the coding errors in the code files one by one, nor can they accurately prompt or correct the coding errors in the code files. Therefore, before compiling the code files, program developers need to manually check the errors in the code files.

[0003] The inventor of the present application found in the specific implementation process that when the amount of code in the code file is large, it will cause a huge workload for program developers, not only causing program developers to spend a lot of time and energy to check the errors in the code file, but also the checking efficiency is extremely low. At the same time, the inventor of the present application found that according to the part of speech of each lexical symbol in the code file, the corresponding lookup table is searched and the syntax tree (syntaxtree) of the code file is generated, which can automatically realize the checking and correction of grammatical errors, writing errors, etc. in the code file, greatly improving the checking efficiency. However, in the process of searching the corresponding lookup table, conflicts will occur, that is, multiple possible processing behaviors are found at the same time, resulting in an inability to determine which behavior should be selected. Based on this, the inventor of the present application provides a method for eliminating conflicts and syntax trees. Summary of the invention

[0004] The purpose of this application is to solve at least one of the above technical defects, and the following technical solutions are proposed:

[0005] In a first aspect, a method for generating a syntax tree of a code file is provided, comprising:

[0006] When receiving a code file to be parsed in a predetermined programming language, the lexical parsing module parses each lexical symbol in the code file to be parsed and generates a corresponding linear linked list;

[0007] Based on the first lookup table and the second lookup table, each lexical symbol in the linear linked list is analyzed in turn, and when it is determined that any lexical symbol belongs to a predetermined conflict type, a corresponding conflict elimination process is performed on any lexical symbol;

[0008] According to the processing result of the conflict elimination processing, a syntax tree of the code file to be parsed is generated.

[0009] Specifically, the predetermined conflict type includes any of the following:

[0010] The processing of any lexical symbol is a conflict between shift processing and reduction processing;

[0011] The processing of any term symbol is a conflict between the first reduction processing and the second reduction processing.

[0012] Furthermore, before performing conflict elimination processing on any lexical symbol, the method further includes:

[0013] The current processing state is saved to obtain a first saved result.

[0014] Furthermore, conflict elimination processing is performed on any lexical symbol, including:

[0015] Performing a first target processing on any lexical symbol according to the context, and performing corresponding processing on the lexical symbols following any lexical symbol in sequence based on the first target processing;

[0016] If no processing error occurs until the processing of the retry end character is completed, the first saved result is deleted, and the subsequent lexical symbols are processed accordingly.

[0017] Furthermore, it also includes:

[0018] If a processing error occurs in the process of performing corresponding processing on the lexical symbols after any lexical symbol, the recovery processing is performed according to the first saved result, and the second target processing is performed on any lexical symbol, and the lexical symbols after any lexical symbol are processed accordingly in sequence.

[0019] Furthermore, the situation of performing conflict elimination processing on any lexical symbol includes any of the following:

[0020] When any lexical symbol belongs to the first predetermined type, if a conflict occurs between reduction processing and shift processing for any lexical symbol, the shift processing is performed on any lexical symbol;

[0021] When any lexical symbol belongs to the second predetermined type, if a conflict occurs between shift processing and reduction processing for any lexical symbol, determining to perform shift processing or reduction processing on any lexical symbol according to the linear relationship between the lexical symbols;

[0022] When any lexical symbol belongs to the third predetermined type, if a conflict occurs between the first reduction process and the second reduction process for any lexical symbol, it is determined whether the first reduction process or the second reduction process is performed on any lexical symbol according to the linear relationship between the lexical symbols.

[0023] Furthermore, after performing conflict elimination processing on any lexical symbol, the method further includes:

[0024] An error recovery process is performed on the processing result of the conflict resolution process.

[0025] Further, based on the first lookup table and the second lookup table, each lexical symbol in the linear linked list is analyzed in turn, including:

[0026] Determine the part of speech of each lexical symbol in the linear linked list in turn, and whenever the part of speech of any lexical symbol is determined, search the first lookup table and the second lookup table according to the part of speech of any lexical symbol to obtain a corresponding search result;

[0027] Based on the search results, any lexical symbol is analyzed.

[0028] Furthermore, the predetermined programming language is any one of C++ programming language and C language.

[0029] In a second aspect, a device for generating a syntax tree of a code file is provided, comprising:

[0030] The parsing module is used to parse each lexical symbol in the code file to be parsed and generate a corresponding linear linked list through the lexical parsing module when receiving the code file to be parsed in the predetermined programming language;

[0031] A first processing module is used to analyze each lexical symbol in the linear linked list in turn based on the first lookup table and the second lookup table, and when it is determined that any lexical symbol belongs to a predetermined conflict type, perform corresponding conflict elimination processing on any lexical symbol;

[0032] The syntax tree generation module is used to generate a syntax tree of the code file to be parsed according to the processing result of the conflict elimination processing.

[0033] Specifically, the predetermined conflict type includes any of the following:

[0034] The processing of any lexical symbol is a conflict between shift processing and reduction processing;

[0035] The processing of any term symbol is a conflict between the first reduction processing and the second reduction processing.

[0036] Furthermore, it also includes a storage module;

[0037] The storage module is used to save the current processing state to obtain a first saving result.

[0038] Further, the first processing module includes a first processing submodule and a second processing submodule;

[0039] A first processing submodule, configured to perform a first target processing on any lexical symbol according to the context, and sequentially perform corresponding processing on lexical symbols following any lexical symbol based on the first target processing;

[0040] The second processing submodule is used to delete the first saved result and continue to process the subsequent lexical symbols accordingly when no processing error occurs until the processing of the retry end character is completed.

[0041] Further, the first processing module includes a third processing submodule;

[0042] The third processing submodule is used to perform recovery processing according to the first saved result when a processing error occurs in the process of performing corresponding processing on the lexical symbols after any lexical symbol in sequence, and perform second target processing on any lexical symbol, and perform corresponding processing on the lexical symbols after any lexical symbol in sequence.

[0043] Furthermore, the situation of performing conflict elimination processing on any lexical symbol includes any of the following:

[0044] When any lexical symbol belongs to the first predetermined type, if a conflict occurs between reduction processing and shift processing for any lexical symbol, the shift processing is performed on any lexical symbol;

[0045] When any lexical symbol belongs to the second predetermined type, if a conflict occurs between shift processing and reduction processing for any lexical symbol, determining to perform shift processing or reduction processing on any lexical symbol according to the linear relationship between the lexical symbols;

[0046] When any lexical symbol belongs to the third predetermined type, if a conflict occurs between the first reduction process and the second reduction process for any lexical symbol, it is determined whether the first reduction process or the second reduction process is performed on any lexical symbol according to the linear relationship between the lexical symbols.

[0047] Furthermore, it also includes a second processing module;

[0048] The second processing module is used to perform error recovery processing on the processing result of the conflict elimination processing.

[0049] Further, the first processing module includes a part-of-speech determination submodule and an analysis submodule;

[0050] The part-of-speech determination submodule is used to determine the part-of-speech of each lexical symbol in the linear linked list in turn, and whenever the part-of-speech of any lexical symbol is determined, the first lookup table and the second lookup table are searched according to the part-of-speech of any lexical symbol to obtain a corresponding search result;

[0051] The analysis submodule is used to analyze any lexical symbol according to the search result.

[0052] Furthermore, the predetermined programming language is any one of C++ programming language and C language.

[0053] In a third aspect, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned method of generating a syntax tree of a code file when executing the program.

[0054] In a fourth aspect, a computer-readable storage medium is provided, on which a computer program is stored, and when the program is executed by a processor, the method for generating a syntax tree of a code file is implemented.

[0055] The method for generating a syntax tree for a code file provided in an embodiment of the present application analyzes each lexical symbol in a linear linked list in turn based on a first lookup table and a second lookup table, and when it is determined that any lexical symbol belongs to a predetermined conflict type, performs corresponding conflict elimination processing on any lexical symbol, thereby effectively solving the situation where multiple possible processing behaviors are found according to the lookup table, but it is impossible to determine which behavior to select, and lays the necessary foundation for the subsequent generation of a syntax tree for the code file to be parsed; based on the processing result of the conflict elimination processing, a syntax tree for the code file to be parsed is generated, and a method for generating a syntax tree for a code file is provided, so that the completed code file can be statically analyzed through the syntax tree, so as to accurately and efficiently check and correct syntax errors, writing errors, etc. in the completed code file, greatly saving the time and energy of program developers.

[0056] Additional aspects and advantages of the present application will be partially given in the following description, which will become apparent from the following description, or will be understood through the practice of the present application. BRIEF DESCRIPTION OF THE DRAWINGS

[0057] The above and / or additional aspects and advantages of the present application will become apparent and easily understood from the following description of the embodiments in conjunction with the accompanying drawings, in which:

[0058] Figure 1 A flowchart of a method for generating a syntax tree of a code file according to an embodiment of the present application;

[0059] Figure 2 A schematic diagram of the basic process of conflict resolution processing in an embodiment of the present application;

[0060] Figure 3 A schematic diagram of the basic process of part of speech determination process in an embodiment of the present application;

[0061] Figure 4 A schematic diagram of a basic process of obtaining the parts of speech of keywords and operators in the part of speech determination process of an embodiment of the present application;

[0062] Figure 5 A schematic diagram of a process of determining part of speech by searching for scope information in an embodiment of the present application;

[0063] Figure 6 A schematic diagram of the basic process of part-of-speech guessing in an embodiment of the present application;

[0064] Figure 7 A schematic diagram of the basic process of error recovery processing in an embodiment of the present application;

[0065] Figure 8 A schematic diagram of the overall process of generating a syntax tree of a code file according to an embodiment of the present application;

[0066] Fig. 9 A schematic diagram of the process of moving in according to an embodiment of the present application;

[0067] Fig.10 A schematic diagram of the basic structure of a device for generating a syntax tree of a code file according to an embodiment of the present application;

[0068] Fig.11 A detailed structural diagram of a device for generating a syntax tree of a code file according to an embodiment of the present application;

[0069] Fig.12 A schematic diagram of the structure of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0070] The embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present application, and cannot be interpreted as limiting the present application.

[0071] It will be understood by those skilled in the art that, unless expressly stated, the singular forms "one", "said", and "the" used herein may also include plural forms. It should be further understood that the term "comprising" used in the specification of the present application refers to the presence of the features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof. It should be understood that when we refer to an element as being "connected" or "coupled" to another element, it may be directly connected or coupled to the other element, or there may be an intermediate element. In addition, the "connection" or "coupling" used herein may include wireless connection or wireless coupling. The term "and / or" used herein includes all or any unit and all combinations of one or more associated listed items.

[0072] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below in conjunction with the accompanying drawings.

[0073] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.

[0074] Embodiment 1

[0075] The present application embodiment provides a method for generating a syntax tree of a code file, such as Figure 1 As shown, including:

[0076] Step S110, when a code file to be parsed in a predetermined programming language is received, each lexical symbol in the code file to be parsed is parsed by a lexical parsing module and a corresponding linear linked list is generated.

[0077] Specifically, the code file of a predetermined programming language is usually composed of code elements such as keywords, identifiers, mathematical operators, scope identifiers, and statement punctuation marks, which are also called lexical symbols (tokens). Through the lexical parsing module, the lexical symbols in the code file to be parsed can be organized into a linear linked list (token-list) in order, providing a prerequisite for the subsequent generation of the syntax tree of the code file.

[0078] Hereinafter, mathematical operators, scope identifiers, statement punctuation marks, and other symbols are collectively referred to as operators, and the first element in the linked list is denoted as token-list.first.

[0079] Furthermore, the basic properties of a lexical symbol (token) are shown in Table 1 below. The left side of Table 1 is the English name corresponding to each lexical symbol, and the right side is the Chinese description or explanation corresponding to each lexical symbol.

[0080] Table 1 - Basic properties of lexical symbols (tokens)

[0081]

[0082] In some cases, in order to simplify the grammar, some lexical symbols (tokens) can be merged during lexical parsing, such as merging lexical symbols (tokens) such as "::new", "::delete", ".template", and "->template" into one lexical symbol (token).

[0083] Step S120: Based on the first lookup table and the second lookup table, analyze each lexical symbol in the linear linked list in turn, and when it is determined that any lexical symbol belongs to a predetermined conflict type, perform corresponding conflict elimination processing on any lexical symbol.

[0084] Specifically, when a conflict occurs in the process of searching the first lookup table and the second lookup table according to the part of speech of the lexical symbol, that is, multiple possible processing behaviors are found at the same time and it is impossible to determine which behavior should be selected, it is necessary to determine the predetermined conflict type corresponding to the current lexical symbol, and then perform conflict elimination processing on the current lexical symbol according to the determined predetermined conflict type.

[0085] Step S130: Generate a syntax tree of the code file to be parsed according to the result of the conflict elimination process.

[0086] Specifically, after the predetermined conflict types of lexical symbols are eliminated, a syntax tree of the code file to be parsed may be generated according to the result of the conflict elimination process.

[0087] Compared with the prior art, the method for generating a syntax tree for a code file provided in an embodiment of the present application analyzes each lexical symbol in a linear linked list in turn based on a first lookup table and a second lookup table, and when it is determined that any lexical symbol belongs to a predetermined conflict type, performs corresponding conflict elimination processing on any lexical symbol, thereby effectively solving the situation where multiple possible processing behaviors are found according to the lookup table, but it is impossible to determine which behavior to select, and lays the necessary foundation for the subsequent generation of a syntax tree for the code file to be parsed; based on the processing result of the conflict elimination processing, a syntax tree for the code file to be parsed is generated, and a method for generating a syntax tree for a code file is provided, so that the completed code file can be statically analyzed through the syntax tree, so as to accurately and efficiently check and correct syntax errors, writing errors, etc. in the completed code file, greatly saving the time and energy of program developers.

[0088] The present application embodiment provides a possible implementation method, in which:

[0089] The predetermined programming language is any one of the C++ programming language and the C language. The following description will be made using the C++ programming language as an example, wherein the processing process of the C language is the same as that of the C++ programming language, that is, the method provided in the embodiment of the present application is compatible with the C++ programming language and the C language.

[0090] Specifically, the predetermined conflict type includes any of the following:

[0091] The processing of any lexical symbol is a conflict between shift processing and reduction processing;

[0092] The processing of any term symbol is a conflict between the first reduction processing and the second reduction processing.

[0093] Specifically, before performing conflict elimination processing on any lexical symbol, the process further includes:

[0094] The current processing state is saved to obtain a first saved result.

[0095] Specifically, conflict elimination processing is performed on any lexical symbol, including:

[0096] Performing a first target processing on any lexical symbol according to the context, and performing corresponding processing on the lexical symbols following any lexical symbol in sequence based on the first target processing;

[0097] If no processing error occurs until the processing of the retry terminator is completed, the first saved result is deleted, and the subsequent lexical symbols are processed accordingly.

[0098] Specifically, it also includes:

[0099] If a processing error occurs in the process of performing corresponding processing on the lexical symbols after any lexical symbol, the recovery processing is performed according to the first saved result, and the second target processing is performed on any lexical symbol, and the lexical symbols after any lexical symbol are processed accordingly in sequence.

[0100] Specifically, the situation of performing conflict elimination processing on any lexical symbol includes any of the following:

[0101] When any lexical symbol belongs to the first predetermined type, if a conflict occurs between reduction processing and shift processing for any lexical symbol, the shift processing is performed on any lexical symbol;

[0102] When any lexical symbol belongs to the second predetermined type, if a conflict occurs between shift processing and reduction processing for any lexical symbol, determining to perform shift processing or reduction processing on any lexical symbol according to the linear relationship between the lexical symbols;

[0103] When any lexical symbol belongs to the third predetermined type, if a conflict occurs between the first reduction process and the second reduction process for any lexical symbol, it is determined whether the first reduction process or the second reduction process is performed on any lexical symbol according to the linear relationship between the lexical symbols.

[0104] The above-mentioned conflict elimination process is described in detail below:

[0105] Because of the conflict, the production list used in the embodiment of the present application does not belong to the classic LR1 grammar, and it is difficult to construct a standard LR1 grammar model of the C++ programming language, so the embodiment of the present application implements a conflict elimination processing module.

[0106] For example, in the production list file, the function parameter declaration is part of the non-terminal declarator, and the declarator can be followed by an initialization list, which can also start with "(", such as:

[0107] int a(0); / / declare variable a, its initial value is 0, "(0)" is the initialization list

[0108] int a(int); / / declare function a, which has one parameter of type int, "(int)" is the parameter list

[0109] This causes a conflict. After reading the two lexical symbols int and a, the top of the symbol stack is noptr-declarator. When "(" is read, it is impossible to determine whether the "(" is the beginning of the parameter list or the beginning of the initialization list, that is, it is impossible to determine whether to reduce the top symbol of the stack to ptr-declarator or continue to move the state to the matching parameter list. Therefore, it is necessary to judge whether the content after "(" is a parameter list or an initialization list based on the context. In some cases, a deterministic conclusion can be drawn based on the linear relationship between symbols, while in other cases, a deterministic conclusion cannot be drawn. When a deterministic conclusion cannot be drawn, it can be parsed as a parameter list first. If it passes (that is, no parsing error occurs), it is determined to be a parameter list. If a parsing error occurs (that is, it does not pass), it is parsed as an initialization list, which provides a "retry mechanism".

[0110] First: When the following productions face a "reduction-shift conflict", they are forced to perform shifting. That is, when any lexical symbol belongs to the first predetermined type, if any lexical symbol has a conflict between reduction processing and shift processing, any lexical symbol will be shifted. Among them, the situation where any lexical symbol belongs to the first predetermined type is as follows:

[0111] (1)selection-statement→if(condition)statement

[0112] When reading an "else", it is mandatory to not reduce but to shift in, that is, each "else" is paired with the nearest "if".

[0113] (2)class-specifier→class-head

[0114] When defining a member class of a class, read ":" to force a shift instead of reduction.

[0115] The reason for the conflict is that ":" can be the beginning of the base class list or the beginning of the bit field. Semantically speaking, the class-head should not be restricted by the bit field, so it can be forced to move in directly.

[0116] (3)exception-specification→noexcept

[0117] When reading "(", "(" can be part of a noexcept declaration or part of an initializer list, which is forced to be a shift.

[0118] For example: int(*pf)(int)noexcept(0);

[0119] Among them, "(0)" can be combined with noexcept to specify that pf points to a function that can throw any exception, and it can also be used as the initial value of pf. This is a grammatical ambiguity and is mandatory to be combined with noexcept.

[0120] (4)enum-base→type-name and enum-base→type-modifier-seq type-name

[0121] When reading "const" and "volatile", shifting is mandatory. The reason for the conflict is that enumeration variables can be defined like this:

[0122] enum E: int const e = xxx;

[0123] Among them, const is ambiguous and has two interpretations:

[0124] 1. enum E: int specification is enum-specifier, const specification is type-modifier-seq

[0125] 2. int const is reduced to enum-base, and enum E:int const is reduced to enum-specifier

[0126] Based on the standard, the second method is enforced.

[0127] (5)new-type-id→trailing-type-specifier and new-declarator→new-ptr-operator

[0128] When reading "*", shifting is mandatory. The reason for the conflict is that new-expression can be used as a basic expression, and "*" can be interpreted as a pointer or a multiplication sign.

[0129] According to the C++ standard, it does not make sense to use new-expression for multiplication, so it is mandatory to use shift.

[0130] (6)conversion-type-id→trailing-type-specifier and conversion-declarator→ptr-operator

[0131] When reading "*", "&", and "&&", shifting is enforced. The reason for the conflict is that conversion-type-id can be part of a basic expression, so "*", "&", and "&&" can be either ptr-operator or operator. These symbols are enforced to be ptr-operator.

[0132] Second: When the following production formula faces a "reduce-shift conflict", it can determine whether to perform "reduce" or "shift" based on the symbol stack and the linear relationship between the symbols. If it cannot be determined, the retry mode can be turned on. That is, when any lexical symbol belongs to the second predetermined type, if any lexical symbol conflicts between shift processing and reduction processing, it is determined whether to perform shift processing or reduction processing on any lexical symbol based on the linear relationship between the lexical symbols. Among them, the situation where any lexical symbol belongs to the second predetermined type is as follows:

[0133] (1)ptr-declarator→noptr-declarator

[0134] When reading "(", it is necessary to determine whether "(" is the beginning of the parameter list or the beginning of the initialization list. If it is the beginning of the parameter list, move it in. If it is the beginning of the initialization list, reduce noptr-declarator to ptr-declarator. If it cannot be determined, start the retry mechanism.

[0135] (2)type-name→nested-class-name

[0136] When reading "(", it is necessary to determine whether "(" is the beginning of the parameter list or the beginning of the declarator. If it is the beginning of the parameter list, move it in. If it is the beginning of the declarator, reduce the nested-class-name to type-name. If it cannot be determined, start the retry mechanism.

[0137] For example (let A be a class name):

[0138] A(a); / / Object definition, "(a)" is the declarator

[0139] A::A(int); / / constructor, "(int)" is the parameter list

[0140] (3)simple-type-specifier→type-name

[0141] When reading "(", it is necessary to determine whether the "(" is followed by an expression or an object definition. If it is an expression, it is shifted in. If it is an object definition, the type-name is reduced to a simple-type-specifier. If it cannot be determined, it enters the retry mode.

[0142] For example:

[0143]

[0144] (4)enum-specifier→enum-head

[0145] When defining a member enumeration class of a class, the production will face a reduce-shift conflict when ":" is read. It is necessary to determine whether the ":" is followed by a bit field definition or an enumeration base class definition. If the ":" is not followed by a type name, the enum-head is reduced to an enum-specifier, otherwise it is shifted.

[0146] (5)ptr-abstract-declarator→ptr-operator

[0147] When defining trailing-return-type, "(" is read in and the production will face a reduction-shift conflict. It is necessary to determine whether "(" is part of the trailing-type-id or the beginning of the initializer list. If so, ptr-operator is reduced to ptr-abstract-declarator. All mainstream compilers do not support this, so it can be directly shifted in.

[0148] (6)ptr-abstract-declarator→noptr-abstract-declarator

[0149] When defining trailing-return-type, "(" is read in and the production rule faces a reduction-shift conflict. It is necessary to determine whether "(" is part of the trailing-type-id or the beginning of the initializer list. If so, the noptr-abstract-declarator is reduced to a ptr-abstract-declarator. None of the mainstream compilers support this, so it can be directly shifted in.

[0150] (7)trailing-type-id→trailing-type-specifier

[0151] When defining trailing-return-type, "(" is read in and the production rule faces a reduction-shift conflict. It is necessary to determine whether "(" is part of the trailing-type-id or the beginning of the initializer list. If so, the trailing-type-specifier is reduced to the trailing-type-id. None of the mainstream compilers support this, so it can be directly shifted in.

[0152] (8)unary-operator→~

[0153] When reading TYPE-NAME or decltype, it is necessary to determine whether "~" is a negation operator or a destructor identifier. If it is a negation operator, the top "~" on the stack is reduced to a unary-operator. If it is a destructor identifier, it is shifted in.

[0154] For example:

[0155]

[0156] Third: Reduction-reduction conflict. When the following productions face "reduction-reduction conflict", the symbol stack and the linear relationship of the symbols can be used to determine which production to reduce to. That is, when any lexical symbol belongs to the third predetermined type, if any lexical symbol conflicts between the first reduction processing and the second reduction processing, then according to the linear relationship between the lexical symbols, determine whether to perform the first reduction processing or the second reduction processing on any lexical symbol. Among them, the situation where any lexical symbol belongs to the third predetermined type is as follows:

[0157] (1)braced-init-list→{} and compound-statement→{}

[0158] When reading ";", it is necessary to determine whether the top of the symbol stack is a function. If it is a function, it is reduced according to the compound-statement→{} convention; otherwise, it is reduced according to the braced-init-list→{} convention.

[0159] (2)initializer-clause→assignment-expression and expression→assignment-expression

[0160] When reading "," it is necessary to determine whether the top of the symbol stack is a function. If it is a function, it is reduced according to the expression→assignment-expression convention; otherwise, it is reduced according to the initializer-clause→assignment-expression convention.

[0161] In addition, when it is not possible to determine whether to "move in" or "reduce", the current state of the model can be saved as a "snapshot", and then one of the actions can be executed experimentally. If an error occurs, the model can be restored according to the previously saved snapshot, and then another action can be executed, which is the "retry mechanism". Each snapshot corresponds to a "retry end". If no error occurs from saving the snapshot until the retry end is read in, the snapshot can be canceled and execution can continue.

[0162] For example, in the following code: int fun(int(*)(int(x)));

[0163] If the first "(" cannot be judged as a parameter list after reading, a model snapshot needs to be saved. The retry terminator corresponding to the snapshot is the last ")". Then, the model is experimentally executed according to the parameter list. If no error occurs from saving the snapshot to reading the retry terminator, the parameter list is considered to be in the brackets, and the previously saved snapshot can be canceled. Figure 2 A schematic diagram of the conflict resolution process is given.

[0164] Figure 2The judge-CSR in it means to select the action to be executed according to the current conflict, save-snapshot means to store the symbol stack, state stack, scope stack, current input symbol, etc. into the snapshot, snapshot-stack means that the snapshots are organized by stacks, that is, retries can also occur during the retry process, roll-back means to restore the model to the previous state, set-trial-end(x) means to set the symbol x as the retry end mark, cancel-trial-end(x) means to set the symbol x as the non-retry end mark, roll-back-flag is used to indicate whether roll-back has been executed before, if roll-back-flag is true, it means to execute roll-back, if roll-back-flag is false, it means not to execute roll-back. In addition, Figure 2 The token in is a lexical symbol, m and x are the table entries obtained by searching the first lookup table (Action table), R represents reduction, S represents shift, x=x(0) represents the first reduction process or the second reduction process, and x=x(1) represents the second reduction process or the first reduction process.

[0165] The present application embodiment provides another possible implementation method, in which:

[0166] Before searching the first lookup table and the second lookup table according to the part of speech of any lexical symbol to obtain the corresponding search result, the grammar of the predetermined programming language may be analyzed in advance to generate the first lookup table and the second lookup table, wherein the method of analyzing the grammar of the predetermined programming language to generate the first lookup table and the second lookup table includes:

[0167] Determine the categories and orders of various lexical symbols in a predetermined programming language;

[0168] Grammar symbols are divided to obtain terminal symbols and non-terminal symbols, wherein the grammar symbols include various lexical symbols and respective corresponding categories of the lexical symbols, the lexical symbols belong to terminal symbols, the categories belong to non-terminal symbols, and the non-terminal symbols represent the hierarchical structure of a predetermined programming language;

[0169] According to the corresponding order of various lexical symbols, determine the non-terminal symbols to which various lexical symbols belong respectively;

[0170] A first lookup table is generated based on the pre-generated state and terminal symbols of the predetermined programming language, and a second lookup table is generated based on the pre-generated state and non-terminal symbols of the predetermined programming language.

[0171] The grammar includes a start symbol and a production list; the start symbol is a predefined non-terminal symbol, and any production in the production list represents the relationship between a terminal symbol and a non-terminal symbol.

[0172] Any of the above productions includes a left part and a right part, the left part is a non-terminal symbol to which the right part belongs, and the right part is a sequence of terminal symbols and / or a sequence of non-terminal symbols.

[0173] The non-terminal symbol on the left side corresponds to a node in the syntax tree, and any terminal symbol or any non-terminal symbol on the right side is a child node of the node.

[0174] Specifically, a grammar is an accurate description of the structure of a predetermined programming language, which specifies the categories and order of various code elements (i.e., lexical symbols). For example, the grammar specifies that the code file of the C++ programming language is composed of categories such as class declarations, function definitions, and namespace declarations, among which namespace declarations can be composed of categories such as class declarations, function definitions, and namespace declarations. Such categories and code elements (i.e., lexical symbols) are collectively referred to as "grammar symbols".

[0175] Further, grammar symbols can be divided into "non-terminals" and "terminals". "Non-terminals" give the hierarchical structure of the programming language, which is the key to grammatical parsing and the goal of the embodiments of the present application. Intuitively speaking, the category corresponds to the "non-terminal", and the lexical symbols (token) that directly constitute the code are "terminals". Lexical symbols (token) can be directly converted into grammar symbols (symbol) as terminals, and non-terminals will correspond to multiple lexical symbols (tokens) or non-terminals after classification.

[0176] Take the code "struct A{int a;};" as an example. This code consists of lexical symbols (tokens) such as "struct", "A", "{", "int", "a", ";", "}", ";", etc. These lexical symbols (tokens) are terminals. The grammar specifies the terminals in this order, which can be classified as non-terminal symbols "class-specifier" (i.e. class declaration). Among them, "classification" can also be called "reduction" according to the terminology in the field of compilation. "Classification" and "reduction" below have the same meaning.

[0177] Among them, the common attributes of grammar symbols are shown in Table 2 below. The left side of Table 2 is the English name of each attribute, and the right side is the Chinese description or explanation corresponding to each English name.

[0178] Table 2 - Common properties of grammar symbols

[0179]

[0180] In addition, specific symbols have specific attributes that record the information needed by the model at runtime.

[0181] Furthermore, the grammar consists of a start symbol and a list of productions, as follows:

[0182] 1. Start symbol, which is a non-terminal symbol corresponding to the top-level description of the entire programming language. The start symbol in the embodiment of the present application is "translation-unit", where the start symbol corresponds to the root of the syntax tree.

[0183] 2. Production-list: A production-list describes the composition relationship between terminals and non-terminals. A production-list consists of a "left part" and a "right part". The left part is a non-terminal, and the right part is a sequence of terminals or non-terminals. Any production-list in the production-list can be formally expressed as: "left part → right part".

[0184] For example: selection-statement→if(condition)statement. The “selection-statement” is the left part, which is a non-terminal symbol. The right part consists of the sequence of the keyword “if”, the terminal symbol “(”, the non-terminal symbol “condition”, the terminal symbol “)”, and the non-terminal symbol “statement”, indicating that the sequence “if(condition)statement” on the right can be classified as the non-terminal symbol “selection-statement” on the left.

[0185] In the syntax tree, the non-terminal symbol on the left side of the production corresponds to a node, and the terminal and non-terminal symbols on the right side are its child nodes. The basic properties of the production are shown in Table 3 below. The left side of Table 3 is the English name of each property, and the right side is the Chinese description or explanation corresponding to each English name.

[0186] Table 3- Basic properties of production

[0187] left left part right Right length() The number of symbols on the right

[0188] Furthermore, the first lookup table generated based on the analysis of the grammar of the predetermined programming language can be an Action table, and the second lookup table can be a Goto table. The Action table is a two-dimensional mapping table of "state-terminal part of speech", that is, one state and one part of speech of a terminal correspond to one table item, and the Action table item can be formally represented as Action(state, cat), where state is the state and cat is the part of speech. The value of Action(state, cat) can be represented as a tuple (m, x), and there are five possible values ​​of m, as shown below:

[0189] 1. Shift (S) means that a new state can be moved into the state stack, and the currently read lexical symbol (token) is converted into a grammar symbol (symbol) and moved into the symbol stack. When m is S (shift), the value of x represents the new state.

[0190] 2. Reduction (R) means that a continuous symbol sequence of a certain length at the top of the symbol stack can be reduced according to a certain production rule, and this continuous symbol sequence is the right part of the production rule. Specifically, the reduction can be: pop this symbol sequence from the symbol stack, and at the same time pop the state sequence of the same length from the state stack, then push the left part of the corresponding production rule into the symbol stack, then search the Goto table for a new state for the current state at the top of the symbol stack and the left part just pushed, and push the new state found into the state stack. When m is R (reduction), x represents the serial number of the production rule to be reduced in the production rule list, and the production rule can be expressed as production-list(x).

[0191] 3. Reduce-Shift Conflict (CSR), which means that either shift or reduce is possible. When m is CSR, x is a tuple, the first component of which is the state to be shifted, and the second component is the number of the production to be reduced.

[0192] 4. Reduce-Reduce Conflict (CRR), which means that the reduction can be performed according to different production rules. When m is CRR, x is a tuple, and its two components represent the sequence numbers of the production rules to be reduced.

[0193] 5. NULL: the expression is wrong, that is, the model does not accept the current lexical symbol (token). When m is empty, x has no meaning and is agreed to be NULL.

[0194] The Action table is obtained by the LR1 algorithm from the "production list file" in the grammar. For the convenience of discussion, the embodiment of the present application uses Action(state, cat) to represent the Action table item. When there is no cat parameter, Action(state) represents the set of all parts of speech corresponding to state in the Action table.

[0195] Furthermore, the Goto table is a two-dimensional mapping table of "state-nonterminal symbol name", that is, one state and one nonterminal symbol correspond to one table entry, each table entry represents a state, and the table entries in the Goto table can be expressed as Goto(state,symbol-name), where state is the state and symbol-name is the nonterminal symbol name. The Goto table is used when reducing production rules, indicating the state that the parser should be in after the reduction. The nonterminal symbols corresponding to the top state of the current state stack in the Goto table represent various possibilities to be reduced next.

[0196] The Goto table is derived from the "production list file" in the grammar by the LR1 algorithm. For ease of discussion, the embodiment of the present application uses Goto(state, symbol-name) to represent the Goto table item. When there is no parameter symbol-name, Goto(state) represents all non-terminal symbols corresponding to state in the Goto table.

[0197] Furthermore, the symbol stack and the state stack are used as stack structures to store the symbolic relationship and state relationship of the model during runtime. The basic properties of the symbol stack and the state stack are shown in Table 4 below. In Table 4, the left side is the English name of each attribute, and the right side is the Chinese description or explanation corresponding to each English name.

[0198] Table 4- Basic properties of production

[0199] top() Top element of the stack pop() Pop the stack, the number is specified by parameter n push() Push the element specified by parameter x onto the stack size() Number of elements in the stack

[0200] Furthermore, each lexical symbol in the linear linked list is analyzed in turn, including:

[0201] When the current lexical symbol in the linear linked list is a lexical symbol indicating the start of a scope, a corresponding top scope is created at the top of the scope stack, and the lexical symbol after the lexical symbol indicating the start of the scope is written into the top scope; and, when the current lexical symbol in the linear linked list is a lexical symbol indicating the end of a scope, the top scope is popped.

[0202] Among them, the bottom scope of the scope stack is the global scope, and the scope other than the bottom scope is the local scope.

[0203] Specifically, if the lexical symbol currently read from the linear linked list is a lexical symbol indicating the start of a scope (e.g., "{"), a corresponding top scope is created at the top of the scope stack, and the lexical symbols (e.g., variables, keywords, etc.) after the lexical symbol indicating the start of the scope are written into the created top scope. If the lexical symbol indicating the start of the scope (e.g., "{") is encountered for the first time in the process of parsing the lexical symbols in the linear linked list, then the bottom of the scope stack is the top of the scope, and the bottom scope is the global scope. If the lexical symbol indicating the start of the scope (e.g., "{") is not encountered for the first time in the process of parsing the lexical symbols in the linear linked list, then a new scope is created at the top of the scope stack, i.e., the top scope, which is a local scope, and then the lexical symbols after the lexical symbol indicating the start of the scope are written into the created top scope.

[0204] Furthermore, if the lexical symbol currently read from the linear linked list is a lexical symbol indicating the end of the scope (for example, "}"), it indicates the end of the scope. At this time, the top scope of the stack is popped, that is, the lexical symbol written into the top scope of the stack is read out, and the created top scope of the stack is deleted.

[0205] Furthermore, in the C++ programming language, namespaces, types, and objects are semantically organized by "scopes", and scopes can be nested. Namespaces, types, function bodies, etc. have their own scopes, where identifiers in different scopes can have the same name, and type names and object names in the same scope can also have the same name. The embodiment of the present application uses a stack to analyze each scope, where the scope stack is an important reference for "dynamic part of speech determination".

[0206] Furthermore, the scope can be divided into namespace scope, class scope, function body scope, etc. Different scopes have their specific properties. The common properties of scope are shown in Table 5 below. In Table 5, the left side is the English name of each property, and the right side is the Chinese description or explanation corresponding to each English name.

[0207] Table 5 - Common properties of scopes

[0208]

[0209]

[0210] The present application embodiment provides another possible implementation method, in which:

[0211] In practical applications, the first lookup table and the second lookup table of the predetermined programming language can be pre-generated based on the grammar of the predetermined programming language. When the code file to be parsed of the predetermined programming language is subsequently generated, each lexical symbol in the linear linked list corresponding to the code file to be parsed can be analyzed in turn based on the pre-generated first lookup table and the second lookup table, so as to generate a syntax tree of the code file to be parsed, so that the code file to be parsed can be statically analyzed through the syntax tree.

[0212] Among them, each lexical symbol in the linear linked list has its own corresponding part of speech, and in the process of analyzing each lexical symbol in the linear linked list in turn based on the first lookup table and the second lookup table, the first lookup table and the second lookup table are actually searched according to the part of speech corresponding to each lexical symbol. Among them, the first lookup table determines the operation or action to be performed after reading the lexical symbol according to the part of speech of the lexical symbol, and the first lookup table will nestedly call the second lookup table. Therefore, before searching based on the first lookup table and the second lookup table, it is necessary to first determine the part of speech of the lexical symbol read in, so as to search the first lookup table and the second lookup table according to the part of speech of the lexical symbol to obtain the corresponding search results.

[0213] If the lexical symbol currently read is "int", its part of speech is determined to be "type name", then the first lookup table and the second lookup table can be searched according to the part of speech "type name" of the lexical symbol "int" to obtain the corresponding search results.

[0214] Specifically, determining the part of speech of any lexical symbol in the linear linked list includes:

[0215] Performing part-of-speech determination on any lexical symbol according to the context to determine the part-of-speech of any lexical symbol; and / or,

[0216] Guess the part of speech of any lexical symbol according to the context and determine the part of speech of any lexical symbol.

[0217] Specifically, the part of speech of any lexical symbol is determined according to the context, including any of the following situations:

[0218] When any lexical symbol is any of type name, object name and keyword, it is regarded as the part of speech of any lexical symbol;

[0219] When any lexical symbol is a lexical symbol of a preset type, determine whether any lexical symbol is a template list symbol or an operation symbol, and use the determination result as the part of speech of any lexical symbol;

[0220] Search for information in the scope and use the result as the part of speech of any lexical symbol.

[0221] Specifically, information is searched in the scope, and the search result is used as the part of speech of any lexical symbol, including:

[0222] Perform a lateral search in the scope, and use the search result of the lateral search as the part of speech of any lexical symbol;

[0223] A lateral search is a search in the current scope and the referenced namespace, or a search in the base class scope of the current scope.

[0224] Specifically, it also includes:

[0225] If no search result is found through the horizontal search, a vertical search is performed in the scope, and the search result of the vertical search is used as the part of speech of any lexical symbol;

[0226] A vertical search is a search in scopes that include the current scope.

[0227] Specifically, guessing the part of speech of any lexical symbol according to the context and determining the part of speech of any lexical symbol includes:

[0228] Guess whether the part of speech of any lexical symbol is a namespace name based on the context;

[0229] If the guess is not a namespace name, and when the guessed part of speech of any lexical symbol is a type name and not an object name, if the first lexical symbol after any lexical symbol is not a lexical symbol of the preset type, then the part of speech of any lexical symbol is determined to be a type name; if the first lexical symbol after any lexical symbol is a lexical symbol of the preset type, then the part of speech of any lexical symbol is determined by performing the first preset processing according to any lexical symbol.

[0230] Specifically, the method further includes:

[0231] If the guess is not a namespace name, and when the part of speech of any lexical symbol is guessed to be an object name and not a type name, the part of speech of any lexical symbol is determined to be an object name.

[0232] Specifically, the method further includes:

[0233] If the guess is not a namespace name, and when the guessed part of speech of any lexical symbol is an object name and a type name, determine whether any lexical symbol is a type name based on the context of any lexical symbol. If it is a type name, determine that the part of speech of any lexical symbol is a type name. If it is not a type name, determine the part of speech of any lexical symbol by performing a second preset processing based on any lexical symbol.

[0234] The specific contents involved in this implementation are described in detail below:

[0235] In the above grammar of the embodiment of the present application, type names (such as class names, structure names, union names, enumeration class names, etc.) and object names (such as variable names, function names, object names of custom classes, etc.) are used as terminal symbols to participate in the grammar formulation, so when reading in an identifier, it is necessary to determine whether the identifier is a type name (TYPE-NAME) or an object name (IDENTIFIER). Among them, the information of the current lexical symbol can be found from the previously read lexical symbols and the constructed syntax tree, so as to determine whether the current lexical symbol is a type name or an object name.

[0236] For example, the following code:

[0237]

[0238] Keywords introduced after C++98, such as final (final) and override (override), can also be used as object names. It is necessary to determine whether they are keywords based on the context during grammatical analysis. In other words, when any lexical symbol is any of a type name, an object name, and a keyword, it is used as the part of speech of any lexical symbol. That is, if any lexical symbol is a type name, the type name is used as the part of speech of any lexical symbol; if any lexical symbol is an object name, the object name is used as the part of speech of any lexical symbol; if any lexical symbol is a keyword, the keyword is used as the part of speech of any lexical symbol.

[0239] Another example is the following code:

[0240]

[0241]

[0242] For lexical symbols of preset types such as “<”, ”>”, it is necessary to determine whether it is the beginning or end of a template list, or a greater than sign or less than sign. For a lexical symbol of preset type “>>”, it is necessary to determine whether it is a shift operator, or two template list terminators written together. If they are written together, the “>>” needs to be replaced with two “>” in the linear linked list (token-list). In other words, when any lexical symbol is a lexical symbol of preset type, it is determined to be a template list symbol or an operator symbol, and the determination result is used as the part of speech of the lexical symbol.

[0243] Among them, all the special terminals that need to determine the word class in the production list file are shown in Table 6 below. In Table 6, the left side is the English name of each special terminal, and the right side is the Chinese description or explanation corresponding to each special terminal.

[0244] Table 6 - All special terminals that require part of speech determination

[0245]

[0246] In addition, since the syntax tree generated by the embodiment of the present application is used for static analysis of the code file, some incomplete codes are also acceptable. Among them, when the part of speech of some lexical symbols cannot be determined, the part of speech of the symbol is guessed by using the "part of speech guessing mechanism".

[0247] In other words, the overall process of determining the part of speech of any lexical symbol in the linear linked list may include the following three sub-processes: (1) determining the part of speech of keywords and operators; (2) determining the part of speech by searching for scope information; (3) determining the part of speech by guessing the part of speech. The following first describes the above (1) and (2) with specific examples, and then describes the above (3) with another implementation method.

[0248] For (1) and (2) above, for example, the following code:

[0249] struct A

[0250] {};

[0251] int A = 0;

[0252] int fun()

[0253] {

[0254] struct A a; / / #1

[0255] return A; / / #2

[0256] }

[0257] In this example, when analyzing "A" at #1, a scope information search is performed and it is found that "A" is both a class name and a variable name. However, the keyword "struct" is used to limit #1, so only class names are accepted. Therefore, the part of speech of "A" at #1 is TYPE-NAME (type name). When analyzing "A" at #2, it is found that both class names and variable names are currently accepted, and variable names are given priority. Therefore, the part of speech of "A" at #2 is IDENTIFIER (variable name). Among them, when the part of speech information cannot be obtained through "keyword, operator part of speech determination" and "scope information search", the "part of speech guessing" mechanism is turned on, that is, the part of speech is determined by part of speech guessing. Figure 3 A diagram of the process of determining the part of speech according to the above code example.

[0258] In addition, member functions defined in class declarations require special treatment because the implementation of member functions can precede the declaration of class members, such as the following code:

[0259]

[0260]

[0261] Among them, the declaration of member variable m is later than the definition of member function fun, which is legal. However, if the "m" in the function body of "fun" is parsed directly, the part-of-speech information cannot be obtained. Therefore, when reading the "{" after "fun()", the function body is transferred outside the class declaration and replaced with ";". The function declaration information after the transfer is stored in the terminator "TRIMED-DECL-INFO". Figure 4 A schematic diagram of the process of obtaining the parts of speech of keywords and operators is given.

[0262] In addition, scope information search is divided into "transverse search (find-transverely)" and "vertical search (find)". "Transverse search" means searching in the current scope and the referenced namespace or the base class scope of the current scope, and "vertical search" means searching in the scope that contains the current scope when "transverse search" has no result. Figure 5 A schematic diagram of the process of searching for information and determining parts of speech in a scope is given.

[0263] In the process of guessing the part of speech of any lexical symbol based on the context and determining the part of speech of any lexical symbol, the following key sub-processes are included: (1) like-simple-type-name: judge whether the part of speech can be TYPE-NAME (type name) through the relationship between the symbols before and after, and do not judge the case where the lexical symbol (token) is followed by "<". (2) match-parametric-type-name: when the lexical symbol (token) is followed by "<", judge whether "<" is the starting symbol of the template parameter list. (3) find-template-list-end: if "<" is the beginning of the template parameter list, return the corresponding ">". (4) handle-sticky-template-list-end: handle the template parameter list terminator ">>" written together.

[0264] in, Figure 6 A schematic diagram of the process of determining the part of speech of a lexical symbol by guessing the part of speech is given. Figure 6The x in the accept(x) function indicates the part of speech (e.g., type name, object name, etc.). The accept(x) function indicates whether the model can currently accept the lexical symbol token with x as the part of speech. The accept(x) function is equivalent to Action(state-stack.top(),x)! = (NULL,NULL), indicating that the entry found according to the first lookup table Action table is not empty, x indicates the part of speech, and state-stack.top() indicates the top of the state stack. In addition, Figure 6 In the acc-tp and acc-id are characters representing variable names. The first preset processing is the above-mentioned find-template-list-end processing and handle-sticky-template-list-end. The second preset processing is the above-mentioned match-parametric-type-name processing and handle-sticky-template-list-end processing, which will not be repeated here.

[0265] The present application embodiment provides another possible implementation method, in which:

[0266] After performing conflict elimination processing on any lexical symbol, the method further includes: performing error recovery processing on the processing result of the conflict elimination processing.

[0267] When the first table entry corresponding to any lexical symbol and the top state of the current state stack in the first lookup table is empty, error recovery processing is performed on any lexical symbol; and a syntax tree of the code file to be parsed is generated according to the processing result of the error recovery processing. Then, a syntax tree is generated according to the result after the error recovery processing.

[0268] Specifically, before performing error recovery processing on any lexical symbol, the method further includes:

[0269] Generate a prompt message to remind the user that an error has occurred in any lexical symbol, and display the prompt message.

[0270] Specifically, error recovery processing is performed on any lexical symbol, including:

[0271] Determine at least one set of predetermined combination of target lexical symbols, target states, and target non-terminal symbols that meet predetermined recovery conditions;

[0272] Based on any set of predetermined combinations of target lexical symbols, target states and target non-terminal symbols, error recovery processing is performed on any lexical symbol.

[0273] Specifically, the predetermined recovery condition is that the third table entry corresponding to the target lexical symbol and the second table entry in the first lookup table is non-empty;

[0274] The second table entry is a table entry corresponding to the target state and the target non-terminal symbol in the second lookup table.

[0275] Specifically, determining at least one set of predetermined combination forms of target lexical symbols, target states, and target non-terminal symbols that satisfy a predetermined recovery condition includes:

[0276] The state that skips a predetermined number of states from the top state of the current state stack is used as the target state;

[0277] Starting from any lexical symbol and skipping a predetermined number of lexical symbols, the lexical symbol is taken as the target lexical symbol;

[0278] Based on the correspondence between the states and the non-terminal symbols in the second lookup table, the non-terminal symbol corresponding to the target state is determined as the target non-terminal symbol.

[0279] Specifically, based on any set of predetermined combination forms of target lexical symbols, target states, and target non-terminal symbols, error recovery processing is performed on any lexical symbol, including:

[0280] In the state stack, the state above the target state is popped out;

[0281] In the symbol stack, the lexical symbols located above any lexical symbol are popped, and the number of lexical symbols popped is the same as the number of states popped in the state stack;

[0282] Error recovery is performed on any lexical symbol by pushing the target non-terminal symbol into the symbol stack and pushing the second table entry into the state stack.

[0283] Specifically, it also includes:

[0284] When there are multiple groups of target lexical symbols, target states and target non-terminal symbols in predetermined combinations that satisfy predetermined recovery conditions, error recovery processing is performed on any lexical symbol based on the first group of target lexical symbols, target states and target non-terminal symbols in predetermined combinations that satisfy the predetermined recovery conditions; or, error recovery processing is performed on any lexical symbol based on the target lexical symbols, target states and target non-terminal symbols in predetermined combinations with the highest priority among the multiple groups of predetermined combinations.

[0285] The above error recovery process is described in detail below:

[0286] When the corresponding entry between the read-in lexical symbol (token) and the current state stack top state in the first lookup table (i.e., Action table) is empty, that is, Action (state-stack.top(), token.cat) is (NULL, NULL), it means that the lexical symbol (token) is not accepted by the current model and there is a grammatical error. Such a lexical symbol is called an error-token (error lexical symbol).

[0287] Among them, we can start from the top of the current state stack and search downward to find a state s, search backward from the current input symbol sequence to find a lexical symbol t, and then find a non-terminal symbol A in the second lookup table Goto(s). These targets (s,A,t) to be found must meet the following conditions: Action(Goto(s,A),t)! = NULL,NULL (that is, the table entry found according to the first lookup table is not empty), which can be called the "recovery condition". After finding (s,A,t) that meets the recovery condition, all states above s are popped out of the stack, and the same number of symbols are popped out of the symbol stack, and then A is pushed into the symbol stack, and Goto(s,A) is pushed into the state stack, so that the error can be "skipped" and execution can continue. This process of skipping errors is called "error recovery".

[0288] Among them, there may be multiple results for the search of (s, A, t). When the first result is found, error recovery can be performed based on the result. This mode can be called "panic mode". Alternatively, multiple results can be compared, and the best result (for example, the highest priority) can be selected. Error recovery can be performed based on the best result. This mode can be called "preferred mode".

[0289] It should be noted that the present application embodiment divides the errors that occur into "code structural errors" and "general syntax errors". Among them, "code structural errors" are the most serious errors, such as errors in mismatched brackets. When such errors are encountered, they are not tolerated, and an error prompt is directly issued and the execution is terminated. However, general syntax errors are tolerated to a certain extent.

[0290] According to the above description of error recovery, when encountering the error symbol "error-token", a set of (s, A, t) can be found for error recovery (s is the state, A is the non-terminal symbol, t is the error-token or a symbol after it). The embodiment of the present application first finds all (s, A, t) that can be error recovered, and selects the best result according to the "optimal mode" (the best result is recorded as the result with the highest priority). If the best result cannot be found, it enters the "panic mode". Among them, the selection of the best result (that is, the result with the highest priority) is based on the following optimization principles:

[0291] 1. When searching for t, try not to jump out of the current scope, and do not search in the scope nested in the current scope. This will minimize the disruption of the scope and allow the code after the error code to be processed correctly.

[0292] 2. s should be as close to the top of the status stack as possible, and t should be as close to the error-token as possible, so as to discard as little code information as possible.

[0293] 3. A should be a non-terminal symbol that can represent the main program structure, that is, a non-terminal symbol that is close to the root node in the syntax tree, such as expression, statement, declaration-statement, etc. If A is a non-terminal symbol close to a leaf node, it is more likely to introduce new errors.

[0294] in, Figure 7 A schematic diagram of the error recovery process is given. Figure 7 A in the expression represents the target non-terminal symbol, t represents the target lexical symbol, get-tolerant-end-token represents the determination of the search boundary of t. Some symbols in the stack have scope boundary symbols as attributes, and the scope boundary can be used as the search boundary of t. In addition, when searching from the top of the symbol stack to the bottom, if an unpaired left bracket ("{", "(", "[")) is found, then the search boundary of t is the right bracket that is paired with it. get-tolerant-symbol-and-token represents the process of selecting (s, A, t), and stack-balance represents that in the process of searching for s, if the right bracket needs to be popped, then the paired left bracket and the symbol between the two brackets should also be popped, otherwise the scope will be disturbed.

[0295] SYMBOL-WEIGHT-MAP is a "name-value" binary mapping table that records all the non-terminal symbols that can be selected and their corresponding weights. The weight reflects the high or low level of the grammatical level of the non-terminal symbol, that is, the distance relationship with the root node. Its value is an empirical value. The larger the weight, the closer it is to the leaf node of the syntax tree. For example, "translation-unit" is the start symbol, that is, the root of the syntax tree, and its weight is 1. "Translation-unit" is composed of "declaration-seq", so the weight of "declaration-seq" is 2. Examples of important non-terminal symbols in the "name-value" binary mapping table SYMBOL-WEIGHT-MAP are shown in Table 7 below. In Table 7, the left side shows the English names of important non-terminal symbols, and the right side shows the Chinese descriptions and weights corresponding to the important non-terminal symbols, where the weights are in brackets.

[0296] Table 7- Chinese descriptions and weights of important non-terminal symbols

[0297] translation-unit Start symbol, indicating the entire compilation unit (1) declaration-seq Declaration sequence, consisting of declarations(2) declaration Declaration (2.1) function-body Function body(4) namespace-body Namespace declaration body(4) class-body Class declaration body(4) enum-body Enumeration declaration body(4) simple-declaration Simple declaration, non-template declaration(4) member-simple-declaration Simple declaration of class members (5.2) template-member-declaration Template class member declaration (5.2) compound-statement Compound statements (7.2) expression-statement Expression statements (7.3) declaration-statement Declaration statements (7.3) selection-statement Selection statements, such as if-else statements and switch statements (7.3) iteration-statement Loop statements, such as for, while, do-while, etc. (7.3) jump-statement Jump statements, such as return and goto statements 7.3, labeled-statement Label statements, such as case statements and custom label statements (7.3) condition Condition (7.6) template-parameter-list Template parameter lists (7.6) template-argument-list Template argument lists (7.6) template-argument Template Parameters(8) exception-declaration Exception declarations (8.1) expression Expression (9) expression-list Expression list (9) assignment-expression Assignment expressions (9.1) constant-expression Constant expressions (9.2) conditional-expression Conditional expressions (9.3) logical-or-expression Logical OR expressions (9.4) logical-and-expression Logical AND expressions (9.5) declarator Declarator(14)

[0298] Among them, the "panic mode" is similar to the "optimal mode" in general process, but it is not restricted by the SYMBOL-WEIGHT-MAP table and does not compare the results. As long as (s, A, t) that meets the "recovery condition" is found, error recovery is performed. Although the panic mode can definitely recover from the error, it may disrupt the scope and discard a lot of code information.

[0299] The present application embodiment provides another possible implementation method, in which:

[0300] Figure 8 A schematic diagram of the process of analyzing each lexical symbol in the linear linked list in turn based on the first lookup table and the second lookup table to generate a syntax tree of the code file to be parsed is given in the embodiment of the present application, and the process includes various sub-processes such as part of speech determination, searching the first lookup table and the second lookup, conflict elimination processing and error recovery processing. Among them, Figure 8 The "token" in the table represents the lexical symbol currently read in, "token-list.first" represents the first symbol of the linear linked list, "state-stack.top()" represents the state found according to the second lookup table, and "token.cat" represents the part of speech of the lexical symbol currently read in. The value of Action(state,cat) can be represented as a tuple (m,x), where the value of m is R, indicating reduction behavior, the value of m is S, indicating shift behavior, state represents state, cat represents part of speech, and token=token.next() means that the lexical symbol after the current lexical symbol is used as the current lexical symbol.

[0301] Furthermore, the shift process is indicated by the first lookup table (Action table), and when the read-in symbol and the related state are shifted into the symbol stack and the state stack, the "shift process" is executed, such as Fig. 9 As shown, Fig. 9Taking the lexical symbol (token) as an example, the processing flow of the shift process is introduced in detail. Among them, if the currently read lexical symbol (token) is related to the scope, the scope operation is performed first, and then the lexical symbol (token) is converted into a grammar symbol and moved into the symbol stack, and the new state is moved into the state stack. This process is the "shift process". The symbols related to the scope include "{", "}", "if", "for", "while", "switch", "catch", etc. For example, when the read lexical symbol (token) is "{", it is necessary to determine the type and operation of the relevant scope based on the symbol stack and the scope stack: (1) If the top of the symbol stack is "named-namespace-head", it means that "{" is the beginning of the namespace declaration. If the top of the symbol stack is "class-head", it means that "{" is the beginning of the class member declaration. It is necessary to create a relevant scope and push the new scope into the scope stack. When the "}" corresponding to "{" is read, it means that the new scope has been completed, and the new scope is popped out of the stack. (2) If the top of the symbol stack is a symbol such as "declarator", "constructor", "destructor", or "convertor", it means that "{" is the beginning of a function definition and the function body scope needs to be operated on. (3) If the top of the symbol stack is not within the expected range and the top of the current scope stack is the function body scope, it means that "{" is the beginning of a compound statement "compound-statement" and compound statements also have their own scope.

[0302] Since the C++0x standard allows new objects to be defined in the conditions or initialization process of if, while, for statements, a scope needs to be created when reading such symbols.

[0303] For example, the following code:

[0304]

[0305] Among them, a new scope (set as "if-scope") is created when "if" is read in. When the relevant production rule is reduced, the variable definition information of "a" in "if(int a=x)" will be written into "if-scope". When "a" in the "if-else" statement is analyzed later, it can be concluded that it is a local variable by "part of speech determination". "{a=1;}" in the "if" statement is still embedded in "if-scope" as a scope. "if-scope" can be popped out of the stack when the production rule: selection-statement→if(condition)statementelse statement is reduced.

[0306] For "(", it is sometimes also related to the scope, such as the following code:

[0307]

[0308]

[0309] Among them, when a member function of a class is implemented outside the class declaration scope, the scope of its parameter list is the class declaration scope. When analyzing the "type" in "int A::fun(type*p)", it can be directly concluded from the "part of speech determination" that it is the type name defined in "A", and according to the C++ standard, it does not need to be written as "int A::fun(A::type*p)".

[0310] Furthermore, the reduction process is indicated by the Action table. When the symbol sequence at the top of the symbol stack can be reduced according to a certain production rule, the "reduction process" is executed. The symbol sequence belonging to the right part of the production rule is popped from the symbol stack, the left part symbol is pushed into the stack, and the right part symbol sequence is recorded in the left part symbol. After executing all the reduction processes, the symbols are composed into a syntax tree. Among them, it is also necessary to record or derive the attributes of each symbol in the reduction process for use by various mechanisms in the embodiments of the present application.

[0311] Embodiment 2

[0312] Fig.10 A schematic diagram of a structure of a device for generating a syntax tree of a code file provided in an embodiment of the present application, such as Fig.10 As shown, the device 100 may include a parsing module 101, a first processing module 102 and a syntax tree generating module 103, wherein:

[0313] The parsing module 101 is used to parse each lexical symbol in the code file to be parsed and generate a corresponding linear linked list through the lexical parsing module when receiving a code file to be parsed in a predetermined programming language;

[0314] The first processing module 102 is used to analyze each lexical symbol in the linear linked list in turn based on the first lookup table and the second lookup table, and when it is determined that any lexical symbol belongs to a predetermined conflict type, perform corresponding conflict elimination processing on any lexical symbol;

[0315] The syntax tree generation module 103 is used to generate a syntax tree of the code file to be parsed according to the processing result of the conflict elimination processing.

[0316] Specifically, the predetermined conflict type includes any of the following:

[0317] The processing of any lexical symbol is a conflict between shift processing and reduction processing;

[0318] The processing of any term symbol is a conflict between the first reduction processing and the second reduction processing.

[0319] Furthermore, a storage module 104 is included, such as Fig.11 As shown, where:

[0320] The storage module 104 is used to save the current processing state to obtain a first saving result.

[0321] Furthermore, the first processing module includes a first processing submodule 1021 and a second processing submodule 1022. Fig.11 As shown, where:

[0322] The first processing submodule 1021 is used to perform a first target processing on any lexical symbol according to the context, and perform corresponding processing on the lexical symbols after any lexical symbol in sequence based on the first target processing;

[0323] The second processing submodule 1022 is used to delete the first saved result and continue to process subsequent lexical symbols accordingly when no processing error occurs until the processing of the retry end symbol is completed.

[0324] Further, the first processing module 102 includes a third processing submodule 1023, such as Fig.11 As shown, where:

[0325] The third processing submodule 1023 is used to perform recovery processing according to the first saved result when a processing error occurs in the process of performing corresponding processing on the lexical symbols after any lexical symbol in sequence, and perform second target processing on any lexical symbol, and perform corresponding processing on the lexical symbols after any lexical symbol in sequence.

[0326] Furthermore, the situation of performing conflict elimination processing on any lexical symbol includes any of the following:

[0327] When any lexical symbol belongs to the first predetermined type, if a conflict occurs between reduction processing and shift processing for any lexical symbol, the shift processing is performed on any lexical symbol;

[0328] When any lexical symbol belongs to the second predetermined type, if a conflict occurs between shift processing and reduction processing for any lexical symbol, determining to perform shift processing or reduction processing on any lexical symbol according to the linear relationship between the lexical symbols;

[0329] When any lexical symbol belongs to the third predetermined type, if a conflict occurs between the first reduction process and the second reduction process for any lexical symbol, it is determined whether the first reduction process or the second reduction process is performed on any lexical symbol according to the linear relationship between the lexical symbols.

[0330] Furthermore, it also includes a second processing module 105, such as Fig.11 As shown, where:

[0331] The second processing module 105 is used to perform error recovery processing on the processing result of the conflict elimination processing.

[0332] Furthermore, the first processing module 102 includes a part-of-speech determination submodule 1024 and an analysis submodule 1025, wherein:

[0333] The part-of-speech determination submodule 1024 is used to determine the part-of-speech of each lexical symbol in the linear chain list in turn, and whenever the part-of-speech of any lexical symbol is determined, the first lookup table and the second lookup table are searched according to the part-of-speech of any lexical symbol to obtain a corresponding search result;

[0334] The analysis submodule 1025 is used to analyze any lexical symbol according to the search result.

[0335] Furthermore, the predetermined programming language is any one of C++ programming language and C language.

[0336] Compared with the prior art, the device provided in the embodiment of the present application analyzes each lexical symbol in the linear linked list in turn based on the first lookup table and the second lookup table, and when it is determined that any lexical symbol belongs to a predetermined conflict type, performs corresponding conflict elimination processing on any lexical symbol, thereby effectively solving the situation where multiple possible processing behaviors are found according to the lookup table, but it is impossible to determine which behavior to select, and lays the necessary foundation for the subsequent generation of a syntax tree for the code file to be parsed; based on the processing result of the conflict elimination processing, a syntax tree for the code file to be parsed is generated, and a method for generating a syntax tree for a code file is provided, so that the completed code file can be statically analyzed through the syntax tree, so as to accurately and efficiently check and correct syntax errors, writing errors, etc. in the completed code file, greatly saving the time and energy of program developers.

[0337] Embodiment 3

[0338] The present application embodiment provides an electronic device, such as Fig.12 As shown, Fig.12 The electronic device 1200 shown includes: a processor 1201 and a memory 1203. The processor 1201 and the memory 1203 are connected, such as through a bus 1202. Further, the electronic device 1200 may also include a transceiver 1204. It should be noted that in actual applications, the transceiver 1204 is not limited to one, and the structure of the electronic device 1200 does not constitute a limitation on the embodiments of the present application.

[0339] The processor 1201 is used in the embodiment of the present application to implement Fig.10 or Fig.11 The functions of the parsing module, the first processing module and the syntax tree generation module shown in FIG. Fig.11 The functions of the storage module and the second processing module are shown.

[0340] Processor 1201 may be a CPU, a general-purpose processor, a DSP, an ASIC, an FPGA or other programmable logic device, a transistor logic device, a hardware component or any combination thereof. It may implement or execute various exemplary logic blocks, modules and circuits described in conjunction with the disclosure of this application. Processor 1201 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.

[0341] The bus 1202 may include a path to transmit information between the above components. The bus 1202 may be a PCI bus or an EISA bus, etc. The bus 1202 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Fig.12 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0342] The memory 1203 may be a ROM or other type of static storage device that can store static information and instructions, a RAM or other type of dynamic storage device that can store information and instructions, or an EEPROM, a CD-ROM or other optical disk storage, an optical disk storage (including a compressed optical disk, a laser disk, an optical disk, a digital versatile disk, a Blu-ray disk, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited to these.

[0343] The memory 1203 is used to store the application code for executing the solution of the present application, and the execution is controlled by the processor 1201. The processor 1201 is used to execute the application code stored in the memory 1203 to implement Fig.10 or Fig.11 The illustrated embodiment provides actions of a device for generating a syntax tree for a code file.

[0344] The electronic device provided by the embodiment of the present application includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, compared with the prior art, the following can be achieved: based on the first lookup table and the second lookup table, each lexical symbol in the linear linked list is analyzed in turn, and when it is determined that any lexical symbol belongs to a predetermined conflict type, corresponding conflict elimination processing is performed on any lexical symbol, thereby effectively solving the situation where multiple possible processing behaviors are found according to the lookup table, but it is impossible to determine which behavior to select, and laying the necessary foundation for the subsequent generation of a syntax tree for a code file to be parsed; based on the processing result of the conflict elimination processing, a syntax tree for the code file to be parsed is generated, and a method for generating a syntax tree for a code file is provided, so that the completed code file can be statically analyzed through the syntax tree, so that the syntax errors, writing errors, etc. in the completed code file can be accurately and efficiently checked and corrected, greatly saving the time and energy of program developers.

[0345] The embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored, and when the program is executed by a processor, the method shown in the first embodiment is implemented. Compared with the prior art, based on the first lookup table and the second lookup table, each lexical symbol in the linear linked list is analyzed in turn, and when it is determined that any lexical symbol belongs to a predetermined conflict type, the corresponding conflict elimination processing is performed on any lexical symbol, thereby effectively solving the situation where multiple possible processing behaviors are found according to the lookup table, but it is impossible to determine which behavior to select, and laying the necessary foundation for the subsequent generation of a syntax tree of a code file to be parsed; based on the processing result of the conflict elimination processing, a syntax tree of the code file to be parsed is generated, and a method for generating a syntax tree of a code file is provided, so that the written code file can be statically analyzed through the syntax tree, so that the syntax errors, writing errors, etc. in the written code file can be accurately and efficiently checked and corrected, which greatly saves the time and energy of program developers.

[0346] The computer-readable storage medium provided in the embodiments of the present application is applicable to any embodiment of the above method, which will not be described in detail here.

[0347] It should be understood that, although the steps in the flowchart of the accompanying drawings are displayed in sequence as indicated by the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least a part of the steps in the flowchart of the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a part of the sub-steps or stages of other steps.

[0348] The above description is only a partial implementation method of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.

Claims

1. A method for generating a syntax tree of a code file, characterized in that: include: When receiving a code file to be parsed in a predetermined programming language, the lexical parsing module parses each lexical symbol in the code file to be parsed and generates a corresponding linear linked list; Based on the first lookup table and the second lookup table, each lexical symbol in the linear linked list is analyzed in turn, and when it is determined that any lexical symbol belongs to a predetermined conflict type, a corresponding conflict elimination process is performed on the any lexical symbol; Generate a syntax tree of the code file to be parsed according to the processing result of the conflict elimination processing; The situation of performing conflict elimination processing on any of the glossary symbols includes any of the following: When any of the vocabulary symbols belongs to the first predetermined type, if a conflict occurs between reduction processing and shift processing of any of the vocabulary symbols, the any of the vocabulary symbols is subjected to shift processing; When any of the lexical symbols belongs to the second predetermined type, if a conflict occurs between shift processing and reduction processing of any of the lexical symbols, determining to perform shift processing or reduction processing on any of the lexical symbols according to a linear relationship between the lexical symbols; When any of the lexical symbols belongs to the third predetermined type, if a conflict occurs between the first reduction process and the second reduction process for any of the lexical symbols, it is determined whether the first reduction process or the second reduction process is to be performed on the any of the lexical symbols according to the linear relationship between the lexical symbols.

2. The method according to claim 1, characterized in that The predetermined conflict type includes any of the following: The processing of any lexical symbol is a conflict between shift processing and reduction processing; The processing of any terminology symbol is a conflict between the first reduction processing and the second reduction processing.

3. The method according to claim 2, characterized in that Before performing conflict elimination processing on any of the glossary symbols, the method further includes: The current processing state is saved to obtain a first saved result.

4. The method according to claim 3, characterized in that: Performing conflict elimination processing on any of the glossary symbols, including: Performing a first target processing on any lexical symbol according to the context, and performing corresponding processing on lexical symbols following any lexical symbol in sequence based on the first target processing; If no processing error occurs until the processing of the retry end character is completed, the first saved result is deleted, and the subsequent lexical symbols are processed accordingly.

5. The method according to claim 4, characterized in that Also includes: If a processing error occurs in the process of performing corresponding processing on the lexical symbols after any lexical symbol, recovery processing is performed according to the first saved result, and the second target processing is performed on any lexical symbol, and the lexical symbols after any lexical symbol are processed accordingly in sequence.

6. The method according to any one of claims 1 to 5, characterized in that: After performing conflict elimination processing on any of the glossary symbols, the method further includes: An error recovery process is performed on the result of the conflict resolution process.

7. The method according to any one of claims 1 to 5, characterized in that: The step of analyzing each lexical symbol in the linear linked list in sequence based on the first lookup table and the second lookup table includes: Determine the part of speech of each lexical symbol in the linear linked list in turn, and whenever the part of speech of any lexical symbol is determined, search the first lookup table and the second lookup table according to the part of speech of any lexical symbol to obtain a corresponding search result; According to the search result, any of the lexical symbols is analyzed.

8. The method according to any one of claims 1 to 5, characterized in that: The predetermined programming language is any one of C++ programming language and C language.

9. A device for generating a syntax tree of a code file, characterized in that: include: A parsing module, configured to parse each lexical symbol in the code file to be parsed and generate a corresponding linear linked list through a lexical parsing module when receiving a code file to be parsed in a predetermined programming language; A first processing module is used to analyze each lexical symbol in the linear linked list in turn based on the first lookup table and the second lookup table, and when it is determined that any lexical symbol belongs to a predetermined conflict type, perform corresponding conflict elimination processing on the any lexical symbol; A syntax tree generation module, used for generating a syntax tree of the code file to be parsed according to the processing result of the conflict elimination processing; The situation of performing conflict elimination processing on any of the glossary symbols includes any of the following: When any of the vocabulary symbols belongs to the first predetermined type, if a conflict occurs between reduction processing and shift processing of any of the vocabulary symbols, the any of the vocabulary symbols is subjected to shift processing; When any of the lexical symbols belongs to the second predetermined type, if a conflict occurs between shift processing and reduction processing of any of the lexical symbols, determining to perform shift processing or reduction processing on any of the lexical symbols according to a linear relationship between the lexical symbols; When any of the lexical symbols belongs to the third predetermined type, if a conflict occurs between the first reduction process and the second reduction process for any of the lexical symbols, it is determined whether the first reduction process or the second reduction process is to be performed on the any of the lexical symbols according to the linear relationship between the lexical symbols.

10. The device according to claim 9, characterized in that The predetermined conflict type includes any of the following: The processing of any lexical symbol is a conflict between shift processing and reduction processing; The processing of any terminology symbol is a conflict between the first reduction processing and the second reduction processing.

11. The device according to claim 10, characterized in that Also included is a storage module; The storage module is used to save the current processing state to obtain a first saving result.

12. The device according to claim 11, characterized in that The first processing module includes a first processing submodule and a second processing submodule; The first processing submodule is used to perform a first target processing on any lexical symbol according to the context, and to perform corresponding processing on lexical symbols following any lexical symbol in sequence based on the first target processing; The second processing submodule is used to delete the first saved result and continue to perform corresponding processing on subsequent lexical symbols when no processing error occurs until the processing of the retry end character is completed.

13. The device according to claim 12, characterized in that The first processing module includes a third processing submodule; The third processing submodule is used to perform recovery processing according to the first saved result when a processing error occurs in the process of performing corresponding processing on the lexical symbols after any lexical symbol in sequence, and perform second target processing on any lexical symbol, and perform corresponding processing on the lexical symbols after any lexical symbol in sequence.

14. The device according to any one of claims 9 to 13, characterized in that: Also included is a second processing module; The second processing module is used to perform error recovery processing on the processing result of the conflict elimination processing.

15. The device according to any one of claims 9 to 13, characterized in that: The first processing module includes a part-of-speech determination submodule and an analysis submodule; The part-of-speech determination submodule is used to determine the part-of-speech of each lexical symbol in the linear linked list in turn, and whenever the part-of-speech of any lexical symbol is determined, the first lookup table and the second lookup table are searched according to the part-of-speech of any lexical symbol to obtain a corresponding search result; The analysis submodule is used to analyze any of the lexical symbols according to the search result.

16. The device according to any one of claims 9 to 13, characterized in that: The predetermined programming language is any one of C++ programming language and C language.

17. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the method for generating a syntax tree of a code file according to any one of claims 1 to 8 is implemented.

18. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the method for generating a syntax tree of a code file according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • An event detection method and device

    CN101060396A