CN118069150A - Compiling method, compiler and implicit conversion method and device - Google Patents
Compiling method, compiler and implicit conversion method and device Download PDFInfo
- Publication number
- CN118069150A CN118069150A CN202410352886.XA CN202410352886A CN118069150A CN 118069150 A CN118069150 A CN 118069150A CN 202410352886 A CN202410352886 A CN 202410352886A CN 118069150 A CN118069150 A CN 118069150A
- Authority
- CN
- China
- Prior art keywords
- data type
- hash table
- layer
- implicit
- implicit conversion
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/37—Compiler construction; Parser generation
Landscapes
- Engineering & Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Devices For Executing Special Programs (AREA)
Abstract
The application relates to a compiling method, a compiler and an implicit converting method and device, wherein the compiling method is applied to the compiler, the compiler is configured with two layers of hash tables, the two layers of hash tables describe the converting relation of a first data type and a second data type, and the method comprises the following steps: determining a first data type and a second data type according to the acquired source code; judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table; when the implicit conversion rule is met, performing implicit conversion on the first data type and the second data type; after the implicit conversion is completed, the object code is generated. Therefore, the two-layer hash table is used for storing the conversion rule of the data types, and when the compiler encounters the condition that the first data type and the second data type need to be converted in the compiling process, the implicit conversion rule can be quickly queried and obtained based on the two-layer hash table, so that the implicit conversion of the data types under different large categories is realized.
Description
Technical Field
The application relates to the technical field of compiling, in particular to a compiling method, a compiler, an implicit conversion method and an implicit conversion device.
Background
A compiler is a computer program that converts source code written in a certain programming language into another programming language (typically referred to as machine executable binary code). During the compiling process of the source code, when static checking is performed, the compiler has the condition of automatically and forcedly converting one data type into the other data type so as to meet the requirement of related operation, wherein the conversion is the implicit conversion of the data type. By implicit conversion of data types, the problem of truncation or overflow caused by the operation between different types of data can be prevented. It can be seen that the design goal of the compiler data type implicit conversion function is to provide more relaxed data type operations, allowing the user to use variables of different data types in computing expressions, assignments, and operations.
The implicit rule of ST (Structured text) language data type specified in IEC61131-3 is that a type with a short byte length is converted into a type with a long byte length, and when SINT type data in ST is calculated with INT type data, the calculation result type is converted into an INT type instead of a SINT type because the byte length of SINT is shorter than that of INT.
The IEC61131-3 specifies that the implicit conversion of the data types can be carried out among the types included in the ANY_INT, wherein the data types included in the ANY_INT are SINT, USINT, INT, UINT, DINT, REAL, UDINT, LINT, LREAL, ULINT; in IEC61131-3, it is also specified that implicit data type conversion can be performed between types included in any_BIT, where any_BIT includes data types BOOL, BYTE, WORD, DWORD, LWORD.
Currently, according to the IEC 61131-3 standard, implicit conversion is allowed between data types included in the ANY_INT major class, and also between data types included in the ANY_BIT major class, but implicit conversion from data types in the ANY_INT major class to data types in the ANY_BIT major class is not allowed. That is, the implicit conversion is limited between data types under the same large class, and the implicit conversion cannot be performed for data types under different large classes.
On the other hand, in the prior art, when determining whether implicit conversion is possible, for example, the expression is sint+int, first, the compiler uses IF-ELSE logic to compare whether the two operands, SINT and INT, are identical; IF the SINT and INT types are the same, the compiler again uses the IF-ELSE logic to check IF the two types support "+" operations; IF the types of SINT and INT are different, the compiler determines whether an implicit conversion rule exists between SINT and INT, IF so, the compiler implicitly converts SINT and INT, and then uses the IF-ELSE logic again to continuously check whether the converted type supports "+" operation; if the SINT and INT types are different and no appropriate implicit conversion rule is found, the compiler will report an error, prompting the user to explicitly make a type conversion.
In the above example, the compiler uses IF-ELSE logic to compare whether the two operands are of the same type and whether implicit conversion is possible. Therefore, this method increases the number of judgments and the time of the comparison operation, reducing the efficiency of the compiler.
Disclosure of Invention
In view of the above problems in the prior art, the application provides a compiling method, a compiler, an implicit conversion method, an implicit conversion device and a computing device, which realize that data types under different large categories can be subjected to implicit conversion, thereby improving the flexibility of data type conversion.
To achieve the above object, a first aspect of the present application provides a compiling method, applied to a compiler, where the compiler is configured with a two-layer hash table, the two-layer hash table describes a conversion relationship between a first data type and a second data type, the method comprising:
determining a first data type and a second data type according to the acquired source code;
Judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
When an implicit conversion rule is satisfied, performing implicit conversion on the first data type and the second data type;
after the implicit conversion is completed, the object code is generated.
By the method, the two-layer hash table is used for storing the conversion rule of the data type, and when the compiler encounters the condition that the first data type and the second data type need to be converted in the compiling process, the implicit conversion rule can be quickly inquired and acquired based on the two-layer hash table, so that the compiling speed is increased, that is, compared with the prior art that the conversion rule needs to be determined by multiple times of comparison, the method reduces the judging times and the time of comparison operation; in addition, as the data types of the first data type and the second data type are different, the implicit conversion of the data types under different large categories is realized, so that the flexibility of data type conversion is improved, and the inconvenience of forcing the same type to perform calculation is avoided.
As a possible implementation manner of the first aspect, the determining a first data type and a second data type according to the acquired source code includes;
Acquiring a source code, and performing lexical and grammatical analysis on the source code to generate a grammar tree; the grammar tree comprises a plurality of nodes, wherein the nodes comprise a first node which is an operator, a second node which is a first operand and a third node which is a second operand, and the data types of the first operand and the second operand are different;
And checking each node in the grammar tree, and respectively determining the data types of the first operand and the second operand as a first data type and a second data type when the first node is checked to be related to the second node and the third node.
By doing lexical and grammatical parsing on the source code, a grammar tree is generated, and the compiler can accurately identify each element in the source code, including operators and operands. And the structure of the grammar tree enables the compiler to conveniently traverse each part of the source code, and the compiler can rapidly determine operands associated with the operators and extract the data types of the operands by checking the node relation in the grammar tree, thereby providing an accurate and efficient basis for the subsequent implicit conversion judgment and being beneficial to improving the performance of the compiler.
As one possible implementation manner of the first aspect, each layer of hash table includes a key-value pair, wherein a key of an outer layer of hash table represents a first data type, a value of the outer layer of hash table points to an inner layer of hash table, a key of the inner layer of hash table represents a second data type, and a value of the inner layer of hash table includes a specific flag bit, and the specific flag bit represents that the second data type is allowed to be implicitly converted into the first data type;
The determining, according to the two-layer hash table, whether the first data type and the second data type satisfy an implicit conversion rule includes:
And taking the data types of the first operand and the second operand as two keys, inquiring the two-layer hash table, and carrying out type conversion on the second operand according to the implicit conversion rule when the data type of the first operand hits a key of the outer-layer hash table, the data type of the second operand hits a key of the inner-layer hash table pointed by the value of the outer-layer hash table and the value of the inner-layer hash table is a specific flag bit.
By using the two-layer hash table structure, the compiler can quickly query the implicit conversion rules based on the first data type and the second data type. The outer hash table is quickly positioned to the corresponding inner hash table according to the first data type, and the inner hash table further judges whether the allowable implicit conversion exists according to the second data type. The query mode has higher efficiency, and significantly reduces the query time, thereby improving the performance of the compiler.
As a possible implementation manner of the first aspect, the specific flag bit is included as a first number, where the first number indicates that the second data type is converted into the first data type, and prompts the user that the current implicit conversion is normal.
By introducing the first digit as an indicator of implicit conversion, clear and definite conversion logic is provided for the compiler, and the compiler can quickly determine and execute necessary data type conversion by directly inquiring the two-layer hash table and using the first digit as a conversion instruction in the compiling process, so that the compiling efficiency is improved. And, through prompting the implicit conversion normal to the user, so that the user can timely know that the current code is being correctly processed, the transparency of the compiler is increased.
As a possible implementation manner of the first aspect, the specific flag bit is included as a second number, where the second number represents that the second data type is converted into the first data type, and prompts a user that there is a precision information loss condition in the current implicit conversion.
By introducing the second number as an indicator of implicit conversion and prompting the user when a conversion that may result in loss of accuracy information occurs, the user using the compiler can be informed of the potential loss of accuracy information and can be assisted in better understanding of the possible problems with the code, while the compiler provides more comprehensive and useful information feedback to the user.
As a possible implementation manner of the first aspect, the specific flag bit is included as a third number, where the third number represents that the second data type is converted into the first data type, and prompts a user that there is a symbol information loss condition in the current implicit conversion.
By introducing the third digit as an indicator of implicit conversion and prompting the user when the conversion possibly causing the loss of the symbol information occurs, the user using the compiler can know the potential loss of the symbol information and can be helped to better understand the possible problems of the code, and meanwhile, the compiler provides more comprehensive and useful information feedback for the user.
To achieve the above object, a second aspect of the present application provides a compiler comprising:
two layers of hash tables, each layer of hash table comprising a key-value pair, wherein a key of an outer layer of hash table represents a first data type, a value of the outer layer of hash table points to an inner layer of hash table, a key of the inner layer of hash table represents a second data type, and a value of the inner layer of hash table comprises a specific flag bit, wherein the specific flag bit represents that the second data type is allowed to be implicitly converted into the first data type;
the determining module is used for determining a first data type and a second data type according to the acquired source codes;
The judging module is used for judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
The conversion module is used for carrying out implicit conversion on the first data type and the second data type when the implicit conversion rule is met;
And the generating module is used for generating the target code after the implicit conversion is completed.
By the method, the compiler uses the two-layer hash table to store the conversion rule of the data type, and when the compiler encounters the condition that the first data type and the second data type need to be converted in the compiling process, the implicit conversion rule can be quickly inquired and acquired based on the two-layer hash table, so that the compiling speed is increased, that is, compared with the prior art that the conversion rule needs to be determined by comparing for a plurality of times, the method reduces the judging times and the comparing operation time; in addition, as the data types of the first data type and the second data type are different, the implicit conversion of the data types under different large categories is realized, so that the flexibility of data type conversion is improved, and the inconvenience of forcing the same type to perform calculation is avoided.
To achieve the above object, a third aspect of the present application provides an implicit conversion method, including two layers of hash tables, each layer of hash table including a key value pair, wherein a key of an outer layer of hash table represents a first data type, a value of the outer layer of hash table points to an inner layer of hash table, a key of the inner layer of hash table represents a second data type, and a value of the inner layer of hash table includes a specific flag bit, the specific flag bit indicating that the second data type is allowed to be implicitly converted into the first data type, and the specific flag bit indicating that the second data type is allowed to be implicitly converted into the first data type; the implicit conversion method comprises the following steps:
Determining a first data type and a second data type;
Judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
and when the implicit conversion rule is met, performing implicit conversion on the first data type and the second data type.
By the method, the two-layer hash table is used for storing the conversion rule of the data type, and when a compiler encounters the condition that the first data type and the second data type need to be converted in the compiling process, the implicit conversion rule can be quickly queried and acquired based on the two-layer hash table, so that the compiling speed is increased, that is, compared with the prior art that the conversion rule needs to be determined by multiple times of comparison, the method reduces the judging times and the comparison operation time; in addition, as the data types of the first data type and the second data type are different, the implicit conversion of the data types under different large categories is realized, so that the flexibility of data type conversion is improved, and the inconvenience of forcing the same type to perform calculation is avoided.
To achieve the above object, a fourth aspect of the present application provides an implicit switching apparatus, including:
two layers of hash tables, each layer of hash table comprising a key-value pair, wherein a key of an outer layer of hash table represents a first data type, a value of the outer layer of hash table points to an inner layer of hash table, a key of the inner layer of hash table represents a second data type, and a value of the inner layer of hash table comprises a specific flag bit, wherein the specific flag bit represents that the second data type is allowed to be implicitly converted into the first data type; the implicit conversion method comprises the following steps:
A determining unit configured to determine a first data type and a second data type;
The judging unit is used for judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
and the conversion unit is used for carrying out implicit conversion on the first data type and the second data type when the implicit conversion rule is met.
By the method, the implicit conversion device is provided, the two-layer hash table is used for storing the conversion rule of the data type, and when a compiler encounters the condition that the first data type and the second data type need to be converted in the compiling process, the implicit conversion rule can be quickly queried and acquired based on the two-layer hash table, so that the compiling speed is increased, that is, compared with the prior art that the conversion rule needs to be determined by comparing for a plurality of times, the method reduces the judging times and the comparing operation time; in addition, as the data types of the first data type and the second data type are different, the implicit conversion of the data types under different large categories is realized, so that the flexibility of data type conversion is improved, and the inconvenience of forcing the same type to perform calculation is avoided.
To achieve the above object, a fifth aspect of the present application provides a computing device, including:
A processor, and
A memory having stored thereon program instructions that, when executed by the processor, cause the processor to perform the compiling method according to any one of the first aspect and the implicit converting method according to the third aspect.
To achieve the above object, a sixth aspect of the present application provides a computer-readable storage medium having stored thereon program instructions which, when executed by a computer, cause the computer to implement the compiling method according to any one of the first aspect and the implicit converting method according to the third aspect.
Drawings
FIG. 1 is a flow chart of main steps of a compiling method according to the present application;
FIG. 2 is a logic flow diagram of a compiler provided by the present application;
FIG. 3 is a schematic diagram of the structure of a syntax tree according to a first embodiment of the present application;
FIG. 4 is a schematic diagram of the structure of an updated syntax tree according to a first embodiment of the present application;
FIG. 5 is a schematic diagram of the syntax tree of a second embodiment provided by the present application;
FIG. 6 is a schematic diagram of an updated syntax tree according to a second embodiment of the present application;
FIG. 7 is a schematic diagram of a syntax tree according to a third embodiment of the present application;
FIG. 8 is a schematic diagram of an updated syntax tree according to a third embodiment of the present application;
FIG. 9 is a schematic diagram of a compiler according to the present application;
FIG. 10 is a flow chart of the main steps of an implicit switching method provided by the present application;
FIG. 11 is a schematic view of a compiling apparatus according to the present application;
FIG. 12 is a schematic structural diagram of a computing device provided by the present application.
It should be understood that in the foregoing structural schematic diagrams, the sizes and forms of the respective block diagrams are for reference only and should not constitute an exclusive interpretation of the embodiments of the present invention. The relative positions and inclusion relationships between the blocks presented by the structural diagrams are merely illustrative of structural relationships between the blocks, and are not limiting of the physical connection of embodiments of the present invention.
Detailed Description
The technical scheme provided by the application is further described below by referring to the accompanying drawings and examples. It should be understood that the system structure and the service scenario provided in the embodiments of the present application are mainly for illustrating possible implementation manners of the technical solutions of the present application, and should not be interpreted as the only limitation to the technical solutions of the present application. As one of ordinary skill in the art can know, with the evolution of the system structure and the appearance of new service scenarios, the technical scheme provided by the application is applicable to similar technical problems.
Unless defined otherwise, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. If there is a discrepancy, the meaning described in the present specification or the meaning obtained from the content described in the present specification is used. In addition, the terminology used herein is for the purpose of describing embodiments of the application only and is not intended to be limiting of the application.
1) The compilation process of a compiler generally comprises the following stages:
Source code lexical analysis: the source code is lexically analyzed and broken down into a series of lexical units or tags, such as keywords, variable names, operators, etc.
Syntax analysis: the parsed lexical elements are combined according to a grammar rule to generate a grammar tree (also called abstract grammar tree), which is an abstract representation of the source code grammar structure, which represents the source code grammar structure in tree form, each node of the tree representing an object in the source code.
Semantic checking: at this stage, the compiler will examine each node in the syntax tree, such as type examination, constant folding, symbol parsing, etc., to complete semantic examination. The purpose of the semantic check is to ensure that the source code is logically correct and that all variables and expressions are of the correct type. Wherein an implicit conversion of the data type occurs at this stage.
Generating an object code: the syntax tree is converted to object code (this conversion may also be referred to as parsing of the syntax tree), which is typically machine language code (i.e. binary code).
In the process of generating the target code by the grammar tree, the partial parser generates intermediate code (the intermediate code is a low-level language which is closer to a machine language than the source code but is still irrelevant to a specific machine) by the grammar tree, and then generates the target code by the intermediate code.
The process from the source code lexical analysis to the semantic check to generate the grammar tree is usually called the execution process of the front end of the compiler, and the process of obtaining the target code according to the grammar tree is usually called the execution process of the back end of the compiler.
2) Implicit conversion of data types: in the process of compiling the source code, if operations such as operation, comparison or assignment are detected among different types of data in a semantic checking stage, the compiler can automatically and forcedly convert one data type into the other data type so as to meet the operation requirement. Since the conversion process is not seen, this conversion is also referred to as implicit conversion of the data type.
3) Key value pairs: it is composed of two parts: a Key (Key) and a Value (Value), the Key being an element that uniquely identifies data, and the Value corresponding thereto being specific information or data. Key-value pairs are a way of storing keys paired with corresponding values, from which specific keys can be found and accessed quickly.
According to the IEC 61131-3 standard, implicit conversion is allowed between data types in any_int and any_bit, but not from a type in any_int to a type in any_bit. This is because the nature and purpose of these two data types are different.
The any_int type is used primarily to represent integer values, including signed and unsigned integers, and double-precision integers, among others. These types are commonly used in situations requiring numerical representations for mathematical calculations, logic control, and the like. Where signed integers may represent positive numbers, negative numbers, and zeros. In computers, signed integers are typically represented in two's complement form. The most significant bit (referred to as the sign bit) is used to represent the sign of the number and the remaining bits are used to represent the magnitude of the value. Unsigned integers can only represent positive numbers and zero, and cannot represent negative numbers. In computers, unsigned integers are typically represented using a two's original or two's complement form. Since there are no sign bits, all bits are used to represent the size of the value.
The any_bit type is mainly used to represent boolean values, i.e. TRUE (TRUE) or FALSE (FALSE). These types are commonly used where a boolean representation is required for logic control, bit manipulation, etc.
Because of the different nature and purpose of these two data types, implicit conversion from the type in any_int to the type in any_bit is not allowed.
However, the SINT type BYTE length in ANY_INT is the same as the BYTE type BYTE length in ANY_BIT, and both SINT type and BYTE type are 1 BYTE long, which has the possibility of implicit conversion. If implicit conversion is allowed between the any_int type and the any_bit type, then more flexibility in the use of data types can be provided to the compiler user, avoiding the inconvenience of forcing the same type to perform computations.
The embodiment of the application provides a compiling method, which is applied to a compiler, wherein the compiler is configured with a two-layer hash table, the two-layer hash table describes the conversion relation between a first data type and a second data type, as shown in fig. 1, and the method comprises the following steps:
s101, determining a first data type and a second data type according to an acquired source code;
s102, judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
S103, when an implicit conversion rule is met, performing implicit conversion on the first data type and the second data type;
s104, after the implicit conversion is completed, generating an object code.
Therefore, the application provides a compiling method, which uses two layers of hash tables to store the conversion rule of the data types, when a compiler encounters the condition that the first data type and the second data type need to be converted in the compiling process, the implicit conversion rule can be quickly inquired and acquired based on the two layers of hash tables, so that the compiling speed is increased, that is, compared with the prior art, the method has the advantages that the conversion rule is determined by comparing for a plurality of times, and the judging times and the comparing operation time are reduced; in addition, as the data types of the first data type and the second data type are different, the implicit conversion of the data types under different large categories is realized, so that the flexibility of data type conversion is improved, and the inconvenience of forcing the same type to perform calculation is avoided.
In some embodiments, the determining the first data type and the second data type based on the acquired source code includes;
Acquiring a source code, and performing lexical and grammatical analysis on the source code to generate a grammar tree; the grammar tree comprises a plurality of nodes, wherein the nodes comprise a first node which is an operator, a second node which is a first operand and a third node which is a second operand, and the data types of the first operand and the second operand are different;
And checking each node in the grammar tree, and respectively determining the data types of the first operand and the second operand as a first data type and a second data type when the first node is checked to be related to the second node and the third node.
Thus, by lexical and grammatical parsing of the source code, a grammar tree is generated, and a compiler can accurately identify individual elements in the source code, including operators and operands. And the structure of the grammar tree enables the compiler to conveniently traverse each part of the source code, and the compiler can rapidly determine operands associated with the operators and extract the data types of the operands by checking the node relation in the grammar tree, thereby providing an accurate and efficient basis for the subsequent implicit conversion judgment and being beneficial to improving the performance of the compiler.
In some embodiments, each layer of hash table includes a pair of key values, wherein a key of an outer layer of hash table represents a first data type, a value of the outer layer of hash table points to an inner layer of hash table, a key of the inner layer of hash table represents a second data type, and a value of the inner layer of hash table includes a specific flag bit that indicates that implicit conversion of the second data type to the first data type is allowed;
The determining, according to the two-layer hash table, whether the first data type and the second data type satisfy an implicit conversion rule includes:
And taking the data types of the first operand and the second operand as two keys, inquiring the two-layer hash table, and carrying out type conversion on the second operand according to the implicit conversion rule when the data type of the first operand hits a key of the outer-layer hash table, the data type of the second operand hits a key of the inner-layer hash table pointed by the value of the outer-layer hash table and the value of the inner-layer hash table is a specific flag bit.
Example 1, the structure of the two-layer hash table is { key: INT, value: { bond: SINT, value: specific flag bit } is illustrated as an example. Wherein a particular flag bit indicates that implicit conversion of the second data type into the first data type is allowed.
Specifically, determining that the data type of the first operand is INT and the data type of the second operand is SINT; then taking the data type of the first operand and the data type of the second operand as two keys, and inquiring a two-layer hash table; when the data type of the first operand hits a key of an outer hash table, the data type of the second operand hits a key of an inner hash table pointed by a value of the outer hash table, and the value of the inner hash table is a specific flag bit, at this time, the data type SINT of the second operand is converted into the data type INT of the first operand, so that implicit conversion between different data types is realized.
In this manner, by using a two-layer hash table structure, the compiler can quickly query the implicit conversion rules based on the first data type and the second data type. The outer hash table is quickly positioned to the corresponding inner hash table according to the first data type, and the inner hash table further judges whether the allowable implicit conversion exists according to the second data type. The query mode has higher efficiency, and significantly reduces the query time, thereby improving the performance of the compiler.
In some embodiments, the specific flag bit is included as a first number that indicates that the second data type is converted to the first data type and prompts the user that the current implicit conversion is normal.
Example 2, the structure of the two-layer hash table is { key: INT, value: { bond: SINT, value: 1} is illustrated as an example. The specific flag bit is a first number, the first number is specifically 1, which indicates that the second data type is allowed to be implicitly converted into the first data type, and the user is prompted that the current implicit conversion is normal.
Specifically, determining that the data type of the first operand is INT and the data type of the second operand is SINT; then taking the data type of the first operand and the data type of the second operand as two keys, and inquiring a two-layer hash table; when the data type of the first operand hits a key of an outer hash table, the data type of the second operand hits a key of an inner hash table pointed by a value of the outer hash table, and the value of the inner hash table is a first number (i.e. 1), at this time, the data type SINT of the second operand is converted into the data type INT of the first operand, and the current implicit conversion of the user is prompted to be normal.
Notably, the manner in which the user is prompted may include, but is not limited to: a message box can be displayed on a display interface of the compiler to inform the user that the implicit conversion has occurred; alternatively, the compiler may add a message to its output log indicating that some implicit conversion has been successfully performed, informing the user that the implicit conversion has occurred.
In practical application, the current mode for prompting the user to perform implicit conversion is not limited to the two modes, and other modes can be adopted, so that the requirements of different scenes are met and the flexibility of design is improved.
Therefore, by introducing the first digit as an indicator of implicit conversion, clear and definite conversion logic is provided for the compiler, and in the compiling process, the compiler can quickly determine and execute necessary data type conversion by directly inquiring the two-layer hash table and using the first digit as a conversion instruction, so that the compiling efficiency is improved. And, through prompting the implicit conversion normal to the user, so that the user can timely know that the current code is being correctly processed, the transparency of the compiler is increased.
In some embodiments, the specific flag bit is included as a second number, the second number representing converting the second data type to the first data type and prompting a user that there is a loss of precision information for the current implicit conversion.
In practical application, the loss of precision information caused by implicit conversion includes: DWORD type converts to REAL type, DINT type converts to REAL type, UDINT converts to REAL type, LWORD, LINT, ULINT converts to REAL, LREAL type, etc.
Example 3, the structure of the two-layer hash table is { key: read, value: { bond: DWORD, value: 2} is illustrated as an example. The specific flag bit is a second number, and the second number is specifically 2, which indicates that the second data type is allowed to be implicitly converted into the first data type, and prompts the user that the current implicit conversion has the condition of losing precision information.
Specifically, determining that the data type of the first operand is REAL and the data type of the second operand is DWORD; then taking the data type of the first operand and the data type of the second operand as two keys, and inquiring a two-layer hash table; when the data type of the first operand hits a key of an outer hash table, the data type of the second operand hits a key of an inner hash table pointed by a value of the outer hash table, and the value of the inner hash table is the second number (i.e. 2), at this time, converting the data type DWORD of the second operand into the data type REAL of the first operand, and prompting the user that the current implicit conversion has the loss of precision information.
Thus, the application introduces the second number as an indicator of implicit conversion and prompts the user when the conversion possibly causing the loss of precision information occurs, so that the user using the compiler can know the potential loss of precision information and can be helped to better understand the possible problems of the code, and meanwhile, the compiler provides more comprehensive and useful information feedback for the user.
In some embodiments, the specific flag bit is included as a third number, the third number representing converting the second data type to the first data type and prompting a user that there is a sign information loss condition for the current implicit conversion.
In practical applications, the symbol loss caused by implicit conversion includes: INT is converted to DWORD, UDINTL, WORD, ULINT, BYTE, implicit between USINT and SINT, implicit between WORD, UINT and INT, implicit between DWOR, DUDINT and DINT, between LWORD, ULIT and LINT, SINT is converted to WORD, UINT, DWORD, UDINT, LWORD, ULIT, INT is converted to DWORD, UDINTL, WORD, ULIT, DINT is converted to LWORD, ULIT.
Example 4, the structure of the two-layer hash table is { key: UDINTL, value: { bond: INT, value: 3} is illustrated as an example. The specific flag bit is a third number, and the third number is specifically 3, which indicates that the second data type is allowed to be implicitly converted into the first data type, and prompts the user that the current implicit conversion has a symbol information loss condition.
Specifically, determining that the data type of the first operand is UDINTL and the data type of the second operand is INT; then taking the data type of the first operand and the data type of the second operand as two keys, and inquiring a two-layer hash table; when the data type of the first operand hits a key of an outer hash table, the data type of the second operand hits a key of an inner hash table pointed to by a value of the outer hash table, and the value of the inner hash table is a third number (i.e., 3), at this time, the data type INT of the second operand is converted into the data type UDINTL of the first operand, and the user is prompted that the current implicit conversion has a sign information loss condition.
In this way, the present application introduces the third digit as an indicator of the implicit conversion and prompts the user when the conversion possibly causing the loss of the symbol information occurs, so that the user using the compiler can know the potential loss of the symbol information and can be assisted to better understand the possible problems of the code, and at the same time, the compiler provides more comprehensive and useful information feedback for the user.
In some embodiments, the particular flag bit is included as a particular number that indicates that the second data type cannot be converted to the first data type and prompts the user that implicit conversion is currently not possible.
Example 5, the structure of the two-layer hash table is { key: BYTE, value: { bond: INT, value: 0 is illustrated as an example. The specific flag bit is a specific number, and the specific number is specifically 0, which indicates that the second data type cannot be converted into the first data type, and prompts the user that the implicit conversion cannot be performed currently.
Specifically, determining that the data type of the first operand is BYTE and the data type of the second operand is USINT; then taking the data type of the first operand and the data type of the second operand as two keys, and inquiring a two-layer hash table; specifically, when the data type of the first operand hits a key of an outer hash table, the data type of the second operand hits a key of an inner hash table to which the value of the outer hash table points, and the value of the inner hash table is a specific number (i.e., 0), at this time, the data type INT of the first operand cannot be converted into the data type BYTE of the second operand, and the user is prompted that the implicit conversion cannot be currently performed.
In this way, when the compiler detects that the data types of two operands cannot be implicitly converted, the user can be explicitly told that such conversion is not possible by using a specific number as an indicator, so that the compiler is prevented from attempting to perform conversion which may cause a runtime error, thereby improving the robustness of the code.
To more clearly illustrate the above compiling method, the present application provides three embodiments that can be combined with the logic flow diagram of the compiler shown in fig. 2.
First embodiment: the structure of the two-layer hash table configured on the compiler is { key: INT, value: { bond: SINT, value: 1 }. For example, wherein the specific flag bit is 1, indicating that the second data type is allowed to be converted into the first data type, and prompting the user that the current implicit conversion is normal.
The method is applied to a compiler and comprises the following steps:
Step one, acquiring source codes of ST language, wherein the source codes of ST language at least comprise one expression containing different data types;
Wherein, the expression is ASINT +AINT, which can be understood as ASINT and AINT perform addition operation.
Secondly, performing lexical and grammatical analysis on the source codes of the ST language to obtain a grammar tree for representing the source codes of the ST language;
The structure of the syntax tree may be shown in fig. 3, where the syntax tree includes three nodes, and the three nodes include a first node that is an operator, a second node that is a first operand, and a third node that is a second operand.
Step three, checking each node in the grammar tree;
As shown in the above figure, in this step, the nodes of the first operand, the second operand, and the operator are specifically detected, where the data type of the operand ain is INT, the data type of the operand ASINT is SINT, and the operator is "+".
Step four, when the first node is checked to be related to the second node and the third node, taking the data types of the first operand and the second operand as two keys, and inquiring a two-layer hash table;
it should be noted that, checking that the first node associates the second node with the third node may be understood as: it is checked that there is some association between the first node, the second node and the third node, for example, such association may be, but is not limited to, an addition operation performed for the second node and the third node (where the "+" operator is the first node), a multiplication operation performed for the second node and the third node (where the "x" operator is the first node), and so on.
Specifically, when the data type of the first operand hits a key of an outer hash table, the data type of the second operand hits a key of an inner hash table pointed by a value of the outer hash table, and the value of the inner hash table is 1, the second operand is subjected to type conversion according to a rule of implicit conversion, that is, INT can be found in the outer hash table, SINT can be found in the inner hash table, and a value corresponding to SINT is determined to be 1, so that the data type of the second operand can be converted into the data type INT of the first operand, and a user can be prompted that the current implicit conversion is normal.
Step five, after the implicit conversion is completed, the structure of the grammar tree is changed, and an updated grammar tree is obtained;
the structure of the updated syntax tree is shown in fig. 4.
And step six, analyzing the updated grammar tree, generating an object code, and ending the compiling.
Second embodiment: the structure of the two-layer hash table configured on the compiler is { key: INT, value: { bond: SINT, value: 1 }. For example, wherein the specific flag bit is 1, indicating that the second data type is allowed to be converted into the first data type, and prompting the user that the current implicit conversion is normal.
The method is applied to a compiler and comprises the following steps:
Step one, acquiring source codes of ST language, wherein the source codes of ST language at least comprise one expression containing different data types;
Wherein, the expression AINT: = ASINT +AINT; it is understood that ASINT performs an addition operation with ain and assigns the result of the addition operation to ain.
Secondly, performing lexical and grammatical analysis on the source codes of the ST language to obtain a grammar tree for representing the source codes of the ST language;
The structure of the syntax tree is shown in fig. 5.
Step three, checking the data types of all nodes in the grammar tree;
As shown in the above figure, in this step, the nodes of the first operand, the second operand and the operator are specifically detected, where the data type of the operand AINT is INT, the data type of the operand ASINT is SINT, and the operators are "+", ": =";
Step four, when the first node is checked to be related to the second node and the third node, taking the data types of the first operand and the second operand as two keys, and inquiring a two-layer hash table;
it should be noted that, checking that the first node associates the second node with the third node may be understood as: it is checked that there is some association between the first node, the second node and the third node, for example, such association may be, but is not limited to, an addition operation performed for the second node and the third node (where the "+" operator is the first node), a multiplication operation performed for the second node and the third node (where the "x" operator is the first node), and so on.
Specifically, when the data type of the first operand hits a key of an outer hash table, the data type of the second operand hits a key of an inner hash table pointed by a value of the outer hash table, and the value of the inner hash table is 1, the second operand is subjected to type conversion according to a rule of implicit conversion, that is, INT can be found in the outer hash table, SINT can be found in the inner hash table, and a value corresponding to SINT is determined to be 1, so that the data type of the second operand can be converted into the data type INT of the first operand, and a user can be prompted that the current implicit conversion is normal.
Step five, after the implicit conversion is completed, assignment operation between the data of the same type exists, and the compiler triggers no implicit conversion because the data of the same type is used, at the moment, the structure of the grammar tree is changed, and an updated grammar tree is obtained;
The structure of the updated syntax tree is shown in fig. 6.
And step six, analyzing the updated grammar tree, generating an object code, and ending the compiling.
Third embodiment: in this embodiment, the structure of the two-layer hash table configured on the compiler is { key: INT, value: { bond: SINT, value: 1}, { key: LWORD, value: { bond: INT, value: 1}, as an example.
The method is applied to a compiler and comprises the following steps:
Step one, acquiring source codes of ST language, wherein the source codes of ST language at least comprise one expression containing different data types;
wherein, expression ALWORD = ASINT +AINT; it is understood that ASINT and ain perform an addition operation and assign the result of the addition operation to ALWORD.
Secondly, performing lexical and grammatical analysis on the source codes of the ST language to obtain a grammar tree for representing the source codes of the ST language;
The structure of the syntax tree is shown in fig. 7.
Step three, checking the data types of all nodes in the grammar tree;
as shown in the above figure, in this step, the nodes of the first operand, the second operand and the operator are specifically detected, where the data type of the operand ain is INT, the data type of the operand ASINT is SINT, the data type of ALWORD is LWORD, and the operators are "+", ": =";
Step four, when the first node is checked to be related to the second node and the third node, taking the data types of the first operand and the second operand as two keys, and inquiring a two-layer hash table;
Specifically, when the data type of the first operand hits a key of an outer hash table, the data type of the second operand hits a key of an inner hash table pointed by a value of the outer hash table, and the value of the inner hash table is 1, the second operand is subjected to type conversion according to a rule of implicit conversion, that is, INT can be found in the outer hash table, SINT can be found in the inner hash table, and it is determined that the value corresponding to SINT is 1, so that the data type of the second operand can be converted into the data type INT of the first operand.
Fifthly, after the implicit conversion is completed, assignment operation between different types of data exists, so that the compiler triggers the implicit conversion again;
Step six, when the fifth node and the sixth node associated with the fourth node are detected, taking the data types of the third operand and the fourth operand as two keys, and inquiring a two-layer hash table;
Specifically, when the data type of the third operand hits a key of an outer hash table, the data type of the fourth operand hits a key of an inner hash table pointed by a value of the outer hash table, and the value of the inner hash table is 1, the third operand is subjected to type conversion according to a rule of implicit conversion, that is, LWORD can be found in the outer hash table, INT can be found in the inner hash table, and a value corresponding to INT is determined to be 1, so that the data type of the third operand can be converted into the data type LWORD of the fourth operand, and therefore INT can be converted into LWORD, the structure of the syntax tree is changed, and an updated syntax tree is obtained;
the structure of the updated syntax tree is shown in fig. 8.
And step seven, analyzing the updated grammar tree, generating an object code, and ending the compiling.
Fig. 9 is a schematic structural diagram of a compiler according to an embodiment of the present application, where a compiler 900 includes:
Two layers of hash tables 901, each layer of hash tables comprising a key-value pair, wherein a key of an outer layer of hash tables represents a first data type, a value of the outer layer of hash tables points to an inner layer of hash tables, a key of the inner layer of hash tables represents a second data type, and a value of the inner layer of hash tables comprises a specific flag bit, wherein the specific flag bit represents that the second data type is allowed to be implicitly converted into the first data type;
A determining module 902, configured to determine a first data type and a second data type according to the acquired source code;
a judging module 903, configured to judge, according to the two-layer hash table, whether the first data type and the second data type satisfy an implicit conversion rule;
a conversion module 904, configured to implicitly convert the first data type and the second data type when an implicit conversion rule is satisfied;
The generating module 905 is configured to generate the object code after the implicit conversion is completed.
Therefore, the application provides a compiler, which uses two layers of hash tables to store the conversion rule of the data types, when the compiler encounters the condition that the first data type and the second data type need to be converted in the compiling process, the implicit conversion rule can be quickly inquired and acquired based on the two layers of hash tables, so that the compiling speed is increased, that is, compared with the prior art, the method and the device need to compare for multiple times to determine the conversion rule, and the judging times and the comparing operation time are reduced; in addition, as the data types of the first data type and the second data type are different, the implicit conversion of the data types under different large categories is realized, so that the flexibility of data type conversion is improved, and the inconvenience of forcing the same type to perform calculation is avoided.
The embodiment of the application provides an implicit conversion method, as shown in fig. 10, comprising two layers of hash tables, wherein each layer of hash table comprises a key value pair, wherein keys of an outer layer of hash table represent a first data type, values of the outer layer of hash tables point to an inner layer of hash table, keys of the inner layer of hash table represent a second data type, and values of the inner layer of hash table comprise specific flag bits, wherein the specific flag bits represent that the second data type is allowed to be implicitly converted into the first data type, and the specific flag bits represent that the second data type is allowed to be implicitly converted into the first data type; the implicit conversion method comprises the following steps:
S1001, determining a first data type and a second data type;
S1002, judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
S1003, when an implicit conversion rule is met, performing implicit conversion on the first data type and the second data type.
Therefore, the application provides an implicit conversion method, which uses a two-layer hash table to store the conversion rule of the data type, when a compiler encounters the condition that the first data type and the second data type need to be converted in the compiling process, the implicit conversion rule can be quickly inquired and acquired based on the two-layer hash table, so that the compiling speed is increased, that is, compared with the prior art, the method has the advantages that the conversion rule is determined by comparing for a plurality of times, and the judging times and the comparing operation time are reduced; in addition, as the data types of the first data type and the second data type are different, the implicit conversion of the data types under different large categories is realized, so that the flexibility of data type conversion is improved, and the inconvenience of forcing the same type to perform calculation is avoided.
Fig. 11 is a schematic structural diagram of an implicit conversion apparatus according to an embodiment of the present application, where an implicit conversion apparatus 1100 includes:
Two layers of hash tables 1101, each layer of hash tables comprising a pair of key values, wherein a key of an outer layer of hash tables represents a first data type, a value of the outer layer of hash tables points to an inner layer of hash tables, a key of the inner layer of hash tables represents a second data type, and a value of the inner layer of hash tables comprises a specific flag bit, the specific flag bit indicating that implicit conversion of the second data type into the first data type is allowed; the implicit conversion method comprises the following steps:
A determining unit 1102, configured to determine a first data type and a second data type;
a judging unit 1103, configured to judge whether the first data type and the second data type satisfy an implicit conversion rule according to the two-layer hash table;
A conversion unit 1104, configured to implicitly convert the first data type and the second data type when an implicit conversion rule is satisfied.
Therefore, the application provides an implicit conversion device, which uses a two-layer hash table to store the conversion rule of the data type, when a compiler encounters the condition that the first data type and the second data type need to be converted in the compiling process, the implicit conversion rule can be quickly inquired and acquired based on the two-layer hash table, so that the compiling speed is increased, that is, compared with the prior art, the conversion rule is determined by comparing for a plurality of times, the application reduces the judging times and the comparing operation time; in addition, as the data types of the first data type and the second data type are different, the implicit conversion of the data types under different large categories is realized, so that the flexibility of data type conversion is improved, and the inconvenience of forcing the same type to perform calculation is avoided.
Fig. 12 is a schematic structural diagram of a computing device 1200 provided by an embodiment of the application. The computing device performs the compiling method and the implicit converting method described above, and as shown in fig. 12, the computing device 1200 includes: processor 1210, memory 1220, and communication interface 1230.
It should be appreciated that the communication interface 1230 in the computing device 1200 shown in fig. 12 may be used to communicate with other devices, and in particular may include one or more transceiver circuits or interface circuits.
The processor 1210 may be coupled to a memory 1220. The memory 1220 may be used to store the program codes and data. Accordingly, the memory 1220 may be a storage unit inside the processor 1210, an external storage unit independent of the processor 1210, or a component including a storage unit inside the processor 1210 and an external storage unit independent of the processor 1210.
Optionally, the computing device 1200 may also include a bus. The memory 1220 and the communication interface 830 may be connected to the processor 1210 through a bus. The bus may be a peripheral component interconnect standard (PERIPHERAL COMPONENT INTERCONNECT, PCI) bus, or an extended industry standard architecture (Extended Industry Standard Architecture, EISA) bus, or the like. The buses may be classified as address buses, data buses, control buses, etc. For ease of illustration, an unbiased line is shown in FIG. 8, but does not represent only one bus or one type of bus.
It should be appreciated that in embodiments of the present application, the processor 1210 may employ a central processing unit (central processing unit, CPU). The processor may also be other general purpose processors, digital Signal Processors (DSP), application SPECIFIC INTEGRATED Circuits (ASIC), off-the-shelf programmable gate arrays (field programmable GATE ARRAY, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. Or the processor 1210 may employ one or more integrated circuits for executing associated programs to carry out the techniques provided by embodiments of the present application.
The memory 1220 may include read only memory and random access memory, and provides instructions and data to the processor 1210. A portion of processor 1210 may also include nonvolatile random access memory. For example, processor 1210 may also store information of a device type.
When the computing device 1200 is running, the processor 1210 executes computer-executable instructions in the memory 1220 to perform any of the operational steps of the methods described above, as well as any alternative embodiment thereof.
It should be understood that the computing device 1200 according to the embodiments of the present application may correspond to a respective subject performing the methods according to the embodiments of the present application, and that the above and other operations and/or functions of the respective modules in the computing device 1200 are respectively for implementing the respective flows of the methods according to the embodiments, and are not described herein for brevity.
Those of ordinary skill in the art will appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the solution. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.
It will be clear to those skilled in the art that, for convenience and brevity of description, specific working procedures of the above-described systems, apparatuses and units may refer to corresponding procedures in the foregoing method embodiments, and are not repeated herein.
In the several embodiments provided by the present application, it should be understood that the disclosed systems, devices, and methods may be implemented in other manners. For example, the apparatus embodiments described above are merely illustrative, e.g., the division of the units is merely a logical function division, and there may be additional divisions when actually implemented, e.g., multiple units or components may be combined or integrated into another system, or some features may be omitted or not performed. Alternatively, the coupling or direct coupling or communication connection shown or discussed with each other may be an indirect coupling or communication connection via some interfaces, devices or units, which may be in electrical, mechanical or other form.
The units described as separate units may or may not be physically separate, and units shown as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
In addition, each functional unit in the embodiments of the present application may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit.
The functions, if implemented in the form of software functional units and sold or used as a stand-alone product, may be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application may be embodied essentially or in a part contributing to the prior art or in a part of the technical solution, in the form of a software product stored in a storage medium, comprising several instructions for causing a computer device (which may be a personal computer, a server, a network device, etc.) to perform all or part of the steps of the method according to the embodiments of the present application. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory (Random Access Memory, RAM), a magnetic disk, or an optical disk, or other various media capable of storing program codes.
The embodiments of the present application also provide a computer-readable storage medium having stored thereon a computer program for executing the above-described method when executed by a processor, the method comprising at least one of the aspects described in the respective embodiments above.
The computer storage media of embodiments of the application may take the form of any combination of one or more computer-readable media. The computer readable medium may be a computer readable signal medium or a computer readable storage medium. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples (a non-exhaustive list) of the computer-readable storage medium would include the following: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device.
The computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, either in baseband or as part of a carrier wave. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device.
Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Computer program code for carrying out operations of the present application may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, smalltalk, C ++ and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any kind of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external computer (for example, through the Internet using an Internet service provider).
In addition, the terms "first, second, third, etc." or module a, module B, module C, etc. in the description and the claims are used merely to distinguish similar objects from a specific ordering of the objects, it being understood that the specific order or sequence may be interchanged if allowed to enable embodiments of the application described herein to be practiced otherwise than as illustrated or described.
In the above description, reference numerals indicating steps such as S110, S120 … …, etc. do not necessarily indicate that the steps are performed in this order, and the order of the steps may be interchanged or performed simultaneously as the case may be.
The term "comprising" as used in the description and claims should not be interpreted as being limited to what is listed thereafter; it does not exclude other elements or steps. Thus, it should be interpreted as specifying the presence of the stated features, integers, steps or components as referred to, but does not preclude the presence or addition of one or more other features, integers, steps or components, or groups thereof. Thus, the expression "a device comprising means a and B" should not be limited to a device consisting of only components a and B.
Reference in the specification to "one embodiment" or "an embodiment" means that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one embodiment of the application. Thus, appearances of the phrases "in one embodiment" or "in an embodiment" in various places throughout this specification are not necessarily all referring to the same embodiment, but may. Furthermore, the particular features, structures, or characteristics may be combined in any suitable manner in one or more embodiments as would be apparent to one of ordinary skill in the art from this disclosure.
Note that the above is only a preferred embodiment of the present application and the technical principle applied. It will be understood by those skilled in the art that the present application is not limited to the particular embodiments described herein, but is capable of various obvious changes, rearrangements and substitutions as will now become apparent to those skilled in the art without departing from the scope of the application. Therefore, while the application has been described in connection with the above embodiments, the application is not limited to the above embodiments, but may include many other equivalent embodiments without departing from the spirit of the application, which fall within the scope of the application.
Claims (10)
1. A compiling method, applied to a compiler, wherein the compiler is configured with a two-layer hash table, and the two-layer hash table describes a conversion relationship between a first data type and a second data type, the method comprising:
determining a first data type and a second data type according to the acquired source code;
Judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
When an implicit conversion rule is satisfied, performing implicit conversion on the first data type and the second data type;
after the implicit conversion is completed, the object code is generated.
2. The compiling method of claim 1, wherein the determining the first data type and the second data type based on the acquired source code comprises;
Acquiring a source code, and performing lexical and grammatical analysis on the source code to generate a grammar tree; the grammar tree comprises a plurality of nodes, wherein the nodes comprise a first node which is an operator, a second node which is a first operand and a third node which is a second operand, and the data types of the first operand and the second operand are different;
And checking each node in the grammar tree, and respectively determining the data types of the first operand and the second operand as a first data type and a second data type when the first node is checked to be related to the second node and the third node.
3. The compilation method of claim 1, wherein each layer of hash tables comprises a pair of key values, wherein keys of an outer layer of hash tables represent a first data type, wherein values of the outer layer of hash tables point to an inner layer of hash tables, wherein keys of the inner layer of hash tables represent a second data type, wherein values of the inner layer of hash tables comprise a specific flag bit that indicates that implicit conversion of the second data type into the first data type is permitted;
The determining, according to the two-layer hash table, whether the first data type and the second data type satisfy an implicit conversion rule includes:
And taking the data types of the first operand and the second operand as two keys, inquiring the two-layer hash table, and carrying out type conversion on the second operand according to the implicit conversion rule when the data type of the first operand hits a key of the outer-layer hash table, the data type of the second operand hits a key of the inner-layer hash table pointed by the value of the outer-layer hash table and the value of the inner-layer hash table is a specific flag bit.
4. The compiling method of claim 3, wherein the specific flag bit is a first number, the first number representing that the second data type is converted into the first data type, and prompting a user that the current implicit conversion is normal.
5. A compiling method according to claim 3, wherein the specific flag bit is a second number, the second number representing that the second data type is converted into the first data type, and prompting a user that there is a loss of precision information in the current implicit conversion.
6. A compiling method according to claim 3, wherein the specific flag bit is a third digit, the third digit representing that the second data type is converted into the first data type, and prompting a user that a symbol information loss condition exists in the current implicit conversion.
7. A compiler, comprising:
two layers of hash tables, each layer of hash table comprising a key-value pair, wherein a key of an outer layer of hash table represents a first data type, a value of the outer layer of hash table points to an inner layer of hash table, a key of the inner layer of hash table represents a second data type, and a value of the inner layer of hash table comprises a specific flag bit, wherein the specific flag bit represents that the second data type is allowed to be implicitly converted into the first data type;
the determining module is used for determining a first data type and a second data type according to the acquired source codes;
The judging module is used for judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
The conversion module is used for carrying out implicit conversion on the first data type and the second data type when the implicit conversion rule is met;
And the generating module is used for generating the target code after the implicit conversion is completed.
8. An implicit conversion method, comprising two layers of hash tables, each layer of hash table comprising a key value pair, wherein keys of an outer layer of hash tables represent a first data type, values of the outer layer of hash tables point to an inner layer of hash tables, keys of the inner layer of hash tables represent a second data type, and values of the inner layer of hash tables comprise specific flag bits, wherein the specific flag bits represent that the second data type is allowed to be implicitly converted into the first data type, and the specific flag bits represent that the second data type is allowed to be implicitly converted into the first data type; the implicit conversion method comprises the following steps:
Determining a first data type and a second data type;
Judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
and when the implicit conversion rule is met, performing implicit conversion on the first data type and the second data type.
9. An implicit switching apparatus, comprising:
two layers of hash tables, each layer of hash table comprising a key-value pair, wherein a key of an outer layer of hash table represents a first data type, a value of the outer layer of hash table points to an inner layer of hash table, a key of the inner layer of hash table represents a second data type, and a value of the inner layer of hash table comprises a specific flag bit, wherein the specific flag bit represents that the second data type is allowed to be implicitly converted into the first data type; the implicit conversion method comprises the following steps:
A determining unit configured to determine a first data type and a second data type;
The judging unit is used for judging whether the first data type and the second data type meet an implicit conversion rule according to the two-layer hash table;
and the conversion unit is used for carrying out implicit conversion on the first data type and the second data type when the implicit conversion rule is met.
10. A computing device, comprising:
A processor, and
A memory having stored thereon program instructions that, when executed by the processor, cause the processor to perform the compiling method of any one of claims 1 to 6 and the implicit conversion method of claim 8.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202410352886.XA CN118069150A (en) | 2024-03-26 | 2024-03-26 | Compiling method, compiler and implicit conversion method and device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202410352886.XA CN118069150A (en) | 2024-03-26 | 2024-03-26 | Compiling method, compiler and implicit conversion method and device |
Publications (1)
Publication Number | Publication Date |
---|---|
CN118069150A true CN118069150A (en) | 2024-05-24 |
Family
ID=91099193
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202410352886.XA Pending CN118069150A (en) | 2024-03-26 | 2024-03-26 | Compiling method, compiler and implicit conversion method and device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN118069150A (en) |
-
2024
- 2024-03-26 CN CN202410352886.XA patent/CN118069150A/en active Pending
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US7389499B2 (en) | Method and apparatus for automatically converting numeric data to a processor efficient format for performing arithmetic operations | |
US8370822B2 (en) | Compiling techniques for providing limited accuracy and enhanced performance granularity | |
US9122540B2 (en) | Transformation of computer programs and eliminating errors | |
CN107644323A (en) | A kind of intelligent checks system of service-oriented stream | |
JP2005141410A (en) | Compiler apparatus and compile method | |
CN117193781B (en) | A method and device for constructing an abstract syntax tree for SIMSCRIPT language | |
Uhl et al. | An attribute grammar for the semantic analysis of Ada | |
CN112306493A (en) | Hot repair patch generation method and device, storage medium and computer equipment | |
CN112231068A (en) | Custom logic method, system and readable medium using domain specific language | |
CN110737431A (en) | Software development method, development platform, terminal device and storage medium | |
Prinz et al. | C in a Nutshell | |
CN112416313B (en) | Compiling method supporting large integer data type and operator | |
CN113900657A (en) | Method for reading data rule, electronic device and storage medium | |
CN112948400A (en) | Database management method, database management device and terminal equipment | |
CN118069150A (en) | Compiling method, compiler and implicit conversion method and device | |
CN115345155B (en) | Expression evaluation method during SV running | |
CN113835688B (en) | Object packaging method of scientific computing language interpreter | |
US20220197617A1 (en) | Idiomatic Source Code Generation | |
Nakasho | Development of a flexible Mizar tokenizer and parser for information retrieval system | |
US12101388B2 (en) | Universal binary specification model | |
CN114416050B (en) | Swift code processing method, device, electronic device and storage medium | |
Waite | An executable language definition | |
Jain et al. | Computer Science With Python Language Made Simple | |
Bhimavarapu et al. | Learning Professional Python: Volume 1: The Basics | |
Shehadeh | A Low Level Language with Precise Integer Types |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |