A PLC programming language retrieval method and terminal

By segmenting and indexing the code text of the PLC programming language, directory information is generated to record program block dependencies, the problem of low retrieval efficiency in the PLC programming environment is solved, rapid retrieval and intelligent analysis are realized, and modular development is supported.

CN119045808BActive Publication Date: 2025-01-17FUZHOU WECON ELECTRONICS TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411529538.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-10-30
Publication Date
2025-01-17
Estimated Expiration
2044-10-30

AI Technical Summary

Technical Problem

The existing PLC programming environment lacks an efficient retrieval mechanism, which makes it difficult for developers to quickly locate compilation units, data type definitions and program organization units in large projects, and lacks effective tracking of dynamic dependencies between source codes, affecting modular development and maintain.

Method used

By obtaining the code text of the target programming language project, segmenting the code text according to the program block keyword, extracting the identification information and location information of the program block unit, generating directory information, and creating indexes to achieve in-depth analysis and retrieval of the code text, and unifying resource indexing , record the dependencies between program blocks.

Benefits of technology

It realizes rapid retrieval and intelligent analysis of the ST programming language, supports modular development and maintenance, improves code reading and understanding efficiency, and shortens the error diagnosis process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119045808B_ABST
    Figure CN119045808B_ABST
Patent Text Reader

Abstract

The present invention discloses a search method and terminal for a PLC programming language, the method comprising: obtaining a code text of a target programming language project; segmenting the code text according to a program block keyword to obtain a plurality of program block units, and extracting identification information and location information of the program block units; generating directory information of the program block units according to the identification information and the location information; creating an index according to the directory information of the plurality of program block units; and locating the target code in the code text through the index in response to a search instruction of a target code. The present invention can realize rapid search and intelligent analysis of the ST programming language, and is convenient for modular development and maintenance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of PLC programming languages, and in particular to a retrieval method and a terminal for a PLC programming language. Background Art

[0002] Currently, PLC (Programmable Logic Controller) programming environments usually use basic file systems to manage ST codes (a structured programming language). This management method lacks an efficient retrieval mechanism to quickly locate compilation units, data type definitions, and program organization units.

[0003] For large projects, developers often rely on simple text searches or limited search functions provided by IDEs, resulting in low development efficiency. This search method is mainly based on string matching and cannot deeply understand the code structure and semantics, resulting in a time-consuming and inaccurate search process. In addition, this search method lacks a unified resource index, which makes the dependencies between data types and program organization units confusing, which is not conducive to modular development and maintenance; and most retrieval schemes only perform one-time source code parsing and lack effective tracking of dynamic dependencies between source codes. This makes it difficult to fully understand the mutual influence between components when analyzing complex project structures, especially when there is a lot of reuse and inheritance. Therefore, it is difficult for existing retrieval methods to effectively manage and reuse code resources. Summary of the invention

[0004] The technical problem to be solved by the present invention is to provide a PLC programming language retrieval method and terminal, which can realize rapid retrieval and intelligent analysis of ST programming language, and facilitate modular development and maintenance.

[0005] In order to solve the above technical problems, the technical solution adopted by the present invention is:

[0006] A PLC programming language retrieval method, comprising:

[0007] Get the code text of the target programming language project;

[0008] Segmenting the code text according to program block keywords to obtain a plurality of program block units, and extracting identification information and location information of the program block units;

[0009] Generate directory information of the program block unit according to the identification information and the location information;

[0010] Creating an index based on the directory information of the plurality of program block units;

[0011] In response to a retrieval instruction of the target code, the target code is located in the code text through the index.

[0012] In order to solve the above technical problems, another technical solution adopted by the present invention is:

[0013] A retrieval terminal for a PLC programming language comprises a memory, a processor and a computer program stored in the memory and running on the processor. When the processor executes the computer program, each step in the retrieval method for a PLC programming language is implemented.

[0014] The beneficial effects of the present invention are: the code text is divided into multiple program block units by program block keywords, and the identification information and location information of the program block units are extracted. Based on the identification information and location information of each program block unit, the directory information corresponding to each program block unit is generated respectively, and an index is created based on the directory information, so that the index can perform in-depth analysis and retrieval on each program block unit. In addition, this method can perform unified resource indexing on the entire code text, effectively store the dependency analysis between program blocks, realize rapid retrieval and intelligent analysis of the ST programming language, and facilitate modular development and maintenance. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] Figure 1 A flowchart of a method for searching a PLC programming language provided by an embodiment of the present invention;

[0016] Figure 2 A code schematic diagram of a program block unit provided by an embodiment of the present invention;

[0017] Figure 3 A code schematic diagram of a variable block unit provided by an embodiment of the present invention;

