Word and syntactic analyzer for converting C language code into PROMELA language

By developing a word and grammar analyzer that converts C language code into PROMELA language, the problem of professional knowledge required for writing PROMELA programs and LTL regulations in the prior art is solved, and users can use SPIN for model detection without professional knowledge, and greatly save development time.

CN120179259APending Publication Date: 2025-06-20TOEC (GRP) CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311754106.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-12-20
Publication Date
2025-06-20

AI Technical Summary

Technical Problem

When using the SPIN model detection method in the prior art, it requires professional knowledge to write PROMELA programs and LTL regulations, and the system learning time is long and the professionalism requirements are high.

Method used

Develop a word and grammar analyzer that converts C language code into PROMELA language. Through the conversion of code, users do not need to have an in-depth understanding of the writing rules of PROMELA language, and automatically generate code to reduce developers' time and professional requirements.

Benefits of technology

This enables users to use SPIN for model detection without having to understand the PROMELA language. Automatically generating code greatly saves developers' time, reduces professional requirements, and improves conversion efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120179259A_ABST
    Figure CN120179259A_ABST
Patent Text Reader

Abstract

The invention discloses a word and syntactic analyzer for converting a C language code into a PROMELA language, which comprises a file reading module, a lexical analysis module, a data transmission module, a syntactic analysis module, an output module and a file writing module, the file reading module is in forward connection with the lexical analysis module, the data transmission module, the grammar analysis module and the file writing module in sequence, and the lexical analysis module and the grammar analysis module are both in forward connection with the output module. And meanwhile, the time of developers is greatly saved due to automatic code generation, and the method has a wide application prospect and is beneficial to popularization and application.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of language conversion, and in particular, to a word and syntax analyzer for converting C language code into PROMELA language. Background Art

[0002] Currently, the SPIN model checking method is widely used. Compared with traditional testing methods, the SPIN model checking method has the following advantages: 1. Comprehensiveness: SPIN can comprehensively traverse the state space of the system, so as to detect all possible states of the system. 2. Formal verification: SPIN uses formal specifications, which can perform precise mathematical analysis on the system, avoiding omissions and errors that may occur in some tests. However, the SPIN model checking method still has many disadvantages, such as the professionalism of specification expression: writing PROMELA programs and LTL specifications requires professional knowledge and a long time of system learning, and has high professionalism requirements. Therefore, there is an urgent need to develop a word and syntax analyzer for converting C language code into PROMELA language to solve the above technical problems.

[0003] In view of this, the present invention is specifically proposed. Summary of the Invention

[0004] The purpose of the present invention is to provide a word and syntax analyzer for converting C language code into PROMELA language. Through code conversion, developers can use SPIN for model checking without having to deeply understand the writing rules of PROMELA language. At the same time, automatic code generation greatly saves the time of developers, has broad application prospects, and is conducive to popularization and application.

[0005] To achieve the above purpose, the present invention provides a word and syntax analyzer for converting C language code into PROMELA language, including a file reading module, a lexical analysis module, a data transmission module, a syntax analysis module, an output module, and a file writing module. The file reading module is positively connected to the lexical analysis module, the data transmission module, the syntax analysis module, and the file writing module in sequence. The lexical analysis module and the syntax analysis module are both positively connected to the output module.

[0006] Preferably, the file reading module is positively connected to the lexical analysis module, and reads the data in the specified file in the form of a stream for subsequent operations of the lexical analysis module.

[0007] Preferably, the lexical analysis module is positively connected to the output module and the data transmission module respectively, parses the read character stream, distinguishes the meaning of each character set, and returns it as a token of the corresponding type to the data transmission module. If an error is encountered, a prompt message will be returned to the output module.

[0008] Preferably, the data transmission module is connected forwardly to the syntax analysis module, receives the tokens returned by the lexical analysis module, and transmits them to the syntax analysis module.

[0009] Preferably, the syntax analysis module is connected forwardly to the file writing module and the output module, parses the transmitted token stream, the output module receives the output information transmitted by other modules, and outputs it on the screen.

[0010] Preferably, the file reading module uses a C language program segment to read the input file character by character by reading the file stream, and transmits it to the lexical analysis module in the form of a stream.

[0011] Preferably, the file writing module uses a C program segment to read out the values corresponding to the received terminators and output them to the destination file.

[0012] Preferably, the lexical analysis module uses the lexical analysis rules officially released by referring to C language, deletes the keywords that cannot be converted to obtain the lex.l file, generates the lex.yy.c lexical analysis file after compilation by gcc. The lexical analysis module receives the character stream transmitted by the file reading module and pushes it onto the stack. If a space or carriage return symbol is encountered, it is regarded as a delimiter, and the string between the two delimiters is matched according to the lexical rules, returns the corresponding type of token to the data transmission module, and outputs the corresponding information in the command line. After receiving the token from the lexical analysis module, the data transmission module transmits it to the syntax analysis module through the yylex method.

