Analysis method and device of expression language and storage medium

By calling the parser in the editor to parse the abstract syntax tree of the expression text, the compatibility problem between the expression language and the proprietary system is solved, and functions such as syntax highlighting, code inspection and auto-completion are implemented, improving the efficiency and accuracy of expression editing.

CN120872344APending Publication Date: 2025-10-31DONSON TIMES INFORMATION TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510852489.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-24
Publication Date
2025-10-31

AI Technical Summary

Technical Problem

Existing expression languages ​​and parsing tools are all closed-source and cannot be compatible with proprietary systems.

Method used

This paper provides a method for parsing expression languages. By calling the parser integrated into the current program, the method parses the abstract syntax tree corresponding to the expression text and responds to the functional operations of the abstract syntax tree, including syntax highlighting, code inspection, auto-completion, and expression evaluation.

Benefits of technology

It achieves automated parsing of expression language, supports compatibility with proprietary systems, improves the interactive experience and development efficiency of the editor, and reduces syntax errors.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120872344A_ABST
    Figure CN120872344A_ABST
Patent Text Reader

Abstract

The invention discloses an expression language analysis method and device and a storage medium, and relates to the technical field of electronic digital data processing.The expression language analysis method comprises the steps that in response to an expression text input by a user in an editor, an analyzer integrated to a current program is called; analyzing an abstract syntax tree corresponding to the expression text based on the analyzer; and in response to function operations for the abstract syntax tree, outputting a function execution result at the editor, the function operations including syntax highlighting, code checking, automatic completion and expression calculation. According to the method and the device, the technical problem that expression languages and analysis tools in related technologies are closed sources and cannot be compatible with own systems is solved, and the technical effect of automatically generating the analyzer to analyze the expressions of the currently defined grammar rules is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of electronic digital data processing technology, and in particular to a method, apparatus and storage medium for parsing an expression language. Background Technology

[0002] In computer programming and data analysis, it's often necessary to parse expressions for calculation, analysis, and processing. Related technologies, such as Power BI, provide a powerful expression language called "DAX (Data Analysis Expressions)." Tableau uses its own computational language to edit expressions, including creating calculated fields, parameters, formulas, etc. However, the expression languages ​​and parsing tools in these technologies are closed-source and incompatible with proprietary systems. Summary of the Invention

[0003] The main purpose of this application is to provide a method, device, and storage medium for parsing expression languages, aiming to solve the technical problem that expression languages ​​and parsing tools in related technologies are closed-source and cannot be compatible with proprietary systems.

[0004] To achieve the above objectives, this application provides a method for parsing an expression language, the method comprising:

[0005] In response to the expression text entered by the user in the editor, the parser integrated into the current program is invoked;

[0006] The parser parses the abstract syntax tree corresponding to the expression text;

[0007] In response to a function operation on the abstract syntax tree, the function execution result is output in the editor, the function operation including syntax highlighting, code inspection, auto-completion, and expression evaluation.

[0008] In one embodiment, prior to the step of invoking the parser integrated into the current program, the following steps are included:

[0009] Obtain the syntax rules of the expression language, including lexical definition rules and expression syntax definition rules;

[0010] A recursive descent parser is generated based on the aforementioned grammar rules;

[0011] Integrate the parser into the current program.

[0012] In one embodiment, the step of generating a recursive descent parser based on the syntax rules includes:

[0013] Parse the syntax description file generated based on the syntax rules;

[0014] The parser, which generates a recursive descent function based on the parsing results of the grammar description file, includes a lexical analyzer and a syntax analyzer.

[0015] In one embodiment, prior to the step of parsing the syntax description file generated based on the syntax rules, the following steps are included:

[0016] Obtain the basic format requirements, field declaration syntax, function call conventions, operation priority, and syntax constraints corresponding to the parser;

[0017] The syntax rules are processed according to the basic format requirements, field declaration syntax, function call specifications, operation priority, and syntax constraints to generate the syntax description file.

[0018] In one embodiment, the lexical definition rules include basic element definitions, which include spaces, identifiers, numbers, and strings; the expression syntax definition rules include expression hierarchy structure definitions and entry rules; the syntax rules also include lexical priorities.

[0019] In one embodiment, the step of parsing the abstract syntax tree corresponding to the expression text based on the parser includes:

[0020] The expression text is split into a sequence of terms;

[0021] Based on the parser, at least one syntax tree node is determined by recognizing the word sequence.

[0022] The syntax tree nodes are assembled into the abstract syntax tree.

[0023] In one embodiment, the step of outputting the function execution result in the editor in response to a function operation on the abstract syntax tree includes:

[0024] In response to a functional operation on the abstract syntax tree, the target function is determined;

[0025] Based on the target function and the abstract syntax tree, output the function execution result.

[0026] In one embodiment, outputting the function execution result based on the target function and the abstract syntax tree includes at least one of the following:

[0027] If the target function is syntax highlighting, mark the text style of each syntax tree node according to the node type of the syntax tree node in the abstract syntax tree;

[0028] If the target function is code inspection, the syntax tree nodes in the abstract syntax tree are traversed based on the verification rules;

[0029] If the target function is auto-completion, candidate content is recommended based on the context of the abstract syntax tree;

[0030] If the target function is expression calculation, the abstract syntax tree is recursively traversed to perform the operation and obtain the calculation result.

[0031] In addition, to achieve the above objectives, this application also provides an expression language parsing device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the expression language parsing method described above.