[0018] Figure 4 A code schematic diagram of a variable code segment provided by an embodiment of the present invention;

[0019] Figure 5 A schematic diagram of a process of converting row and column information of an offset value provided by an embodiment of the present invention;

[0020] Figure 6 A schematic diagram of a flow chart of searching a target character string based on an index provided by an embodiment of the present invention;

[0021] Figure 7 A schematic diagram of the structure of a retrieval terminal for a PLC programming language provided by an embodiment of the present invention;

[0022] Description of labels:

[0023] 100. A retrieval terminal for a PLC programming language; 101. Memory; 102. Processor. DETAILED DESCRIPTION

[0024] In order to explain the technical content, achieved objectives and effects of the present invention in detail, the following is an explanation in combination with the implementation modes and the accompanying drawings.

[0025] An embodiment of the present invention provides a method for retrieving a PLC programming language, comprising:

[0026] Get the code text of the target programming language project;

[0027] Segmenting the code text according to program block keywords to obtain a plurality of program block units, and extracting identification information and location information of the program block units;

[0028] Generate directory information of the program block unit according to the identification information and the location information;

[0029] Creating an index based on the directory information of the plurality of program block units;

[0030] In response to a retrieval instruction of the target code, the target code is located in the code text through the index.

[0031] From the above description, it can be seen that the beneficial effects of the present invention are: the code text is divided into multiple program block units through program block keywords, and the identification information and location information of the program block units are extracted. Based on the identification information and location information of each program block unit, the directory information corresponding to each program block unit is generated respectively, and an index is created based on the directory information, so that the index can perform in-depth analysis and retrieval of each program block unit. In addition, this method can perform unified resource indexing on the entire code text, effectively store the dependency analysis between program blocks, realize rapid retrieval and intelligent analysis of the ST programming language, and facilitate modular development and maintenance.

[0032] Further, the identification information includes program block identification information, variable block identification information and variable identification information;

[0033] The position information includes program block position information and variable position information;

[0034] The extracting the identification information and location information of the program block unit includes:

[0035] Extracting program block identification information and program block location information of the program block unit;

[0036] Splitting the program block unit according to the variable block keyword to obtain a plurality of variable block units corresponding to the program block unit, and extracting variable block identification information of the variable block unit;

[0037] A variable code segment is extracted from the variable block unit based on the variable block keyword, and variable identification information and variable position information of the variable code segment are extracted.

[0038] From the above description, it can be seen that each program block contains multiple variable blocks, each variable block contains variable code segments, and each program block unit is divided step by step through different keywords, so that the subsequent index structure can deeply analyze the syntax tree structure of the target programming language project based on the hierarchical architecture of the code text, thereby realizing fast retrieval.

[0039] Further, the extracting the variable identification information and the variable location information of the variable code segment includes:

[0040] Extracting variable identification information of each variable in the variable code segment by using a regular expression;

[0041] The variable position information is obtained by extracting the offset value of the variable identification information relative to the starting position of the code text.

[0042] As can be seen from the above description, regular expression is a text processing tool used to match character combinations in strings. Since the variable code segment may contain multiple types of strings, in order to ensure extraction efficiency, regular expressions are used to identify and extract variable identification information in a specific format. After identifying the variable identification information, the offset value of the current variable identification information relative to the starting position of the code text can be directly determined, so that the position information of the variable can be preliminarily determined.

[0043] Further, the generating the directory information of the program block unit according to the identification information and the location information includes:

[0044] Generate first sub-directory information according to the variable identification information and the variable position information;

[0045] Generate second sub-directory information according to the variable block identification information and the first sub-directory information;

[0046] Generate directory information according to the program block identification information, the program block location information and the second sub-directory information.

[0047] From the above description, it can be seen that since there is a nested relationship between variable code segments, variable block units and program block units, the directory information corresponding to the three should record this nested relationship to ensure that the index can accurately analyze the dependencies between program organization units, which is convenient for modular development and maintenance.

[0048] Further, the creating an index according to the directory information of the plurality of program block units comprises:

[0049] Traversing the directory information of the plurality of program block units, obtaining all variable identification information and corresponding variable position information of the plurality of program block units;

[0050] Converting the variable position information into row and column position information of the code text according to the symbol position information of the line break character in each line of the code text;

[0051] Generate a mapping table according to the variable identification information and the row and column position information;

[0052] An index is created according to the mapping table.

[0053] From the above description, it can be seen that since there is a certain reference relationship between multiple program block units, the dependency relationship between program block units is recorded by traversing the directory information of the program block units to ensure the integrity between the program block units. At the same time, since the current variable position information only records the offset value of the variable identification position relative to the starting position of the code text, it is impossible to quickly locate the specified code. Therefore, based on the line break in the code text, its offset value is converted into row and column position information to achieve rapid positioning.

