CN112947909A - Reconstruction method and device of dynamic programming language - Google Patents

Reconstruction method and device of dynamic programming language Download PDF

Info

Publication number
CN112947909A
CN112947909A CN202110299206.9A CN202110299206A CN112947909A CN 112947909 A CN112947909 A CN 112947909A CN 202110299206 A CN202110299206 A CN 202110299206A CN 112947909 A CN112947909 A CN 112947909A
Authority
CN
China
Prior art keywords
programming language
dynamic programming
type
grammar
syntax
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
Application number
CN202110299206.9A
Other languages
Chinese (zh)
Inventor
李扬
王宁
肖玉配
屈宏远
李昌志
张嘉欢
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Changting Future Technology Co ltd
Original Assignee
Beijing Changting Future Technology Co ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Beijing Changting Future Technology Co ltd filed Critical Beijing Changting Future Technology Co ltd
Priority to CN202110299206.9A priority Critical patent/CN112947909A/en
Publication of CN112947909A publication Critical patent/CN112947909A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis
    • G06F8/436Semantic checking
    • G06F8/437Type checking
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/70Software maintenance or management
    • G06F8/72Code refactoring

Abstract

The embodiment of the invention discloses a method and a device for reconstructing a dynamic programming language, wherein the method comprises the steps of redefining the grammar of the original dynamic programming language to obtain a new dynamic programming language; carrying out grammar check on a source program based on the new dynamic programming language; the source program of the new dynamic programming language which passes the grammar check is generated into the source program which is interpreted and executed by the original dynamic programming language interpreter.

Description

