Programming language compiling method and device, readable storage medium and terminal equipment

By encapsulating the ST language detection module and conversion module in the compiler and using regular expressions for syntax matching and conversion, the problem of joint compilation of C++ and ST languages ​​is solved, and the readability and development efficiency of the code are improved.

CN119201126APending Publication Date: 2024-12-27BEIJING GUODIAN ZHISHEN CONTROL TONGDY
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411264008.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-10
Publication Date
2024-12-27

AI Technical Summary

Technical Problem

Existing compilers do not support the embedded ST language in C++, resulting in cumbersome code processing modules and low readability.

Method used

The ST language detection module and the ST language to C++ module are encapsulated in the compiler, and the ST language program files are syntaxed and converted through regular expressions to realize the joint compilation of C++ and ST languages.

Benefits of technology

It realizes the joint compilation of C++ and ST languages, improving the readability and development efficiency of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119201126A_ABST
    Figure CN119201126A_ABST
Patent Text Reader

Abstract

The invention provides a programming language compiling method and device, a readable storage medium and terminal equipment, and relates to the technical field of computers.The method comprises the steps that in response to a file compiling instruction, a to-be-compiled first program file is obtained; under the condition of determining that the file identifier of the first program file is a specified file identifier, performing grammar matching on a program text of the first program file through a regular expression to obtain a grammar matching result; if it is determined that the writing language of the first program file is the first program language according to the grammar matching result, the first program file is converted into a second program file based on a second program language. According to the method and the device, the ST language detection module and the ST language to C + + module are packaged in the compiler, so that the effect of compiling the C + + and the ST language together is realized by migrating the ST language in the C + + code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a programming language compiling method, a programming language compiling device, a computer-readable storage medium and a terminal device. Background Art

[0002] C++ and ST are both programming languages. Among them, ST language supports industrial behavior flexibly and conveniently. Using ST language to implement process flow can improve development efficiency and code readability. At present, there are many compilers for C++ language, such as GCC, TinyC, etc., and there are also compilers integrated into IDEs such as VisualStudio, while ST language compilers are generally integrated into PLC development software. At present, the host computer basic software and processing units in the industrial control industry usually require powerful and high-performance C++ language to develop the main framework, but the existing compilers do not support embedding ST language in C++, and C++ and ST language cannot be compiled together. Therefore, the modules currently involved in internal process flow processing are also developed in C++, resulting in cumbersome internal process flow codes and low readability. Summary of the invention

[0003] The purpose of the embodiments of the present application is to provide a programming language compilation method, a programming language compilation device, a computer-readable storage medium and a terminal device to solve the above-mentioned problems.

[0004] In order to achieve the above-mentioned object, the first aspect of the present application provides a programming language compiling method, comprising:

[0005] In response to a file compiling instruction, obtaining a first program file to be compiled;

[0006] When it is determined that the file identifier of the first program file is a designated file identifier, performing syntax matching on the program text of the first program file by using a regular expression to obtain a syntax matching result;

[0007] If it is determined according to the grammar matching result that the writing language of the first program file is the first program language, the first program file is converted into a second program file based on a second program language.

[0008] Optionally, the first programming language is ST language, and the second programming language is C++ language.

[0009] Optionally, the file identifier is a file name suffix identifier of the first program file.

[0010] Optionally, the designated file identifier is a file name suffix identifier of an ST language program file.

[0011] Optionally, performing syntax matching on the program text of the first program file by using a regular expression to obtain a syntax matching result includes:

[0012] Performing keyword scanning on a program text of the first program file to obtain at least one keyword of the first program file;

[0013] Based on the extracted keywords, the first program file is subjected to grammar recognition by using a preset regular expression rule to obtain a plurality of grammar recognition results for the first program file;

[0014] The grammar matching result is determined based on a plurality of grammar recognition results for the first program file.

[0015] Optionally, the grammar recognition result includes a grammar recognition result of true or a grammar recognition result of false; and determining the grammar matching result based on multiple grammar recognition results of the first program file includes:

[0016] Obtaining each grammar recognition result of the first program file;

[0017] Determine the proportion of true grammar recognition results to all grammar recognition results;

[0018] If the obtained ratio is not lower than a preset ratio threshold, it is determined that the writing language of the first program file is the first program language, and a syntax matching result indicating that the writing language of the first program file is the first program language is generated.