[0054] Further, the converting the variable position information into row and column position information of the code text according to the symbol position information of the line break in each line of the code text comprises:

[0055] Get the row information with the initial value of 1 and the first position information of the line break character in the previous row;

[0056] Get the line break position array of the code text;

[0057] Determine whether the variable position information is less than the array element of the line break position array at the line information minus one;

[0058] If yes, set the column information equal to the difference between the variable position information and the first position information, and output the row information and the column information to obtain row and column position information;

[0059] Otherwise, the row information is incremented to make the first position information equal to the array element, and the process returns to determine whether the variable position information is less than the array element of the line break position array at the row information minus one.

[0060] From the above description, it can be seen that since the line break position array records the row information of each line break in the code text, the line break position array is traversed here through the row information, and the line break position is compared with the offset value. If the offset value is greater than the line break position, it means that the variable position has not been reached yet, and the array continues to be traversed; if the offset value is less than the line break position, it means that the variable position has been reached, and the column information is determined according to the line break position of the previous line, thereby converting the offset value into row and column position information, thereby improving positioning efficiency and accuracy.

[0061] Furthermore, the mapping table includes a variable mapping table, a type mapping table and a call type mapping table;

[0062] The variable identification information includes variable name, data type information and initial value;

[0063] The generating of a mapping table according to the variable identification information and the row and column position information comprises:

[0064] Generate a variable mapping table according to the variable identification information and the row and column position information corresponding to the variable name;

[0065] Generate a type mapping table according to the data type information, a data type pointer corresponding to the data type information, and row and column position information;

[0066] A call type mapping table is generated according to the call data type information directly called in the data type information and the row and column position information corresponding to the call data type information.

[0067] From the above description, it can be seen that different types of identification information in the code text are stored separately through the variable mapping table, type mapping table and call type mapping table to avoid confusion in the dependency relationship between data types and program organization units in the index structure. For some complex project structures, especially when there is a lot of reuse and inheritance, different types of mapping tables can fully understand the mutual influence between components and effectively ensure retrieval accuracy.

[0068] Further, locating the target code in the code text by using the index comprises:

[0069] If the target code is a data type declaration, the type mapping table is accessed to obtain row and column position information of the target code.

[0070] From the above description, it can be seen that for data type declarations, the type mapping table can be directly accessed to obtain row and column position information, thereby improving the retrieval efficiency of the target code.

[0071] Further, locating the target code in the code text by using the index comprises:

[0072] If the target code is a multi-level variable name, the multi-level variable name is segmented according to the multi-level variable symbols to obtain multiple variable character strings, and the multiple variable character strings are written into a preset storage object;

[0073] Obtaining the target character strings in the storage object in sequence according to the set index parameters;

[0074] Searching the variable mapping table, the call type mapping table, and the type mapping table in sequence according to the target character string obtained for the first time to obtain data type information corresponding to the target character string;

[0075] A recursive search is performed in the type mapping table according to the target character string and the data type information that are not obtained for the first time to output row and column position information.

[0076] From the above description, it can be seen that for multi-level variable names, which are nested and contain multiple variables, if you need to find the deepest nested variable, you first need to determine its variable name through symbol segmentation, and then query the multiple nested variables in turn through the index parameter. Search in the variable mapping table to determine whether the variable at the current level is a global variable; if it is not a global variable, search in the call type mapping table to determine whether the variable at the current level is a variable of other program pages; if it is not a variable of other program pages, search in the type mapping table to determine whether the variable at the current level is a local variable of the program; finally, based on the determined data type information, output the row and column position information of the variable to achieve positioning retrieval.

[0077] Another embodiment of the present invention provides a retrieval terminal for a PLC programming language, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, wherein the processor implements each step of the retrieval method for a PLC programming language described above when executing the computer program.

[0078] From the above description, it can be seen that the beneficial effects of the present invention are: the code text is divided into multiple program block units through program block keywords, and the identification information and location information of the program block units are extracted. Based on the identification information and location information of each program block unit, the directory information corresponding to each program block unit is generated respectively, and an index is created based on the directory information, so that the index can perform in-depth analysis and retrieval of each program block unit. In addition, this method can perform unified resource indexing on the entire code text, effectively store the dependency analysis between program blocks, realize rapid retrieval and intelligent analysis of the ST programming language, and facilitate modular development and maintenance.

[0079] The embodiments of the present invention provide a PLC programming language retrieval method and terminal, which can be applied to PLC language compilation and development scenarios, and can realize rapid retrieval and intelligent analysis of ST programming language, and facilitate modular development and maintenance. The following is an explanation through specific embodiments:

