Real-time incremental grammar highlighting and error detection method based on ST language

By using an incremental syntax highlighting and error detection method based on Antlr, the problems of low parsing efficiency and resource waste in ST language editing tools are solved, achieving efficient and accurate syntax highlighting and error detection, thus improving the user's programming experience.

CN121809465APending Publication Date: 2026-04-07NANJING SCIYON AUTOMATION GRP
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-16
Publication Date
2026-04-07

AI Technical Summary

Technical Problem

The existing ST language editing tools' full-data parsing scheme results in low parsing efficiency, high resource consumption, and poor user experience, failing to meet the high-efficiency programming needs of the industrial control field.

Method used

We employ a real-time incremental syntax highlighting and error detection method based on Antlr. By monitoring user operations, we only parse the changed areas and context scope. Combined with syntax dependency analysis, we generate a local syntax analysis tree and update the highlighting and error information in real time.

Benefits of technology

Significantly improves parsing efficiency and resource utilization, reduces parsing time and resource consumption, enhances the accuracy of syntax highlighting and error detection, and improves the user's programming experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121809465A_ABST
    Figure CN121809465A_ABST
Patent Text Reader

Abstract

The invention discloses a real-time incremental grammar highlighting and error detection method based on an ST language, and the optimization method comprises the steps: (1) monitoring the editing operation of a user, and detecting a minimum recognition range based on the input of the user in real time, (2) only executing incremental lexical and grammar reanalysis on a change region and an associated context by using an antlr in combination with a predefined grammar rule, and (3) obtaining an error detection result. (3) an antlr grammar analysis formula only needs to contain a keyword, a variable name and a grammar analysis formula of assignment operation; (4) traversing the abstract grammar tree to carry out symbol search and type identification in a corresponding symbol table; (5) traversing the abstract grammar tree to carry out grammar integrity judgment; (6) adding a cache mechanism, storing an analysis result in a cache, and storing the analysis result in the cache; and repeated analysis is avoided. According to the method, the problems of low efficiency and response lag of traditional full-amount analysis are solved, the instantaneity and interaction experience of ST language editing are improved, and the method is suitable for ST language programming environment development in the field of industrial control.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer programming language processing technology, specifically to a real-time incremental syntax highlighting and error detection method for ST language based on Antlr, applicable to ST language editing scenarios such as PLC (Programmable Logic Controller) programming and industrial automation control system development in the industrial control field. Background Technology

[0002] ST (Structured Text) is an industrial control programming language specified by the IEC 61131-3 standard. It features a high-level language-like syntax structure and is widely used in PLC program development, industrial robot control, and other scenarios. In ST programming, syntax highlighting enhances readability by distinguishing code elements (keywords, comments, etc.), while error detection promptly identifies syntax and semantic errors. These two features are core to improving programming efficiency.

[0003] Traditional ST language editing tools often employ a full parsing approach for syntax highlighting and error detection: every time a user modifies the text, the tool re-executes lexical and syntactic parsing of the entire document. This approach has significant drawbacks: first, it has low parsing efficiency; when editing large programs (tens of thousands of lines of code), full parsing can take hundreds of milliseconds, resulting in sluggish interface response; second, it consumes a lot of system resources, as repeatedly parsing unchanged content wastes CPU and memory; and third, it provides a poor user experience, with noticeable delays between editing operations and result feedback.

[0004] Antlr (Another Tool for Language Recognition) is a powerful parser generation tool that supports generating lexical and syntax parsers based on custom rules and is widely used in programming language processing. However, current ST language processing solutions based on Antlr mostly focus on full parsing and have not yet formed a mature incremental processing system. This fails to solve the efficiency problem of traditional solutions and restricts the performance improvement of ST language editing tools. Summary of the Invention

[0005] The purpose of this invention is to address the shortcomings of existing technologies and provide a real-time incremental syntax highlighting and error detection method for the ST language based on Antlr, which balances parsing efficiency and accuracy.