[0019] Optionally, converting the first program file into a second program file based on a second program language includes:

[0020] Performing keyword scanning on a program text of the first program file to obtain at least one keyword of the first program file;

[0021] Based on the extracted keywords, the first program file is subjected to grammar recognition by using a preset regular expression rule to extract program statements from the first program file;

[0022] A preset grammar matching table is called to convert the statements extracted from the first program file into statements satisfying the grammar of the second program language, thereby obtaining a converted second program file, wherein the grammar matching table at least includes a mapping relationship between different grammars of the first program language and different grammars of the second program language.

[0023] In a second aspect of the present application, a programming language compiling device is provided, comprising:

[0024] A file acquisition module, configured to acquire a first program file to be compiled in response to a file compilation instruction;

[0025] A syntax matching module is configured to, when determining that the file identifier of the first program file is a specified file identifier, perform syntax matching on the program text of the first program file through a regular expression to obtain a syntax matching result;

[0026] The grammar conversion module is configured to convert the first program file into a second program file based on a second program language if it is determined according to the grammar matching result that the writing language of the first program file is the first program language.

[0027] According to a third aspect of the present application, a computer-readable storage medium is provided, which stores a computer program that, when executed by a processor, causes the processor to execute the programming language compilation method as described above.

[0028] In a fourth aspect of the present application, a terminal 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 programming language compilation method when executing the computer program.

[0029] The embodiments provided in this application have the following beneficial effects:

[0030] This application encapsulates an ST language detection module and an ST language to C++ module in the compiler, thereby migrating the ST language in the C++ code, thereby achieving the effect of compiling C++ and ST languages ​​together.

[0031] Other features and advantages of the embodiments or implementations of the present application will be described in detail in the subsequent specific implementation section. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] The accompanying drawings are used to provide a further understanding of the embodiments of the present application and constitute a part of the specification. Together with the following specific implementations, they are used to explain the embodiments of the present application, but do not constitute a limitation on the embodiments of the present application. In the accompanying drawings:

[0033] Figure 1 A method flow chart schematically showing a programming language compilation method in an embodiment of the present application;

[0034] Figure 2 A schematic block diagram schematically shows a programming language compiling device according to an embodiment of the present application;

[0035] Figure 3 A schematic diagram of the structure of a terminal device according to an embodiment of the present application is shown schematically.

[0036] Description of Reference Numerals

[0037] 10-terminal device, 100-processor, 101-memory, 102-computer program. DETAILED DESCRIPTION

[0038] The specific implementation of the embodiment of the present application is described in detail below in conjunction with the accompanying drawings. It should be understood that the specific implementation described here is only used to illustrate and explain the embodiment of the present application, and is not used to limit the embodiment of the present application.

[0039] In order to solve the above problems, Figure 1 As shown, the first aspect of the present application provides a programming language compilation method, comprising:

[0040] S100, in response to a file compiling instruction, obtaining a first program file to be compiled;

[0041] S200, when it is determined that the file identifier of the first program file is a designated file identifier, performing syntax matching on the program text of the first program file by using a regular expression to obtain a syntax matching result;

[0042] S300: If it is determined according to the grammar matching result that the writing language of the first program file is the first program language, convert the first program file into a second program file based on the second program language.

[0043] In this way, the present application encapsulates the ST language detection module and the ST language to C++ module in the compiler, thereby migrating the ST language in the C++ code, thereby achieving the effect of compiling C++ and ST languages ​​together.

[0044] As you can understand, ST language, also known as Structured Text, is a high-level programming language designed specifically for automation and control applications. It is derived from the IEC 61131-3 standard, which defines a variety of languages ​​for programming programmable logic controllers (PLCs). Among them, ST language is popular in the field of industrial automation due to its advanced data structure, control structure and concise syntax. ST language supports complex algorithm implementation, which facilitates the implementation of logic control, data processing and mathematical operations. Its main features include its powerful data processing capabilities, flexible control structure and support for complex algorithms, allowing developers to express complex logic in a nearly natural language form, which is very suitable for implementing automation systems with complex requirements and high process control requirements. In practical applications, ST language is widely used in various industrial automation projects, ranging from simple logic control to complex robot programming. For example, in the manufacturing industry, ST language can be used to control robots on high-speed production lines, and in the energy industry, it is used to control power distribution in substations.

