Menu

Build Your Own Python Interpreter in 30 Minutes!

By EuroPython Conference ยท 9/19/2023

๐Ÿ‘€ 3,498 views๐Ÿ‘ 85 likesโญ 0 favorites

Part 1

Key Points

  • Learn how to build a Python interpreter from scratch in under 30 minutes.
  • Understand the core components: tokenizer, parser, and interpreter.
  • Discover that contributing to Python is accessible and not as complex as it seems.

Introduction

In this video, Tushar Sadhwani explains how a Python interpreter works by building a simple interpreter from scratch. The goal is to demystify the process of creating a programming language and encourage contributions to Python, which has been around for 32 years and has many contributors. Watch the introduction.

Building a JSON Parser

The session begins with a story about wanting to create a JSON parser similar to Python's built-in functionality. The parser will take a string and extract structured data from it. The first step is tokenization, which involves breaking the string into meaningful components. Learn about tokenization.

Tokenization Process

The tokenizer identifies different parts of the string, such as braces and colons, and creates tokens. This process is guided by the JSON specification, which defines the structure of JSON objects. Explore the tokenizer function.

Parsing JSON

Once tokenized, the parser processes these tokens to create a structured representation of the data. It uses recursive descent parsing to handle nested structures effectively. Understand the parsing phase.

Creating the Python Interpreter

After building the JSON parser, Tushar transitions to creating a Python interpreter. The interpreter consists of three main components: the tokenizer, the parser, and the execution engine. Get an overview of the interpreter.

Tokenization in Python

The Python tokenizer is similar to the JSON tokenizer but includes additional features like handling new lines and indentation, which are crucial in Python syntax. Discover the tokenizer's role.

Parsing Python Code

The parser converts the tokenized input into a nested data structure, known as an abstract syntax tree (AST). This structure represents the program's logic and flow. Learn about the parsing process.

Execution of Code

The interpreter executes the AST by visiting each node and performing the corresponding operations. This involves a visitor pattern to handle different types of nodes effectively. See how execution works.

Conclusion

Tushar concludes by emphasizing that while this implementation is a simplified version of a Python interpreter, it lays the groundwork for further enhancements. He encourages viewers to explore the resources available for deeper understanding. Watch the conclusion.

Part 2

Building a Python Interpreter from Scratch in 30 Minutes

Overview

  • Tushar Sadhwani presents a talk on creating a Python interpreter from scratch during EuroPython 2023.
  • The session explores the inner workings of the Python interpreter, including syntax checking, module importing, and code execution.

Key Points

  • Specialized Implementations: Discusses implementations like stackless Python and MicroPython, which tailor the Python interpreter for specific use cases. Learn more
  • Using Parser Generators: Tushar mentions libraries like Lark that allow users to define custom grammars and generate parsers, highlighting the trade-offs of using such tools versus building from scratch. Explore parser generators
  • Trade-offs: Both handwritten parsers and parser generators have their advantages, with examples from languages like Ruby. Understand the trade-offs

Conclusion

  • The talk concludes with an invitation to the upcoming keynote session, encouraging attendees to stay engaged. Stay tuned

You Might Also Like