Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m thinking of making a Python-JavaScript converter/compile. I\'ve been wonder

ID: 646855 • Letter: I

Question

I'm thinking of making a Python-JavaScript converter/compile. I've been wondering about the pros and cons of the following methods to parse the Pythonic code, into JavaScript equivalent.

Working with the output of ast.dump(ast.parse(code))
Working with tokens.
I've been trying weigh the pros and cons of these methods, but well I'm unable to think of any major one, as I've never used them..
So, Basically could someone help me by telling me what the pros and cons of the two methods I've mentioned above are?
Also, if there is something better that I can use for this purpose, notify me about it..

PS: I'll be using Python to write the converter..

Explanation / Answer

Big con of tokens: You'll have to parse, and this is both unnecessary and nontrivial.

Big con of ast.dump(ast.parse(code)): It's pointlessly stupid. ast.parse gives you a perfectly good AST, structured in an appropriate data structure. ast.dump gives you a string representation of that data structure, so you'd have to parse that to get back what ast.parse already gave you!

Leaving out ast.dump and using the AST from ast.parse, there are two big pros:

You don't have to worry about parsing Python code at all (and doing so is moderately complex).
ASTs are really the minimum you need to look at for any compiler-ish project at this scale. While the ast AST may have some complexity you don't need (e.g. line information, or Python features you don't support), I assume that simplifying a Python AST to your custom AST is simpler than generating your custom AST from tokens.
A minor benefit: There's already a visitor class built into ast, so you don't have to create one (a simple but somewhat boring task) for easy AST traversal.
Cons? Not so much. Go for ast, but don't be afraid to use additional data structures beyond that.