[0006] To achieve the above objectives, this invention provides a real-time incremental syntax highlighting and error detection method for the ST language based on Antlr, comprising the following steps: Step (1) Define the lexical and grammatical rules of the ST language based on the Antlr tool, generate the ST language lexical parser and grammatical parser, and construct the syntax analysis tree generation model; Step (2) Listen to user operations in the ST language editing area, capture text change events, extract the start and end positions of the changed area and the changed content, and determine the context scope of incremental parsing through syntax dependency analysis; wherein, the context scope includes the statement blocks, function definitions and variable declaration areas associated with the changed area; Step (3) Use the lexical parser generated in step (1) to perform incremental lexical analysis on the text of the changed region and context range, generate a lexical unit sequence, and combine it with the predefined lexical unit-highlighting style mapping rules to realize incremental syntax highlighting update; wherein, the predefined lexical unit-highlighting style mapping rules include the color and font style configuration of keywords, identifiers, constants, operators and comments; Step (4) Perform syntactic analysis on the incremental lexical analysis results using the parser generated in step (1), generate a local parsing tree, traverse the tree nodes to identify syntactic error nodes, and match the error type library to determine the error information; where the error information includes syntactic errors and semantic errors. Syntactic errors include mismatched parentheses, unfinished statements, and misuse of keywords. Semantic errors include undefined variables, mismatched types, and incorrect number of function parameters. Step (5) provides real-time feedback of syntax highlighting update results and error messages to the editing interface to complete real-time display; at the same time, the parsed text area is cached, and the cached results are called first when editing again.

[0007] This invention performs real-time incremental parsing of the ST language, parsing only text within changed regions and contextual ranges. This effectively reduces parsing time, avoids redundant parsing, and significantly reduces resource consumption. Furthermore, based on Antlr's precise parsing capabilities and combined with syntax dependency analysis, it effectively improves the accuracy of syntax highlighting and error detection.

[0008] More specifically, the steps of the real-time incremental syntax highlighting and error detection method for the ST language based on Antlr in this invention are as follows: 1. Parser Construction Based on the Antlr tool and in accordance with the syntax specifications of the ST language in the IEC 61131-3 standard, lexical rules and grammatical rules are defined.

[0009] Lexical rules: Define the recognition rules for keywords (such as "PROGRAM", "FUNCTION", "IF"), identifiers (variable names, function names), numeric constants (integers, floating-point numbers), string constants, operators (arithmetic operators, logical operators), and comments (single-line comments " / / ", multi-line comments " / ... / "); Syntax rules: Define the composition rules of syntactic units such as assignment statements, conditional statements (IF-ELSE), loop statements (FOR, WHILE), function definitions, and program block structures.

[0010] Antlr's code generation function converts the above rules into ST language lexer and parser, and at the same time builds a syntax tree (AST) generation model to provide basic parsing capabilities for subsequent incremental parsing.

[0011] 2. Incremental Region Capture and Context Determination Event Listening: Real-time monitoring of user editing operations, including input, deletion, modification, batch pasting, undo / redo, etc., through hook functions in the editing interface (such as based on VS Code plugin, Qt editor), capturing text change events; supports precise extraction of changed areas for batch editing and pasting operations; Change Extraction: Parse change events, extract the starting row number, column number, ending row number, column number, and change content (added text, deleted text) of the changed area, and generate a description of the changed area; Context analysis: Based on the syntax structure tree of the ST language, perform syntax dependency analysis on the changed region—traverse the parent node (such as statement block node), child node (such as expression node), and sibling node (such as adjacent statement node) of the syntax node corresponding to the changed position to determine the context scope of incremental parsing, ensuring that the parsing covers all syntax units affected by the change and avoiding errors caused by local parsing.

[0012] 3. Incremental syntax highlighting implementation Incremental lexical analysis employs a "change region marker - local scan - result fusion" mechanism: the lexical parser generated in step 1 is invoked, and only the text within the changed region and context is scanned to generate a sequence of lexical units (such as "KEYWORD_IF", "IDENTIFIER_var", and "CONSTANT_INT_10"); the historical lexical analysis results are directly reused for unchanged regions, eliminating the need for repeated scanning and reducing parsing overhead; Highlighting rule matching: Predefined mapping rules between lexical units and highlighting styles, as shown in the following example: Keywords: red, bold; Identifier: Black, regular font; Constants (numeric, string): Blue, regular font; Operators: Green, regular font; Note: Gray, italic; Highlighting update: Based on the predefined lexical unit-highlighting style mapping rules, the highlighting style is updated only for the text in the changed area and context range in the editing interface. The unchanged area retains the original highlighting effect, realizing efficient incremental syntax highlighting.

