CN115481396A - NC code abnormality detection method, device, equipment and storage medium - Google Patents
NC code abnormality detection method, device, equipment and storage medium Download PDFInfo
- Publication number
- CN115481396A CN115481396A CN202211047591.9A CN202211047591A CN115481396A CN 115481396 A CN115481396 A CN 115481396A CN 202211047591 A CN202211047591 A CN 202211047591A CN 115481396 A CN115481396 A CN 115481396A
- Authority
- CN
- China
- Prior art keywords
- code
- linked list
- abnormal
- codes
- lexical
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F21/00—Security arrangements for protecting computers, components thereof, programs or data against unauthorised activity
- G06F21/50—Monitoring users, programs or devices to maintain the integrity of platforms, e.g. of processors, firmware or operating systems
- G06F21/55—Detecting local intrusion or implementing counter-measures
- G06F21/56—Computer malware detection or handling, e.g. anti-virus arrangements
- G06F21/562—Static detection
- G06F21/563—Static detection by source code analysis
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/20—Natural language analysis
- G06F40/253—Grammatical analysis; Style critique
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/30—Semantic analysis
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/70—Software maintenance or management
- G06F8/75—Structural analysis for program understanding
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2221/00—Indexing scheme relating to security arrangements for protecting computers, components thereof, programs or data against unauthorised activity
- G06F2221/03—Indexing scheme relating to G06F21/50, monitoring users, programs or devices to maintain the integrity of platforms
- G06F2221/033—Test or assess software
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Health & Medical Sciences (AREA)
- Computer Security & Cryptography (AREA)
- General Health & Medical Sciences (AREA)
- Computer Hardware Design (AREA)
- Artificial Intelligence (AREA)
- Audiology, Speech & Language Pathology (AREA)
- Computational Linguistics (AREA)
- Virology (AREA)
- Debugging And Monitoring (AREA)
Abstract
The invention relates to the technical field of text processing, and provides a method, a device, equipment and a storage medium for detecting NC code abnormity. The method comprises the following steps: acquiring an NC code file to be detected, performing lexical analysis on an NC code in the NC code file, and detecting an abnormal lexical in the NC code; constructing a single-direction linked list based on the NC codes; performing syntax semantic analysis on the NC codes based on the single-direction linked list, and detecting abnormal syntax and abnormal semantics in the NC codes; and determining the abnormal information of the NC code according to the abnormal lexical method, the abnormal grammar and the abnormal semantics. The NC code anomaly detection method provided by the invention can detect the specific abnormal code by performing lexical analysis, syntactic analysis and semantic analysis on the NC code and detecting the code anomaly based on the analysis of the NC code content, thereby improving the detection precision of the NC code anomaly.
Description
Technical Field
The invention relates to the technical field of text processing, in particular to a method, a device, equipment and a storage medium for detecting NC code abnormity.
Background
With the rapid development of information technology, the openness of the internet has enhanced the capabilities of manufacturing equipment in terms of communication, automation, storage, and computation. The industrial internet has made originally discrete, isolated manufacturing equipment into interconnected, intelligent equipment. The novel manufacturing system has the capabilities of self-perception, self-prediction, self-optimization, self-matching and the like. However, the openness of the internet brings a series of information security problems to the manufacturing industry, and enlarges the attack surface of the manufacturing system, so that an attacker can invade the manufacturing system to tamper with or steal data. In an industrial manufacturing system, the core control logic file is an NC code file, and the manufacturing system manufactures and processes the workpiece in accordance with a processing procedure specified in the NC file. At present, most of the existing methods adopt a side channel method to detect the NC code abnormality based on physical performance and indexes, can detect the physical change of the manufacturing system, but cannot determine the specific information of the NC code abnormality, so that the detection precision of the NC code abnormality is insufficient.
Disclosure of Invention
The invention provides an NC code anomaly detection method, device, equipment and storage medium, which are used for solving the defects that the detection precision of the NC code anomaly is insufficient when the anomaly of an NC code is detected in a side channel mode in the prior art.
The invention provides an NC code abnormality detection method, which comprises the following steps:
acquiring an NC code file to be detected, performing lexical analysis on an NC code in the NC code file, and detecting an abnormal lexical in the NC code;
constructing a single-direction linked list based on the NC codes;
performing syntax semantic analysis on the NC codes based on the single-direction linked list, and detecting abnormal syntax and abnormal semantics in the NC codes;
and determining the abnormal information of the NC codes according to the abnormal lexical method, the abnormal grammar and the abnormal semantics.
In one embodiment, the step of performing lexical analysis on the NC code and detecting abnormal lexical in the NC code includes:
converting the character sequence of the NC code into a marking sequence, and generating a plurality of marking blocks according to the marking sequence;
detecting the mark type of each mark block, and acquiring keyword information in the mark blocks of the preset mark type when the mark blocks of the preset mark type are detected; the preset mark types comprise a keyword mark type and an illegal character mark type;
and detecting abnormal lexical forms in the mark blocks of the preset mark types according to the keyword information in the mark blocks of the preset mark types.
In one embodiment, the step of constructing the singly linked list based on the NC codes in the NC code file includes:
acquiring each program segment of an NC code in the NC code file;
creating a linked list node corresponding to each program segment;
storing the target mark blocks in any program segment into corresponding linked list nodes, and generating a data domain and a pointer domain corresponding to each target mark block; storing keyword information in each target mark block in the data field; the pointer field is used for indicating the position information of each target mark block in the linked list nodes;
and generating a one-way linked list corresponding to the NC code file according to each linked list node.
In one embodiment, the step of performing syntax semantic analysis on the NC code based on the singly linked list, and detecting abnormal syntax and abnormal semantics in the NC code includes:
according to a preset grammar rule, performing grammar analysis on the NC codes based on the one-way linked list, and detecting abnormal grammars in the NC codes;
generating a code corpus according to a preset programming manual; the code corpus comprises a function code corpus and an auxiliary function code corpus;
and according to the code corpus and preset semantic rules, performing semantic analysis on the NC codes on the basis of the single-direction linked list, and detecting abnormal semantics in the NC codes.
In one embodiment, the step of performing syntax analysis on the NC code based on the single linked list according to a preset syntax rule to detect abnormal syntax in the NC code includes:
acquiring target keyword information in each linked list node based on the pointer domain of each linked list node in the one-way linked list; the target keyword information comprises a function code and an auxiliary function code;
matching the target keyword information with a preset grammar rule, and detecting abnormal grammar which is unsuccessfully matched with the grammar rule; the grammar rules comprise a first grammar rule corresponding to the function codes and a second grammar rule corresponding to the auxiliary function codes.
In one embodiment, the step of performing semantic analysis on the NC code based on the single linked list according to the code corpus and a preset semantic rule to detect abnormal semantics in the NC code includes:
acquiring target keyword information in each linked list node based on the pointer domain of each linked list node in the one-way linked list; the target keyword information comprises a function code and an auxiliary function code;
analyzing the target keyword information according to the code corpus to generate a natural language corresponding to the target keyword information;
performing context semantic detection on the natural language to acquire semantic information of the natural language;
and matching the semantic information with a preset semantic rule, and detecting abnormal semantics which are unsuccessfully matched with the semantic rule.
In one embodiment, the abnormal lexical method includes an illegal parameter and an illegal character; the exception syntax comprises an instruction combination error, an instruction and functional word matching error, an instruction sequence error, a logic error, a process error and a program starting instruction error; the exception semantics comprise value exception of a functional word, necessary instruction exception, processing sequence error and processing parameter error.
The present invention also provides an NC code abnormality detection apparatus, including:
the lexical analysis module is used for acquiring an NC code file to be detected, performing lexical analysis on an NC code in the NC code file, and detecting an abnormal lexical in the NC code;
the linked list construction module is used for constructing a single-direction linked list based on the NC code;
the syntax semantic analysis module is used for carrying out syntax semantic analysis on the NC codes based on the one-way linked list and detecting abnormal syntax and abnormal semantics in the NC codes;
and the anomaly detection module is used for determining the anomaly information of the NC code according to the anomaly lexical method, the anomaly grammar and the anomaly semantic.
The invention also provides an electronic device, which comprises a memory, a processor and a computer program which is stored on the memory and can run on the processor, wherein when the processor executes the program, the NC code abnormality detection method is realized.
The present invention also provides a non-transitory computer-readable storage medium having stored thereon a computer program which, when executed by a processor, implements an NC code anomaly detection method as described in any one of the above.
The present invention also provides a computer program product comprising a computer program which, when executed by a processor, implements a method of NC code anomaly detection as described in any one of the above.
According to the NC code anomaly detection method, the device, the equipment and the storage medium, lexical analysis, syntactic analysis and semantic analysis are carried out on the NC codes in the NC file, and the code anomalies are detected based on the analysis of the NC code content, so that not only can existing code anomalies be detected, but also specific anomalous codes can be detected, the anomalies of the NC codes can be detected by integrating the detection results of the lexical analysis, the syntactic analysis and the semantic analysis, and the detection precision of the NC code anomalies is improved.
Drawings
In order to more clearly illustrate the technical solutions of the present invention or the prior art, the drawings needed for the description of the embodiments or the prior art will be briefly described below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and those skilled in the art can also obtain other drawings according to the drawings without creative efforts.
FIG. 1 is a schematic flow chart of an NC code anomaly detection method provided by the present invention;
FIG. 2 is one of the schematic construction flows of the single linked list of the NC code anomaly detection method provided by the present invention;
FIG. 3 is a schematic diagram of a lexical analysis flow of the NC code anomaly detection method provided by the present invention;
FIG. 4 is a schematic diagram of a syntax analysis flow of the NC code anomaly detection method provided by the present invention;
FIG. 5 is a schematic diagram of a semantic analysis flow of the NC code anomaly detection method provided by the present invention;
FIG. 6 is a schematic structural diagram of an NC code abnormality detection apparatus provided in the present invention;
fig. 7 is a schematic structural diagram of an electronic device provided by the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings, and it is obvious that the described embodiments are some, but not all embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The NC code abnormality detection method of the present invention is described below with reference to fig. 1 to 5.
Fig. 1 is a schematic flowchart of an NC code anomaly detection method provided in an embodiment of the present invention, and based on fig. 1, the NC code anomaly detection method provided in the embodiment of the present application includes:
200, constructing a single-direction linked list based on the NC codes;
and step 400, determining abnormal information of the NC code according to the abnormal lexical method, the abnormal grammar and the abnormal semantic meaning.
Aiming at some NC code abnormality detection methods in the prior art, the method judges the abnormality information by analyzing the NC code content from the angle of an NC code file, not only can detect the abnormality of the NC code, but also can detect the specific code with the abnormality, and improves the detection precision of the NC code abnormality.
Specifically, an NC code file to be detected is firstly acquired, the file can be identified when the file is acquired, and after the file is identified to be the NC code file, the NC codes in the identified NC code file are analyzed to detect the abnormality of the NC codes.
The analysis of the NC codes includes lexical analysis, syntactic analysis, and semantic analysis. After the NC code file is obtained, firstly, performing lexical analysis on an NC code in the NC code file, and detecting an abnormal lexical in the NC code; and then constructing a single-direction linked list based on the NC codes, carrying out syntactic and semantic analysis on the NC codes based on the constructed single-direction linked list, and detecting abnormal grammars and abnormal semantics in the NC codes.
It can be understood that, the NC code is lexically analyzed to obtain an abnormal lexical form, which is a lexical error and includes an illegal parameter and an illegal character, for example:
program number or post-instruction value is illegal: for example, a number of an unspecified function exists in an instruction number after an M code in an NC code; the program number exceeds a preset limit range and the like;
and (3) format error: the first lattice in the first row of the program segment cannot be a numeral, or other symbols except a specific symbol such as "%", etc.;
illegal character: all the other characters except the character contained in the instruction are illegal characters; in addition, there are also, for example, special symbols dedicated to optional program segment jumps, such as "/", that must be at the beginning of a program segment; the parameter value assignment instruction, such as a lettering character in ' P = ' _ ', must be a preset character, such as ' 0-9 ', ' a-Z ', ' - ', ' # ', etc.
Further, performing semantic syntax analysis on the NC code to obtain abnormal syntax and abnormal semantics in the NC code, wherein the abnormal syntax includes:
instruction combination error: for example, each program segment cannot contain two G codes or M codes in the same group, and the like;
the matching error of the instruction and the functional word is that characters such as 'I' and 'K' can not appear in the G01 code;
instruction sequence error: for example, the arc insertion center "I" and "K" should be behind "X" and "Z", etc.;
logic errors: the numerical control machining program must meet programming and machining logic;
and (3) process errors: mainly, the processing technology is wrong, and a numerical control processing program must meet the requirements of a numerical control processing technological process;
program start instruction error: such as the sub-program and main program end identifications are typically not the same.
The exception semantics include:
value exception of functional word: when the numerical control code is initialized, the values of all the functional words in the numerical control code are limited by upper and lower limits, and if the values of the functional words do not meet the requirements or exceed the range limited by the upper and lower limits, the numerical control code is abnormal. For example, N-1000, if the value after limiting N is an integer greater than 0, if N is-1000, the value exceeds the limited range, and if the condition is not met, the value of the functional word is abnormal;
a necessary instruction exception: the method is characterized in that necessary instructions such as M02 or M30 are lacked at the end of the numerical control program, or the necessary instructions are wrong; in this case, although the program has been executed, when there is no one of the above two instructions or the instruction is erroneous, the machine tool cannot stop operating, and an error occurs;
the processing sequence is wrong: if the tool instruction is in front of the G, M code, the tool should be moved to a tool changing point before tool changing, and the first instruction after the tool changing instruction is G00 and the like;
error of processing parameters: if the end point and the start point of the arc are on the same arc, the state of the spindle and the coolant during machining should be correct.
According to the detected abnormal lexical, abnormal grammar and abnormal semantics, determining abnormal information of the NC codes, wherein generally, abnormal codes have a certain association relation, and the abnormality of the former codes may cause the abnormality of the latter corresponding codes. Therefore, comprehensive analysis is carried out according to the detected abnormal lexical method, abnormal grammar and abnormal semantics, the real reason causing the NC code abnormality is determined, and specific abnormal information is obtained; the exception information comprises specific codes with exceptions and position information of the exception codes, wherein the position information comprises positions of the exception codes in the constructed single-direction linked list and positions of the exception codes in the NC codes; the position in the NC code, such as which program segment in particular, etc.
In the embodiment, the NC codes in the NC file are subjected to lexical analysis, syntactic analysis and semantic analysis, and the code abnormality is detected based on the analysis of the NC code content, so that not only the existing code abnormality can be detected, but also the specific code of the abnormality can be detected, and the abnormality of the NC codes is detected by integrating the detection results of the lexical analysis, the syntactic analysis and the semantic analysis, thereby improving the detection accuracy of the NC code abnormality.
Furthermore, when the NC code content is analyzed, the linked list structure is used for storage, so that the storage space can be saved, and the detection efficiency is improved.
In one embodiment, in step 100, performing lexical analysis on the NC code to detect an abnormal lexical in the NC code specifically includes:
step 101, converting the character sequence of the NC code into a mark sequence, and generating a plurality of mark blocks according to the mark sequence;
102, detecting the mark type of each mark block, and acquiring keyword information in the mark block with a preset mark type when the mark block with the preset mark type is detected; the preset mark types comprise a keyword mark type and an illegal character mark type;
103, detecting abnormal lexical forms in the mark blocks of the preset mark types according to the keyword information in the mark blocks of the preset mark types.
When the lexical analysis is performed on the NC code, the NC code is first tokenized, a character sequence in the code is converted into a token sequence, and a plurality of token blocks (i.e., token blocks) are generated from the converted token sequence. Detecting the mark type of each mark block, namely detecting the token type of each token block, when the token block with the preset mark type is detected, acquiring the keyword information in the token block with the preset mark type, and detecting the abnormal lexical method according to the keyword information in the token block with the preset mark type. The preset mark types comprise keyword mark types and illegal character mark types, when the token types of the token blocks are detected, the token blocks are matched with token blocks preset by the token types, and the token types are determined according to matching results.
Further, in the lexical analysis process, token blocks generated based on the character sequence of the NC codes are matched, and blank characters and token blocks corresponding to the annotation information are skipped during matching; when a keyword token block is detected, reading values behind the keywords in the token block, detecting the range, the form and the type of the values, and recording when error information is detected; displaying an illegal character when an undefined token block is detected, and recording the illegal character as error information; and obtaining the abnormal lexical method according to the recorded error information until the file detection is finished.
In the lexical analysis, the detection of the marker blocks may be performed simultaneously or sequentially, and is not limited herein.
Further, step 200 may comprise:
step 201, acquiring each program segment of an NC code in the NC code file;
step 202, creating a linked list node corresponding to each program segment;
step 203, storing the target mark blocks in any program segment into corresponding linked list nodes, and generating a data domain and a pointer domain corresponding to each target mark block; storing keyword information in each target mark block in the data field; the pointer field is used for indicating the position information of each target mark block in the linked list nodes;
and 204, generating a one-way linked list corresponding to the NC code file according to each linked list node.
When constructing the single linked list, obtaining each program segment in the NC code, wherein different program segments are divided into specific symbols such as "; "to perform the division. Creating linked list nodes corresponding to the program segments, storing one or more target mark blocks generated by codes corresponding to any program segment into the corresponding linked list nodes, and generating data and pointer fields of the target mark blocks in the linked list nodes. The marking blocks in the same program segment are stored in the same linked list node, and the marking blocks in different program segments are stored in different linked list nodes; each link list node comprises a data field and a pointer field, wherein the data field stores the key word information of each target mark block, and the pointer field is used for indicating the position information of each target mark block in the link list node. And generating a one-way linked list corresponding to the NC code based on each linked list node.
Further, when constructing the single linked list, the tag blocks in each program segment may be processed in batch, or may be processed sequentially, which is not limited herein. When the mark blocks are sequentially processed, referring to the construction flow schematic of the single linked list shown in fig. 2, type matching is performed on each mark block based on the mark blocks generated in the lexical analysis process, and when the mark blocks of specific division symbols are detected, namely program line feed is detected, a new linked list node is created for storing keyword information of each mark block in the next program segment; when keyword information is detected, storing the keyword information into a link list node which is newly created at present; then judging whether the file is the tail of the file, namely the last marked block of the NC code; if yes, constructing a one-way linked list according to data stored in each linked list node, and finishing the processing of NC codes; if not, continuing to match the next mark block until the last mark block.
Furthermore, because the tag blocks are generated in the lexical analysis process and the one-way linked list is constructed based on the tag blocks, the one-way linked list can be constructed based on the generated tag blocks in the lexical analysis process, namely the lexical analysis process and the one-way linked list construction process can be carried out simultaneously. Specifically, referring to the flow diagram of lexical analysis shown in fig. 3, after the character sequence of the NC code is tokenized to generate corresponding token blocks, type matching is performed on each generated token block, and the token type of each token block is detected one by one in order. If the mark block is a blank character or a mark block of the mark type of the annotation information, skipping the mark block; if the mark block of the line feed type represented by the segmentation symbol of the specific program segment is the mark block, a new linked list node is created; if the key word marks the mark block of the type, storing the mark block into a linked list node corresponding to the program segment where the mark block is located, and detecting whether the parameter in the key word information is legal or not so as to detect an illegal parameter; if the mark block is of an illegal character mark type, a lexical error is recorded; then judging whether the file is the tail of the file, if so, completing lexical analysis of NC codes and construction of a one-way linked list; if not, acquiring the next mark block and continuing to detect until the tail of the file. Finally, obtaining an abnormal lexical method according to the detected illegal parameters and illegal characters; and completing the construction of the one-way linked list according to the linked list nodes corresponding to the program segments and the key word information stored in the linked list nodes.
The lexical analysis function is to check whether lexical errors exist in NC codes, the lexical errors mainly comprise illegal characters, invalid numerical values, redundant spaces and the like, the illegal characters refer to character instructions which are not contained in a machine tool instruction set, and different types of machine tool instructions can be detected according to the difference of the instruction sets among different machine tool types in the detection process.
In the embodiment, the NC codes in the NC file are sequentially read in according to the character sequence through lexical analysis, corresponding NC code entities are established based on the generated marker blocks, a one-way linked list is generated, the original front-back sequence relation among program segments is guaranteed, on the basis of detecting abnormal lexical methods, the position information of the abnormal lexical methods in the source NC codes can be identified, abnormal positioning is realized, and the detection accuracy is improved.
In one embodiment, step 300 specifically includes:
301, according to a preset grammar rule, performing grammar analysis on the NC code based on the single-direction linked list, and detecting abnormal grammar in the NC code;
step 302, generating a code corpus according to a preset programming manual; the code corpus comprises a function code corpus and an auxiliary function code corpus;
and 303, performing semantic analysis on the NC codes based on the single linked list according to the code corpus and preset semantic rules, and detecting abnormal semantics in the NC codes.
In the embodiment, a syntax semantic rule is set, and the syntax semantic analysis is performed on the NC code based on the constructed one-way linked list. Specifically, firstly, on the basis of a preset grammar rule, performing grammar analysis on an NC code, and detecting abnormal grammar; and then generating a code corpus according to a preset programming manual, and performing semantic analysis on the NC codes based on preset semantic rules and the code corpus to detect abnormal semantics. The generated code corpus includes a function code corpus and an auxiliary function code corpus, namely a G code corpus and an M code corpus.
Further, in step 301, based on a preset syntax rule and a constructed one-way linked list, performing syntax analysis on the NC code, which specifically includes:
3011, acquiring target keyword information in each linked list node based on a pointer field of each linked list node in the unidirectional linked list; the target keyword information comprises a function code and an auxiliary function code;
step 3012, match the target keyword information with a preset grammar rule, detect the abnormal grammar of the unsuccessful grammar rule matching; the grammar rules comprise a first grammar rule corresponding to the function codes and a second grammar rule corresponding to the auxiliary function codes.
The grammar analysis function is that on the basis of lexical analysis, word sequences are combined into various grammar phrases, such as 'programs', 'sentences', 'expressions' and the like, whether the grammar structure of the programs conforms to preset grammar rules is analyzed and judged, and errors occurring on the structure of the numerical control codes are checked, namely the format of the numerical control codes and the use of characters are correct, but the codes are not written according to the grammar rules, namely grammar errors or logic problems in instruction sequences exist.
Specifically, the information of the linked list nodes corresponding to each program segment of the NC codes is stored in a form of a single linked list, and the linked list is traversed in a head pointer mode based on the pointer field in each linked list node. Each node of the constructed single-direction linked list comprises a data field and a pointer field, wherein the data field stores the information of each keyword and the identification information of whether the keyword exists. When syntax analysis is carried out, target keyword information in each linked list node is obtained, wherein the target keyword information comprises a G code and an M code; and matching the preset grammar rules of the acquired target keyword information field, and detecting abnormal grammars which are not matched with the preset grammar rules in the target keyword information. The preset grammar rules comprise a first grammar rule corresponding to the G code and a second grammar rule corresponding to the M code.
Preferably, nodes in the single-direction linked list are traversed, and abnormal grammar detection is performed one by one according to the sequence. Specifically, referring to the syntax analysis flow diagram shown in fig. 4, a link table node is selected from the unidirectional link table as a target node according to the sequence, whether the target node is the last node of the unidirectional link table is determined, if the target node is the last node, whether an end instruction is included in the target node is detected, if the end instruction is included, syntax analysis on the NC code is completed, and if the end instruction is not included, a syntax error is recorded into an error pool. If the target node is not the last node in the one-way linked list, detecting whether the keyword information stored in the target node contains target keyword information, namely a G code and an M code; if the code does not contain the G code and the M code, acquiring a next linked list node from the one-way linked list again according to the sequence as a target node for syntax detection; if the selected target node is the last node in the unidirectional linked list, acquiring the G code and the M code, matching the G code and the M code with a preset grammar rule, and if the matching is successful, acquiring a next linked list node as the target node according to the sequence to continue abnormal grammar detection until the selected target node is the last node in the unidirectional linked list; and if the matching is unsuccessful, recording error grammars which are unsuccessfully matched as abnormal grammars into an error pool, and then continuously acquiring the next linked list node as a target node according to the sequence to perform abnormal grammar detection. And when the grammar rules are matched, matching the G code with the first grammar rule, matching the M code with the second grammar rule, and recording grammar errors if the matching is unsuccessful. The end instruction contained in the last node is an instruction for ending execution of the NC code. In the present embodiment, an error pool is provided for storing error information such as detected abnormal lexical, abnormal syntax, and abnormal semantic.
Further, step 303 specifically includes:
3031, acquiring target keyword information in each linked list node based on the pointer domain of each linked list node in the one-way linked list; the target keyword information comprises a function code and an auxiliary function code;
3032, analyzing the target keyword information according to the code corpus to generate a natural language corresponding to the target keyword information;
3033, carrying out context semantic detection on the natural language to acquire semantic information of the natural language;
and 3034, matching the semantic information with a preset semantic rule, and detecting abnormal semantics which are unsuccessfully matched with the semantic rule.
Semantic analysis is to detect the context-related properties of the correct source program on the NC code structure, that is, to detect the static semantics of each grammatical structure, and further to verify whether the grammatical structure is meaningful. The influence generated by each statement is abstracted into one state of the finite state automata, and then the NC file is analyzed through the analysis finite state machine.
Specifically, traversing each node in the one-way linked list based on the pointer field of each linked list node in the one-way linked list to acquire target keyword information in each linked list node, namely identifying specific G codes and M codes; analyzing the G code and the M code based on the generated corpus, and analyzing parameters such as position coordinates, feeding speed, cutter number and the like of the G code and the M code, so that a natural language corresponding to the G code and the M code is generated according to the corpus; performing context semantic detection on the natural language, namely performing context semantic detection to acquire semantic information of the natural language; and matching the acquired semantic information with a preset semantic rule, thereby detecting abnormal semantics which are unsuccessfully matched with the preset semantic rule.
Preferably, when performing semantic analysis on the NC code, based on the constructed one-way linked list, adopting a traversal form to perform abnormal semantic detection on each linked list node in the one-way linked list one by one according to the sequence. Specifically, referring to the flow diagram of semantic analysis shown in fig. 5, basically the same as the syntax analysis, first, a linked list node is obtained from the one-way linked list in sequence as a target node, and whether the target node is the last node in the one-way linked list is judged; if the last node is the target node, detecting whether the keyword information stored in the target node contains an ending instruction, and if the keyword information contains the ending instruction, ending the semantic analysis; if the semantic error does not contain the ending instruction, recording the semantic error into an error pool; if the target node is not the last node in the one-way linked list, detecting whether the target node contains a G code and an M code; if the link list does not contain the G code and the M code, acquiring a next link list node again as a target node according to the sequence; if the natural language comprises the G code and the M code, respectively acquiring the G code and the M code, and respectively analyzing the acquired G code and the acquired M code according to a G code corpus and an M code corpus to obtain a corresponding natural language; performing context semantic detection based on the obtained natural language to obtain semantic information, and matching the semantic information with a preset semantic rule; if the matching is not successful, recording semantic errors into an error pool, and if the matching is successful, continuously acquiring a next linked list node as a target node according to the sequence to perform semantic detection until the acquired target node is the last node in the unidirectional linked list.
It can be understood that, because the syntax analysis and the semantic analysis are performed based on the constructed single linked list and the G code and the M code in the NC code are both detected, the syntax analysis and the semantic analysis may be performed simultaneously or sequentially, and when the syntax analysis and the semantic analysis are performed simultaneously, the order is not distinguished, and thus, the description is omitted.
In the embodiment, based on the constructed single-direction linked list, abnormal grammar and abnormal semantics in the NC codes are detected by performing syntactic analysis and semantic analysis on the NC codes, and the detected abnormal lexical method is combined to perform comprehensive analysis on the abnormal of the NC codes, so that the detection accuracy of the abnormal NC codes is improved.
The NC code abnormality detection apparatus provided by the present invention is described below, and the NC code abnormality detection apparatus described below and the NC code abnormality detection method described above may be referred to in correspondence with each other.
Referring to fig. 6, an NC code abnormality detection apparatus provided in an embodiment of the present invention includes:
the lexical analysis module 10 is configured to acquire an NC code file to be detected, perform lexical analysis on an NC code in the NC code file, and detect an abnormal lexical in the NC code;
a linked list construction module 20, configured to construct a single-direction linked list based on the NC code;
a syntax semantic analysis module 30, configured to perform syntax semantic analysis on the NC code based on the one-way linked list, and detect abnormal syntax and abnormal semantics in the NC code;
and the anomaly detection module 40 is used for determining the anomaly information of the NC codes according to the anomaly lexical method, the anomaly grammar and the anomaly semantic.
In one embodiment, the lexical analysis module 10 is further configured to:
converting the character sequence of the NC code into a marking sequence, and generating a plurality of marking blocks according to the marking sequence;
detecting the mark type of each mark block, and acquiring keyword information in the mark blocks of the preset mark type when the mark blocks of the preset mark type are detected; the preset mark types comprise a keyword mark type and an illegal character mark type;
and detecting abnormal lexical forms in the mark blocks of the preset mark types according to the keyword information in the mark blocks of the preset mark types.
In one embodiment, the linked list building module 20 is further configured to:
acquiring each program segment of an NC code in the NC code file;
creating a linked list node corresponding to each program segment;
storing the target mark blocks in any program segment into corresponding linked list nodes, and generating a data domain and a pointer domain corresponding to each target mark block; storing keyword information in each target mark block in the data field; the pointer field is used for indicating the position information of each target mark block in the linked list nodes;
and generating a one-way linked list corresponding to the NC code file according to each linked list node.
In one embodiment, the syntax parsing module 30 is further configured to:
according to a preset grammar rule, performing grammar analysis on the NC codes based on the one-way linked list, and detecting abnormal grammars in the NC codes;
generating a code corpus according to a preset programming manual; the code corpus comprises a function code corpus and an auxiliary function code corpus;
and performing semantic analysis on the NC codes based on the single linked list according to the code corpus and preset semantic rules, and detecting abnormal semantics in the NC codes.
In one embodiment, the syntax parsing module 30 is further configured to:
acquiring target keyword information in each linked list node based on the pointer domain of each linked list node in the one-way linked list; the target keyword information comprises a function code and an auxiliary function code;
matching the target keyword information with a preset grammar rule, and detecting abnormal grammar which is unsuccessfully matched with the grammar rule; the grammar rules comprise a first grammar rule corresponding to the function codes and a second grammar rule corresponding to the auxiliary function codes.
In one embodiment, the syntax parsing module 30 is further configured to:
acquiring target keyword information in each linked list node based on the pointer domain of each linked list node in the one-way linked list; the target keyword information comprises a function code and an auxiliary function code;
analyzing the target keyword information according to the code corpus to generate a natural language corresponding to the target keyword information;
performing context semantic detection on the natural language to acquire semantic information of the natural language;
and matching the semantic information with a preset semantic rule, and detecting abnormal semantics which are unsuccessfully matched with the semantic rule.
In one embodiment, the exception lexical includes illegal parameters and illegal characters; the exception syntax comprises an instruction combination error, an instruction and functional word matching error, an instruction sequence error, a logic error, a process error and a program starting instruction error; the exception semantics comprise value exception of a functional word, necessary instruction exception, processing sequence error and processing parameter error.
Fig. 7 illustrates a physical structure diagram of an electronic device, and as shown in fig. 7, the electronic device may include: a processor (processor) 710, a communication Interface (Communications Interface) 720, a memory (memory) 730, and a communication bus 740, wherein the processor 710, the communication Interface 720, and the memory 730 communicate with each other via the communication bus 740. Processor 710 may call logic instructions in memory 730 to perform an NC code exception detection method comprising:
acquiring an NC code file to be detected, performing lexical analysis on an NC code in the NC code file, and detecting an abnormal lexical in the NC code;
constructing a single-direction linked list based on the NC codes;
performing syntax semantic analysis on the NC codes based on the single-direction linked list, and detecting abnormal syntax and abnormal semantics in the NC codes;
and determining the abnormal information of the NC code according to the abnormal lexical method, the abnormal grammar and the abnormal semantics.
In addition, the logic instructions in the memory 730 can be implemented in the form of software functional units and stored in a computer readable storage medium when the software functional units are sold or used as independent products. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.
In another aspect, the present invention also provides a computer program product, the computer program product including a computer program, the computer program being storable on a non-transitory computer-readable storage medium, the computer program being capable of executing, when executed by a processor, the NC code anomaly detection method provided by the above methods, the method including:
acquiring an NC code file to be detected, performing lexical analysis on an NC code in the NC code file, and detecting an abnormal lexical in the NC code;
constructing a single-direction linked list based on the NC codes;
performing syntax semantic analysis on the NC codes based on the single-direction linked list, and detecting abnormal syntax and abnormal semantics in the NC codes;
and determining the abnormal information of the NC code according to the abnormal lexical method, the abnormal grammar and the abnormal semantics.
In yet another aspect, the present invention also provides a non-transitory computer-readable storage medium, on which a computer program is stored, the computer program being implemented by a processor to perform the NC code anomaly detection method provided by the above methods, the method comprising:
acquiring an NC code file to be detected, performing lexical analysis on an NC code in the NC code file, and detecting an abnormal lexical in the NC code;
constructing a single-direction linked list based on the NC codes;
performing syntax semantic analysis on the NC codes based on the single-direction linked list, and detecting abnormal syntax and abnormal semantics in the NC codes;
and determining the abnormal information of the NC codes according to the abnormal lexical method, the abnormal grammar and the abnormal semantics.
The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.
Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware. With this understanding in mind, the above-described technical solutions may be embodied in the form of a software product, which can be stored in a computer-readable storage medium such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the methods described in the embodiments or some parts of the embodiments.
Finally, it should be noted that: the above examples are only intended to illustrate the technical solution of the present invention, but not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.
Claims (10)
1. An NC code abnormality detection method, characterized by comprising:
acquiring an NC code file to be detected, performing lexical analysis on an NC code in the NC code file, and detecting an abnormal lexical in the NC code;
constructing a single-direction linked list based on the NC codes;
performing syntax semantic analysis on the NC codes based on the single-direction linked list, and detecting abnormal syntax and abnormal semantics in the NC codes;
and determining the abnormal information of the NC code according to the abnormal lexical method, the abnormal grammar and the abnormal semantics.
2. The NC code anomaly detection method according to claim 1, wherein the step of performing lexical analysis on the NC code to detect anomalous lexical patterns in the NC code includes:
converting the character sequence of the NC code into a marking sequence, and generating a plurality of marking blocks according to the marking sequence;
detecting the mark type of each mark block, and acquiring keyword information in the mark blocks of the preset mark type when the mark blocks of the preset mark type are detected; the preset mark types comprise a keyword mark type and an illegal character mark type;
and detecting abnormal lexical forms in the mark blocks of the preset mark types according to the keyword information in the mark blocks of the preset mark types.
3. The NC code anomaly detection method according to claim 2, wherein the step of constructing a singly linked list based on NC codes in the NC code file includes:
acquiring each program segment of an NC code in the NC code file;
creating a linked list node corresponding to each program segment;
storing the target mark blocks in any program segment into corresponding linked list nodes, and generating a data domain and a pointer domain corresponding to each target mark block; storing keyword information in each target mark block in the data field; the pointer field is used for indicating the position information of each target mark block in the linked list nodes;
and generating a one-way linked list corresponding to the NC code file according to each linked list node.
4. The NC code anomaly detection method according to claim 3, wherein the step of performing syntax semantic analysis on the NC code based on the singly linked list to detect anomalous syntax and anomalous semantics in the NC code includes:
according to a preset grammar rule, performing grammar analysis on the NC codes based on the one-way linked list, and detecting abnormal grammars in the NC codes;
generating a code corpus according to a preset programming manual; the code corpus comprises a function code corpus and an auxiliary function code corpus;
and according to the code corpus and preset semantic rules, performing semantic analysis on the NC codes on the basis of the single-direction linked list, and detecting abnormal semantics in the NC codes.
5. The NC code anomaly detection method according to claim 4, wherein the step of detecting the anomalous syntax in the NC code by parsing the NC code based on the one-way linked list according to a preset syntax rule includes:
acquiring target keyword information in each linked list node based on the pointer domain of each linked list node in the one-way linked list; the target keyword information comprises a function code and an auxiliary function code;
matching the target keyword information with a preset grammar rule, and detecting abnormal grammar which is unsuccessfully matched with the grammar rule; the grammar rules comprise a first grammar rule corresponding to the function codes and a second grammar rule corresponding to the auxiliary function codes.
6. The NC code anomaly detection method according to claim 4, wherein the step of performing semantic analysis on the NC codes based on the single linked list according to the code corpus and preset semantic rules to detect anomalous semantics in the NC codes comprises:
acquiring target keyword information in each linked list node based on the pointer domain of each linked list node in the one-way linked list; the target keyword information comprises a function code and an auxiliary function code;
analyzing the target keyword information according to the code corpus to generate a natural language corresponding to the target keyword information;
performing context semantic detection on the natural language to acquire semantic information of the natural language;
and matching the semantic information with a preset semantic rule, and detecting abnormal semantics which are unsuccessfully matched with the semantic rule.
7. The NC code abnormality detection method according to claim 1, wherein the abnormal lexical method includes an illegal parameter and an illegal character; the exception syntax comprises an instruction combination error, an instruction and functional word matching error, an instruction sequence error, a logic error, a process error and a program starting instruction error; the exception semantics comprise value exception of a functional word, necessary instruction exception, processing sequence error and processing parameter error.
8. An NC code abnormality detection apparatus, comprising:
the lexical analysis module is used for acquiring an NC code file to be detected, performing lexical analysis on an NC code in the NC code file, and detecting an abnormal lexical in the NC code;
the linked list construction module is used for constructing a single-direction linked list based on the NC code;
the syntax semantic analysis module is used for carrying out syntax semantic analysis on the NC codes based on the one-way linked list and detecting abnormal syntax and abnormal semantics in the NC codes;
and the anomaly detection module is used for determining the anomaly information of the NC code according to the anomaly lexical method, the anomaly grammar and the anomaly semantic.
9. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the NC code anomaly detection method according to any one of claims 1 to 7 when executing the program.
10. A non-transitory computer-readable storage medium having stored thereon a computer program, wherein the computer program, when executed by a processor, implements the NC code anomaly detection method according to any one of claims 1 to 7.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211047591.9A CN115481396A (en) | 2022-08-29 | 2022-08-29 | NC code abnormality detection method, device, equipment and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211047591.9A CN115481396A (en) | 2022-08-29 | 2022-08-29 | NC code abnormality detection method, device, equipment and storage medium |
Publications (1)
Publication Number | Publication Date |
---|---|
CN115481396A true CN115481396A (en) | 2022-12-16 |
Family
ID=84422946
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202211047591.9A Pending CN115481396A (en) | 2022-08-29 | 2022-08-29 | NC code abnormality detection method, device, equipment and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN115481396A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115994099A (en) * | 2023-03-22 | 2023-04-21 | 中科航迈数控软件(深圳)有限公司 | Automatic checking method, device and equipment for numerical control machine tool codes and storage medium |
CN116126736A (en) * | 2023-04-17 | 2023-05-16 | 青岛青软晶尊微电子科技有限公司 | Chip code optimization method based on user requirements |
-
2022
- 2022-08-29 CN CN202211047591.9A patent/CN115481396A/en active Pending
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115994099A (en) * | 2023-03-22 | 2023-04-21 | 中科航迈数控软件(深圳)有限公司 | Automatic checking method, device and equipment for numerical control machine tool codes and storage medium |
CN115994099B (en) * | 2023-03-22 | 2023-05-30 | 中科航迈数控软件(深圳)有限公司 | Automatic checking method, device and equipment for numerical control machine tool codes and storage medium |
CN116126736A (en) * | 2023-04-17 | 2023-05-16 | 青岛青软晶尊微电子科技有限公司 | Chip code optimization method based on user requirements |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN115481396A (en) | NC code abnormality detection method, device, equipment and storage medium | |
US11775414B2 (en) | Automated bug fixing using deep learning | |
CN102339252B (en) | Static state detecting system based on XML (Extensive Makeup Language) middle model and defect mode matching | |
CN108170468B (en) | Method and system for automatically detecting annotation and code consistency | |
CN109885479A (en) | Software obfuscation test method and device based on path record truncation | |
WO2022226716A1 (en) | Deep learning-based java program internal annotation generation method and system | |
CN111611788A (en) | Data processing method and device, electronic equipment and storage medium | |
CN116360794A (en) | Database language analysis method, device, computer equipment and storage medium | |
CN111754352A (en) | Method, device, equipment and storage medium for judging correctness of viewpoint statement | |
CN107153564B (en) | Lexical analysis tool | |
CN110989991A (en) | Method and system for detecting source code clone open source software in application program | |
CN112925874B (en) | Similar code searching method and system based on case marks | |
CN113535461B (en) | Visual verification method for interlocking lower computer data based on configuration file | |
JP5413623B2 (en) | Assemble device, syntax analysis method, and assembler program | |
CN114201177A (en) | File generation method, file generation device, electronic equipment, medium and computer program product | |
CN114327614A (en) | Method and application for recording and analyzing data flow of reference model | |
CN113515443A (en) | Multi-language automatic checking method, device, computer equipment and storage medium | |
CN112486479A (en) | Data acquisition method and device | |
CN112733517A (en) | Method for checking requirement template conformity, electronic equipment and storage medium | |
CN117707987B (en) | Test case detection method and device, electronic equipment and storage medium | |
CN112698836B (en) | Code quality attribute judgment method for complex user comments | |
CN116955211B (en) | Standard compliance testing method and device, electronic equipment and storage medium | |
CN116431474B (en) | Security configuration item constraint extraction method of program source code based on static analysis | |
CN116414445B (en) | Homology detection method and system based on source code watermark | |
Marchenko et al. | English text parsing by means of error correcting automaton |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |