• 中国计算机学会会刊
  • 中国科技核心期刊
  • 中文核心期刊

计算机工程与科学 ›› 2025, Vol. 47 ›› Issue (06): 1041-1049.

• 软件工程 • 上一篇    下一篇

基于AST的MATLAB到Python转换器

郭瑞1,徐文浩1,谢鹏志2,杨威2,宋友1   

  1. (1.北京航空航天大学软件学院,北京 100191;2.中国航空制造技术研究院,北京 100024)
  • 收稿日期:2024-04-01 修回日期:2024-07-30 出版日期:2025-06-25 发布日期:2025-06-26

MATLAB-to-Python code converter based on AST

GUO Rui1,XU Wenhao1,XIE Pengzhi2,YANG Wei2,SONG You1   

  1. (1.School of Software,Beihang University,Beijing 100191;
    2.AVIC Manufacturing Technology Institute,Beijing 100024,China)
  • Received:2024-04-01 Revised:2024-07-30 Online:2025-06-25 Published:2025-06-26

摘要: MATLAB语言被广泛应用于工业领域产品研发的各个环节,但是在实际工程应用场景中,需要将其建立的机理模型脱离MATLAB运行环境,与实际的工程系统集成应用,故需要一种将MATLAB建立的模型快速工程化的工具。为此,提出一种MATLAB到Python转换器M2P,该转换器基于抽象语法树AST,将源代码转换到AST结构,对该结构进行分析并利用替换规则生成结果代码,最终实现MATLAB到Python的代码等价转换。对比代码转换实验的结果表明,所提转换器与现有的其他MATLAB到Python转换转换器相比具有更高的转换性能。

关键词: 代码转换, MATLAB-to-Python, 抽象语法树

Abstract: MATLAB is widely used in various stages of industrial product development. However, in practical engineering applications, the mechanistic models built in MATLAB often need to be decoupled from the MATLAB runtime environment and integrated into real-world engineering systems. Thus, a tool  for rapidly converting MATLAB models into deployable engineering solutions is required. To address this, this paper proposes M2P (MATLAB-to-Python), a  converter based on abstract syntax tree (AST). The converter  involves transforming source code into an AST structure, analyzing it, and applying substitution rules to generate equivalent Python code. Comparative experiments demonstrate that the proposed converter  achieves superior conversion  performance compared to existing MATLAB-to-Python converters.

Key words: code conversion, MATLAB-to-Python, abstract syntax tree (AST)