[0032] In addition, to achieve the above objectives, this application also provides a storage medium, which is a computer-readable storage medium, on which a program implementing a parsing method for an expression language is stored, and the program implementing the parsing method for an expression language is executed by a processor to implement the steps of the parsing method for an expression language as described above.

[0033] This application provides a method for parsing an expression language. First, in response to user-inputted expression text in an editor, a parser integrated into the current program is invoked. The parser then parses the abstract syntax tree corresponding to the expression text. In response to functional operations performed on the abstract syntax tree, the results are output to the editor. These functional operations include syntax highlighting, code checking, auto-completion, and expression calculation. This method solves the technical problem in related technologies where expression languages ​​and parsing tools are closed-source and incompatible with proprietary systems. It achieves the technical effect of automatically generating a parser to parse expressions based on currently defined syntax rules. Attached Figure Description

[0034] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0035] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0036] Figure 1 A flowchart illustrating the first embodiment of the expression language parsing method of this application;

[0037] Figure 2 A flowchart illustrating the second embodiment of the expression language parsing method of this application;

[0038] Figure 3 A flowchart illustrating the parsing method for the expression language of this application, provided in Embodiment 4;

[0039] Figure 4 This is a schematic diagram of the system architecture involved in the parsing device for the expression language of this application.

[0040] The purpose, features, and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0041] It should be understood that the specific embodiments described herein are merely illustrative of the technical solutions of this application and are not intended to limit this application.

[0042] To better understand the technical solution of this application, a detailed description will be provided below in conjunction with the accompanying drawings and specific implementation methods.

[0043] Currently, in computer programming and data analysis, it is frequently necessary to parse expressions for calculation, analysis, and processing. Related technologies, such as Power BI, provide a powerful expression language called "DAX (Data Analysis Expressions)." Tableau uses its own computational language to edit expressions, including creating calculated fields, parameters, formulas, etc. However, the expression languages ​​and parsing tools in these technologies are closed-source and incompatible with proprietary systems.

[0044] The main solution of this application is as follows: in response to the expression text entered by the user in the editor, a parser integrated into the current program is invoked; based on the parser, the abstract syntax tree corresponding to the expression text is parsed; in response to the functional operation on the abstract syntax tree, the function execution result is output to the editor, the functional operation including syntax highlighting, code inspection, auto-completion, and expression calculation. This achieves the technical effect of automatically generating a parser to parse expressions according to currently defined syntax rules.

[0045] It should be noted that the execution subject in this embodiment can be a terminal device, or a computing service device with data processing, network communication, and program execution functions, such as a tablet computer, personal computer, or mobile phone, or an expression language parsing device capable of implementing the above functions. This embodiment does not specifically limit it in this way. The following uses an expression language parsing device as the execution subject as an example to describe this embodiment and the following embodiments.

[0046] Based on this, Embodiment 1 of this application proposes a method for parsing expression language. Please refer to... Figure 1The parsing method for the expression language includes steps S10 to S30:

[0047] Step S10: In response to the expression text entered by the user in the editor, the parser integrated into the current program is invoked.

[0048] In this embodiment, the editor refers to the software interface component used for inputting and editing expressions, which can be presented as a text input box or a rich text editing area. The parser is a JavaScript code module generated based on lezer-generator, which has the function of converting expression text into an abstract syntax tree.

[0049] As an alternative implementation, when a user completes expression input in the editor and triggers an execution event (such as clicking the OK button or pressing the Enter key), the program captures the input content through an event listening mechanism and then calls the parsing method from the preloaded parser instance.

[0050] As another optional implementation, a real-time parsing strategy is adopted. During the process of user inputting expressions, text fragments are obtained in real time through the editor's input events (such as keyup or input), and the parser is dynamically called to perform partial parsing in order to achieve real-time feedback.

[0051] Step S20: Parse the abstract syntax tree corresponding to the expression text based on the parser.

[0052] In this embodiment, an abstract syntax tree is a data structure that represents the syntax structure of an expression in a tree structure. Nodes contain information such as type, position, and child nodes, which are used for subsequent functional processing.

[0053] As an optional implementation, the parser first performs lexical analysis on the expression text, splitting it into a sequence of tokens, i.e., a sequence of words. Then, the parser uses a syntax analyzer to combine the tokens (words) into an AST (Abstract Syntax Tree) according to predefined grammar rules (such as token definitions and expression syntax), ultimately constructing a complete AST. In this embodiment, all tokens represent words, and all ASTs represent abstract syntax trees.

[0054] As another alternative implementation, the parser generated by lezer-generator supports incremental parsing. When the expression text is partially modified, only the modified part is re-parsed, and the tree structure is quickly updated based on the historical AST, thereby improving parsing efficiency.

[0055] Step S30: In response to a function operation on the abstract syntax tree, the function execution result is output to the editor. The function operation includes syntax highlighting, code inspection, auto-completion, and expression calculation.

[0056] In this embodiment, syntax highlighting involves styling expression text based on AST node types, giving different visual effects to different types of content. Code inspection involves traversing AST nodes to verify whether the expression conforms to syntax rules and business constraints. Auto-completion is based on the AST context, recommending possible functions, variables, etc., to the user. Expression evaluation involves evaluating the AST to obtain the result of the expression.

