A Solidity parser in OCaml with Menhir

Origin Labs
May 19, 2020 · 4 min read

(this article is cross-posted on OCamlPro’s blog)

We are happy to announce the first release of our Solidity parser, written in OCaml using Menhir. This is a joint effort with OCamlPro, the OCaml company, to implement a full interpreter for the Solidity language directly in a blockchain.

Solidity is probably the most popular language for smart-contracts, small pieces of code triggered when accounts receive transactions on a blockchain.Solidity is an object-oriented strongly-typed language with a Javascript-like syntax.

Solidity was first implemented for the Ethereum blockchain, with a compiler to the EVM, the Ethereum Virtual Machine.

Dune Network takes a different approach, as Solidity smart-contracts will be executed natively, after type-checking. Solidity will be the third native language on Dune Network, with Michelson, a low-level strongly-typed language inherited from Tezos, and Love, an higher-level strongly-typed language, also implemented jointly by OCamlPro and Origin Labs.

A first step has been accomplished, with the completion of the Solidity parser and printer, written in OCaml with Menhir.

This parser (and its printer companion) is now available as a standalone library under the LGPLv3 license with Linking Exception, allowing its integration in all projects. The source code is available at https://gitlab.com/o-labs/solidity-parser-ocaml.

Our parser should support all of Solidity 0.6, with the notable exception of inline assembly (may be added in a future release).

Example contract

pragma solidity >=0.6.0 <0.7.0;contract C {    address owner;
int x;
constructor() public {
owner = msg.sender;
x = 0;
}
function add(int d) public {
require(msg.sender == owner);
x += d;
}
function read_x() public view returns(int) {
return x;
}
}

Parser Usage

Executable

./solp contract.sol

This will parse the file contract.sol and reprint it on the terminal.

Library

Solidity_parser.parse_contract_file :
string -> Solidity_parser.Solidity_types.module_

It takes a filename and returns a Solidity AST.

If you wish to print this AST, you may turn it into its string representation by sending it to the following function:

Solidity_parser.Printer.string_of_code :
Solidity_parser.Solidity_types.module_ -> string

Conclusion

Connect with Dune Network:

About Origin Labs

Origin Labs is a company founded in 2019 by the former blockchain team at OCamlPro. At Origin Labs, they have been developing Dune Network, a fork of the Tezos blockchain, its ecosystem, and applications over the Dune Network platform. At OCamlPro, they developed TzScan, the most popular block explorer at the time, Liquidity, a smart contract language, and were involved in the development of the core protocol and node.Do not hesitate to reach out by email: contact@origin-labs.com.

About OCamlPro

OCamlPro is a company founded in 2011, with the mission to help industrial users benefit from state-of-the art programming languages like OCaml and Rust. We design, create and implement custom ad-hoc software for our clients. We also have a long experience in developing and maintaining open-source tooling for OCaml, such as Opam, TryOCaml, ocp-indent etc. and we contribute to the core-development of OCaml, notably with our work on the Flambda optimizer branch with Jane Street. Another area of expertise is that of Formal Methods, with tools such as our SMT Solver Alt-Ergo (check our Alt-Ergo Users’ Club). We provide consulting and vocational trainings in OCaml and Rust, and we can build courses on formal methods on-demand. Do not hesitate to reach out by email: contact@ocamlpro.com.

Dune Network

Blog, updates, and technical articles on the Dune Network

Dune Network

Dune Network ( http://dune.network ) is a 3rd-generation blockchain platform, with a focus on security and accessibility. Its main features are delegated Proof-of-Stake, multiple native smart contract languages, formal verification and easy private chains deployments.

Origin Labs

Written by

The blockchain studio that developed Dune Network. Formerly, the team worked at OCamlPro and developed TzScan, Liquidity and many other tools for blockchains.

Dune Network

Dune Network ( http://dune.network ) is a 3rd-generation blockchain platform, with a focus on security and accessibility. Its main features are delegated Proof-of-Stake, multiple native smart contract languages, formal verification and easy private chains deployments.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store