[0013] 4. Real-time error detection and information matching Incremental parsing: The parser generated in step 1 is invoked to perform grammatical analysis on the sequence of lexical units generated by incremental lexical analysis, generating a local parsing tree; the local parsing tree is then merged with the historical complete parsing tree, updating only the changed related nodes and retaining the parsing results of the unchanged nodes; Error node identification: Traverse the local parsing tree and identify syntax error nodes through the parser's error callback interface, such as mismatched parentheses, unfinished statements, and expression syntax errors. Error message matching: The system calls a preset error type library to perform type matching on error nodes—syntax errors include mismatched parentheses, unfinished statements, misused keywords, etc.; semantic errors include undefined variables, mismatched variable types, incorrect number of function parameters, etc.; at the same time, it extracts the error location (line number, column number) and combines it with the repair suggestions in the error type library (such as "missing right parenthesis ')'", "variable 'temp' is undefined, please check the declaration") to generate a complete error message; Error library optimization: The error type library supports iterative optimization through machine learning algorithms—collecting new error cases during the user's editing process, automatically labeling error types and supplementing repair suggestions, thereby improving the coverage and accuracy of error detection.

[0014] 5. Results Feedback and Caching Optimization Real-time feedback: The incremental syntax highlighting update results are rendered to the editing interface in real time, ensuring that editing operations are synchronized with the highlighting display; error messages are displayed in two forms: inline annotation (adding a red wavy line to the error location) and pop-up prompts (displaying the error location, error type, and repair suggestions), making it convenient for users to view and correct errors in a timely manner; Caching mechanism: An LRU (Least Recently Used) caching strategy is introduced to cache the lexical unit sequence, parsing tree nodes, and highlighting style information of the parsed text region; when the user edits the region again, the cached results are called first to reduce repeated parsing; cached data that has not been accessed for a long time is cleaned up regularly to optimize memory usage and avoid memory leaks.

[0015] Beneficial effects:

[0016] 1) Significantly improved parsing efficiency: Adopting an incremental parsing mechanism, only the changed areas and related contexts are parsed. Compared with traditional full parsing, the parsing time is reduced by more than 60%, and the response latency is controlled within 10 milliseconds when editing tens of thousands of lines of code. 2) Significantly reduced resource consumption: Avoids repeated parsing of unchanged content, reduces CPU usage by more than 50%, and optimizes memory usage by more than 30%; 3) High detection accuracy: Based on Antlr's precise parsing capabilities, combined with syntax dependency analysis, the accuracy of syntax highlighting and error detection can both reach over 99.5%; 4) Highly extensible: It supports adapting to the extended syntax of ST language by modifying lexical / syntactic rules, and the error type library can be continuously optimized through machine learning, making it suitable for ST language editing needs in different industrial control scenarios; 5) User experience optimization: Real-time highlight updates and error prompts eliminate delays in editing operations and result feedback, improving programming efficiency and interactive experience. Attached Figure Description

[0017] Figure 1 This is a flowchart of the real-time incremental syntax highlighting and error detection method for the ST language based on Antlr, as described in this invention. Figure 2 This is a schematic diagram of the abstract syntax tree constructed in an embodiment of the present invention; Figure 3 This is a schematic diagram of the caching mechanism used in an embodiment of the present invention. Detailed Implementation

[0018] The present invention will now be described in further detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are for illustrative purposes only and are not intended to limit the invention. Furthermore, it should be noted that, for ease of description, only the parts relevant to the present invention are shown in the drawings, not the entire structure.

[0019] Implementation example: Qt-based industrial PLC programming software like Figure 1 As shown, the specific steps are as follows: 1. Parser Construction: Generate a C++ version of the ST language parser using Antlr and integrate it into Qt-developed programming software. The Antlr parser g4 file is shown below: / / Language Structured Text (ST) keywords:OR| labels:EXPT|RANGE|ASSIGN|NE_SIGN|LE_SIGN|GE_SIGN|EQ_SIGNEXPT|RANGE|ASSIGN|NE_SIGN|LE_SIGN|GE_SIGN|EQ_SIGN|LT_SIGN|GT_SIGN|PLUS|MINUS|AMP|PERCENT|MUL_SIGN|DIV|SCOLON; lang_keys: keywords|labels|elem_type_name ; case_list: case_list_elem ( COMMA case_list_elem )* COLON; case_list_elem: constant (RANGE constant)?; expression: func_call|sizeofcall|ref_deref|lang_keys| variable_access|case_list|constant; primary_expr:(LPAREN (expression|primary_expr)* RPAREN)|expression; func_call: symbolic_variable LPAREN ( param_assign ( COMMA param_assign )* ) ? RPAREN; sizeofcall : SIZEOF LPAREN (array_spec|data_type_access|variable)RPAREN; param_assign: ( (variable_name ASSIGN )?primary_expr*)| (NOT ?variable_name MOVE_OUT (variable_access|MUL_SIGN)) ; variable_access : variable (multibit_part_access)*; multibit_part_access : DOT ( UNSIGNED_INT | MULTIBIT_PART_SPECIFIER); 2. Incremental processing: Event listening is performed, and text changes are captured through the signal and slot mechanism of Qt's QTextEdit control. Combined with a custom syntax dependency analysis module, the parsing scope is determined, and context analysis is performed. 3. Taking the text "var1:=func(a[bc].de[0].f) + 1;" as an example, the abstract syntax tree constructed after parsing is as follows: Figure 2 The tree diagram in black text below is shown.