[0080] Please refer to Figures 1 to 4 , Embodiment 1 of the present invention is:

[0081] A PLC programming language retrieval method, comprising:

[0082] S1. Obtain the code text of the target programming language project.

[0083] In some embodiments, the target programming language project is ST source code in a programming environment based on the IEC 61131-3 standard.

[0084] S2. Segment the code text according to program block keywords to obtain multiple program block units, and extract identification information and location information of the program block units.

[0085] In some embodiments, program block keywords include PROGRAM (main program block, the highest level program organization unit), FUNCTION (function, a program block without internal variable state retention), FUNCTION_BLOCK (function block, a program block with internal variable state retention), etc. Among them, PROGRAM contains the overall structure and execution flow of the program; FUNCTION is a stateless program block that performs specific tasks or calculations and is used to encapsulate reusable code segments; FUNCTION_BLOCK is used to implement control logic that needs to maintain state. Figure 2 It is a program block unit obtained by splitting the code text based on the program block keyword PROGRAM.

[0086] The identification information includes program block identification information, variable block identification information and variable identification information; the location information includes program block location information and variable location information.

[0087] Specifically, extracting the identification information and location information of the program block unit includes:

[0088] S21. Extracting program block identification information and program block location information of the program block unit.

[0089] In some embodiments, the program block identification information includes a program block name, a program block type, and a program block return value. Figure 2 The program block name corresponding to the program block unit is Main and the program block type is PROGRAM.

[0090] S22. Split the program block unit according to the variable block keyword to obtain a plurality of variable block units corresponding to the program block unit, and extract variable block identification information of the variable block unit.

[0091] In some embodiments, variable block keywords include VAR, VAR_INPUT, VAR_OUTPUT, VAR_IN_OUT, and VAR_TEMP, etc. These variable block keywords are used to declare variables in a program block and specify the scope and type of these variables. Among them, VAR is used to start the variable declaration part, indicating the beginning of the variable declaration area; END_VAR is used to end the variable declaration part, the end of the variable declaration area; VAR_INPUT is used to declare input variables, which are read-only; VAR_OUTPUT is used to declare output variables, which are write-only; VAR_IN_OUT is used to declare bidirectional variables, which can be read and modified; VAR_TEMP is used to declare temporary variables, which exist during execution and are destroyed after execution is completed. Figure 3 Split based on variable block keyword VAR Figure 2 The variable block unit is obtained by the program block unit. The variable block identification information includes the variable block type, that is, the variable block keyword of the corresponding type. Figure 3 The variable block type corresponding to the variable block unit is VAR.

[0092] S23. Extract a variable code segment from the variable block unit based on the variable block keyword, and extract variable identification information and variable position information of the variable code segment.

[0093] In some embodiments, the variable code segment corresponding to the variable block unit can be obtained by removing the variable block keyword of the variable block unit. Figure 4 For variable blocks based on the keywords VAR and END_VAR Figure 3 The variable code segment extracted from the variable block unit. The variable identification information includes the variable name, data type, initial value and comment information. It should be noted that the comment information corresponding to the variable is marked so that the comment information can be skipped during the retrieval process, thereby speeding up the traversal retrieval speed. Figure 4 The variables corresponding to the variable code segment include test and test1. The data types, initial values, and comment information corresponding to the variables test and test1 are INT, =1, and (*test code*), respectively.

[0094] Specifically, the step of extracting the variable identification information and the variable location information of the variable code segment includes:

[0095] S231. Extract variable identification information of each variable in the variable code segment by using a regular expression.

[0096] In some embodiments, the regular expression is specifically "[a-zA-Z_][a-zA-Z_0-9]*", where [a-zA-Z_] means matching a string starting with a letter (uppercase or lowercase) or an underscore, and [a-zA-Z_0-9]* means that the string can be followed by zero or more letters (uppercase or lowercase), numbers or underscores. The regular expression can match the format of a legal variable name. Similarly, the regular expression can match data types, initial values, and comment information.

[0097] S232. Extract the offset value of the variable identification information relative to the starting position of the code text to obtain variable position information.

[0098] In some embodiments, an offset value of a variable name relative to a starting position of a code text is extracted as variable position information.

[0099] In some embodiments, when extracting variable position information, the file name where the current variable is located is synchronously recorded to facilitate subsequent determination of the symbol position information of the line break character.

[0100] In some embodiments, each variable code segment may include multiple variables. If the variable identification information and variable location information of the current variable are extracted, the process returns to execute steps S231 to S232 to extract the variable identification information and variable location information of the next variable in the variable code segment.

