15

I'm trying to build a parser for Solidity, and it would help if I could build a working version off of the existing parser. I've tried looking at the source code on the official git, but I haven't worked on a project of this magnitude before and it's confusing. Could some one tell me how I could isolate the parsing and AST modules so I can read a script and build an AST from it?

(I understand the scope might be a little too broad for this question, but I'd really appreciate any pointers)

5 Answers 5

10

If you use solc --ast, the compiler will output a json-representation of the AST (this is also included in the npm version of the compiler). Is that enough for your purposes? If not, please come to gitter to discuss.

1
  • 2
    Is there a way to convert an AST back into Solidity code? i.e. for transpilation purposes Commented Apr 21, 2016 at 3:24
3

In addition to chriseth's answer, there's also the solidity-parser project which is useful if you A) are developing in a Javascript context, or B) don't want the parser to error if there are unresolved imports (unfortunately solc --ast will error if there are imports it cannot find).

2

In addition to Tim's answer there's also a python-solidity-parser based on the ANTLR syntax and AST format used in solidity-parser-antlr that you can use if you want to avoid depending on the compiler. Note: compiler AST is the truth.

1

I have used 3 tools, maybe you can try:

  1. https://github.com/iamdefinitelyahuman/py-solc-ast
  2. https://github.com/ConsenSys/solc-typed-ast
  3. https://github.com/ethereum/solidity/tree/develop/docs/grammar
1

Use flag --ast-compact-json, which gives the "AST of all source files in a compact JSON format."

solc --ast-compact-json contract.sol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.