Rock It 《ML》JupyterLab 【丁】Code《七》語義【四】I

Stack Exchange 上,有人提出了一個問題︰

Why don’t we store the syntax tree instead of the source code?

We have a lot of programming languages. Every language is parsed and syntax checked before being translated into code so an abstract syntax tree (AST) is built.

We have this abstract syntax tree, why don’t we store this syntax tree instead of the source code (or next to the source code)?

By using an AST instead of the source code. Every programmer in a team can serialize this tree to any language they want (with the appropriate context free grammar) and parse back to AST when they are finished. So this would eliminate the debate about the coding style questions (where to put the { and }, where to put whitespace, indentation, etc.)

What are the pros and cons of this approach?

 

激起漣漪引發討論,值得認真思考也!

誠如 Green Tree Snakes 文件所言,派生 Python 本身並沒有提供『回返』之法︰

Going backwards

Python itself doesn’t provide a way to turn a compiled code object into an AST, or an AST into a string of code. Some third party tools can do these things:

  • astor can convert an AST back to readable Python code.
  • Meta also tries to decompile Python bytecode to an AST, but it appears to be unmaintained.
  • uncompyle6 is an actively maintained Python decompiler at the time of writing. Its documented interface is a command line program producing Python source code.

 

不過 CPython 語法分析器工具裡有一個 unparse.py 軟件,似乎可以逆轉『 AST 』還原『原始碼』哩!!

且讓我們借著這個工具的改寫版

/astunparse

An AST unparser for Python

AST Unparser

https://badge.fury.io/py/astunparse.png https://travis-ci.org/simonpercivall/astunparse.png?branch=master https://readthedocs.org/projects/astunparse/badge/

An AST unparser for Python.

This is a factored out version of unparse found in the Python source distribution; under Demo/parser in Python 2 and under Tools/parser in Python 3.

Basic example:

import inspect
import ast
import astunparse

# get back the source code
astunparse.unparse(ast.parse(inspect.getsource(ast)))

# get a pretty-printed dump of the AST
astunparse.dump(ast.parse(inspect.getsource(ast)))

This library is single-source compatible with Python 2.6 through Python 3.5. It is authored by the Python core developers; I have simply merged the Python 2.7 and the Python 3.5 source and test suites, and added a wrapper. This factoring out is to provide a library implementation that supports both versions.

Added to this is a pretty-printing dump utility function.

The test suite both runs specific tests and also roundtrips much of the standard library.

Extensions and Alternatives

Similar projects include:

None of these roundtrip much of the standard library and fail several of the basic tests in the test_unparse test suite.

This library uses mature and core maintained code instead of trying to patch existing libraries. The unparse and thetest_unparse modules are under the PSF license.

Extensions include:

  • typed-astunparse: extends astunparse to support type annotations.

Features

  • unparses Python AST.
  • pretty-prints AST.

 

嘗試自己回答『原始碼』和『 AST 』真一樣嗎??