[0057] As an optional implementation, for syntax highlighting, a mapping relationship between AST node types and CSS style classes is established. When traversing the AST, style classes are added to the corresponding text fragments, and the editor implements highlighting through CSS rendering. For code inspection, a set of rules is defined (such as checking the number of function parameters and the format of field declarations). When traversing the AST, the rules are matched, and error messages are marked in the corresponding positions in the editor.

[0058] As another optional implementation, for auto-completion, when the user triggers a completion operation (such as entering "#{" or a space), the current AST context is analyzed to determine the completion position, and matching items are filtered from the predefined function library and field table and displayed in the editor in the form of a drop-down list; for expression calculation, the AST is recursively traversed, and the corresponding operation is performed according to the node type (such as arithmetic operation nodes performing numerical calculations, and function nodes calling business logic implementations), and the results are displayed in a specified area of ​​the editor or fed back to the business system.

[0059] For example, in a business intelligence report development scenario, a user enters the expression "IF(#{sales||0}>1000,"High Sales","Low Sales")+DATE(#{date})" in the editor. When the user clicks the OK button, the program captures the input text and calls the parser. The parser parses the expression into an AST containing IF function calls, addition operations, and DATE function calls, where #{sales||0} is a field reference node with default values, and ">1000" is a comparison operation node. Syntax highlighting displays the number "1000" in blue, the string "High Sales" in green, and the function name in purple. Code checks verify that the field "sales" exists and its default value is correctly formatted. When the user enters "#{", the program automatically completes and recommends available fields such as "sales" and "date". During expression calculation, the actual value of #{sales} is obtained for conditional judgment, combined with #{date} to generate the final result and display it in the report's calculated field.

[0060] This embodiment generates an Abstract Syntax Tree (AST) by responding to user input and calling the parser. Based on the AST, it implements multiple functional operations, achieving efficient parsing and visualization of expressions. This enhances the interactive experience and usability of the editor, enabling developers to write and validate expressions more conveniently, reducing syntax errors, and improving report development efficiency.

[0061] Based on any of the above embodiments, in Embodiment 2 of this application, referring to Figure 2 Before step S10, the following are included:

[0062] Step S01: Obtain the syntax rules of the expression language, which include lexical definition rules and expression syntax definition rules.

[0063] In this embodiment, the lexical definition rules are the specifications for the basic lexical units in the expression language, such as the format definitions of numbers, strings, identifiers, operators, etc. The expression syntax definition rules are the definitions of the hierarchical structure and logical rules of how lexical units are combined into expressions, such as the rules for constructing arithmetic expressions and function call expressions.

[0064] As an alternative implementation, grammar rules can be obtained from a pre-written grammar description file. The grammar description file adopts a Lark-like format and contains @tokens block definition lexical rules and expression block definition expression grammar rules, such as obtaining rules by reading a locally stored .lark file.

[0065] As another alternative implementation, syntax rules can be dynamically configured through a program interface. During program runtime, user input can be received or word definitions and expression syntax definitions can be read from a database to dynamically construct a set of syntax rules.

[0066] Step S02: Generate a recursive descent parser based on the grammar rules.

[0067] In this embodiment, the recursive descent parser is a top-down parser that parses expressions by recursively calling functions that process different syntax rules, with each syntax rule corresponding to a processing function.

[0068] As an alternative implementation, the lezer-generator tool is used. The acquired grammar rules are input into the tool, which automatically generates recursive descent JavaScript parser code. This parser contains complete logic for lexical analysis and syntax analysis.

[0069] As another alternative implementation, a recursive descent parser is manually written, defining processing functions for each grammar rule according to the grammar rules. This enables the lexical analyzer to convert the input text into a sequence of tokens, and the parser to construct an abstract syntax tree by recursively calling the processing functions.

[0070] Step S03: Integrate the parser into the current program.

[0071] In this embodiment, "current program" refers to a software program that needs to use expression parsing functionality, such as an expression editor program in a business intelligence system. "Integration" refers to embedding the generated parser code into the current program, making it part of the program and enabling it to be called.

[0072] As an optional implementation, a modular integration approach is adopted, in which the generated parser is encapsulated as an independent module, imported into the current program through a module import statement, and a parser instance is declared and the relevant interfaces are configured in the program.

[0073] As another alternative implementation method, the source code integration approach is adopted, in which the source code of the parser is directly copied into the code library of the current program, and the parser code is adapted according to the architecture of the current program to ensure that the parser interacts normally with other parts of the program.

[0074] For example, when developing a business intelligence reporting system, the syntax rules of the expression language are first obtained from the aforementioned content. The lexical definition rules stipulate that numbers can be in decimal or scientific notation, and identifiers must begin with a letter or an underscore. The expression syntax definition rules stipulate that function call expressions consist of a function name followed by parameters in parentheses. Then, based on the syntax rules mentioned above, a recursive descent parser is generated using the lezer-generator tool. Finally, the parser generated above is integrated into the expression editor program of the reporting system as a module, enabling the editor to parse expressions.

[0075] This embodiment provides fundamental support for expression parsing by acquiring syntax rules, generating a parser, and integrating it into the program. This enables the program to parse expressions according to custom syntax rules, improving the system's adaptability and scalability to expression languages. It lays the foundation for subsequent implementation of functions such as syntax highlighting and code inspection based on the parser, avoiding the tedious work of manually writing parsers and improving development efficiency.

[0076] Based on any of the above embodiments, in Embodiment 3 of this application, step S02 includes:

[0077] Step S021: Parse the syntax description file generated based on the syntax rules.