Reconstruction method and device of dynamic programming language
Technical Field
The embodiment of the invention relates to the field of computer programs, in particular to a method and a device for reconstructing a dynamic programming language.
Background
Dynamic programming languages, which are a category of high-level programming languages that have been widely used in the field of computer science, are a class of languages that can change their type at runtime: for example, new functions, objects, and even code are dynamically imported, dynamic languages are currently very dynamic, such as Lua, PHP, Ruby, Python, etc. belong to dynamic languages, and C, C + + do not belong to dynamic languages.
Lua is a very simple programming language, but also has some drawbacks, such as: the Lua data structure is relatively original, for example, map, struct or array types of other common programming languages do not exist, but a data structure called table is used. In the Lua code, one data is transmitted, and the type of the data cannot be known clearly; the Lua syntax is too loose, for example, a function is defined to receive three parameters, 'function a (arg1, arg2, arg 3)', but when the function is called, some parameters such as, 'a (1, 2)' are also acceptable to be lacked, and the lacked parameters automatically become 'nil'. If the programmer neglects to cause less transfer parameters, the hidden danger of code stability is brought; type errors and some code bugs may report errors only when running, which is inconvenient to find during development.
Disclosure of Invention
It is an object of the present application to overcome the above problems or to at least partially solve or mitigate the above problems. In order to reduce the difficulty in writing and maintaining a source program formed by a dynamic language and find the problem of codes in advance, the technical scheme disclosed by the invention designs the dynamic programming language, and then the dynamic programming language is converted into Lua language codes through the language check of a compiler, so that the dynamic programming language is conveniently integrated with a standard Lua interpreter.
In a first aspect, the present invention provides a reconfiguration method for a dynamic programming language, including,
redefining the grammar of the original dynamic programming language to obtain a new dynamic programming language;
carrying out grammar check on a source program based on the new dynamic programming language;
and generating the source program of the new dynamic programming language which passes the syntax check into the source program which is interpreted and executed by the dynamic programming language interpreter.
As a preferred embodiment of the present application, the redefining of the syntax of the original dynamic programming language results in a new dynamic programming language including,
based on the grammar of the original dynamic programming language, adding part of the grammars of the static programming language to form the grammar of a new dynamic programming language;
and describing the grammar of the new dynamic programming language through the open source grammar analyzer framework to obtain the new dynamic programming language.
As a preferred embodiment of the present application, the syntax of the added part static programming language includes,
number type, string type, boolean type, array type, dictionary type and complex type and function type.
As a preferred embodiment of the present application, the syntax checking of the source program based on the new dynamic programming language includes,
for the traversed and analyzed syntax tree, judging the syntax type of each node in the syntax tree;
a corresponding check rule is invoked for the syntax type of each node.
As a preferred embodiment of the present application, the checking rule includes a basic type check, a parameter return value matching check and a field module presence check.
As a preferred embodiment of the present application, the basic type checking includes obtaining a syntax tree using syntax parsing, and traversing the syntax tree to check whether the syntax type is correct, and if the syntax type is incorrect, the checking fails.
As a preferred embodiment of the present application, the checking of the matching of the parameter return values includes obtaining a syntax tree by using syntax parsing, traversing the syntax tree, and checking function definitions and function calls in the syntax tree.
As a preferred embodiment of the present application, the checking of function definitions and function statement calls in the syntax tree includes,
for function definition, checking the number of parameters and the type of the parameters of the function, the number of return values and the type of the return values of the function, and if the number of the parameters are not matched, and/or the types of the parameters are not matched; and/or the number of return values does not match, and/or the type of return values does not match, then the check fails.
And checking the number of the parameters and the types of the parameters of the function for the function call, and if the number of the parameters does not match and/or the types of the parameters do not match, failing to check.
As a preferred embodiment of the present application, the field module presence check includes obtaining a syntax tree using syntax parsing, traversing the syntax tree, checking whether a field exists, and if not, checking not to pass.
As a preferred embodiment of the present application, the dynamic programming language is Lua, PHP, Ruby, Python language.
As a preferred embodiment of the present application, the static language is C, C + +.
As a preferred embodiment of the present application, the open source parser framework is an open source parser that automatically generates and visualizes a display of a syntax tree from input.
Compared with the prior art, the method for reconstructing the dynamic programming language defines a new language grammar, adds part of grammars of the static programming language in addition to the grammar of the original dynamic programming language, greatly improves the original dynamic programming language, reduces the maintenance cost of codes, improves the quality of the codes, keeps the compatibility of a finally output source program to an original dynamic programming language standard interpreter, and cannot reduce the applicability of the languages.
In a second aspect, the present invention also provides a dynamic programming language reconfiguration device, including,
the grammar definition module is used for redefining the grammar of the dynamic programming language to obtain a new dynamic programming language;
the grammar checking module is used for carrying out grammar checking on the source program based on the new dynamic programming language;
and the code generation module is used for generating the source program of the new dynamic programming language which passes the grammar check into the source program which is interpreted and executed by the original dynamic programming language interpreter.
Compared with the prior art, the beneficial effect of the dynamic programming language reconfiguration device disclosed by the invention is the same as that of the dynamic programming language reconfiguration method disclosed by any one of the technical schemes, and the description is omitted here.
Drawings
The accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this application, illustrate embodiment(s) of the application and together with the description serve to explain the application and not to limit the application. Some specific embodiments of the present application will be described in detail hereinafter by way of illustration and not limitation with reference to the accompanying drawings. The same reference numbers will be used throughout the drawings to refer to the same or like parts or portions, and it will be understood by those skilled in the art that the drawings are not necessarily drawn to scale, in which:
FIG. 1 is a flow chart of a method for restructuring a dynamic programming language in accordance with an embodiment of the present invention;
FIG. 2 is a flow chart of a method for restructuring a dynamic programming language in accordance with another embodiment of the present invention;
FIG. 3 is a flowchart of a reconfiguration method for a dynamic programming language according to another embodiment of the present invention;
FIG. 4 is a syntax tree structure according to one embodiment of the present invention;
FIG. 5 is a syntax tree structure according to one embodiment of the present invention;
FIG. 6 is a syntax tree structure according to one embodiment of the present invention;
FIG. 7 is a schematic structural diagram of a reconfiguration device of a dynamic programming language according to another embodiment of the present invention.
Detailed Description
In order to make the technical solutions better understood by those skilled in the art, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only partial embodiments of the present application, but not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
As shown in fig. 1, a reconfiguration method of a dynamic programming language provided in an embodiment of the present invention includes,
step S11, redefining the grammar of the original dynamic programming language to obtain a new dynamic programming language;
step S12, carrying out grammar check on the source program based on the new dynamic programming language;
in step S32, the source program of the new dynamic programming language passing the syntax check generates the source program to be interpreted and executed by the original dynamic programming language interpreter.
As an embodiment of the present application, step S11 specifically includes,
step S111, based on the grammar of the original dynamic programming language, adding part of the grammars of the static programming language to form the grammar of a new dynamic programming language;
and step S112, describing the grammar of the new dynamic programming language through the open source grammar analyzer framework to obtain the new dynamic programming language.
In the embodiment of the application, firstly, a grammar structure of a new dynamic programming language needs to be defined, an antlr grammar rule is used for describing, in the definition process, the grammar of an original dynamic programming language is compatible, part of the grammar of a static programming language is added to form the new dynamic programming language, the grammar of the new dynamic programming language is described through an open source grammar analyzer framework to obtain the new dynamic programming language, and the open source grammar analyzer framework is an open source grammar analyzer which automatically generates a grammar tree according to input and displays the grammar tree in a visualized mode.
In the present application, the syntax of a partial static programming language is added, including,
number type, string type, boolean type, array type, dictionary type and complex type and function type.
Wherein, the number type is defined as number in grammar, corresponding to the number type in Lua language, and the number of local example is 1
The character string type is defined as string in grammar, and corresponds to the character string type in Lua language, and the local example is string ═ a ".
Wherein, the boolean type is boolean in the syntax definition, corresponding to the boolean type in the Lua language, and local example: boolean ═ false.
The array type is { T } in the syntax definition, and corresponds to a table type in the Lua language, and the local example: { number } - {1,2,3 }.
The dictionary type is { T: T } in the grammar definition, and corresponds to a table type in the Lua language, and the dictionary type is { string: number } { "key" } value "}.
The composite type is defined as record in grammar, and corresponds to a table type in Lua language, wherein each field of the record has its own type.
local type example=record
a:string
b:number
end
The array type, the dictionary type and the composite type are not clearly distinguished in the Lua language, so that inconvenience is brought to development and use.
The function type is defined as function in the grammar, corresponds to the function type in the Lua language, and in the Typed Lua, the function type needs to label the type of the parameter and the return value.
local function example(arg1:number,arg2:string):string,number
return arg2,arg1
end
In the present embodiment, in step S12, the syntax checking is performed on the source program based on the new dynamic programming language, including,
step S121, for the traversed and analyzed syntax tree, judging the syntax type of each node in the syntax tree;
step S122, a corresponding check rule is invoked for the syntax type of each node.
Based on the above syntax definition and type definition, type check is required for the code to find possible errors in the code, and the main check types are: checking a basic type; checking the matching of the parameter return values; field module presence check.
The basic flow is that for traversing the parsed syntax tree, the syntax type of each node in the syntax tree is judged, and relevant check rules are applied.
And the basic type check comprises the steps of obtaining a syntax tree by using syntax analysis, traversing the syntax tree, checking whether the syntax type is correct or not, and if the syntax type is incorrect, failing to check. Taking the local example that string is 1 code as an example, the syntax tree can be obtained by parsing using the syntax definition as shown in fig. 4, when traversing the syntax tree, it is found that this is an assignment statement, and 1 with a type of number is assigned to an example with a type of string as a type definition, which is not allowed in type check and requires error reporting processing.
And the parameter return value matching check comprises the steps of obtaining a syntax tree by using syntax analysis, traversing the syntax tree and checking function definition and function call in the syntax tree.
Source program samples are as follows
local function example(arg1:number,arg2:string):string,number
return arg1,arg2
end
example("",1)
Syntax tree as shown in fig. 5, when traversing the syntax tree, it is found that the syntax tree contains function definitions and function call statements.
The checking of function definitions and function statement calls in the syntax tree includes,
for function definition, checking the number of parameters and the type of the parameters of the function, the number of return values and the type of the return values of the function, and if the number of the parameters are not matched, and/or the types of the parameters are not matched; and/or the number of return values does not match, and/or the type of return values does not match, then the check fails.
And checking the number of the parameters and the types of the parameters of the function for the function call, and if the number of the parameters does not match and/or the types of the parameters do not match, failing to check.
Specifically, for function definition, it is necessary to check the matching of the number and type of the function parameters and the number and type of the function return values, for example, in this example, the function return value is defined as string, number but return values are return arg1, arg2, because it is known that arg1, the type of arg2 is defined as number, and the types of string return values do not match, and the check does not pass.
For function call, the number and type of function parameters need to be checked, for example, in this example, the function parameters are defined as number and string, but the function parameters are "",1, the number of function parameters is inconsistent, and type check fails.
And the field module existence check comprises the steps of obtaining a syntax tree by using syntax analysis, traversing the syntax tree, checking whether a field exists or not, and if not, failing to check.
Source program samples are as follows
local type example=record
a:string
end
local x:example={}
print(b.f)
Syntax tree as shown in fig. 6, when traversing the syntax tree, it is found that the syntax tree contains a record field-fetching statement. For the record field-taking statement, it needs to check whether the field exists, for example, in the present example, the field name is f, but does not exist in the example record, and the check does not pass.
As an embodiment of the present application, the whole process is developed into a compiler of a new dynamic programming language, and the functions include: checking a grammar structure; translating the new dynamic programming language into a Lua source program; after the source program code is developed, firstly, using the/compiler check example to check the grammar structure, if the grammar structure check fails, terminating the flow, prompting a specific error, and revising the returned code; if the check passes, the Lua source program will be translated into an example. Lua source program using the/completer general example.
In the present application, the dynamic programming language is Lua, PHP, Ruby, Python language.
In this application, the static language is C, C + +.
In a second aspect, the present application also provides a dynamic programming language reconfiguration device 700, comprising,
a grammar definition module 71, configured to redefine a grammar of the dynamic programming language to obtain a new dynamic programming language;
a syntax checking module 72 for performing syntax checking on the source program based on the new dynamic programming language;
and the code generation module 73 generates the source program of the new dynamic programming language which passes the grammar check into the source program which is interpreted and executed by the original dynamic programming language interpreter.
In the embodiment of the application, firstly, a grammar structure of a new dynamic programming language needs to be defined, an antlr grammar rule is used for describing, in the definition process, the grammar of an original dynamic programming language is compatible, part of the grammar of a static programming language is added to form the new dynamic programming language, the grammar of the new dynamic programming language is described through an open source grammar analyzer framework to obtain the new dynamic programming language, and the open source grammar analyzer framework is an open source grammar analyzer which automatically generates a grammar tree according to input and displays the grammar tree in a visualized mode.
Finally, it should be noted that: the above embodiments are only used to illustrate the technical solution of the present invention, and not to limit the same; while the invention has been described in detail and with reference to the foregoing embodiments, it will be understood by those skilled in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some or all of the technical features may be equivalently replaced; and the modifications or the substitutions do not make the essence of the corresponding technical solutions depart from the scope of the technical solutions of the embodiments of the present invention.