[0045] Among them, the method of the present application can be applied to the compiler constructed by the present application, and the compiler of the present application can be constructed by encapsulating the ST language detection module and the ST language to C++ module in the outer layer of the existing C++ compiler. In the present application, the first program language is the ST language, the second program language is the C++ language, and the file identifier is the file name suffix identifier of the first program file. For example, the file name suffix of the ST language program file is ".st". Then, in the present application, the specified file identifier is the file name suffix identifier of the ST language program file, that is, ".st".

[0046] In step S100, the file compilation instruction may be a startup instruction of the compiler. For example, the program file to be compiled is a control flow code file control.st written in ST language. When the compiler is started, the location path of the control.st file is passed to the compiler as a parameter. The compiler obtains the control.st file to be compiled through the obtained location path.

[0047] In step S200, the compiler first identifies the file name of the first program file obtained, for example, the file name of the control.st file is identified, and if the file name suffix of the control.st file is identified as ".st", the control.st file is determined to be a program file in the ST language. Then, the preset regular expression rule is called to perform syntax matching on the program text of the control.st file.

[0048] In step S200, a syntax match is performed on the program text of the first program file by using a regular expression to obtain a syntax match result, including:

[0049] S210 : Perform keyword scanning on a program text of a first program file to obtain at least one keyword of the first program file.

[0050] It can be understood that the keywords of the ST language include but are not limited to data type keywords, such as BOOL, BYTE, WORD, DWORD, SINT, INT, DINT, LINT, USINT...; declaration keywords such as VAR, VAR_INPUT, VAR_OUTPUT, VAR_IN_OUT, VAR_GLOBAL, VAR_PERSISTENT, VAR_TEMP, CONST, etc.; process control keywords, such as IF / THEN / ELSE / ENDIF, CASE / OF / ESAC, FOR / TO / BY / DO / END_FOR, WHILE / DO / END_WHILE, REPEAT / UNTIL / END_REPEAT, etc.; other keywords such as: PROCEDURE, FUNCTION, FUNCTION_BLOCK, PROGRAM, RETURN, EXIT, etc.

[0051] S220: Based on the extracted keywords, perform syntax recognition on the first program file through a preset regular expression rule to obtain multiple syntax recognition results for the first program file. For example, taking a simple regular expression rule constructed in advance as an example, the regular expression can be expressed as: ^(?s).*\b(VAR|IF|FOR|WHILE|PROCEDURE|FUNCTION)\b.*\b:=.*$, where the meanings of the symbols are as follows:

[0052] ^: matches the beginning of a string;

[0053] (?s): turns on single-line mode, so that . can match a newline character;

[0054] .*: matches any character zero or more times;

[0055] \b: matches a word boundary;

[0056] (VAR|IF|FOR|WHILE|PROCEDURE|FUNCTION): matches Structured Text keywords;

[0057] \b: matches a word boundary;

[0058] .*\b:=.*: matches:=assignment operation;

[0059] $: Matches the end of the string.

[0060] Taking the code INT a:=10 in the program text to be matched as an example, after matching the statement with the above regular expression rules, it can be determined that the statement satisfies the grammar of the ST language, and a grammar recognition result thereof is obtained. It can be understood that the grammar recognition result includes a grammar recognition result of true or a grammar recognition result of false, wherein when the grammar recognition result is true, it indicates that the grammar of the current statement satisfies the ST grammar, and when the grammar recognition result is false, it indicates that the grammar of the current statement does not satisfy the ST grammar. Similarly, the regular expression statement can be used to perform grammar matching on keywords such as VAR, IF, FOR, WHILE, PROCEDURE, FUNCTION, and assignment operations:= related program statements. For example, through the pre-configured regular expression, when the IF keyword is detected, the subsequent keywords corresponding to IF, such as THEN, END_IF, etc., can be further scanned, and the corresponding statements between these keywords can be determined according to the regular expression. If they are not filled in according to the grammar rules corresponding to the ST language, an error message is returned, and a grammar matching result indicating that the statement does not satisfy the SY grammar is generated. It can be understood that the syntax matching of the ST program file can be performed line by line scanning and matching through regular expressions, and the corresponding syntax matching results can be obtained for each line or multiple lines of statements.