[0078] In this embodiment, the syntax description file is a text file written according to a specific format (such as Lark, BNF, or a custom format) used to define the lexical and grammatical rules of the expression language. Parsing refers to converting the file content into an internal data structure for subsequent processing.

[0079] As an alternative implementation, regular expressions are used to match keywords (such as @tokens, expression) and rule definitions in the syntax description file, parsing line by line to extract lexical patterns and grammatical structures. For example, the regular expression `Number{[0-9]+}` defines a numeric lexical.

[0080] As an alternative implementation, a grammar file parser using parser generation tools such as ANTLR can directly convert a description file conforming to a specific grammar into an Abstract Syntax Tree (AST). For example, Lezer's own parser can be used to parse .grammar files to obtain a structured representation of grammar rules.

[0081] Step S022: Generate a recursive descent parser based on the parsing result of the grammar description file. The parser includes a lexical analyzer and a syntax analyzer.

[0082] In this embodiment, the recursive descent parser is a top-down parser implementation. The lexical analyzer is responsible for converting the input text into a sequence of tokens, and the syntax analyzer constructs an abstract syntax tree based on the sequence of tokens.

[0083] As an alternative implementation, the Lezer-generator tool is used to convert the parsed syntax rules into JavaScript code. For example, a function is generated to recursively process addition expressions based on BinaryExpression{left=expression"+"right=expression}, automatically handling operator precedence and associativity.

[0084] As an alternative implementation, the lexical analyzer and parser implementation code can be written manually. For example, a corresponding processing function can be created for each grammar rule, and expression parsing can be implemented through recursive calls.

[0085] For example, when implementing the expression parser of a business intelligence system, the syntax description file is first parsed, which defines the TemplateExpression{"#{"Identifier"}"} rule; then, a lexical analyzer is generated based on the rule to identify #{sales} as a template expression lexical, and a syntax analyzer is generated to recursively process the nested expression IF(#{sales}>100,"#{category}","other"), and finally an abstract syntax tree containing function calls, comparison operations and template references is constructed.

[0086] This embodiment automates the conversion from language definition to executable parsing logic by parsing the syntax description file and generating a recursive descent parser, thus improving parser development efficiency. The generated parser supports modular expansion (such as adding new functions or operators) and naturally handles nested relationships in expressions through recursive structures, enhancing the parsing capability and accuracy of complex expressions.

[0087] Optionally, before step S021, the following steps are included:

[0088] Step S0211: Obtain the basic format requirements, field declaration syntax, function call specifications, operation priority, and syntax constraints corresponding to the parser.