Claims (10)

1. A reconstruction method of a dynamic programming language is characterized by comprising the following steps,
redefining the grammar of the original dynamic programming language to obtain a new dynamic programming language;
carrying out grammar check on a source program based on the new dynamic programming language;
and generating the source program of the new dynamic programming language which passes the syntax check into the source program which is interpreted and executed by the dynamic programming language interpreter.
2. The method of claim 1, wherein redefining the grammar of the dynamic programming language results in a new dynamic programming language comprising,
based on the grammar of the original dynamic programming language, adding part of the grammars of the static programming language to form the grammar of a new dynamic programming language;
and describing the grammar of the new dynamic programming language through the open source grammar analyzer framework to obtain the new dynamic programming language.
3. The method of claim 2, wherein the adding of the grammar of the partial static programming language includes,
number type, string type, boolean type, array type, dictionary type and complex type and function type.
4. The method of claim 1, wherein the syntax checking of the source program based on the new dynamic programming language comprises,
for the traversed and analyzed syntax tree, judging the syntax type of each node in the syntax tree;
a corresponding check rule is invoked for the syntax type of each node.
5. The method of claim 4, wherein the checking rules include basic type checking, parameter return value matching checking, and field module presence checking.
6. A method for restructuring a dynamic programming language according to claim 5 wherein said base type check includes using syntax parsing to obtain a syntax tree and traversing the syntax tree to check whether the syntax type is correct and if the syntax type is incorrect, the check fails.
7. The method of claim 5, wherein the checking for parameter return value matching comprises using syntax parsing to obtain a syntax tree and traversing the syntax tree to check for function definitions and function calls in the syntax tree.
8. The method of claim 7, wherein said checking function definitions and function statement calls in said syntax tree comprises,
for function definition, checking the number of parameters and the type of the parameters of the function, the number of return values and the type of the return values of the function, and if the number of the parameters are not matched, and/or the types of the parameters are not matched; and/or the number of return values does not match, and/or the type of return values does not match, then the check fails.
And checking the number of the parameters and the types of the parameters of the function for the function call, and if the number of the parameters does not match and/or the types of the parameters do not match, failing to check.
9. A method for restructuring a dynamic programming language according to claim 5 wherein said field module presence check comprises using syntax parsing to obtain a syntax tree and traversing the syntax tree to check whether a field is present and if not, failing the check.
10. A dynamic programming language reconfiguration device, comprising,
the grammar definition module is used for redefining the grammar of the original dynamic programming language to obtain a new dynamic programming language;
the grammar checking module is used for carrying out grammar checking on the source program based on the new dynamic programming language;
and the code generation module is used for generating the source program of the new dynamic programming language which passes the grammar check into the source program which is interpreted and executed by the original dynamic programming language interpreter.
CN202110299206.9A 2021-03-21 2021-03-21 Reconstruction method and device of dynamic programming language Pending CN112947909A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110299206.9A CN112947909A (en) 2021-03-21 2021-03-21 Reconstruction method and device of dynamic programming language

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110299206.9A CN112947909A (en) 2021-03-21 2021-03-21 Reconstruction method and device of dynamic programming language

