Nested structure based syntax analysis method for simulation modeling language and application thereof
Patent Information
- Application Number
- CN202610756761.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-05-28
- Publication Date
- 2026-10-27
AI Technical Summary
[0003]然而,由于仿真建模语言通常是一种解释执行的仿真语言,即仿真建模语言是边解析边运行的,没有提前将程序编辑成机器码的编译过程,因此,仿真建模语言程序的运行速度通常低于通用编程语言(如C++)程序的运行速度,尤其是在仿真模型规模较大的情况下,仿真建模语言程序的运行速度慢的问题会格外明显,甚至会出现运行卡顿现象
[0010]基于本公开上述实施例提供的仿真建模语言的基于嵌套结构的语法分析方法、装置、存储介质以及电子设备,通过针对仿真建模语言设置复合类型,并为每一种复合类型分别设置相应的解析函数,使各解析函数均可以在语法层面上有针对性对程序语句中的多个词法单元的组合进行语法分析,从而实现了基于上下文感知的语法解析,有利于准确识别出程序语句所包含的各复合类型;由于各层解析函数在执行语法分析过程中,会出现解析函数的嵌套调用(如递归调用等)现象,因此,使所有复合类型可以呈现层级化的特点,从而不仅有利于使解析函数具有逻辑性强、针对性强、易开发、易维护和易扩展等特点,而且有利于降低对仿真建模语言进行语法分析的代码量,从而使冗余代码率保持在较好水平,甚至可以使冗余代码率接近零。由此可知,本公开提供的技术方案有利于保证仿真建模语言程序的语法分析的准确性,并有利于降低仿真建模语言程序的语法分析实现成本,进一步的,有利于使仿真建模语言程序便捷且准确的转换为通用编程语言程序,从而提高仿真建模语言程序的运行速度。
Smart Images