[0089] In this embodiment, the basic format requirements refer to the file format specifications supported by the parser (such as Lark, Yacc), the field declaration syntax defines the format of variable references (such as #{field}), the function call specification specifies the function name and parameter passing method, the operation precedence defines the execution order of operators, and the syntax constraints refer to the rules to avoid ambiguity (such as prohibiting left recursion).

[0090] Step S012: Process the syntax rules according to the basic format requirements, field declaration syntax, function call specifications, operation priority, and syntax constraints to generate the syntax description file.

[0091] In this embodiment, processing refers to converting abstract rules into specific syntax definitions that the parser can recognize. For example, the #{field} syntax is converted into the regular expression "#{[a-zA-Z_][a-zA-Z0-9_]*}".

[0092] As an optional implementation, a template engine is used to generate the syntax description file. For example, this embodiment achieves the intermediate transformation from business requirements to the parser implementation by obtaining the key rule elements of the parser and generating a syntax description file. This method decouples the definition of syntax rules from the specific parser implementation, supports rapid adjustments to the syntax (such as adding new functions), and ensures that the generated syntax description file conforms to the parser's format requirements and constraints, thus improving the maintainability and extensibility of the parser.

[0093] For example, when using lezer-generator to generate a parser, the syntax description file must meet the following core requirements, especially for expression language customization in BI scenarios: For basic formatting requirements, the following blocks are mandatory: @tokens: Defines lexical units (Tokens), including basic elements such as spaces, identifiers, numbers, and strings (e.g., the definition of NumberStringIdentifier). @precedence: Declares the token priority (e.g., spaces are the lowest, Identifier is the highest), controlling the order of lexical analysis. At least one syntax rule block (e.g., expression): Defines the expression hierarchy structure and must include an entry rule (e.g., @topexpression).

[0094] Optional extensions: @external: Imports external token definitions (e.g., otherTokens from tokens.js), supporting modular development. @grammar meta-information: Annotates syntax version and author (not mandatory, but recommended).

[0095] For token definition rules, it is mandatory that variable / field declarations be explicitly marked (e.g., enclosed in #{}) to facilitate subsequent AST recognition (e.g., TemplateExpression). Operators must be annotated with @name (e.g., ArithOp<"+">CompareOp<">") for syntax highlighting and semantic analysis. Spaces must be defined separately (e.g., spaces) and declared as having the lowest priority using @precedence to avoid interfering with other tokens.

[0096] Syntax rule hierarchy: Basic expressions: numbers, strings, variables (e.g., NumberStringTemplateExpression). Compound expressions: operations and function nesting defined through BinaryExpression, FormulaExpression, etc. (e.g., IF(condition, true, false)). Mandatory explicit operator precedence: marked with !, times, plus, etc. (e.g., ** precedes *, * precedes +). Entry rules must be marked with @top (e.g., @topexpression) to specify the parsing starting point. Avoiding ambiguity: handling local ambiguity (e.g., the associativity of conditional expressions ?:) through @isGroup or GLR annotations (e.g., @glr).

[0097] For field declaration syntax, the format is: variables must be enclosed in #{}, and default values ​​are supported (e.g., #{interact_cnt||0}). Syntax rules: TemplateExpression can be directly referenced in expressions, such as IF(#{cnt||0}>100,...).

[0098] For function calling conventions, Builtin functions require a predefined list of functions (such as IFDATELEFT), marked with the Builtin keyword in the syntax. Parameter validation involves constraining parameter types through syntax rules (e.g., LEFT requires both string and numeric parameters), in conjunction with subsequent AST checks.

[0099] Regarding operator precedence: Arithmetic operations: **(exponentiation)>* / / >+ / - (e.g., the !times!plus marker in the BinaryExpression of the disclosure document). Logical operations: && is lower than comparison operations (e.g., >ˋˋ==), ensuring that IF(a>b&&c,...) is correctly parsed. Conditional expressions: right associative, defined separately through ConditionalExpression (e.g., a?b:c?d:e is parsed as a?b:(c?d:e)).

[0100] For grammatical constraints, the AST node reduction principle applies: only explicitly declared grammatical rules generate AST nodes (e.g., FormulaExpression generates nodes, while SequenceExpression may not), reducing memory usage. Error recovery is supported, allowing incomplete input, and generating an AST containing error nodes (e.g., a function not found error message) through the @errorRecovery flag or GLR ambiguity handling. An ErrorToken must be defined in the grammar to wrap illegal content (e.g., unclosed #{). For incremental parsing adaptation, grammatical rules must avoid global left recursion to ensure the parser can be quickly updated based on historical ASTs. Example: Use right recursion to define expressions (e.g., expression = term(opterm)*) instead of left recursion.

[0101] Optionally, the lexical definition rules include basic element definitions, which include spaces, identifiers, numbers, and strings; the expression syntax definition rules include expression hierarchy structure definitions and entry rules; the syntax rules also include lexical priorities.

[0102] Furthermore, the lexical definition rules include basic element definitions, which include spaces, identifiers, numbers, and strings; the expression syntax definition rules include expression hierarchy structure definitions and entry rules; and the syntax rules also include lexical priorities.

[0103] For example, an example of a token definition rule is as follows: @tokens

[0104] spaces[@export]{$[\ug009u000b\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff]+}

[0105] hex{@digit|$[a-fA-F]}

[0106] Number(@digit(”"|@digit)*("."(”"|@digit)*)?|"."@digit(”"|@digit)*)(("e”|"E")("+"|"_")?(”"|@digit)+)?@digit(""|@digit)*"n""0x"(hex|"")+"n"?|"@b”$

[01] +"n"?|“0o”$[0-7]+"n"?

[0107] String{(!["\W]1"'"(!['\!"\

[0108] identifierchar{@asciiLetter|$[$\u{a1}-\u{10ffff}]}

[0109] word{identifierchar(identifierchar|@digit)*}

[0110] Identifier{word}

[0111] @precedence{spaces,Identifier}

[0112] questionop[@name=Logicop]{"?”}

[0113] divide[@name=Arithop]{" / ”}

[0114] Arithop <expr>{expr}Logicop <expr>{expr}Bitop <expr>{expr}Compare0p <expr>{expr}

[0115] LessThan[@name=Compareop]{"<”}

[0116] "="[@name=Equals]

[0117] "("")""{""}""[""]""<"">"

[0118] The rules for defining the expression syntax are as follows: FormulaExpression{

[0119] Builtin ParenthesizedExpression}

[0120] ConditionalExpression{

[0121] ExpressionNoComma!ternary questionOp expressionNocomma Logicop<":">expressionNocomma}

[0122] TemplateExpression

[0123] "#{”Identifier"}"|

[0124] "#{"Identifier"||"(Number l string)"}"}

[0125] @external tokens otherTokens from". / tokens"{Builtin}

[0126] ParenthesizedExpression{"("expression")"}

[0127] BinaryExpression{

[0128] expressionNocomma!exp ArithOp<"**">expressionNocomma|

[0129] expressionNocomma!times(divide|Arithop<"%">|Arithop<"*">)expressionNocomma|

[0130] expressionNocomma! plusArithop<"+"|"-">expressionNocomma|

[0131] expressionNoComma! shiftBitop<">>"">"? |"<<">expressionNoComma|

[0132] expressionNocomma! rel(LessThanCompare0p<"<=""="?>)expressionNocomma|

[0133] expressionNocomma! equalCompare0p<"==""="? |"!=""="? >expressionNoComma|

[0134] expressionNoComma! bitOrBitop{""}expressionNocomma|

[0135] expressionNocomma! bitXorBitop<"^">expressionNocomma|

[0136] expressionNoComma! bitAndBitop{"&"}expressionNocomma|

[0137] expressionNocomma! and LogicOp<"&&">expressionNocomma}.

[0138] This embodiment achieves the intermediate transformation from business requirements to parser implementation by acquiring the key rule elements of the parser and generating a syntax description file. This method decouples the definition of syntax rules from the specific parser implementation, supports rapid adjustment of syntax (such as adding new functions), and ensures that the generated syntax description file conforms to the format requirements and constraints of the parser, thereby improving the maintainability and extensibility of the parser.

[0139] Based on any of the above embodiments, in Embodiment 4 of this application, referring to Figure 3 Step S20 includes:

[0140] Step S21: Split the expression text into a sequence of terms.

[0141] In this embodiment, the token sequence is an ordered list of tokens after the expression text is segmented according to lexical rules. Each token contains a type (such as a number or identifier) ​​and a value (such as "123" or "IF").

[0142] As an alternative implementation, a sliding window algorithm is used to scan the expression text and match token types based on the regular expression. For example, for the text IF(#{age}>18,"adult","minor"), IF is identified as the function name token, ( as the left bracket token, and #{age} as the template expression token.

[0143] As an alternative implementation, lexical analysis is performed using a finite state automaton (FSA), which identifies lexical boundaries through state transitions. For example, upon encountering a '#', the expression enters a template expression state and continues until a '}' is matched.

[0144] Step S22: Determine at least one syntax tree node based on the parser's recognition of the lexical sequence.

[0145] In this embodiment, a syntax tree node is the basic unit of an AST, containing type (such as function call, binary operation) and child node information.

[0146] As an alternative implementation, a recursive descent parser is used, defining a processing function for each grammar rule. For example, when an IF ( is encountered, the parseFunctionCall() function is called, generating a FunctionCall node whose child nodes are the conditional expression, the truth value, and the false value.

[0147] As an alternative implementation, a predictive parsing table-driven parsing method is used, selecting a production rule based on the current lexical unit and the grammar rule table. For example, when the lexical unit is >, the table is looked up to determine the production rule to apply the comparison operation, generating a BinaryExpression node.

[0148] Step S23: Assemble the syntax tree nodes into the abstract syntax tree.

[0149] In this embodiment, the abstract syntax tree is a structured tree representation that reflects the logical structure and order of operations of the expression.

[0150] As an alternative implementation, parent nodes are constructed step by step by traversing the word sequence in post-order traversal. For example, when processing a+b*c, the BinaryExpression node of b*c is constructed first, and then it is used as the right child node to construct the root node of a+(b*c).

[0151] As an alternative implementation, a stack structure can be used to manage node context. For example, when a () is encountered, the current node is pushed onto the stack, and when a () is encountered, the top node of the stack is popped and added as a child node, ensuring that nested expressions are handled correctly.

[0152] For example, consider the expression IF(#{age||18}>20,LEN(#{name}),0):

[0153] Lexical analysis breaks the text down into IF, (, #{age||18}, >, 20, ,, LEN, (, #{name},), ,, 0,).

[0154] Syntax analysis identifies IF as a function call node, the conditional expression is #{age||18}>20 (generating a comparison operation node), the true value is LEN(#{name}) (generating a nested function call node), and the false value is 0.

[0155] The nodes are assembled to form an AST with the root node being FunctionCall (of type IF). Its conditional subtree contains TemplateExpression and NumberLiteral nodes, and its truth subtree contains another FunctionCall (of type LEN) node.

[0156] This embodiment achieves the transformation from text to structured data by splitting words through lexical analysis, identifying nodes through syntactic analysis, and assembling nodes to construct an Abstract Syntax Tree (AST). This hierarchical processing approach makes the parsing logic clear, supports the rapid location of syntax errors (such as unclosed parentheses), and the generated AST provides the basic data structure for subsequent functions such as syntax highlighting and code inspection, improving the flexibility and scalability of expression processing.

[0157] Based on any of the above embodiments, in embodiment five of this application, step S30 includes:

[0158] Step S31: In response to the functional operation on the abstract syntax tree, determine the target function.

[0159] In this embodiment, the function operation refers to the specific action triggered by the user (such as syntax highlighting or code checking), and the target function is the processing logic corresponding to the operation.

[0160] As an alternative implementation, operation types are mapped to processing functions through the editor's command system. For example, when a user triggers a syntax highlighting command, the system maps it to the highlightAST function.

[0161] As an alternative implementation, the strategy pattern can be used to dynamically select processing logic. For example, the corresponding code inspection processor can be retrieved from a set of strategies based on the operation type (such as "check").

[0162] Step S32: Output the function execution result based on the target function and the abstract syntax tree.

[0163] In this embodiment, the execution result is a visual feedback (such as highlighted text or error messages) after functional processing.

[0164] As an alternative implementation, for syntax highlighting, the AST nodes are traversed and style classes are added to the corresponding text segments. For example, the NumberLiteral node is identified and a blue style is applied, and the FunctionCall node is identified and a purple style is applied.

[0165] As an alternative implementation, for code inspection functionality, a rule inspector is defined to traverse the AST. For example, it verifies whether an IF function contains three parameters, or checks whether the #{field} format is valid, and marks the error message in the corresponding position in the editor.

[0166] For example, when a user enters the expression IF(#{age||18}>20,LEN(#{name}),0) in a business intelligence system and a syntax check is triggered:

[0167] The system responded and determined the target function to be "codeCheck".

[0168] The inspector traversed the AST and found that the LEN function was called but not imported, marking the error "undefined function" on line 5 of the editor; at the same time, it verified that the #{age||18} format conformed to the #{field||default} specification, and the inspection passed.

[0169] The editor displays a red wavy line in the corresponding position and indicates "1 error found" in the status bar.

[0170] This embodiment maps functional operations to specific processing logic and performs targeted processing based on the Abstract Syntax Tree (AST), enabling diversified applications of the parsed results. This design allows the editor to quickly respond to different operational needs, such as real-time highlighting of function calls for complex expressions and automatic completion of field names, significantly improving user experience and development efficiency while maintaining the flexibility for functional expansion (such as adding code formatting features).

[0171] Optionally, step S32 includes at least one of the following:

[0172] Step S321: If the target function is syntax highlighting, mark the text style of each syntax tree node according to the node type of the syntax tree node in the abstract syntax tree.

[0173] In this embodiment, node types include numeric literals, function calls, template expressions, etc., and text styles are implemented through CSS classes or inline styles.

[0174] As an optional implementation, a mapping table is established between node types and style classes, such as mapping function names to purple font and variable references to blue font, and traversing AST nodes to add style classes to the corresponding text fragments.

[0175] As another alternative implementation, the visitor pattern is used to dynamically process node styles, defining specific style processing logic for different node types.

[0176] Step S322: If the target function is code inspection, traverse the syntax tree nodes in the abstract syntax tree based on the verification rules.

[0177] In this embodiment, the validation rules include the number of function parameters, the validity of field references, and operator matching.

[0178] As an optional implementation, an array of rule checkers is defined, and all rules are applied sequentially when traversing the AST, error information is collected, and the error is marked at the corresponding position in the editor.

[0179] As an alternative implementation, an assertion mechanism is used to set constraints on node attributes, and an error message is generated when the constraints are violated.

[0180] Step S323: If the target function is auto-completion, recommend candidate content based on the context of the abstract syntax tree.

[0181] In this embodiment, the context refers to the AST nodes surrounding the cursor position, and the candidate content includes function names, field names, etc.

[0182] As an alternative implementation, the parent node type of the node where the cursor is located is analyzed, and the possible completion content is inferred based on the node type.

[0183] As an alternative implementation, a global symbol table is maintained, and when the user enters a specific trigger character, candidate content is retrieved from the symbol table and displayed in a drop-down list.

[0184] Step S324: If the target function is expression calculation, recursively traverse the abstract syntax tree to perform the operation and obtain the calculation result.

[0185] In this embodiment, corresponding operations are performed according to the node type. For example, a binary expression node performs arithmetic operations, and a function call node calls the corresponding business logic.

[0186] As an optional implementation, a post-order traversal strategy is adopted, which calculates the values ​​of child nodes first and then the values ​​of parent nodes.

[0187] As an alternative implementation, dynamic type handling is supported, which performs corresponding operations based on the actual type of the node value.

[0188] For example, when a user enters the expression IF(#{age||18}>20,LEN(#{name}),0) in a business intelligence system and triggers syntax checking, the system first determines that the target function is code checking. Then, based on the validation rules, it traverses the AST and finds that the LEN function is called but not imported. It marks the "undefined function" error in the corresponding position in the editor. At the same time, it verifies that the #{age||18} format conforms to the specification. After passing the check, it finally prompts "1 error found" in the editor status bar.

[0189] This embodiment maps functional operations to specific processing logic and performs targeted processing based on the Abstract Syntax Tree (AST), enabling diversified applications of the parsed results. This design allows the editor to quickly respond to different operational needs, such as real-time highlighting of function calls for complex expressions and automatic completion of field names, significantly improving user experience and development efficiency while maintaining flexibility for functional expansion.

[0190] This application provides an expression language parsing device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the expression language parsing method in Embodiment 1 above.

[0191] The following is for reference. Figure 4 The diagram illustrates a structural schematic of a parsing device suitable for implementing the expression language embodiments of this application. The parsing device for the expression language in the embodiments of this application may include, but is not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, personal digital assistants (PDAs), tablets, and in-vehicle terminals, as well as fixed terminals such as digital TVs and desktop computers. Figure 4 The expression language parsing device shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.

[0192] like Figure 4 As shown, the expression language parsing device may include a processing unit 1001 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes according to a program stored in read-only memory (ROM) 1002 or a program loaded from storage device 1003 into random access memory (RAM) 1004. The random access memory 1004 also stores various programs and data required for the operation of the expression language parsing device. The processing unit 1001, ROM 1002, and RAM 1004 are interconnected via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to I / O interface 1006: input devices 1007 including, for example, touchscreens, touchpads, keyboards, mice, image sensors, microphones, accelerometers, gyroscopes, etc.; output devices 1008 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 1003 including, for example, magnetic tapes, hard disks, etc.; and communication devices 1009. Communication device 1009 allows the expression language parsing device to communicate wirelessly or wiredly with other devices to exchange data. Although expression language parsing devices with various systems are shown in the figures, it should be understood that it is not required to implement or possess all of the systems shown. More or fewer systems may be implemented alternatively.

[0193] Specifically, according to the embodiments disclosed in this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from storage device 1003, or installed from ROM 1002. When the computer program is executed by processing device 1001, it performs the functions defined in the methods of the embodiments disclosed in this application.

[0194] The expression language parsing device provided in this application, employing the expression language parsing method described in the above embodiments, can solve the technical problem that expression languages ​​and parsing tools in related technologies are closed-source and incompatible with proprietary systems. Compared with the prior art, the beneficial effects of the expression language parsing device provided in this application are the same as those of the expression language parsing device provided in the above embodiments, and other technical features in this expression language parsing device are the same as those disclosed in the method of the previous embodiment, and will not be repeated here.

[0195] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any suitable manner in one or more embodiments or examples.

[0196] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

[0197] This application provides a computer-readable storage medium having computer-readable program instructions (i.e., a computer program) stored thereon, the computer-readable program instructions being used to execute the expression language parsing method in the above embodiments.

[0198] The computer-readable storage medium provided in this application may be, for example, a USB flash drive, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to: electrical connections having one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM), flash memory, optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, system, or device. The program code contained on the computer-readable storage medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, radio frequency (RF), or any suitable combination thereof.

[0199] The aforementioned computer-readable storage medium may be included in the expression language parsing device; or it may exist independently and not be assembled into the expression language parsing device.

[0200] The aforementioned computer-readable storage medium carries one or more programs that, when executed by an expression language parsing device, cause the expression language parsing device to: in response to expression text input by the user in the editor, invoke a parser integrated into the current program;

[0201] The parser parses the abstract syntax tree corresponding to the expression text;

[0202] In response to a function operation on the abstract syntax tree, the function execution result is output in the editor, the function operation including syntax highlighting, code inspection, auto-completion, and expression evaluation.

[0203] Computer program code for performing the operations of this application can be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages ​​such as Java, Smalltalk, and C++, as well as conventional procedural programming languages ​​such as "C" or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a local area network (LAN) or a wide area network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0204] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0205] The modules described in the embodiments of this application can be implemented in software or hardware. The names of the modules do not necessarily limit the functionality of the unit itself.

[0206] The readable storage medium provided in this application is a computer-readable storage medium that stores computer-readable program instructions (i.e., a computer program) for executing the expression language parsing method described above. This solves the technical problem in related technologies where expression languages ​​and parsing tools are closed-source and incompatible with proprietary systems. Compared to the prior art, the beneficial effects of the computer-readable storage medium provided in this application are the same as those of the expression language parsing method provided in the above embodiments, and will not be elaborated upon here.

[0207] This application provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the expression language parsing method described above.

[0208] The computer program product provided in this application solves the technical problem that expression languages ​​and parsing tools in related technologies are closed-source and cannot be compatible with proprietary systems. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as the beneficial effects of the expression language parsing method provided in the above embodiments, and will not be repeated here.

[0209] The above are merely preferred embodiments of this application and do not limit the patent scope of this application. Any equivalent structural or procedural transformations made using the content of this application's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent scope of this application.< / expr> < / expr> < / expr> < / expr>

Claims

1. A parsing method for an expression language, characterized in that, The parsing methods for the expression language include: In response to the expression text entered by the user in the editor, the parser integrated into the current program is invoked; The parser parses the abstract syntax tree corresponding to the expression text; In response to a function operation on the abstract syntax tree, the function execution result is output in the editor, the function operation including syntax highlighting, code inspection, auto-completion, and expression evaluation.

2. The expression language parsing method as described in claim 1, characterized in that, Prior to the step of invoking the parser integrated into the current program, the following steps are included: Obtain the syntax rules of the expression language, including lexical definition rules and expression syntax definition rules; A recursive descent parser is generated based on the aforementioned grammar rules; Integrate the parser into the current program.

3. The expression language parsing method as described in claim 2, characterized in that, The step of generating a recursive descent parser based on the grammar rules includes: Parse the syntax description file generated based on the syntax rules; The parser, which generates a recursive descent function based on the parsing results of the grammar description file, includes a lexical analyzer and a syntax analyzer.

4. The expression language parsing method as described in claim 3, characterized in that, Before the step of parsing the syntax description file generated based on the syntax rules, the following steps are included: Obtain the basic format requirements, field declaration syntax, function call conventions, operation priority, and syntax constraints corresponding to the parser; The syntax rules are processed according to the basic format requirements, field declaration syntax, function call specifications, operation priority, and syntax constraints to generate the syntax description file.

5. The expression language parsing method as described in claim 2, characterized in that, The lexical definition rules include basic element definitions, which include spaces, identifiers, numbers, and strings; the expression syntax definition rules include expression hierarchy structure definitions and entry rules; the syntax rules also include lexical priorities.

6. The expression language parsing method as described in claim 1, characterized in that, The step of parsing the abstract syntax tree corresponding to the expression text based on the parser includes: The expression text is split into a sequence of terms; Based on the parser, at least one syntax tree node is determined by recognizing the word sequence. The syntax tree nodes are assembled into the abstract syntax tree.

7. The expression language parsing method as described in claim 1, characterized in that, The step of outputting the function execution result in the editor in response to a function operation on the abstract syntax tree includes: In response to a functional operation on the abstract syntax tree, the target function is determined; Based on the target function and the abstract syntax tree, output the function execution result.

8. The expression language parsing method as described in claim 7, characterized in that, The step of outputting the function execution result based on the target function and the abstract syntax tree includes at least one of the following: If the target function is syntax highlighting, mark the text style of each syntax tree node according to the node type of the syntax tree node in the abstract syntax tree; If the target function is code inspection, the syntax tree nodes in the abstract syntax tree are traversed based on the verification rules; If the target function is auto-completion, candidate content is recommended based on the context of the abstract syntax tree; If the target function is expression calculation, the abstract syntax tree is recursively traversed to perform the operation and obtain the calculation result.

9. A parsing device for an expression language, characterized in that, The expression language parsing device includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the expression language parsing method as described in any one of claims 1 to 8.

10. A storage medium, characterized in that, The storage medium is a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, it implements the steps of the expression language parsing method as described in any one of claims 1 to 8.

Citation Information

Cited By

  • Mathematical expression diagnosis method and device based on abstract syntax tree and semantic network

    CN121118886A