[0101] In some embodiments, each program block unit may include multiple variable block units. If the variable code segment identification of the current variable block unit is completed, the process returns to step S22 to step S23 to split the next variable block unit in the program block unit until the identification information of all declared variables in the program block unit has been extracted.

[0102] S3. Generate directory information of the program block unit according to the identification information and the location information.

[0103] In some embodiments, the identification information and location information of each program block unit generates directory information of the corresponding program block unit, that is, the program block unit corresponds to the directory information one by one.

[0104] In some embodiments, there is a mutual reference relationship between program block units, and this reference relationship can be recorded in the directory information to facilitate analysis of the dependency relationship between program block units during subsequent retrieval.

[0105] Specifically, step S3 includes:

[0106] S31. Generate first sub-directory information according to the variable identification information and the variable position information.

[0107] In some embodiments, the first sub-directory information includes a variable name, a data type, an initial value, comment information, and a variable location information. The structure of the first sub-directory information is shown in Table 1.

[0108] Table 1 First subdirectory information (Variable)

[0109]

[0110] S32. Generate second sub-directory information according to the variable block identification information and the first sub-directory information.

[0111] In some embodiments, the second sub-directory information includes a variable block type and a pointer to the first sub-directory information. The structure of the second sub-directory information is shown in Table 2.

[0112] Table 2 Second subdirectory information (VariableBlock)

[0113]

[0114] S33: Generate directory information according to the program block identification information, the program block location information and the second sub-directory information.

[0115] In some embodiments, the directory information includes a program block name, a program block type, a program block return value, a pointer to the second sub-directory information, and program block location information. The structure of the directory information is shown in Table 3.

[0116] Table 3 Directory information (parseUnit)

[0117]

[0118] In some embodiments, both the variable location information and the program block location information include their offset values ​​relative to the starting position of the code text and the file name where they are located. The structure of the location information is shown in Table 4.

[0119] Table 4 Position information (SourcePos)

[0120]

[0121] S4. Create an index according to the directory information of the multiple program block units.

[0122] Specifically, step S4 includes:

[0123] S41. Traverse the directory information of the multiple program block units to obtain all variable identification information and corresponding variable position information of the multiple program block units.

[0124] S42. Convert the variable position information into row and column position information of the code text according to the symbol position information of the line break characters in each line of the code text.

[0125] S43. Generate a mapping table according to the variable identification information and the row and column position information.

[0126] The mapping table includes a variable mapping table, a type mapping table and a call type mapping table; the variable identification information includes a variable name, data type information and an initial value.

[0127] Specifically, the step S43 includes:

[0128] S431. Generate a variable mapping table according to the variable identification information and the row and column position information corresponding to the variable name.

[0129] In some embodiments, the variable mapping table is used to store variable index information, which includes the variable name, data type, initial value and corresponding location information. The key in the variable mapping table is the variable name, and the value is the variable index information corresponding to the variable name. The structure of the variable index information is shown in Table 5.

[0130] Table 5 Variable index information (VariableIndexItem)

[0131]

[0132] S432: Generate a type mapping table according to the data type information, a data type pointer corresponding to the data type information, and row and column position information.

[0133] In some embodiments, the data type pointer in the type mapping table is Info, which is an abstract data type base class pointer that actually points to actual type information such as structures, unions, enumerations, programs, function blocks, functions, etc. This information can be used to quickly locate the declaration information in the original code snippet.

[0134] In some embodiments, the type mapping table is used to store all data type index information used in the ST source code calling process. The data type index information includes the data type name, variable member list and corresponding location information. The key in the type mapping table is the data type name, and the value is the data type index information corresponding to the data type name. The structure of the data type index information is shown in Table 6.

[0135] Table 6 Data type index information (DataTypeIndexItem)

[0136]

[0137] S433: Generate a call type mapping table according to the call data type information directly called in the data type information and the row and column position information corresponding to the call data type information.

[0138] In some embodiments, the call type mapping table is used to record all data type index information that can be directly called. For example, if a multi-level variable is named Main.var, the first-level variable Main is the name of the data type that can be directly called. The key and value structure of the call type mapping table is the same as that of the type mapping table. The structure of the data type index information that is directly called is shown in Table 7.

[0139] Table 7 Index information of data types directly called (callIndexItem)

[0140]

[0141] It should be noted that by setting the calling type mapping table, you can avoid directly searching for variables in the type mapping table. Because the type mapping table has a large amount of data, searching by calling the type mapping table with a small amount of data can effectively speed up the search efficiency.

[0142] S44: Create an index according to the mapping table.

[0143] In some embodiments, an index is created according to a variable mapping table, a type mapping table, and a call type mapping table, and the structure of the index is shown in Table 8.

[0144] Table 8 Index

