Crafting Interpreters: Part 2 Representing Code

📰 Medium · Programming

Learn to represent code using abstract syntax trees in Go by following along with the Crafting Interpreters book, applying the concepts to build an interpreter from scratch

intermediate Published 19 Apr 2026
Action Steps
  1. Define the grammar for the interpreter using a context-free grammar
  2. Implement a lexer to tokenize the input code
  3. Build an abstract syntax tree (AST) from the tokens
  4. Write a parser to construct the AST from the tokens
  5. Use the AST to execute the code or perform other operations
Who Needs to Know This

Software engineers and developers who want to learn about interpreter design and implementation in Go will benefit from this tutorial, as it provides a hands-on approach to building an interpreter from the ground up

Key Insight

💡 Abstract syntax trees (ASTs) are a fundamental data structure in interpreter design, allowing for efficient and flexible representation of code

Share This
Build an interpreter in Go from scratch! Learn how to represent code using abstract syntax trees and follow along with the Crafting Interpreters book #go #interpreter #ast

Key Takeaways

Learn to represent code using abstract syntax trees in Go by following along with the Crafting Interpreters book, applying the concepts to build an interpreter from scratch

Full Article

Title: Crafting Interpreters: Part 2 Representing Code

URL Source: https://matthewmacfarquhar.medium.com/crafting-interpreters-part-2-representing-code-cf6ac0ddd371?source=rss------programming-5

Published Time: 2026-04-19T16:15:16Z

Markdown Content:
# Crafting Interpreters: Part 2 Representing Code | by Matthew MacFarquhar | Apr, 2026 | Medium

[Sitemap](https://matthewmacfarquhar.medium.com/sitemap/sitemap.xml)

[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)

Sign up

[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmatthewmacfarquhar.medium.com%2Fcrafting-interpreters-part-2-representing-code-cf6ac0ddd371&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

[](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)

Get app

[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)

[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)

Sign up

[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmatthewmacfarquhar.medium.com%2Fcrafting-interpreters-part-2-representing-code-cf6ac0ddd371&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

![Image 1](https://miro.medium.com/v2/resize:fill:32:32/1*dmbNkD5D-u45r44go_cf0g.png)

# Crafting Interpreters: Part 2 Representing Code

[![Image 2: Matthew MacFarquhar](https://miro.medium.com/v2/resize:fill:32:32/1*zWtTzv3PflmCz2m7jyN0lQ.png)](https://matthewmacfarquhar.medium.com/?source=post_page---byline--cf6ac0ddd371---------------------------------------)

[Matthew MacFarquhar](https://matthewmacfarquhar.medium.com/?source=post_page---byline--cf6ac0ddd371---------------------------------------)

Follow

5 min read

·

Just now

[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fvote%2Fp%2Fcf6ac0ddd371&operation=register&redirect=https%3A%2F%2Fmatthewmacfarquhar.medium.com%2Fcrafting-interpreters-part-2-representing-code-cf6ac0ddd371&user=Matthew+MacFarquhar&userId=5b4bcd924433&source=---header_actions--cf6ac0ddd371---------------------clap_footer------------------)

[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fbookmark%2Fp%2Fcf6ac0ddd371&operation=register&redirect=https%3A%2F%2Fmatthewmacfarquhar.medium.com%2Fcrafting-interpreters-part-2-representing-code-cf6ac0ddd371&source=---header_actions--cf6ac0ddd371---------------------bookmark_footer------------------)

[Listen](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2Fplans%3Fdimension%3Dpost_audio_button%26postId%3Dcf6ac0ddd371&operation=register&redirect=https%3A%2F%2Fmatthewmacfarquhar.medium.com%2Fcrafting-interpreters-part-2-representing-code-cf6ac0ddd371&source=---header_actions--cf6ac0ddd371---------------------post_audio_button------------------)

Share

## Introduction

Press enter or click to view image in full size

![Image 3](https://miro.medium.com/v2/resize:fit:700/1*3Aguz1Jj0Ukg4fpES3Cq_Q.png)

In this article series, we’ll work through the excellent [Crafting Interpreters](https://craftinginterpreters.com/), but with a twist: we’ll implement everything in Go instead of Java. Along the way, we’ll explore how the ideas in the book translate to idiomatic Go while building an interpreter from the ground up.

You can find the current state of the code corresponding to this article [here](https://github.com/mattmacf98/tree-walk-interpreter/tree/93426b94950e399c263a3a6f36131d7ee3e82a79).

## What we will build

In this chapter, we’ll define the grammar that will be used to build an abstract syntax tree from our tokens. If tokens are the words of the interpre
Read full article → ← Back to Reads