Date Mon 18 March 2019 By serge-sans-paille Category compilation.

In spite of its dynamic nature, the Python language has been granted a rich ecosystem of Abstract Syntax Tree(AST) analysis and transformations. Off the top of my head, I can already cite:

Pyflake, a Python linter;

Pylint, a Python linter and more;

TorchScript, a DSL embedded into Python for PyTorch

autograph another DSL embedded into Python, for Tensorflow;

Pythran (of course!);

PEP-511 even proposed to normalize an API for code transformers-it got rejected.

And there probably are plenty of other users of the ast module from the Python standard library.

Foreword If you're using the ast module, then you should consider using the great (and home-backed) gast package. It offers a common tree structure to deal with the different Python version and their changes. In other words, if you can transform or analyze gast AST, then you can handle most Python versions. Not convinced? It's already used by Tensorflow and Pythran. And Beniget :-)

About Use-Def Chains Use-def chains is a very common compiler abstraction. It makes it possible to link any use of an identifier to its definition, enabling many code optimisations like constant folding or dead code elimination. The following statements are annotated with DEF (resp. USE ) to mark that the annotated statement defines (resp. uses) the associated identifier. a = 1 # DEF(a) b = a + 2 # DEF(b) USE(a) print ( a , b ) # USE(a, b) if c : # USE(c) a = "12" # DEF(a') else : a = [] # DEF(a'') print ( a ) # USE(a', a'') From these annotations, using a data-flow analysis, one can build the def-use chains as: DEF(a) -> USE(a) # a + 2 -> USE(a) # print(a, b) DEF(b) -> USE(b) # print(a, b) DEF(a') -> USE(a') # print(a) DEF(a'') -> USE(a'') # print(a) There is no DEF for USE(c) which means a probable NameError