Publications (1)

Publication Number Publication Date
CN112947909A true CN112947909A (en) 2021-06-11

Family

ID=76228459

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110299206.9A Pending CN112947909A (en) 2021-03-21 2021-03-21 Reconstruction method and device of dynamic programming language

Country Status (1)

Country Link
CN (1) CN112947909A (en)

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20120311546A1 (en) * 2011-05-31 2012-12-06 Microsoft Corporation Transforming dynamic source code based on semantic analysis
CN105404584A (en) * 2015-11-25 2016-03-16 广州博冠信息科技有限公司 LPC static code inspection method, apparatus and system
CN111459500A (en) * 2020-06-17 2020-07-28 北京机电工程研究所 Safety compiling method and device based on sea eagle wing brightness operating system
CN111694726A (en) * 2019-03-15 2020-09-22 南京大学 Python program type derivation method based on type labeling
CN111767076A (en) * 2020-06-23 2020-10-13 中国工商银行股份有限公司 Code reconstruction method and device

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20120311546A1 (en) * 2011-05-31 2012-12-06 Microsoft Corporation Transforming dynamic source code based on semantic analysis
CN105404584A (en) * 2015-11-25 2016-03-16 广州博冠信息科技有限公司 LPC static code inspection method, apparatus and system
CN111694726A (en) * 2019-03-15 2020-09-22 南京大学 Python program type derivation method based on type labeling
CN111459500A (en) * 2020-06-17 2020-07-28 北京机电工程研究所 Safety compiling method and device based on sea eagle wing brightness operating system
CN111767076A (en) * 2020-06-23 2020-10-13 中国工商银行股份有限公司 Code reconstruction method and device

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
WEIXIN-3993234: "python静态语言-Python的静态类型之旅", HTTPS://BLOG.CSDN.NET/WEIXIN_39932344/ARTICLE/DETAILS/110446473, pages 1 - 3 *