[0145]

[0146] S5. In response to a retrieval instruction for the target code, locate the target code in the code text through the index.

[0147] In an optional implementation, the step S5 includes:

[0148] S510. If the target code is a data type declaration, access the type mapping table to obtain row and column position information of the target code.

[0149] In an optional implementation, the step S5 includes:

[0150] S520. If the target code is a multi-level variable name, the multi-level variable name is segmented according to the multi-level variable symbols to obtain multiple variable character strings, and the multiple variable character strings are written into a preset storage object.

[0151] In some embodiments, the multi-level variable symbol is "." For example, the multi-level variable name is ABCD, which means that variable A structure contains variable B, variable B structure contains variable C, variable C structure contains variable D, and the multi-level variable name ABCD is split based on the "." symbol to obtain variable strings A, B, C and D.

[0152] S530: Obtain target character strings in the storage object in sequence according to the set index parameters.

[0153] S540: Search the variable mapping table, the call type mapping table, and the type mapping table in sequence according to the target character string obtained for the first time to obtain data type information corresponding to the target character string.

[0154] S550: recursively search the type mapping table according to the target character string that is not obtained for the first time and the data type information to output row and column position information.

[0155] Please refer to Figure 5 , Embodiment 2 of the present invention is:

[0156] A method for searching a PLC programming language is different from the first embodiment in that a specific implementation method of step S42 is limited.

[0157] Specifically, the step S42 includes:

[0158] S421, obtaining the row information with an initial value of 1 and the first position information of the line break character of the previous row.

[0159] In some embodiments, the row information is Line, the first position information is preNewlinePos, the column information is Column, and the initial values ​​of Line, preNewlinePos and Column are all 1.

[0160] S422. Obtain a line break position array of the code text.

[0161] In some embodiments, the newline position array is newlineArray. It should be noted that the newline position array records the offset value of each newline relative to the starting position of the code text.

[0162] S423. Determine whether the variable position information is smaller than the array element of the line break position array at the line information minus one.

[0163] In some embodiments, the array element of the newline position array at the line information minus one is the second position information newlinePos of the currently traversed newline character, newlinePos=newlineArray[Line-1].

[0164] S424. If yes, set the column information equal to the difference between the variable position information and the first position information, and output the row information and the column information to obtain row and column position information.

[0165] In some embodiments, if the variable position information offet is smaller than newlinePos, Column=offet-preNewlinePos is set, and the current line information Line is output.

[0166] It should be noted that the offset value in the variable position information refers to the number of characters from the starting position of the text to a certain character position. For example, if the offset value of the starting position of the text is 0, the offset value of the first character is 1, the offset value of the second character is 2, and so on.

[0167] S425. Otherwise, increment the row information, set the first position information equal to the array element, and return to execute step S423.

[0168] In some embodiments, if the variable position information offet is not less than newlinePos, the line information Line++ is added, and the second position information newlinePos of the current line break is recorded as the first position information preNewlinePos of the previous line break, that is, preNewlinePos=newlinePos.

[0169] The following describes step S42 through a specific application scenario:

[0170] Currently there is a 20-character code text str and an array array that records the position of line breaks. The array array records the offset values ​​of each line break in the code text str, which are 4, 7, and 15, respectively, that is, newlineArray=[4,7,15]. The offset value of the current variable is 13, that is, offet=13. If you want to convert the offset value into row and column position information, first traverse the array newlineArray based on the line information Line=1, that is, let newlinePos=newlineArray[1-1]=4, offe=13 is not less than newlinePos=4, increase the line information Line to 2, and record the offset value of the previous line break preNewlinePos=newlinePos=4; then traverse the array newlineArray based on the line information Line=2, that is, let newlinePos=newlineArray[2-1]=7, offe=13 is not less than newlinePos=4 os=7, increment the row information Line to 3, record the offset value of the previous line break preNewlinePos=newlinePos=7; then traverse the array newlineArray based on the row information Line=3, that is, let newlinePos=newlineArray[3-1]=15, offe=13 is less than newlinePos=15, so the row information Line where the current variable is located is 3, and the column information Column where the current variable is located is Column=offet-the offset value of the previous line break preNewlinePos=13-7=6, thereby determining that the current variable is located in row 3 and column 6.

[0171] Please refer to Figure 6 , Embodiment 3 of the present invention is:

[0172] A method for searching a PLC programming language is different from the first embodiment in that the specific implementation of step S530, step S540 and step S550 is limited.

[0173] In some embodiments, the preset storage object is a one-dimensional array vecVariable. The index parameter is index, and the initial value of the index parameter index is 1. vecVariable.size represents the number of strings contained in the storage object vecVariable, that is, for a multi-level variable name ABCD, vecVariable.size=4.