[0013] Preferably, the syntax analysis module writes the syntax rules and corresponding operations into the yacc.y file, generates the yacc.tab.c and yacc.tab.h syntax analysis files after compilation by gcc, regards the tokens as non-terminals, pushes the non-terminals onto the stack one by one, judges whether the current state in the stack meets any reduction rule. If it meets, the corresponding non-terminal is converted into another non-terminal or terminal specified by the reduction rule, and the non-terminal continues to flow into the stack. This process is repeated until there is only one terminal in the stack at the end, and this process ends. At the same time, the terminal and its value in the stack are written into the specified output file, and the analysis information is output.

[0014] Preferably, the output module uses a C program segment to output the corresponding prompt information in the command line interface after certain specific processes end. If an error occurs in a certain process, error information will be output.

[0015] The word and syntax analyzer for converting C language code into PROMELA language provided by the present invention has the following beneficial effects.

[0016] 1. Through the conversion of code, users can use SPIN for model checking without having to deeply understand the writing rules of the PROMELA language, with low professional requirements.

[0017] 2. The present invention can automatically generate code, greatly saving the time of developers and improving the conversion efficiency.

[0018] 3. The present invention is written using code and tools oriented to the computer bottom layer, and the running speed is faster than that using high-level languages. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 It is a structural block diagram of a word and syntax analyzer for converting C language code into PROMELA language provided by the present invention; Figure 2 It is a working flowchart of a word and syntax analyzer for converting C language code into PROMELA language provided by the present invention. DETAILED DESCRIPTION OF THE INVENTION

[0020] The following further describes the present invention in conjunction with specific embodiments and drawings to help understand the content of the present invention.

[0021] As Figure 1 shown, it is a structural block diagram of a word and syntax analyzer for converting C language code into PROMELA language provided by the present invention. The word and syntax analyzer for converting C language code into PROMELA language includes a file reading module, a lexical analysis module, a data transmission module, a syntax analysis module, an output module, and a file writing module. The file reading module is sequentially and positively connected to the lexical analysis module, the data transmission module, the syntax analysis module, and the file writing module. The lexical analysis module and the syntax analysis module are both positively connected to the output module.

[0022] The file reading module is positively connected to the lexical analysis module, reads the data in the specified file in the form of a stream for subsequent operations of the lexical analysis module. The lexical analysis module is positively connected to the output module and the data transmission module, parses the read character stream, distinguishes the meaning of each character set and returns it as a corresponding type of token to the data transmission module, and returns a prompt message to the output module if an error is encountered. The data transmission module is positively connected to the syntax analysis module, receives the token returned by the lexical analysis module, and transmits it to the syntax analysis module. The syntax analysis module is positively connected to the file writing module and the output module, parses the transmitted token stream, and the output module receives the output information transmitted by other modules and outputs it on the screen.

[0023] The file reading module uses a C language program fragment to read the input file character by character by reading the file stream and transmit it to the lexical analysis module in the form of a stream. The file writing module uses a C program fragment to read the value corresponding to the received terminal symbol and output it to the target file.

[0024] The lexical analysis module uses the lexical analysis rules officially released by C language. After deleting the keywords that cannot be converted, it obtains the lex.l file. After compiling with gcc, it generates the lex.yy.c lexical analysis file. The lexical analysis module receives the character stream transmitted by the file reading module and pushes it onto the stack. If a space or carriage return symbol is encountered, it is regarded as a delimiter. According to the lexical rules, it matches the string between two delimiters, returns the corresponding type of token to the data transmission module, and outputs the corresponding information on the command line. After receiving the token from the lexical analysis module, the data transmission module transmits it to the syntax analysis module through the yylex method.

[0025] The syntax analysis module writes the syntax rules and corresponding operations into the yacc.y file. After compiling with gcc, it generates the yacc.tab.c and yacc.tab.h syntax analysis files. It regards the token as a non-terminal symbol and pushes the non-terminal symbols onto the stack one by one. It judges whether the current state in the stack satisfies any reduction rule. If it does, it converts the corresponding non-terminal symbol into another non-terminal symbol or terminal symbol specified by the reduction rule, and continues to flow the non-terminal symbol into the stack. This process is looped until there is only one terminal symbol left in the stack at the end, and this process ends. At the same time, the terminal symbol and its value in the stack are written into the specified output file, and the analysis information is output. The output module uses a C program fragment to output the corresponding prompt information on the command line interface after certain specific processes end. If an error occurs in a certain process, an error message will be output.

