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
Action Steps
- Define the grammar for the interpreter using a context-free grammar
- Implement a lexer to tokenize the input code
- Build an abstract syntax tree (AST) from the tokens
- Write a parser to construct the AST from the tokens
- 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------------------)

# Crafting Interpreters: Part 2 Representing Code
[](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

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
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------------------)

# Crafting Interpreters: Part 2 Representing Code
[](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

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
DeepCamp AI