[0174] Specifically, the step S530 includes:

[0175] S5301. Every time a set of detailed information of the data type name (i.e., the variable member list and row and column position information) is obtained in the type mapping table, the current index parameter is incremented, and it is determined whether the current index parameter is less than the number of strings contained in the storage object. If so, execute step S5302; otherwise, execute step S5303.

[0176] S5302. Obtain the target string of the storage object at the index parameter.

[0177] S5303. Output the row and column position information corresponding to the current target character string.

[0178] Specifically, the step S540 includes:

[0179] S5401, searching the target string in the variable mapping table. If the target string is found in the variable mapping table, obtaining the data type name corresponding to the target string in the variable mapping table, and executing step S5404.

[0180] It should be noted that the target string is searched in the variable mapping table to determine whether the variable corresponding to the target string is a global variable.

[0181] S5402. If the target string is not retrieved in the variable mapping table, the target string is retrieved in the call type mapping table. If the target string is retrieved in the call type mapping table, the data type name corresponding to the target string is obtained in the call type mapping table, and step S5404 is executed.

[0182] It should be noted that the target string is retrieved in the call type mapping table to determine whether the variable corresponding to the target string is another program page variable.

[0183] S5403. If the target string is not found in the calling type mapping table, the target string is searched in the type mapping table; if the target string is found in the type mapping table, detailed information of the data type name corresponding to the target string is obtained in the type mapping table.

[0184] It should be noted that the target string is searched in the type mapping table to determine whether the variable corresponding to the target string is a local variable of the current program.

[0185] S5404. Retrieve the data type name in the type mapping table. If the data type name is retrieved in the type mapping table, obtain detailed information corresponding to the data type name in the type mapping table, return to execute step S5301 to obtain a new target string, and then execute step S5501.

[0186] S5405. If the target character string is not found in the type mapping table, the search is terminated.

[0187] Specifically, the step S550 includes:

[0188] D5501. Search for a new target string in the current variable member list. If a new target string is found, obtain the data type name corresponding to the new target string, and return to execute step S5404 until all target strings are retrieved, and output the final row and column position information.

[0189] S5502. If the target string is not found in the current variable member list, the search ends.

[0190] It should be noted that the variables called in the ST program include global variables, variables in other PROGRAM type program pages, and local variables. The calling method of these three variables is: specific calling field.variable name. Specifically, the global variable is GVL.name (variable name), the variables in other program pages are PROGRAMNAME.name (variable name), and the local variable is the name of the program page where the local variable is located.name (variable name).

[0191] In some embodiments, based on the above step S540, the multi-level variable name ABCD is recursively traversed layer by layer, where the first-level variable is A, the second-level variable is B, the third-level variable is C, and the fourth-level variable is D. The recursive traversal process is specifically as follows: 1. Obtain the string A of the first-level variable, and use the string A to search in the variable mapping table and the call type mapping table to obtain the data type name A1 of the first-level variable A. 2. Search the type mapping table according to the obtained data type name A1 of the first-level variable A to obtain detailed information of the data type name A1 (such as the variable member list A11, location information, etc.). 3. Get the string B of the second-level variable and the variable member list A11 obtained in step 2, search the string B in the variable member list A11 to get the data type name B1 of the second-level variable B, and then return to step 2 based on the obtained data type name B1 of the second-level variable B to get the detailed information of the data type name B1, until the strings A, B, C, and D are all traversed, to get the position information in the detailed information of the data type name D1 of the final fourth-level variable D for output.

[0192] Please refer to Figure 7 , Embodiment 4 of the present invention is:

[0193] A retrieval terminal 100 for a PLC programming language includes a memory 101, a processor 102, and a computer program stored in the memory 102 and running on the processor 102. When the processor 102 executes the computer program, each step in the retrieval method for a PLC programming language in the above-mentioned embodiments 1 to 3 is implemented.

[0194] In summary, the present invention provides a retrieval method and terminal for a PLC programming language, which divides the code text into program block units, variable block units and variable code segments in turn through program block keywords and variable block keywords, and extracts corresponding identification information and position information layer by layer based on the divided program block units, variable block units and variable code segments to form directory information, so that the directory information not only records the metadata of the program block, but also records the nested structure between the program block, variable block and variable. Create an index based on the directory information, so that the index can analyze and retrieve each program block unit layer by layer based on the syntax tree structure, thereby improving the depth and breadth of the retrieval. This method can perform a unified resource index on the entire code text, effectively store the dependency analysis between program blocks, realize the rapid retrieval and intelligent analysis of ST programming language, and facilitate modular development and maintenance. Based on the retrieval method of the present invention, developers can quickly find specified data types, variables, functions or function blocks, greatly improving the efficiency of code reading and understanding. In addition, when there is a problem with the compiler, variables, digital type definitions, etc. can be quickly located, the error diagnosis process can be accelerated, and downtime can be shortened.