[0026] Such as Figure 2As shown in the figure, it is the working flowchart of a word and syntax analyzer that converts C language code into PROMELA language. When working, the lexical analyzer lex.yy.c and the syntax analyzer yacc.tab.c are compiled together, and the parameter is set to the file name of the C program to be analyzed to start the working process. In this process, first, the file reading module reads the input file character by character in the form of a file stream and transmits it to the lexical analysis module in the form of a stream. The lexical analysis module will receive the character stream transmitted by the file reading module and push it onto the stack. If symbols such as spaces and carriage returns are encountered, they are regarded as delimiters, and the string between two delimiters is matched according to the lexical rules, and the corresponding type of token is returned to the data transmission module, and the corresponding information is output on the command line. The data transmission module will send the received data to the syntax analysis module. After receiving the data from the data transmission module, the syntax analysis module performs shift / reduce operations on it in the process of continuously obtaining tokens until all non-terminal tokens are converted into a terminal token. After that, the file writing module writes the structure converted by the syntax analysis module into the output file output.pml. Thus, the conversion from a C program to a PROMELA program is completed.

[0027] Through the conversion of the code, users can use SPIN for model detection without having to deeply understand the writing rules of the PROMELA language, and the professional requirements are low. The present invention can automatically generate code, greatly saving the time of developers and improving the conversion efficiency. The present invention is written using code and tools oriented to the computer bottom layer, and the running speed is faster than that using high-level languages.

[0028] In this article, specific examples are used to elaborate on the inventive concept in detail. The description of the above embodiments is only used to help understand the core idea of the present invention. It should be pointed out that for those of ordinary skill in the art of this technology, any obvious modification, equivalent replacement or other improvement made without departing from the inventive concept should be included in the protection scope of the present invention.

Claims

1. A lexical and syntax analyzer for converting C language code into PROMELA language, characterized in that It includes a file reading module, a lexical analysis module, a data transmission module, a syntax analysis module, an output module, and a file writing module. The file reading module is connected in series with the lexical analysis module, the data transmission module, the syntax analysis module, and the file writing module in the forward direction. Both the lexical analysis module and the syntax analysis module are connected to the output module in the forward direction.

2. The lexical and syntax analyzer for converting C language code into PROMELA language according to claim 1, characterized in that The file reading module is connected to the lexical analysis module in the forward direction, reads the data in the specified file in the form of a stream, and provides it for subsequent operations of the lexical analysis module.

3. The lexical and syntax analyzer for converting C language code into PROMELA language according to claim 2, characterized in that The lexical analysis module is connected to the output module and the data transmission module in the forward direction, parses the read character stream, distinguishes the meaning of each character set, and returns the corresponding type of token to the data transmission module. If an error is encountered, a prompt message will be returned to the output module.

4. The lexical and syntax analyzer for converting C language code into PROMELA language according to claim 3, characterized in that The data transmission module is connected to the syntax analysis module in the forward direction, receives the token returned by the lexical analysis module, and transmits it to the syntax analysis module.

5. The lexical and syntax analyzer for converting C language code into PROMELA language according to claim 4, characterized in that The syntax analysis module is connected to the file writing module and the output module in the forward direction, parses the transmitted token stream. The output module receives the output information transmitted by other modules and outputs it on the screen.

6. The lexical and syntax analyzer for converting C language code into PROMELA language according to claim 5, characterized in that The file reading module uses a C language program segment to read the input file character by character by reading the file stream and transmits it to the lexical analysis module in the form of a stream.

7. The lexical and syntax analyzer for converting C language code into PROMELA language according to claim 6, characterized in that The file writing module uses a C program segment to read the value corresponding to the received terminal symbol and output it to the target file.

8. The lexical and syntax analyzer for converting C language code into PROMELA language according to claim 7, characterized in that The lexical analysis module uses the lexical analysis rules officially released by C language for reference, deletes the keywords that cannot be converted to obtain the lex.l file, and generates the lex.yy.c lexical analysis file after compilation by gcc. The lexical analysis module pushes the character stream transmitted by the file reading module onto the stack. If a space or carriage return symbol is encountered, it is regarded as a delimiter, and the string between the two delimiters is matched according to the lexical rules, and the corresponding type of token is returned to the data transmission module, and the corresponding information is output on the command line. After receiving the token from the lexical analysis module, the data transmission module transmits it to the syntax analysis module through the yylex method.

9. The lexical and syntax analyzer for converting C language code into PROMELA language according to claim 8, characterized in that The syntax analysis module writes the syntax rules and corresponding operations into the yacc.y file, generates the yacc.tab.c and yacc.tab.h syntax analysis files after compilation by gcc, regards the token as a non-terminal symbol, pushes the non-terminal symbols onto the stack one by one, and judges whether the current state in the stack satisfies any reduction rule. If it is satisfied, the corresponding non-terminal symbol is converted into another non-terminal symbol or terminal symbol specified by the reduction rule, and the non-terminal symbol continues to flow into the stack. This process is looped until there is only one terminal symbol in the stack at the end, and this process ends. At the same time, the terminal symbol and its value in the stack are written into the specified output file, and the analysis information is output.

10. The lexical and syntax analyzer for converting C language code into PROMELA language according to claim 9, characterized in that The output module uses a C program segment to output the corresponding prompt message on the command line interface after certain specific processes end. If an error occurs in a certain process, an error message will be output.