[0020] 4. Traversal Figure 2 The abstract syntax tree is used for rule matching, symbol table lookup, and error detection. The traversal result is... Figure 2 The blue markers at the nodes of the tree diagram.

[0021] 5. Match the parsed text results to the corresponding highlighting rules and store them in the cache pool, such as... Figure 3 As shown.

[0022] 6. Results Display: Syntax highlighting is achieved using Qt's QSyntaxHighlighter, and error messages are displayed using QToolTip; Optimization results: Compared with the original full parsing solution, parsing efficiency is improved by 70%, interface response latency is reduced from 300 milliseconds to less than 10 milliseconds, and user satisfaction is significantly improved.

[0023] The above description is only a preferred embodiment of the present invention. It should be noted that those skilled in the art can make several improvements without departing from the principle of the present invention, and these improvements should also be considered within the scope of protection of the present invention.

Claims

1. A real-time incremental syntax highlighting and error detection method for the ST language based on Antlr, characterized in that, Includes the following steps: Step (1) Define the lexical and grammatical rules of the ST language based on the Antlr tool, generate the ST language lexical parser and grammatical parser, and construct the syntax analysis tree generation model; Step (2) Listen to user operations in the ST language editing area, capture text change events, extract the start and end positions of the changed area and the changed content, and determine the context scope of incremental parsing through syntax dependency analysis; the context scope includes the statement blocks, function definitions and variable declaration areas associated with the changed area; Step (3) Use the lexical parser to perform incremental lexical analysis on the text of the changed region and context range, generate a lexical unit sequence, and combine it with the predefined lexical unit-highlighting style mapping rules to realize incremental syntax highlighting update; the predefined lexical unit-highlighting style mapping rules include the color and font style configuration of keywords, identifiers, constants, operators and comments; Step (4) Perform syntactic analysis on the incremental lexical analysis results through the syntactic parser, generate a local syntactic analysis tree, traverse the tree nodes to identify syntactic error nodes, and match the error type library to determine the error information. The error information includes syntactic errors and semantic errors. Syntactic errors include mismatched parentheses, unfinished statements, and misuse of keywords. Semantic errors include undefined variables, mismatched types, and incorrect number of function parameters. Step (5) provides real-time feedback of syntax highlighting update results and error messages to the editing interface to complete real-time display; at the same time, the parsed text area is cached, and the cached results are called first when editing again.

2. The ST language real-time incremental syntax highlighting and error detection method according to claim 1, characterized in that, The lexical rules in step (1) include the identification rules for keywords, identifiers, numerical constants, string constants, operators, and comments; the syntax rules include the composition rules for assignment statements, conditional statements, loop statements, function definitions, and program block structures.

3. The ST language real-time incremental syntax highlighting and error detection method according to claim 1, characterized in that, In step (2), text change events are captured in real time through the hook function of the editing interface; text change events include input, deletion, modification, batch pasting, and undo / redo.

4. The ST language real-time incremental syntax highlighting and error detection method according to claim 1, characterized in that, In step (2), the syntax dependency analysis is based on the syntax structure tree of the ST language. The context of incremental parsing is determined by traversing the parent node, child node and sibling node of the syntax node corresponding to the change position.

5. The ST language real-time incremental syntax highlighting and error detection method according to claim 1, characterized in that, The specific method of incremental lexical analysis in step (3) is as follows: local scanning is used for the text of the changed region and context range, and the lexical unit sequence is generated by the "changed region marking-local scanning-result fusion" mechanism. The historical lexical unit sequence is directly reused for the unchanged region.

6. The ST language real-time incremental syntax highlighting and error detection method according to claim 1, characterized in that, Step (4) also includes error type library optimization: collecting new error cases during the user's editing process and continuously iterating and optimizing them through machine learning algorithms.

7. The ST language real-time incremental syntax highlighting and error detection method according to claim 1, characterized in that, In step (5), the parsed text region is cached using the following mechanism: an LRU strategy is adopted to periodically clean up parsed results that have not been accessed for a long time, thereby optimizing memory usage.