Figure CN122985175A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of simulation modeling technology, and in particular to a nested structure-based syntax analysis method for simulation modeling languages, a nested structure-based syntax analysis device for simulation modeling languages, a storage medium, and an electronic device. Background Technology
[0002] Simulation modeling languages (such as SimScript) are programming languages specifically designed for simulation modeling. Because they have built-in core simulation concepts (such as entities, events, resources, and time), syntax that fits business scenarios, and a focus on model description, simulation modeling languages offer advantages such as high modeling efficiency, low learning curve (allowing even non-professional programmers to quickly build simulation models), and clear simulation logic.
[0003] However, since simulation modeling languages are usually interpreted simulation languages, meaning they are parsed and run simultaneously without a pre-compilation process to convert the program into machine code, the running speed of simulation modeling language programs is usually lower than that of general programming languages (such as C++). This is especially true when the simulation model is large in scale, where the slow running speed of simulation modeling language programs becomes particularly noticeable and may even cause stuttering.
[0004] To improve the execution speed of simulation modeling language programs, it is possible to convert them into general-purpose programming languages (such as C++) and compile the converted programs. This allows the compiled code to be executed directly at runtime. How to conveniently and accurately convert simulation modeling language programs into general-purpose programming languages is a technical problem worthy of attention. Summary of the Invention
[0005] To address the aforementioned technical problems, this disclosure is proposed. Embodiments of this disclosure provide a nested structure-based syntax analysis method for simulation modeling languages, a nested structure-based syntax analysis apparatus for simulation modeling languages, a storage medium, and an electronic device.
[0006] According to a first aspect of the present disclosure, a nested structure-based syntax analysis method for a simulation modeling language is provided. A pre-set set of parsing functions is provided, each parsing function corresponding to a composite type in the simulation modeling language. The method includes the steps of: obtaining the first lexical unit of the statement to be analyzed in the simulation modeling language program; determining the statement type of the statement to be analyzed based on the first lexical unit; calling the corresponding parsing function based on the composite type corresponding to the statement type of the statement to be analyzed; during the process of extracting the composite type from the current lexical unit to be processed in the statement to be analyzed, when the current lexical unit to be processed triggers nested composite types, the called parsing function calls the parsing function corresponding to the nested composite type, until all lexical units in the statement to be analyzed are sequentially processed as the current lexical unit to be processed; wherein the tree node structure information of the composite types contained in the statement to be analyzed, output by each of the called parsing functions, is stored in the form of a statement-level syntax tree.
[0007] According to a second aspect of the present disclosure, a nested structure-based syntax analysis device for a simulation modeling language is provided, comprising: a set of parsing functions formed by multiple parsing functions, each parsing function corresponding to a composite type in the simulation modeling language; an acquisition module for acquiring the first lexical unit of a statement to be analyzed in the simulation modeling language program; a statement type determination module for determining the statement type of the statement to be analyzed based on the first lexical unit; a calling module for calling the corresponding parsing function based on the composite type corresponding to the statement type of the statement to be analyzed; wherein the called parsing function, during the process of extracting the composite type of the current lexical unit to be processed in the statement to be analyzed, when it is determined that the current lexical unit to be processed triggers nested composite types, calls the parsing function corresponding to the nested composite type until all lexical units in the statement to be analyzed are sequentially processed as the current lexical unit to be processed; and a storage module, wherein the tree node structure information of the composite types contained in the statement to be analyzed, output by each of the called parsing functions, is stored in the storage module in the form of a statement-level syntax tree.
[0008] According to a third aspect of the present disclosure, a computer-readable storage medium is provided, the storage medium storing a computer program for implementing any of the methods described above.
[0009] According to a fourth aspect of the present disclosure, an electronic device is provided, comprising: a processor; a memory for storing processor-executable instructions; the processor being configured to read the executable instructions from the memory and execute the instructions to implement any of the methods described above.
[0010] Based on the nested structure-based syntax analysis method, apparatus, storage medium, and electronic device for simulation modeling languages provided in the above embodiments of this disclosure, by setting composite types for simulation modeling languages and setting corresponding parsing functions for each composite type, each parsing function can perform targeted syntax analysis on the combination of multiple lexical units in program statements at the syntax level, thereby realizing context-aware syntax parsing, which is beneficial for accurately identifying the composite types contained in program statements. Since nested calls (such as recursive calls) occur in the parsing functions at each level during the syntax analysis process, all composite types can exhibit hierarchical characteristics. This not only helps to make the parsing functions have strong logic, strong targeting, easy development, easy maintenance, and easy expansion, but also helps to reduce the amount of code for syntax analysis of simulation modeling languages, thereby keeping the redundancy rate at a good level, or even making the redundancy rate close to zero. Therefore, the technical solution provided in this disclosure is beneficial to ensuring the accuracy of syntax analysis of simulation modeling language programs, reducing the implementation cost of syntax analysis of simulation modeling language programs, and further, enabling simulation modeling language programs to be conveniently and accurately converted into general programming language programs, thereby improving the running speed of simulation modeling language programs.
[0011] The technical solutions of this disclosure will be further described in detail below with reference to the accompanying drawings and embodiments. Attached Figure Description
[0012] The above and other objects, features, and advantages of this disclosure will become more apparent from the more detailed description of the embodiments thereof in conjunction with the accompanying drawings. The drawings are provided to further illustrate the embodiments of this disclosure and form part of the specification. They are used together with the embodiments of this disclosure to explain the disclosure and do not constitute a limitation thereof. In the drawings, the same reference numerals generally represent the same components or steps.
[0013] Figure 1 A flowchart illustrating an embodiment of the nested structure-based syntax analysis method for simulation modeling languages disclosed herein;
[0014] Figure 2 This is a schematic diagram of the structure of an embodiment of the nested structure-based syntax analysis device for the simulation modeling language of this disclosure;
[0015] Figure 3 This is a structural diagram of an electronic device provided in an exemplary embodiment of this disclosure. Detailed Implementation
[0016] Example embodiments according to this disclosure will now be described in detail with reference to the accompanying drawings. Obviously, the described embodiments are merely some embodiments of this disclosure, and not all embodiments of this disclosure, and it should be understood that this disclosure is not limited to the example embodiments described herein.
[0017] It should be noted that, unless otherwise specifically stated, the relative arrangement, numerical expressions, and values of the components and steps set forth in these embodiments do not limit the scope of this disclosure.
[0018] Those skilled in the art will understand that the terms "first," "second," etc., in the embodiments of this disclosure are only used to distinguish different steps, devices, or modules, and do not represent any specific technical meaning, nor do they indicate a necessary logical order between them.
[0019] It should also be understood that in the embodiments disclosed herein, "a plurality of" may refer to two or more, and "at least one" may refer to one, two or more.
[0020] It should also be understood that any component, data or structure mentioned in the embodiments of this disclosure can generally be understood as one or more unless expressly defined or given to the contrary in the context.
[0021] Furthermore, the term "and / or" in this disclosure is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, or B existing alone. Additionally, the character " / " in this disclosure generally indicates that the preceding and following related objects have an "or" relationship.
[0022] It should also be understood that the description of the various embodiments in this disclosure emphasizes the differences between the various embodiments, and the similarities or similarities can be referred to each other. For the sake of brevity, they will not be described in detail.
[0023] At the same time, it should be understood that, for ease of description, the dimensions of the various parts shown in the accompanying drawings are not drawn according to actual scale.
[0024] The following description of at least one exemplary embodiment is merely illustrative and is in no way intended to limit this disclosure or its application or use.
[0025] Techniques, methods, and equipment known to those skilled in the art may not be discussed in detail, but where appropriate, such techniques, methods, and equipment should be considered part of the specification.
[0026] It should be noted that similar labels and letters in the following figures indicate similar items; therefore, once an item is defined in one figure, it does not need to be discussed further in subsequent figures.
[0027] The embodiments of this disclosure can be applied to electronic devices such as terminal devices, computer systems, and servers, and can operate with a wide range of other general-purpose or special-purpose computing system environments or configurations. Examples of well-known terminal devices, computing systems, environments, and / or configurations suitable for use with electronic devices such as terminal devices, computer systems, or servers include, but are not limited to: personal computer systems, server computer systems, thin clients, thick clients, handheld or laptop devices, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputer systems, mainframe computer systems, and distributed cloud computing environments that include any of the above systems, etc.
[0028] Electronic devices such as terminal devices, computer systems, and servers can be described in the general context of computer system executable instructions (such as program modules) executed by a computer system. Typically, program modules can include routines, programs, object programs, components, logic, data structures, etc., which perform specific tasks or implement specific abstract data types. Computer systems / servers can be implemented in a distributed cloud computing environment. In a distributed cloud computing environment, tasks can be performed by remote processing devices linked through a communication network. In a distributed cloud computing environment, program modules can reside on local or remote computing system storage media, including storage devices.
[0029] This disclosure outlines
[0030] In developing this disclosure, the inventors discovered that performing syntax analysis on simulation modeling language programs (such as SimScript programs) to construct an AST (Abstract Syntax Tree) is a crucial step in converting simulation modeling language programs into general-purpose programming language programs (such as C++). However, existing syntax analyzers cannot perform accurate syntax analysis on simulation modeling language programs for the following reasons:
[0031] Because simulation modeling languages support array access (e.g., accessing a(5)), object attribute access (e.g., accessing a.the.unit), complex expressions (e.g., a + b * c), and Boolean expressions (e.g., a == b OR c > d), and the same operator may express different meanings in different positions (e.g., the operator "+" / "-" combined with different contexts may express the meaning of addition / subtraction, or positive / negative signs), the syntax structure of simulation modeling languages is usually quite complex. However, traditional parsers, such as those based on LL(1) (Left-to-right, Leftmost derivation, scanning tokens from left to right, leftmost derivation) and those based on LR (Left-to-right, Rightmost derivation, scanning tokens from left to right, rightmost derivation), have inherent limitations in their parsing strategies, making it difficult to obtain accurate parsing results when using traditional parsers to perform parsing on simulation modeling language programs.
[0032] Although the syntax of simulation modeling languages is relatively complex, analysis reveals that it can be divided into various structured syntactic semantic units. A program statement can include multiple structured syntactic semantic units, and nesting relationships can exist between different structured syntactic semantic units (such as mathematical expressions containing variable expressions, and Boolean expressions containing mathematical expressions). If a corresponding syntax analysis process can be set for each structured syntactic semantic unit, and each syntax analysis process allows for syntax parsing across multiple tokens at the syntax level to achieve context-aware syntax parsing, and allows for timely switching between syntax analysis processes, accurate syntax analysis can be performed on each structured syntactic semantic unit at the syntax level. This allows for the successful construction of an AST for the simulation modeling language program, which can then be used to convert the simulation modeling language program into a general-purpose programming language program.
[0033] Exemplary Overview
[0034] The nested structure-based syntax analysis technique for simulation modeling languages disclosed herein can be applied to various application scenarios, such as visual debugging of simulation modeling language programs and converting simulation modeling language programs into C++ programs and compiling them.
[0035] In a concrete example, suppose a SimScript program includes multiple functions. The technical solution disclosed herein can be used to perform syntax analysis on each complete statement within each function, thereby obtaining the Abstract Syntax Tree (AST) for each function, i.e., obtaining multiple function-level ASTs. Similarly, the complete statements within the SimScript program that are not functions can also be analyzed using the technical solution disclosed herein, thereby obtaining the AST for each complete statement, i.e., obtaining multiple statement-level ASTs. Based on the relationships between all function-level ASTs and all statement-level ASTs, all function-level ASTs and all statement-level ASTs can be merged into the AST of the SimScript program, i.e., obtaining the program-level AST. Subsequent compilation operations on this program-level AST yield the compiler for the SimScript program. Running this compiler allows the SimScript program to be executed quickly.
[0036] Another concrete example: Given a SimScript program containing multiple functions, the technical solution disclosed herein can be used to perform syntax analysis on each complete statement within each function, thereby obtaining the Abstract Syntax Tree (AST) for each function, i.e., obtaining multiple function-level ASTs. Each function-level AST is then transformed into a graphical tree structure (such as a node + connection visualization interface), allowing program developers to intuitively perceive the syntax hierarchy and nesting relationships within each function, thus facilitating the rapid location of syntax errors within functions. Similarly, each complete statement within a SimScript program that is not a function can also be analyzed using the technical solution disclosed herein, thereby obtaining the AST for each complete statement, i.e., obtaining multiple statement-level ASTs. Based on the relationships between all function-level ASTs and all statement-level ASTs, all function-level ASTs and all statement-level ASTs can be merged into the AST of the SimScript program, i.e., obtaining the program-level AST. The program-level AST is then transformed into a graphical tree structure (such as a node + connection visualization interface), allowing program developers to intuitively perceive the syntax hierarchy and nesting relationships of the entire program, thus facilitating the rapid location of errors in the SimScript program.
[0037] Exemplary methods
[0038] In the technical solution of the nested structure-based syntax analysis method for simulation modeling languages disclosed in this invention, multiple parsing functions are pre-defined. Each parsing function corresponds to a composite type in the simulation modeling language; that is, one parsing function is used to perform syntax analysis on a composite type in the simulation modeling language, and all parsing functions form a set of parsing functions. It should be noted that there is a nested relationship between the composite types in this disclosure. That is, a composite type can be a component of other composite types or itself. For example, a program code segment formed by multiple lexical units is defined as belonging to the first composite type, and part of the content in this code segment belongs to the second composite type, while another part belongs to the first composite type. Due to the nested relationship between the composite types, there is a phenomenon of nested calls between the parsing functions in this disclosure. For example, during the execution of syntax analysis, a parsing function may call another parsing function (this call can be considered a typical nested call), or it may call itself (this call can be considered a special case of nested call, i.e., a recursive call).
[0039] The composite types in this disclosure are not data types in simulation modeling languages, but rather structured syntactic units; that is, a composite type is a structured unit at the syntactic level. The composite types in this disclosure align with the syntax analysis logic of "from atomic units to composite structures" in compiler theory. Compared to atomic types (such as lexical units / tokens) in simulation modeling languages, the composite types in this disclosure typically refer to structured syntactic semantic units composed of multiple atomic types combined according to fixed syntactic rules. It should be noted that even when a single atomic type has independent syntactic semantics, it is possible for a single atomic type to be recognized as a composite type. For example, during the syntax analysis process of a corresponding parsing function, if a lexical unit / token and its context meet certain conditions, the parsing function will recognize the lexical unit as a composite type.
[0040] In one example, the simulation modeling language of this disclosure may include at least four composite types: variable expressions (since the simulation modeling language supports various complex forms of variables such as array-type variables and object attribute-type variables, this disclosure refers to all variables supported by the simulation modeling language as variable expressions), mathematical expressions, Boolean expressions, and complete statements; in this case, the set of parse functions includes at least the first-level parse function corresponding to the variable expression, the second-level parse function corresponding to the mathematical expression, the third-level parse function corresponding to the Boolean expression, and the fourth-level parse function corresponding to the complete statement.
[0041] The first-level parsing function is mainly used to extract the variable expressions contained in the statement to be analyzed. These variable expressions can be ordinary variables (such as the variable name of a single token), array variables (such as a(5,*)), or object property variables (such as a.the.unit). During the parsing process, the first-level parsing function may call the second-level parsing function, for example, if a symbol marking the beginning of a mathematical expression is detected, the second-level parsing function will be called.
[0042] The second-level parsing function is mainly used to extract the mathematical expressions contained in the statement to be analyzed, and these mathematical expressions can be complex. During the parsing process, the second-level parsing function may call the first-level parsing function, or it may recursively call the second-level parsing function. For example, once a symbol marking the beginning of a mathematical expression is detected, the second-level parsing function is recursively called; conversely, once a variable expression is detected within a mathematical expression, the first-level parsing function is called.
[0043] The third-level parsing function is mainly used to extract the Boolean expressions contained in the statement to be analyzed. During the parsing process, the third-level parsing function may be called recursively, and it may also call the second-level parsing function. For example, once a symbol marking the beginning of a Boolean expression is detected, the third-level parsing function is called recursively, while once a mathematical expression is detected to appear in the Boolean expression, the second-level parsing function is called.
[0044] The fourth-level parsing function is primarily used to initiate the parsing process for the statement to be analyzed. It completes the parsing of the statement by calling other parsing functions. In other words, the method of this disclosure can initiate the parsing process by calling the fourth-level parsing function and complete the parsing of the entire statement by calling the corresponding parsing function. For example, after successfully obtaining a statement to be analyzed in a simulation modeling language program, the fourth-level parsing function can be called based on that statement (e.g., by providing the statement to be analyzed as an input parameter or providing the statement's identifier information (such as line numbers) as an input parameter, so that the fourth-level parsing function clearly knows which statement needs to be parsed). The fourth-level parsing function then executes the relevant steps in the method of this disclosure, thereby performing syntax analysis on the statement to be analyzed and ultimately obtaining the syntax tree (such as a statement-level AST) of the statement to be analyzed. During the syntax analysis process, the fourth-level parsing function can selectively call the first-level, second-level, or third-level parsing functions according to the actual situation.
[0045] This disclosure divides the composite types into four levels and sets corresponding parsing functions for each level of composite types. This divides the syntax analysis process of the program statements in the simulation modeling language into multiple levels, which helps to make the syntax analysis process logically clear and easy to extend. It also makes the nested calls between parsing functions more suitable for the combination of structured syntactic semantic units in the simulation modeling language, thereby helping to ensure the accuracy of the identification of each composite type (i.e. each structured syntactic semantic unit) in the simulation modeling language.
[0046] In one example, the input parameters of the first-level parsing function may include one or more terminators in the simulation modeling language program (such as an array of terminators) and a tolerance flag. The terminator indicates that the first-level parsing function should terminate the parsing process when the current lexical unit to be processed is a terminator. The tolerance flag indicates whether the first-level parsing function should skip the current lexical unit and continue parsing subsequent lexical units if the current lexical unit does not meet the relevant requirements of the parsing operation. For example, if the tolerance flag is the first value, it means that the first-level parsing function should skip the current lexical unit that does not meet the relevant requirements of the parsing operation and continue with subsequent operations (such as continuing to perform parsing operations on subsequent lexical units). If the tolerance flag is the second value, it means that the first-level parsing function cannot skip the current lexical unit that does not meet the relevant requirements of the parsing operation, and the first-level parsing function should output an error message and stop performing subsequent operations (such as ceasing to perform parsing operations on subsequent lexical units).
[0047] In one example, the output of the first-level parsing function may include a description of the variable expression obtained in this parsing. For instance, the description of the variable expression may include the original string of the variable expression and the list of lexical units (such as a token list) that make up the original string. More specifically, the original string of the variable expression ultimately output by the first-level parsing function through nested calls could be a(5,*), and the list of lexical units that make up the original string could be ["a", "(", "5", ",", "*", ")"]; where a(5,*) represents all columns in the 5th row of the two-dimensional array a. Additionally, the first-level parsing function can also output composite type information, such as the variable expression type (Variable), i.e., the first-level parsing function outputs a triple (Variable, word, wordlist).
[0048] In one example, the input parameters of the second-level parsing function may include one or more terminators in the simulation modeling language program (such as an array of terminators) and a tolerance flag. The terminator indicates that the second-level parsing function should terminate the parsing process when the current lexical unit to be processed is a terminator. The tolerance flag indicates whether the second-level parsing function should skip the current lexical unit and continue parsing subsequent lexical units if the current lexical unit does not meet the relevant requirements of the parsing operation. For example, if the tolerance flag is the first value, the second-level parsing function should skip the current lexical unit that does not meet the relevant requirements and continue with subsequent operations (such as continuing to perform parsing operations on subsequent lexical units). If the tolerance flag is the second value, the second-level parsing function cannot skip the current lexical unit that does not meet the relevant requirements and should output an error message and stop performing subsequent operations (such as ceasing to perform parsing operations on subsequent lexical units).
[0049] In one example, the output of the second-level parsing function may include a description of the mathematical expression obtained in this parsing. For instance, the description of the mathematical expression may include the original string of the mathematical expression and a list of lexical units (such as a token list) that make up the original string. More specifically, after being called, the second-level parsing function might output the original string of the mathematical expression as a + b * c, and the list of lexical units that make up the original string could be ["a", "+", "b", "*", "c"]. Additionally, the second-level parsing function can also output composite type information, such as the mathematical expression type (Expression), meaning the second-level parsing function outputs a triplet (Expression, word, wordlist).
[0050] By setting fault tolerance flags in the input parameters of the first-level and second-level parsing functions, these two independent parsing processes can be interrupted by individual parsing errors based on actual needs. For example, in cases where strict parsing is required, setting this fault tolerance flag helps ensure the syntactic correctness of each statement in the simulation modeling language. Conversely, in cases where fault-tolerant parsing is required, setting this flag helps prevent local syntactic problems in the simulation modeling language program from causing the entire parsing process to terminate prematurely. This improves the flexibility of parsing the simulation modeling language.
[0051] In one example, the input parameters of the third-level parsing function may include one or more terminators from the simulation modeling language program (such as an array of terminators). The output of the third-level parsing function may include descriptive information about the Boolean expression contained in the statement to be parsed. For example, the descriptive information about the Boolean expression may include the original string of the Boolean expression and a list of lexical units (such as a token list) that make up the original string. A more specific example is that the original string of the mathematical expression output by the third-level parsing function after being called is a == b OR c > d, and the list of lexical units that make up the original string can be ["a", "=", "=", "b", "OR", "c", " >", "d"]. In addition, the third-level parsing function can also output compound type information, such as the Boolean expression type (BoolExpression), that is, the third-level parsing function outputs a triple (BoolExpression, word, wordlist).
[0052] In one example, the input parameters of the fourth-level parsing function may include a complete statement in the simulation modeling language program or identification information representing a complete statement. The output of the fourth-level parsing function may include the syntax tree form of the variable expressions, mathematical expressions, and Boolean expressions contained in the complete statement. For example, the fourth-level parsing function forms the syntax tree (such as a statement-level AST) of the complete statement based on the variable expressions, mathematical expressions, and Boolean expressions contained in the complete statement, and stores the statement-level AST in the appropriate location. Additionally, the fourth-level parsing function can also output composite type information, such as the complete statement type (Statement), i.e., the fourth-level parsing function outputs a triple (Statement, word, wordlist).
[0053] The above describes the pre-defined composite types and their corresponding analytical functions in this disclosure. The following section, in conjunction with the accompanying drawings, provides a detailed explanation of the specific application of each analytical function in the method and technical solution of this disclosure.
[0054] Figure 1 This is a flowchart illustrating an embodiment of the nested structure-based syntax analysis method for the simulation modeling language disclosed herein. Figure 1 The method shown includes steps S100, S101, S102, S103, and S104, and this disclosure can execute steps S100, S101, S102, S103, and S104 by calling a fourth-level parsing function. The following describes... Figure 1 Each step in the process will be explained separately.
[0055] S100: Obtain the first lexical unit of the statement to be analyzed in the simulation modeling language program.
[0056] The simulation modeling language program in this disclosure can be a SimScript program, etc. The statement to be analyzed is usually a complete statement (such as a semantically complete statement) in the simulation modeling language program. This disclosure can obtain the statement to be analyzed in the simulation modeling language program in various ways. For example, according to the standard terminator in the program and preset rules such as keyword beginning + exclusive keyword closure, the simulation modeling language program can be scanned line by line to obtain all the complete statements in the simulation modeling language program (excluding comments in the program). Then, a complete statement can be read sequentially according to the order of all complete statements, and the currently read complete statement can be used as the statement to be analyzed.
[0057] In this disclosure, a lexical unit can refer to the smallest indivisible basic syntactic unit (such as a token) obtained after the program code has undergone lexical analysis. A lexical unit in this disclosure can be considered an atomic unit of a composite type (i.e., the smallest syntactic unit of a composite type). In one example, a token can be a word, an operator, or a number, etc.
[0058] This disclosure allows for lexical analysis of a statement to be analyzed by calling the function fetchWord(), thereby sequentially obtaining all lexical units and their types contained in the statement to be analyzed, with the first lexical unit obtained being the first lexical unit.
[0059] S101. Determine the statement type of the statement to be analyzed based on the first lexical unit.
[0060] Since any complete statement in a simulation modeling language program usually begins with a unique statement type identifier keyword, the first lexical unit of the statement to be analyzed is the unique statement type identifier keyword. Therefore, this disclosure can accurately determine the statement type of the statement to be analyzed based on the first lexical unit of the statement to be analyzed.
[0061] In one example, the statement types involved in a simulation modeling language program typically include: the various statement types specified in the syntax manual of the simulation modeling language, such as assignment statement types, conditional statement types (conditional loop statement types can be considered a special type of conditional statement type), loop statement types (such as fixed-cycle loop statement types, etc.), simulation delay statement types, declaration statement types, activation statement types, entity scheduling statement types, creation of simulation entity statement types, and no-operation statement types, etc. The first lexical unit corresponding to the assignment statement type can be ASSIGN or LET, etc.; the first lexical unit corresponding to the conditional statement type can be IF; the first lexical unit corresponding to the conditional loop statement type can be WHILE; the first lexical unit corresponding to the fixed-order loop statement type can be FOR; the first lexical unit corresponding to the simulation delay statement type can be HOLD; the first lexical unit corresponding to the declaration statement type can be Define, Accumulate, or Tally, etc.; the first lexical unit corresponding to the activation statement type can be Activate; the first lexical unit corresponding to the entity scheduling statement type can be Schedule; the first lexical unit corresponding to the simulation entity creation statement type can be Create; the first lexical unit corresponding to the simulation entity creation statement type can be Define; and the first lexical unit corresponding to the no-operation statement type can be NOP. The statement types involved in the simulation modeling language program disclosed herein are not limited to the statement types listed above.
[0062] S102. Call the corresponding parsing function based on the composite type corresponding to the statement type of the statement to be analyzed.
[0063] Each statement type in this disclosure corresponds to a composite type, and the composite types corresponding to two statement types may be the same or different.
[0064] In one example, the fourth-level parsing function, upon determining that the statement to be analyzed is an assignment statement, sets the input parameters of the first-level parsing function (such as an array of terminators and tolerance flags) and calls the first-level parsing function based on these input parameters. Furthermore, when calling the first-level parsing function, the fourth-level parsing function can set the current lexical unit to be processed (such as a global lexical unit variable accessible to all parsing functions) to the next lexical unit in the statement to be analyzed (e.g., using the fetchWord() function to obtain the current lexical unit and its type). Since assignment statement type program statements are usually variable expressions following the first lexical unit (such as ASSIGN), calling the first-level parsing function allows for accurate continuation of the syntax analysis of the statement to be analyzed.
[0065] In one example, the fourth-level parsing function, upon determining that the statement to be analyzed is a conditional statement (including conditional loop statements), sets the input parameters (such as an array of terminators) for the third-level parsing function and calls the third-level parsing function based on these input parameters. Furthermore, when calling the third-level parsing function, the fourth-level parsing function can set the current lexical unit to be processed as the next lexical unit in the statement to be analyzed (e.g., using the `fetchWord()` function to obtain the current lexical unit and its type). Since conditional statements are typically followed by a Boolean expression after the first lexical unit (such as `IF` or `WHILE`), calling the third-level parsing function allows for accurate continuation of the syntax analysis of the statement to be analyzed.
[0066] In one example, the fourth-level parsing function, upon determining that the statement to be analyzed is a loop statement (such as a fixed-order loop), sets the input parameters of the first-level parsing function (such as an array of terminators and fault tolerance flags) and calls the first-level parsing function based on these input parameters. Furthermore, when calling the first-level parsing function, the fourth-level parsing function can set the current lexical unit to be processed as the next lexical unit in the statement to be analyzed (e.g., using the `fetchWord()` function to obtain the current lexical unit and its type). Since loop statement types typically initialize variables after the first lexical unit (such as `FOR`), this can be considered as assigning initial values to variables. By calling the first-level parsing function, the syntax analysis operation of the statement to be analyzed can be accurately continued.
[0067] The above only uses assignment statement type, conditional judgment statement type, and loop statement type as examples to illustrate how the fourth-level parsing function calls other-level parsing functions based on statement type. In practical applications, simulation modeling language programs usually contain a large number of other statement types. The fourth-level parsing function in this disclosure can adopt a multi-way branch judgment method to set up corresponding parsing function call operations for various statement types (including various variants) in the syntax library of the simulation modeling language, so that each statement in the simulation modeling language program can be correctly parsed.
[0068] It should be noted that after the fourth-level parsing function initiates calls to other parsing functions based on the first lexical unit, it will also return parsing results (such as successfully parsed word and wordlist, or parsing failure information) to the corresponding parsing functions after their execution. If there are still unparsed lexical units in the statement to be analyzed, the fourth-level parsing function will continue to identify the corresponding lexical units in the statement to be analyzed and continue to call the corresponding parsing functions according to the identification results, until all lexical units in the statement to be analyzed are successfully parsed or the parsing of lexical units in the statement to be analyzed cannot be completed and a statement parsing error message is output. In addition, for statement types with at least one variant (such as Accumulate and Activate statement types with multiple variants), in some application scenarios, the fourth-level parsing function in this disclosure can call the corresponding parsing function according to the composite type corresponding to the statement type of the statement to be analyzed, or it can call the corresponding parsing function according to the statement type of the statement to be analyzed and the specific lexical units after the first lexical unit. That is, when variants may correspond to different composite types, the fourth-level parsing function can combine the statement type of the statement to be analyzed and the subsequent specific lexical units to determine the corresponding composite type and call the corresponding parsing function based on the composite type. For example, for the Activate statement, the fourth-level parsing function can call the corresponding parsing function after identifying the specific lexical units called and / or given in the statement. Some examples in this embodiment are generally described for non-variant statement types; variant cases are not described in detail here.
[0069] S103. During the process of extracting compound types from the current lexical unit to be processed in the statement to be analyzed, when the current lexical unit to be processed triggers compound type nesting, the called parsing function calls the parsing function corresponding to the nested compound type until all lexical units in the statement to be analyzed are processed as the current lexical unit to be processed in sequence.
[0070] In one example, the current lexical unit to be processed is represented by the global variable aWord. If the called parsing function is a first-level parsing function, the operations performed by the first-level parsing function include:
[0071] Determine the type of the current lexical unit aWord to be processed;
[0072] If the type of aWord is name, then aWord is added to word and wordList respectively, and the function fetchWord() is used to sequentially obtain the next lexical unit and its type. The obtained lexical unit is used to update aWord so that the first-level parsing function can continue to parse subsequent lexical units, which is beneficial to obtaining the complete variable expression.
[0073] If the type of aWord is a symbol that marks the beginning of a mathematical expression (such as leftParen, i.e., a left parenthesis), then the first lexical unit appearing after that symbol is retrieved (e.g., the next lexical unit and its type are retrieved sequentially using the fetchWord() function), and aWord is updated using that lexical unit. The input parameters for the second-level parsing function should also be set, and the second-level parsing function should be called so that it can continue to perform syntax analysis on the content within the parentheses. This allows for a smooth transition to the process of extracting mathematical expressions from the statement to be analyzed, thus helping the first-level parsing function accurately parse array-like variables. Special note is required. In one application scenario, after the first-level parsing function calls the second-level parsing function, the second-level parsing function begins parsing the content after the symbol (such as the content within parentheses). When the second-level parsing function encounters a symbol that is not an operator (such as a comma), even though it has not yet encountered the end symbol, it considers the current parsing process to be complete and returns the current parsing result to the first-level parsing function (for example, the second-level parsing function parses the 5 after the left parenthesis in a(5,*), stops parsing when it encounters a comma, and returns the parsing results of word=5 and wordlist=5 to the first-level parsing function). Afterwards, the first-level parsing function can then process the content after the parenthesis. The content within the parentheses continues to be parsed (e.g., the first-level parsing function obtains the array variable a(5,*) by parsing commas and asterisks); of course, this disclosure can also use other methods to implement parsing. For example, after the first-level parsing function calls the second-level parsing function, the second-level parsing function begins to parse the content within the parentheses. If the second-level parsing function determines, based on the parsing result, that the content within the parentheses does not belong to a mathematical expression (e.g., since the content within the parentheses of a(5,*) does not contain mathematical operators, therefore, the content within the parentheses does not belong to a mathematical expression), then the second-level parsing function considers the current parsing process complete and returns a parsing failure message to the first-level parsing function. Then, the first-level parsing function can continue to parse the content within the parentheses (e.g., the first-level parsing function obtains the array variable a(5,*) by continuing parsing); for another example, after the first-level parsing function calls the second-level parsing function, the second-level parsing function begins to parse the content within the parentheses, and continues to parse until the right parenthesis according to the end character in the input parameters. The second-level parsing function considers the current parsing process to be complete and returns the parsing result to the first-level parsing function (e.g., returning the parsing result of word (5,*) and its wordlist). In this way, the first-level parsing function obtains the parsing result of word a(5,*) and its wordlist.
[0074] If the type of aWord is an object attribute access operator (such as dot, i.e., "."), then aWord is added to word and wordList respectively, and the first lexical unit that appears after the access operator is obtained (such as using the function fetchWord() to sequentially obtain the next lexical unit and its type). aWord is updated using this lexical unit so that the first-level parsing function can continue to parse the object's attributes (such as the.unit, etc.), which helps the first-level parsing function to accurately parse the object attribute form variable;
[0075] If the type of aWord does not meet any of the above conditions, and the fault tolerance flag in the input parameters of the first-level parsing function indicates that errors should be ignored, then the first-level parsing function can use the function fetchWord() to sequentially obtain the next lexical unit and its type, and use the obtained lexical unit to update aWord, so that the first parsing function can continue to parse subsequent lexical units, thereby helping to avoid the phenomenon that a single syntax error will cause the entire syntax analysis process to be interrupted.
[0076] If the type of aWord does not meet any of the above conditions, and the fault tolerance flag in the input parameters of the first-level parsing function indicates that errors should not be ignored, then the first-level parsing function should output error information (such as returning error information to the fourth-level parsing function) and immediately stop executing subsequent operations, which is conducive to meeting the need for timely syntax correction.
[0077] The first-level parsing function repeats the above operation until aWord belongs to the endWords (end character) in the input parameters of the first-level parsing function or the first-level parsing function returns an error message.
[0078] In one example, the information output by the first-level parsing function may include: type, word, and wordList, for example:
[0079] Type: Variable
[0080] word: a(5,*)
[0081] wordList: ["a", "(", "5", ",", "*", ")"]
[0082] It should be noted that the first-level parsing function in this disclosure may also be called by other-level parsing functions as needed. When called by other-level parsing functions, the first-level parsing function returns the parsing result to the caller. The second-level and third-level parsing functions in this disclosure also return the parsing result to the caller.
[0083] In one example, if the current lexical unit to be processed is represented by aWord, and the called parsing function is a second-level parsing function, the operations performed by the second-level parsing function include:
[0084] Set the initial value of needVar to true (i.e., a number or variable is needed at present);
[0085] Determine the type of the current lexical unit aWord to be processed;
[0086] If the type of aWord is string or label, then add string or label to word and wordList, retrieve the next lexical unit (e.g., use the function fetchWord() to sequentially retrieve the next lexical unit and its type), and update aWord with that lexical unit;
[0087] If aWord is a symbol that marks the beginning of a mathematical expression (such as leftParen), then the first lexical unit that appears after this symbol is obtained (e.g., the next lexical unit and its type are obtained sequentially using the function fetchWord()), and aWord is updated using this lexical unit. The input parameters of the second-level parsing function should also be set, and the second-level parsing function should be called recursively so that the second-level parsing function can continue to perform syntax analysis on the content within the parentheses, thereby helping the second-level parsing function to accurately parse out the nested mathematical expression variables within the mathematical expression variables.
[0088] When needVar is true (i.e., indicating a required variable):
[0089] If the type of aWord is number or name, the second-level parsing function sets needVar to false and calls the first-level parsing function to parse aWord, which helps to obtain the complete variable expression in the mathematical expression table.
[0090] If the type of aWord is plus (plus sign) or minus (minus sign), the second-level parsing function can use the function fetchWord() to sequentially obtain the next lexical unit and its type;
[0091] If the type of the next lexical unit is number or variable, the second-level parsing function identifies aWord as positive / negative and adds plus / minus and the next lexical unit as operands of the mathematical expression variable to word and wordList. The second-level parsing function then uses the function fetchWord() to sequentially retrieve the next lexical unit and its type, and updates aWord. Otherwise, if the error tolerance flag isHideError in its input parameters indicates that skipping aWord is prohibited, the second-level parsing function outputs an error message. If the error tolerance flag isHideError in its input parameters indicates that skipping aWord is prohibited, the second-level parsing function then uses the function fetchWord() to sequentially retrieve the next lexical unit and its type, and updates aWord.
[0092] When needVar is false (i.e., indicating that an operator is required):
[0093] If the type of aWord is a mathematical operator (such as +, -, *, or / ), then aWord is added to word and wordList as an operator used as a variable in the mathematical expression. The second-level parsing function uses the function fetchWord() to sequentially retrieve the next lexical unit and its type, updates aWord, and sets needVar to true. It is important to note that in simulation modeling languages where mathematical operators are formed by two or more lexical units, the second-level parsing function should determine the mathematical operator across multiple lexical units. For example, in simulation modeling languages, * represents the multiplication operator, while **... This represents the square operator. If the second-level parsing function determines that the type of aWord is an operator and it is the * operator, it needs to continue to check whether the next lexical unit is still *. If the next lexical unit is still *, then the second-level parsing function should treat ** (i.e., two consecutive *) as the square operator and add the operator aWord + the next lexical unit as a variable in the mathematical expression to word and wordList. The second-level parsing function uses the function fetchWord() to sequentially obtain the next lexical unit and its type, and updates aWord. If the next lexical unit is not *, then * will be treated as a multiplication operator and added to word and wordList.
[0094] If the type of aWord does not meet any of the above conditions, and the fault tolerance flag in the input parameters of the second-level parsing function indicates that errors should be ignored, then the second-level parsing function can use the function fetchWord() to sequentially obtain the next lexical unit and its type, and use the obtained lexical unit to update aWord, so that the second parsing function can continue to parse subsequent lexical units, thereby helping to avoid the phenomenon that a single syntax error will cause the entire syntax analysis process to be interrupted.
[0095] If the type of aWord does not meet any of the above conditions, and the fault tolerance flag in the input parameters of the second-level parsing function indicates that errors should not be ignored, then the second-level parsing function should output error information (such as returning error information to the fourth-level parsing function) and immediately stop executing subsequent operations, which is conducive to meeting the need for timely syntax correction.
[0096] Repeat the above operation until aWord belongs to the endWords (end character) in the input parameters of the second-level parsing function or the second-level parsing function returns an error message.
[0097] In one example, the information output by the second-level parsing function may include: type, word, and wordList, for example:
[0098] Type: expression
[0099] word: a + b * c
[0100] wordList: ["a", "+", "b", "*", "c"]
[0101] In one example, if the current lexical unit to be processed is represented by aWord, and the called parsing function is a third-level parsing function, the operations performed by the third-level parsing function include:
[0102] Determine the type of the current lexical unit aWord to be processed;
[0103] If the type of aWord is a symbol at the beginning of a mathematical expression (such as leftParen, i.e., a left parenthesis), the third-level parsing function will attempt to call the second-level parsing function first. Specifically, the third-level parsing function obtains the first lexical unit that appears after the symbol (e.g., using the function fetchWord() to sequentially obtain the next lexical unit and its type), and updates aWord using that lexical unit. It should also set the input parameters of the second-level parsing function and call the second-level parsing function so that it can continue to perform syntax analysis on the content within the parentheses. This allows for accurate identification of nested mathematical expressions.
[0104] If the type of aWord is number or name, the third-level parsing function sets the input parameters of the second-level parsing function and calls the second-level parsing function so that the second-level parsing function can perform syntax analysis on the number or name, thus enabling accurate recognition of mathematical expressions;
[0105] If the symbol indicating the start of a mathematical expression is followed by a non-mathematical expression (e.g., the content within parentheses is a Boolean expression, specifically, let's say a Boolean expression is a OR (a == b OR c > d)), the third-level parsing function, upon encountering the left parenthesis, calls the second-level parsing function. The second-level parsing function, upon encountering the == symbol, returns information indicating it's not a mathematical expression. At this point, the third-level parsing function recursively calls another third-level parsing function (since this called third-level function no longer attempts to call the second-level function, it can also be called a third-level sub-parsing function) to parse the Boolean expression within the parentheses (e.g., parsing the sub-Boolean expression a == b OR c > d within the parentheses of the aforementioned Boolean expression). This facilitates accurate identification of sub-Boolean expressions nested within Boolean expressions. If the content following the symbol is a mathematical expression, the third-level parsing function, upon receiving the content returned by the second-level parsing function, treats this content as part of the Boolean expression (e.g., adding it to word and wordList) and continues with subsequent parsing operations. For example, when parsing a == b OR During the parsing of `c>d`, when `aWord` is `a`, since `a`'s type is `name`, the third-level parsing function calls the second-level parsing function for the first time. The second-level parsing function performs the parsing operation and returns a parsing result with `type: expression`, `word` as `a`, and `wordList` as `"a"`. The third-level parsing function then continues parsing, writing `==` to `word` and `wordList` when it recognizes `==` as a comparison operator. Since `b`'s type is `name`, the third-level parsing function calls the second-level parsing function for the second time. The second-level parsing function performs the parsing operation and returns a parsing result with `type: expression`, `word` as `b`, and `wordList` as `"b"`. The third-level parsing function continues parsing, writing `OR` to `word` and `wordList` when it recognizes `OR` as a logical operator. Finally, since `c`'s type is `name`, the third-level parsing function calls the second-level parsing function for the third time. The second-level parsing function performs the parsing operation and returns a parsing result with `type: expression`. The parsing results are "c" for word and "c" for wordList. Then, the third-level parsing function continues to perform parsing operations. When it recognizes ">" as a comparison operator, it writes ">" into word and wordList.Then, since the type of d is name, the third-level parsing function calls the second-level parsing function for the fourth time. The second-level parsing function performs the parsing operation and returns the parsing result to the third-level parsing function with type as expression, word as d, and wordList as "d". Thus, the third-level parsing function completes the parsing process of this pair of Boolean expressions and finally returns the parsing result to the caller (the fourth-level parsing function) with type as boolean expression, word as a==b OR c>d, and wordList as "a", "==", "b", "OR", "c", ">", "d". It should be noted that, in order to ensure the correctness of the parsing result of the third-level parsing function, this disclosure can perform syntax error detection on the parsing process of the third-level parsing function to ensure that the parsing result is a valid Boolean expression. Specifically, the third-level parsing function of this disclosure sets a marker variable (such as lastType), which has the following five possible values:
[0106] With an initial value (such as -1), the third parsing function is called.
[0107] The first value (e.g., 0) indicates that a mathematical expression has been obtained and is waiting for a comparison operator. If the result obtained by subsequent parsing is not a comparison operator (e.g., a logical operator), a syntax error is considered to have occurred. For example, when the third-level parsing function receives the mathematical expression returned by the second-level parsing function, it sets the value of the marker variable to 0.
[0108] The second value (e.g., 1) obtains a comparison operator and waits for the subsequent mathematical expression; for example, after receiving the mathematical expression returned by the second-level parsing function, the third-level parsing function continues to perform the parsing operation and obtains the comparison operator. The third-level parsing function then sets the value of the marker variable to 1.
[0109] The third value (e.g., 2) indicates that a logical operator has been obtained and the system is waiting for the subsequent logical expression; for example, after the third-level parsing function obtains OR, it sets the value of the marker variable to 2.
[0110] The fourth value (e.g., 3) indicates that a Boolean expression has been obtained; for example, after receiving the mathematical expression returned by the second-level parsing function, the third-level parsing function sets the value of the marker variable to 3.
[0111] In a concrete example, the fourth-level parsing function calls the third-level parsing function to parse the Boolean expression `a == b OR c > d`. When the third-level parsing function is called, it sets the value of the marker variable to -1. The third-level parsing function, by calling the second-level parsing function, successfully obtains the mathematical expression `a` and sets the value of the marker variable to 0. After parsing and obtaining the comparison operator `==`, the third-level parsing function sets the value of the marker variable to 1. The third-level parsing function, by calling the second-level parsing function, successfully obtains the mathematical expression `b` and sets the value of the marker variable to 3. After parsing and obtaining `OR`, the third-level parsing function sets the value of the marker variable to 2. The third-level parsing function, by calling the second-level parsing function, successfully obtains the mathematical expression `c` and sets the value of the marker variable to 0. After parsing and obtaining the comparison operator `>`, the third-level parsing function sets the value of the marker variable to 1. The third-level parsing function, by calling the second-level parsing function, successfully obtains the mathematical expression `d` and sets the value of the marker variable to 3. The third-level parsing function successfully obtains the Boolean expression and returns the parsing result to the fourth-level parsing function.
[0112] In one example, the information output by the third-level parsing function may include: type, boolword, and boolwordList, for example:
[0113] Type: boolexpression
[0114] boolword: a == b OR c > d
[0115] boolwordList:["a", "==", "b", "OR", "c", ">", "d"]
[0116] S104. Store the tree node structure information of the complex types contained in the statements to be analyzed, output by each of the called parsing functions, in the form of a statement-level syntax tree.
[0117] This disclosure predefines a structured storage structure containing three attributes: type (composite type, such as variable expression, mathematical expression, Boolean expression, and complete statement), word (cumulative string of composite type), and wordList (lexical unit list of cumulative string of composite type). The structured storage structure is a nested storage structure, which can be implemented using a dictionary, and the contents stored in the dictionary can be represented in the form of a syntax tree.
[0118] A concrete example is a statement in a simulation modeling language program: `schedule Event_Sleepat time .a + 20`. This statement is stored in the following form using the technical solution of this disclosure: [
[0120] type: statement
[0121] Word: "schedule Event_Sleep at time .a + 20"
[0122] wordList:[
[0123] [type:name,word:”schedule”]
[0124] [type:name,word:”Event_Sleep”]
[0125] [type:name,word:"at"]
[0126] [type:expression,word:”time .a + 20”,wordList: [
[0128] [type:variable,word:”time.a”]
[0129] [type:expressionop,word:”+”]
[0130] [type:number,word:”20”] ] ] ]
[0134] The structured storage method of this statement can be considered as a statement-level AST, and the structured storage method of all statements of a function can form a function-level AST.
[0135] Exemplary device
[0136] An embodiment of the nested structure-based syntax analysis device for the simulation modeling language disclosed herein has the following structure: Figure 2 As shown. The apparatus of this embodiment can be used to implement the corresponding method embodiments of this disclosure. Figure 2The apparatus shown includes: a set of parsing functions 200, an acquisition module 201, a statement type determination module 202, a calling module 203, and a storage module 204. The set of parsing functions 200 includes multiple parsing functions, and each parsing function corresponds to a composite type in the simulation modeling language.
[0137] The acquisition module 201 is mainly used to acquire the first lexical unit of the statement to be analyzed in the simulation modeling language program.
[0138] The statement type determination module 202 is mainly used to determine the statement type of the statement to be analyzed based on the first lexical unit.
[0139] The calling module 203 is mainly used to call the corresponding parsing function according to the composite type corresponding to the statement type of the statement to be analyzed.
[0140] The called parsing function is used during the extraction of compound types from the current lexical unit to be processed in the statement to be analyzed. When it is determined that the current lexical unit to be processed triggers compound type nesting, the parsing function corresponding to the nested compound type is called until all lexical units in the statement to be analyzed have been processed as the current lexical unit to be processed in sequence.
[0141] Storage module 204 is used to store the tree node structure information of the complex types contained in the statements to be analyzed, which are output by each of the called parsing functions, in the form of a statement-level syntax tree.
[0142] In one example, the composite types disclosed herein include at least: variable expressions, mathematical expressions, Boolean expressions, and complete statements, and the parsing function set 200 includes at least: a first-level parsing function 2001 corresponding to variable expressions, a second-level parsing function 2002 corresponding to mathematical expressions, a third-level parsing function 2003 corresponding to Boolean expressions, and a fourth-level parsing function 2004 corresponding to complete statements, and the fourth-level parsing function 2004 may include the aforementioned acquisition module 201, statement type determination module 202, calling module 203, and storage module 204.
[0143] The statement acquisition module 205 is mainly used to acquire a statement to be analyzed in the simulation modeling language program, and call the fourth-level parsing function 2004 according to the statement to be analyzed, so that each module in the fourth-level parsing function 2004 can perform corresponding operations.
[0144] In one example, the input parameters of the first-level parsing function 2001 and the second-level parsing function 2002 both include at least one terminator and a tolerance flag; the input parameters of the third-level parsing function 2003 include at least one terminator; the terminator is used to indicate that the parsing operation ends when the current lexical unit to be processed belongs to the terminator; the tolerance flag is used to indicate whether the first-level parsing function and the second-level parsing function should skip the current lexical unit to be processed and continue to perform parsing operations on subsequent lexical units if the current lexical unit to be processed does not meet the relevant requirements of the parsing operation during the parsing operation.
[0145] In one example, the statement type of the statement to be analyzed includes: assignment statement type, conditional statement type, or loop statement type. If the fourth-level parsing function 2004 determines that the statement type is an assignment statement, it calls the first-level parsing function 2001; if the fourth-level parsing function 2004 determines that the statement type is a conditional statement, it calls the third-level parsing function 2003; if the fourth-level parsing function 2004 determines that the statement type is a loop statement, it calls the first-level parsing function 2001.
[0146] In one example, the process of the invoked first-level parsing function 2001 extracting the compound type of the current lexical unit to be processed in the statement to be parsed includes: when the type of the current lexical unit to be processed is determined to be a name, adding the current lexical unit to be processed to the current statement accumulation string and the lexical unit list of the current statement accumulation string, and taking the next lexical unit as the current lexical unit to be processed; when the type of the current lexical unit to be processed is determined to be an attribute access operator, adding the current lexical unit to be processed to the current statement accumulation string and the lexical unit list of the current statement accumulation string, and taking the next lexical unit as the current lexical unit to be processed; when the type of the current lexical unit to be processed is determined to be a type other than the name, the symbol marking the start of a mathematical expression, the attribute access operator, and the terminator, returning an error flag when the fault tolerance flag indicates that skipping is prohibited, or taking the next lexical unit as the current lexical unit to be processed when the fault tolerance flag indicates that skipping is prohibited; and when the current lexical unit to be processed is determined to be a terminator in the input parameters of the first-level parsing function 2001, the first-level parsing function 2001 outputs the current statement accumulation string and the lexical unit list of the current statement accumulation string. In addition, when the first-level parsing function 2001 determines that the type of the current lexical unit to be processed is a symbol that marks the beginning of a mathematical expression, it takes the next lexical unit as the current lexical unit to be processed and calls the second-level parsing function.
[0147] In one example, the process of extracting the compound type of the current lexical unit to be processed in the parsed statement by the called second-level parsing function 2002 includes: when it is determined that the type of the current lexical unit to be processed is a string or a tag, adding the current lexical unit to be processed to the current statement accumulation string and the lexical unit list of the current statement accumulation string, and taking the next lexical unit as the current lexical unit to be processed; when it is determined that a variable is needed, if the current lexical unit to be processed is a plus / minus sign, and the next lexical unit to be processed is a number or a variable, then adding the plus / minus sign as a positive or negative sign to the current lexical unit to be processed to the current statement accumulation string and the lexical unit list of the current statement accumulation string; when it is determined that... If an operator is required, and the current lexical unit to be processed is an operator, then the operator is added to the current statement accumulation string and the lexical unit list of the current statement accumulation string, and the next lexical unit is taken as the current lexical unit to be processed. If the current lexical unit to be processed is a symbol other than the operator and the terminator, then an error flag is returned when the fault tolerance flag is set to prohibit skipping, or the next lexical unit is taken as the current lexical unit to be processed when the fault tolerance flag is set to allow skipping. If it is determined that the current lexical unit to be processed belongs to the terminator in the input parameters of the second-level parsing function 2002, the second-level parsing function 2002 outputs the current statement accumulation string and the lexical unit list of the current statement accumulation string. In addition, when the called second-level parsing function determines that the type of the current lexical unit to be processed is a symbol that marks the beginning of a mathematical expression, it takes the next lexical unit as the current lexical unit to be processed and calls the second-level parsing function 2002. When the called second-level parsing function 2002 determines that a variable is required and the current lexical unit to be processed is a number or a name, it calls the first-level parsing function 2001.
[0148] In one example, the third-level parsing function 2003, called by the fourth-level parsing function 2004, attempts to call the second-level parsing function 2002. If it determines that the parsing result output by the second-level parsing function 2002 is not a mathematical expression, it recursively calls the third-level sub-parsing function 2003 to parse the current lexical unit. The operations performed by the recursively called third-level sub-parsing function include: if a mathematical expression is currently obtained, and the next obtained expression is not a comparison operator, then the third-level sub-parsing function determines that a Boolean expression syntax error has occurred; if a comparison operator is currently obtained, and the next obtained expression is not a mathematical expression, then the third-level sub-parsing function determines that a Boolean expression syntax error has occurred; if a logical operator is currently obtained, and the next obtained expression is not a logical expression, then the third-level sub-parsing function determines that a Boolean expression syntax error has occurred.
[0149] In one example, for any function in a simulation modeling language program, the storage module 204 of this disclosure can form a function-level syntax tree based on the statement-level syntax tree of all complete statements contained in the function, and store the function-level syntax tree.
[0150] Exemplary electronic devices
[0151] The following is for reference. Figure 3 To describe an electronic device according to embodiments of the present disclosure. Figure 3 A block diagram of an electronic device according to an embodiment of the present disclosure is shown. (As follows) Figure 3 As shown, the electronic device 31 includes one or more processors 311 and memory 312.
[0152] The processor 311 may be a central processing unit (CPU) or other form of processing unit with data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device 31 to perform desired functions.
[0153] The memory 312 may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may, for example, include random access memory (RAM) and / or cache memory. The non-volatile memory may, for example, include read-only memory (ROM), hard disk, and flash memory. One or more computer program instructions may be stored on the computer-readable storage medium, and the processor 311 may execute the program instructions to implement the nested structure-based syntax analysis method of the simulation modeling language of the various embodiments of this disclosure described above, and / or other desired functions.
[0154] In one example, electronic device 31 may further include input device 313 and output device 314, etc., these components being interconnected via a bus system and / or other forms of connection mechanism (not shown). Furthermore, the input device 313 may include, for example, a keyboard, mouse, etc. The output device 314 can output various information to the outside. The output device 314 may include, for example, a display, speaker, printer, and communication network and its connected remote output devices, etc.
[0155] Of course, for the sake of simplicity, Figure 3 Only some of the components of the electronic device 31 relevant to this disclosure are shown, omitting components such as buses, input / output interfaces, etc. In addition, the electronic device 31 may include any other suitable components depending on the specific application.
[0156] Exemplary computer program products and computer-readable storage media
[0157] In addition to the methods and apparatus described above, embodiments of this disclosure may also be computer program products comprising computer program instructions that, when executed by a processor, cause the processor to perform the steps in the nested structure-based syntax analysis method for simulation modeling languages according to various embodiments of this disclosure as described in the "Exemplary Methods" section above.
[0158] The computer program product can be written in any combination of one or more programming languages to perform the operations of the embodiments of this disclosure. The programming languages include object-oriented programming languages such as Java and C++, as well as conventional procedural programming languages such as C or similar languages. The program code can be executed entirely on a user's computing device, partially on a user's computing device, as a standalone software package, partially on a user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0159] Furthermore, embodiments of this disclosure may also be computer-readable storage media storing computer program instructions that, when executed by a processor, cause the processor to perform the steps in the nested structure-based syntax analysis method for simulation modeling languages according to various embodiments of this disclosure as described in the "Exemplary Methods" section above.
[0160] The computer-readable storage medium may be any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may, for example, include, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatuses, or devices, or any combination thereof. More specific examples of readable storage media (not an exhaustive list) may include: an electrical connection having one or more wires, a portable disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0161] The basic principles of this disclosure have been described above with reference to specific embodiments. However, it should be noted that the advantages, benefits, and effects mentioned in this disclosure are merely examples and not limitations, and should not be considered as essential features of each embodiment of this disclosure. Furthermore, the specific details disclosed above are for illustrative and facilitative purposes only, and are not limitations. These details do not limit the scope of this disclosure to the necessity of employing the aforementioned specific details for implementation.
[0162] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For system embodiments, since they largely correspond to method embodiments, the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments.
[0163] The block diagrams of devices, apparatuses, devices, and systems disclosed herein are merely illustrative examples and are not intended to require or imply that they must be connected, arranged, or configured in the manner shown in the block diagrams. As those skilled in the art will recognize, these devices, apparatuses, devices, and systems can be connected, arranged, and configured in any manner. Words such as “comprising,” “including,” “having,” etc., are open-ended terms meaning “including but not limited to,” and are used interchangeably with them. The terms “or” and “and” as used herein refer to the terms “and / or,” and are used interchangeably with them unless the context clearly indicates otherwise. The term “such as” as used herein refers to the phrase “such as but not limited to,” and is used interchangeably with it.
[0164] The methods and apparatus of this disclosure may be implemented in many ways. For example, they may be implemented by software, hardware, firmware, or any combination of software, hardware, and firmware. The above-described order of steps for the methods is for illustrative purposes only, and the steps of the methods of this disclosure are not limited to the order specifically described above unless otherwise specifically stated. Furthermore, in some embodiments, this disclosure may also be implemented as a program recorded on a recording medium, the program including machine-readable instructions for implementing the methods according to this disclosure. Thus, this disclosure also covers recording media storing programs for performing the methods according to this disclosure.
[0165] It should also be noted that in the apparatus, devices, and methods of this disclosure, the components or steps can be disassembled and / or recombined. These disassemblies and / or recombinations should be considered as equivalent solutions to this disclosure.
[0166] The above description of the disclosed aspects is provided to enable any person skilled in the art to make or use this disclosure. Various modifications to these aspects will be readily apparent to those skilled in the art, and the general principles defined herein can be applied to other aspects without departing from the scope of this disclosure. Therefore, this disclosure is not intended to be limited to the aspects shown herein, but rather to be carried out within the widest scope consistent with the principles and novel features disclosed herein.
[0167] The above description has been given for purposes of illustration and description. Furthermore, this description is not intended to limit the embodiments of this disclosure to the forms disclosed herein. Although numerous exemplary aspects and embodiments have been discussed above, those skilled in the art will recognize certain variations, modifications, alterations, additions, and sub-combinations thereof.
Claims
1. A syntax analysis method for a simulation modeling language based on nested structures, characterized in that, A pre-defined set of analytical functions is provided, wherein each analytical function in the set corresponds to a composite type in a simulation modeling language, and the method includes the following steps: Obtain the first lexical unit of the statement to be analyzed in the simulation modeling language program; The statement type of the statement to be analyzed is determined based on the first lexical unit; The corresponding parsing function is called based on the composite type corresponding to the statement type of the statement to be analyzed; During the process of extracting compound types from the current lexical unit to be processed in the statement to be analyzed, when the current lexical unit to be processed triggers compound type nesting, the called parsing function calls the parsing function corresponding to the nested compound type until all lexical units in the statement to be analyzed are processed as the current lexical unit to be processed in sequence. The tree node structure information of the complex types contained in the statement to be analyzed, output by each of the called parsing functions, is stored in the form of a statement-level syntax tree.
2. The method according to claim 1, characterized in that, The composite types in the simulation modeling language include at least: variable expressions, mathematical expressions, Boolean expressions, and complete statements; The set of parsing functions includes at least: a first-level parsing function corresponding to variable expressions, a second-level parsing function corresponding to mathematical expressions, a third-level parsing function corresponding to Boolean expressions, and a fourth-level parsing function corresponding to complete statements; The method further includes: obtaining a statement to be analyzed in the simulation modeling language program, and calling the fourth-level parsing function according to the statement to be analyzed, so that the fourth-level parsing function executes the steps in the method.
3. The method according to claim 2, characterized in that, The input parameters of the first-level parsing function and the second-level parsing function both include at least one terminator and a tolerance flag; the input parameters of the third-level parsing function include at least one terminator; wherein, the terminator is used to indicate that the parsing operation ends when the current lexical unit to be processed belongs to the terminator; the tolerance flag is used to indicate whether the first-level parsing function and the second-level parsing function should skip the current lexical unit to be processed and continue to perform parsing operations on subsequent lexical units if the current lexical unit to be processed does not meet the relevant requirements of the parsing operation during the parsing operation.
4. The method according to claim 3, characterized in that, The process by which the first-level parsing function, when invoked, extracts compound types from the current lexical units to be processed in the statement to be analyzed includes: When the type of the current lexical unit to be processed is determined to be a name, the current lexical unit to be processed is added to the lexical unit list of the current statement accumulation string and the current statement accumulation string, and the next lexical unit is taken as the current lexical unit to be processed. When it is determined that the type of the current lexical unit to be processed is an attribute access operator, the current lexical unit to be processed is added to the current statement accumulation string and the lexical unit list of the current statement accumulation string, and the next lexical unit is taken as the current lexical unit to be processed. When it is determined that the type of the current lexical unit to be processed is other than the name, the symbol that marks the beginning of a mathematical expression, the attribute access operator, and the terminator, an error flag is returned when the fault tolerance flag indicates that skipping is prohibited, or the next lexical unit is taken as the current lexical unit to be processed when the fault tolerance flag indicates that skipping is prohibited. Upon determining that the current lexical unit to be processed belongs to the terminator in the input parameters of the first-level parsing function, the first-level parsing function outputs the current statement accumulation string and the list of lexical units of the current statement accumulation string.
5. The method according to claim 3, characterized in that, During the process of extracting compound types from the current lexical unit to be processed in the statement to be analyzed, when the called parsing function determines that the current lexical unit to be processed triggers compound type nesting, it calls the parsing function corresponding to the nested compound type, including: When the first-level parsing function determines that the type of the current lexical unit to be processed is a symbol that marks the beginning of a mathematical expression, it takes the next lexical unit as the current lexical unit to be processed and calls the second-level parsing function.
6. The method according to claim 3, characterized in that, The process by which the invoked second-level parsing function extracts compound types from the current lexical units to be processed in the statement to be analyzed includes: When it is determined that the type of the current lexical unit to be processed is a string or a tag, the current lexical unit to be processed is added to the current statement accumulation string and the lexical unit list of the current statement accumulation string, and the next lexical unit is taken as the current lexical unit to be processed. If the current variable is determined to be a plus or minus sign, and the next lexical unit to be processed is a number or a variable, then the plus or minus sign is used as a positive or negative sign, and the current lexical unit to be processed is added to the current statement accumulation string and the lexical unit list of the current statement accumulation string. If an operator is determined to be needed, and the current lexical unit to be processed is an operator, then the operator is added to the current statement accumulation string and the lexical unit list of the current statement accumulation string, and the next lexical unit is taken as the current lexical unit to be processed; if the current lexical unit to be processed is a symbol other than the operator and the terminator, then an error flag is returned when the fault tolerance flag is set to prohibit skipping, or the next lexical unit is taken as the current lexical unit to be processed when the fault tolerance flag is set to allow skipping. If it is determined that the current lexical unit to be processed belongs to the terminator in the input parameters of the second-level parsing function, the second-level parsing function outputs the current statement accumulation string and the list of lexical units of the current statement accumulation string.
7. The method according to claim 3, characterized in that, During the process of extracting compound types from the current lexical unit to be processed in the statement to be analyzed, when the called parsing function determines that the current lexical unit to be processed triggers compound type nesting, it calls the parsing function corresponding to the nested compound type, including: When the invoked second-level parsing function determines that the type of the current lexical unit to be processed is a symbol that marks the beginning of a mathematical expression, it takes the next lexical unit as the current lexical unit to be processed and invokes the second-level parsing function. The second-level parsing function, when it determines that the current required variable is a number or a name, calls the first-level parsing function.
8. The method according to claim 3, characterized in that, During the process of extracting compound types from the current lexical unit to be processed in the statement to be analyzed, when the called parsing function determines that the current lexical unit to be processed triggers compound type nesting, it calls the parsing function corresponding to the nested compound type, including: The third-level parsing function, which is called by the fourth-level parsing function, attempts to call the second-level parsing function. When it is determined that the parsing result output by the second-level parsing function is not a mathematical expression, the third-level sub-parsing function is recursively called to parse the current lexical unit to be processed.
9. The method according to claim 8, characterized in that, The operations performed by the recursively called third-level sub-parse function include: If the mathematical expression obtained at the current level is not a comparison operator, then the third-level sub-parse function determines that a Boolean expression syntax error has occurred. If the comparison operator obtained at the current level is not a mathematical expression, then the third-level sub-parse function determines that a Boolean expression syntax error has occurred. If the next logical operator obtained is not a logical expression, the third-level sub-parse function determines that a Boolean expression syntax error has occurred.
10. The method according to any one of claims 1 to 9, characterized in that, The method further includes: For any function in a simulation modeling language program, a function-level syntax tree is formed based on the statement-level syntax tree of all complete statements contained in the function, and the function-level syntax tree is stored.
11. A nested structure-based syntax analysis device for a simulation modeling language, characterized in that, The device includes: A set of analytical functions, each of which corresponds to a composite type in a simulation modeling language; The acquisition module is used to acquire the first lexical unit of the statement to be analyzed in the simulation modeling language program; The statement type determination module is used to determine the statement type of the statement to be analyzed based on the first lexical unit; The calling module is used to call the corresponding parsing function according to the composite type corresponding to the statement type of the statement to be analyzed; The called parsing function is used to call the parsing function corresponding to the nested compound type when it is determined that the current lexical unit to be processed triggers compound type nesting during the process of extracting compound types from the current lexical unit to be processed in the statement to be analyzed, until all lexical units in the statement to be analyzed are processed as the current lexical unit to be processed in sequence. The storage module is used to store the tree node structure information of the complex types contained in the statement to be analyzed, which is output by each of the called parsing functions, in the form of a statement-level syntax tree.
12. A computer-readable storage medium storing a computer program for performing the method of any one of claims 1-10.
13. An electronic device, the electronic device comprising: processor; Memory used to store the processor's executable instructions; The processor is configured to read the executable instructions from the memory and execute the instructions to implement the method of any one of claims 1-10.