[0061] S230, determining a grammar matching result based on multiple grammar recognition results of the first program file, including: obtaining each grammar recognition result of the first program file, for example, a grammar recognition result for each line of statements; determining the proportion of grammar recognition results that are true to all grammar recognition results, for example, after performing grammar matching on all program statements of the ST program file, N matching results are obtained, of which n are true results, then calculating n / N; if the obtained proportion is not lower than a preset proportion threshold, for example, if n / N≥90%, then determining that the writing language of the first program file is the first program language, i.e., the ST language, generating a grammar matching result that indicates that the writing language of the first program file is the first program language, i.e., outputting a grammar matching result indicating that the first program file is an ST language program file. After confirming that the first program file is an ST code file, calling a language conversion module to perform grammar conversion on the first program file.

[0062] It can be understood that in the present application, syntax matching of ST program files can also be achieved by using existing technologies. For example, a lexical analyzer is constructed through the existing flex tool and pre-configured regular expressions, lexical analysis is performed on the ST program files, scanned keywords are marked with symbols, and a syntax analyzer is constructed based on the pre-configured regular expressions of the existing bison syntax analysis toolset. The syntax analyzer establishes a syntax tree according to the symbols returned by the lexical analyzer and the rules of the pre-configured regular expressions, and then syntax matching of the ST program files is performed.

[0063] In step S300, the first program file is converted into a second program file based on a second program language, including: performing keyword scanning on the program text of the first program file to obtain at least one keyword of the first program file; based on the extracted keywords, performing syntax recognition on the first program file through a preset regular expression rule to extract program statements from the first program file; calling a preset syntax matching table to convert the statements extracted from the first program file into statements that satisfy the syntax of the second program language to obtain the converted second program file, wherein the syntax matching table at least includes a mapping relationship between different syntaxes of the first program language and different syntaxes of the second program language. It can be understood that the pre-configured syntax matching table includes a mapping relationship between different syntaxes with the same semantics in the ST language and the C++ language, for example, the syntax matching table includes {(ST, INT; C++, int), (ST, :=; C++, =), ..., (ST, a; C++, a)}, indicating that INT in the ST language corresponds to int in the C++ language, := in the ST language corresponds to = in the C++ language, and so on. Taking the statement INTa:=10 extracted from the ST program file as an example, INT is matched with the syntax matching table, the C++ keyword int corresponding to INT is found in the syntax matching table, INT is replaced with int, and then based on the syntax matching table, a is matched and replaced with the corresponding variable a in the C++ language, and := is matched and replaced with the = in the C++ language in the syntax matching table, and 10 is matched and replaced with the integer 10 in C++ in the syntax matching table, so that INTa:=10 is interpreted as int a=10, thereby realizing the conversion of the ST language program file into a C++ language program file.

[0064] like Figure 2 As shown, in a second aspect of the present application, a programming language compiling device is provided, comprising:

[0065] A file acquisition module, configured to acquire a first program file to be compiled in response to a file compilation instruction;

[0066] A syntax matching module is configured to perform syntax matching on a program text of the first program file by using a regular expression to obtain a syntax matching result when it is determined that the file identifier of the first program file is a specified file identifier;

[0067] The grammar conversion module is configured to convert the first program file into a second program file based on a second program language if it is determined that the writing language of the first program file is the first program language according to the grammar matching result.

[0068] According to a third aspect of the present application, a computer-readable storage medium is provided, which stores a computer program that, when executed by a processor, causes the processor to execute the programming language compilation method as described above.

[0069] In a fourth aspect of the present application, a terminal 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 programming language compilation method when executing the computer program.

[0070] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0071] like Figure 3 FIG. 1 is a schematic diagram of a terminal device provided in an embodiment of the present application. Figure 3 As shown, the terminal device 10 of this embodiment includes: a processor 100, a memory 101, and a computer program 102 stored in the memory 101 and executable on the processor 100. When the processor 100 executes the computer program 102, the steps in the above method embodiment are implemented. Alternatively, when the processor 100 executes the computer program 102, the functions of each module / unit in the above device embodiments are implemented.

[0072] Exemplarily, the computer program 102 may be divided into one or more modules / units, one or more modules / units are stored in the memory 101, and are executed by the processor 100 to complete the present application. One or more modules / units may be a series of computer program instruction segments capable of completing specific functions, and the instruction segments are used to describe the execution process of the computer program 102 in the terminal device 10.