[0195] The above descriptions are merely embodiments of the present invention and are not intended to limit the patent scope of the present invention. Any equivalent transformations made using the contents of the present invention's specification and drawings, or directly or indirectly applied in related technical fields, are also included in the patent protection scope of the present invention.

Claims

1. A method for retrieving a PLC programming language, characterized in that: include: Get the code text of the target programming language project; Segmenting the code text according to program block keywords to obtain a plurality of program block units, and extracting identification information and location information of the program block units; Generate directory information of the program block unit according to the identification information and the location information; Creating an index based on the directory information of the plurality of program block units; In response to a retrieval instruction of the target code, locating the target code in the code text by using the index; The identification information includes program block identification information, variable block identification information and variable identification information; The position information includes program block position information and variable position information; The extracting the identification information and location information of the program block unit includes: Extracting program block identification information and program block location information of the program block unit; Splitting the program block unit according to the variable block keyword to obtain a plurality of variable block units corresponding to the program block unit, and extracting variable block identification information of the variable block unit; Extracting a variable code segment from the variable block unit based on the variable block keyword, and extracting variable identification information and variable position information of the variable code segment; The step of extracting the variable identification information and the variable location information of the variable code segment includes: Extracting variable identification information of each variable in the variable code segment by using a regular expression; Extracting the offset value of the variable identification information relative to the starting position of the code text to obtain variable position information; The step of creating an index according to the directory information of the plurality of program block units comprises: Traversing the directory information of the plurality of program block units, obtaining all variable identification information and corresponding variable position information of the plurality of program block units; Converting the variable position information into row and column position information of the code text according to the symbol position information of the line break character in each line of the code text; Generate a mapping table according to the variable identification information and the row and column position information; Creating an index according to the mapping table; The mapping table includes a variable mapping table, a type mapping table and a call type mapping table; The variable identification information includes variable name, data type information and initial value; The generating of a mapping table according to the variable identification information and the row and column position information comprises: Generate a variable mapping table according to the variable identification information and the row and column position information corresponding to the variable name; Generate a type mapping table according to the data type information, a data type pointer corresponding to the data type information, and row and column position information; A call type mapping table is generated according to the call data type information directly called in the data type information and the row and column position information corresponding to the call data type information.

2. The method for retrieving a PLC programming language according to claim 1, characterized in that: The step of generating the directory information of the program block unit according to the identification information and the location information includes: Generate first sub-directory information according to the variable identification information and the variable position information; Generate second sub-directory information according to the variable block identification information and the first sub-directory information; Generate directory information according to the program block identification information, the program block location information and the second sub-directory information.

3. The method for retrieving a PLC programming language according to claim 1, characterized in that: The converting the variable position information into row and column position information of the code text according to the symbol position information of each line break in the code text comprises: Get the row information with the initial value of 1 and the first position information of the line break character in the previous row; Get the line break position array of the code text; Determine whether the variable position information is less than the array element of the line break position array at the line information minus one; If yes, set the column information equal to the difference between the variable position information and the first position information, and output the row information and the column information to obtain row and column position information; Otherwise, the row information is incremented, the first position information is set equal to the array element, and the execution is returned to determine whether the variable position information is less than the array element of the line break position array at the row information minus one.

4. The method for retrieving a PLC programming language according to claim 1, characterized in that: The locating the target code in the code text by the index comprises: If the target code is a data type declaration, the type mapping table is accessed to obtain row and column position information of the target code.

5. The method for retrieving a PLC programming language according to claim 1, characterized in that: The locating the target code in the code text by the index comprises: If the target code is a multi-level variable name, the multi-level variable name is segmented according to the multi-level variable symbols to obtain multiple variable character strings, and the multiple variable character strings are written into a preset storage object; Obtaining the target character strings in the storage object in sequence according to the set index parameters; Searching the variable mapping table, the call type mapping table, and the type mapping table in sequence according to the target character string obtained for the first time to obtain data type information corresponding to the target character string; A recursive search is performed in the type mapping table according to the target character string and the data type information that are not obtained for the first time to output row and column position information.

6. A PLC programming language retrieval terminal, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that: When the processor executes the computer program, the processor implements the various steps in the retrieval method of a PLC programming language as described in any one of claims 1 to 5.

Citation Information

Patent Citations

  • Information flow analysis method based on system source code searching concealed channel

    CN101377806A

  • Keyword search system based on security indexes and method for establishing same

    CN104283930A