Similar Documents

Publication Publication Date Title
CN108985073B (en) Highly-automated intelligent contract formalized verification system and method
CN110196719B (en) Business rule generation method and system based on natural language processing
US7346897B2 (en) System for translating programming languages
US5794043A (en) Method for testing at least one class of an object-oriented program on a computer
EP1027648B1 (en) Apparatus for semi-automated generation and application of language conformity tests
CN110543421B (en) Unit test automatic execution method based on test case automatic generation algorithm
US8806452B2 (en) Transformation of computer programs and eliminating errors
CN106371997B (en) Code checking method and device
US10459829B2 (en) Overall test tool migration pipeline
CN101751281A (en) System and method for generating compiler
CN111626028A (en) Report generation method, device and equipment based on function analysis and computer medium
CN101499015A (en) Method and converter for converting high level language to other high level languages
CN113901083A (en) Heterogeneous data source operation resource analysis positioning method and equipment based on multiple analyzers
CN111694726B (en) Python program type derivation method based on type labeling
CN112947909A (en) Reconstruction method and device of dynamic programming language
CN114706571B (en) DSL-based declarative visualization chart development method and system
CN115357492A (en) Formal verification method and device for Java software
US8650536B2 (en) Modular server architecture
CN114895914A (en) Log output code generation method and device, electronic equipment and storage medium
Merino et al. Projecting Textual Languages
CN111176993A (en) Code static detection method based on abstract syntax tree
Xavier et al. Type checking Circus specifications
CN115794119A (en) Case automatic analysis method and device
CN117667089A (en) Front-end form generation method and device, storage medium and electronic equipment
Lorenzen Type sound syntactic language extension

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