[0073] The terminal device 10 may be a computing device such as a desktop computer, a notebook, a PDA, or a cloud server. The terminal device 10 may include, but is not limited to, a processor 100 and a memory 101. Those skilled in the art will appreciate that Figure 3It is only an example of the terminal device 10 and does not constitute a limitation of the terminal device 10. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the terminal device may also include input and output devices, network access devices, buses, etc.

[0074] The processor 100 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc.

[0075] The memory 101 may be an internal storage unit of the terminal device 10, such as a hard disk or memory of the terminal device 10. The memory 101 may also be an external storage device of the terminal device 10, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the terminal device 10. Further, the memory 101 may also include both an internal storage unit of the terminal device 10 and an external storage device. The memory 101 is used to store computer programs and other programs and data required by the terminal device 10. The memory 101 may also be used to temporarily store data that has been output or is to be output.

[0076] A fourth aspect of the present application provides a computer-readable storage medium storing a computer program which, when executed by a processor, causes the processor to execute the alarm sounding method as described above.

[0077] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0078] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0079] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.

Claims

1. A programming language compiling method, characterized in that: include: In response to a file compiling instruction, obtaining a first program file to be compiled; When it is determined that the file identifier of the first program file is a designated file identifier, performing syntax matching on the program text of the first program file by using a regular expression to obtain a syntax matching result; If it is determined according to the grammar matching result that the writing language of the first program file is the first program language, the first program file is converted into a second program file based on a second program language.

2. The programming language compiling method according to claim 1, characterized in that: The first programming language is ST language, and the second programming language is C++ language.

3. The programming language compiling method according to claim 1, characterized in that: The file identifier is a file name suffix identifier of the first program file.

4. The programming language compiling method according to claim 2, characterized in that: The designated file identifier is a file name suffix identifier of the ST language program file.

5. The programming language compiling method according to claim 1, characterized in that: Performing syntax matching on the program text of the first program file by using a regular expression to obtain a syntax matching result includes: Performing keyword scanning on a program text of the first program file to obtain at least one keyword of the first program file; Based on the extracted keywords, the first program file is subjected to grammar recognition by using a preset regular expression rule to obtain a plurality of grammar recognition results for the first program file; The grammar matching result is determined based on a plurality of grammar recognition results for the first program file.

6. The programming language compiling method according to claim 5, characterized in that: The grammar recognition result includes a grammar recognition result of true or a grammar recognition result of false; Determining the grammar matching result based on multiple grammar recognition results of the first program file includes: Obtaining each grammar recognition result of the first program file; Determine the proportion of true grammar recognition results to all grammar recognition results; If the obtained ratio is not lower than a preset ratio threshold, it is determined that the writing language of the first program file is the first program language, and a syntax matching result indicating that the writing language of the first program file is the first program language is generated.

7. The programming language compiling method according to claim 1, characterized in that: Converting the first program file into a second program file based on a second program language comprises: Performing keyword scanning on a program text of the first program file to obtain at least one keyword of the first program file; Based on the extracted keywords, the first program file is subjected to grammar recognition by using a preset regular expression rule to extract program statements from the first program file; A preset grammar matching table is called to convert the statements extracted from the first program file into statements satisfying the grammar of the second program language, thereby obtaining a converted second program file, wherein the grammar matching table at least includes a mapping relationship between different grammars of the first program language and different grammars of the second program language.

8. A programming language compiling device, characterized in that: include: A file acquisition module, configured to acquire a first program file to be compiled in response to a file compilation instruction; A syntax matching module is configured to, when determining that the file identifier of the first program file is a specified file identifier, perform syntax matching on the program text of the first program file through a regular expression to obtain a syntax matching result; The grammar conversion module is configured to convert the first program file into a second program file based on a second program language if it is determined according to the grammar matching result that the writing language of the first program file is the first program language.

9. A computer-readable storage medium storing a computer program which, when executed by a processor, causes the processor to execute the programming language compiling method according to any one of claims 1 to 7.

10. A terminal 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 computer program, the programming language compiling method according to any one of claims 1 to 7 is implemented.

Citation Information

Cited By

  • Code generation method and device, equipment and storage medium

    CN121477769A