An Interpreter in Object Pascal

Writing an Interpreter in Object Pascal

UPDATE (9/22/2021): It's a year late but Finally, I have the source code ready for Book 3, it seems stable after much debugging. No memory leaks (base don FastMM) as far as I can tell which is what I've been working on the last two weeks. You can comment out FastMM in the dpr file if you don't need it. I've committed the code to GitHub together with a debug exe for your convenience if you just want to try it out:

UPDATE (9/16/2020): All the source code for part 1 and 2 of the book series has been moved to GitHub in preparation for part 3:

Part I - Published

Part II - Published (3/19/2020)

YouTube Playlist which demos some of the functionality of Version 1

Purchase the printed copy that describes the code in detail from Amazon

or as a pdf ebook from Gumroad

The source code, which is open source, can be found at

Part III

Book for Part 1: How to Purchase

Paperback Copy (with the option to get 50% off the pdf version) $16.95

Get the eBook (pdf) $15.95

For more information contact:

This is part 1 of a series that will show you how to write an interactive interpreter in Object Pascal. Part 1 of the series will cover introductory material including a description of the language we’ll create, a full lexical analyzer for the language, how to use DUnitX for unit testing, and an introduction to the essential concepts in syntax analysis, recursive descent, grammar, and EBNF. Along the way, we’ll create a simple REPL, give a detailed discussion of how to parse expressions and build a simple interactive calculator to illustrate the theory. The book provides fully working code and explains in plain English how the code works and why certain decisions were made, including alternative designs. The book makes liberal use of code throughout the book chapters. Everything is done without the help of third-party tools such as Yacc, ANTLR or Flex. All you need is a standard installation of Free Pascal or Embarcaderos’s Delphi (including the free community edition).

The text is geared to hobbyists and midlevel developers who need an accessible introduction to lexical analysis and parsing. It’s also for students starting out in compiler and interpreter design and need something more digestible.

All source code is open source under Apache 2.0 and available from Github.

Available in paperback form from Amazon.

Price $16.95 (paper), $15.95 (eBook) 170 pages

ISBN: 978-1-7325486-0-2


  1. Introduction

  2. a) Why Object Pascal

  3. b) What is an interpreter

  4. c) Parts of an interpreter

  5. The Rhodus Language

  6. Lexical Analysis

    1. Initial API

    2. Input streams

    3. Retrieving tokens

    4. First run

    5. Adding more tokens

  7. Testing

    1. Introduction to testing

    2. Using DUnitX

  8. An Interactive Console

  9. Introduction to Syntax Analysis

    1. Grammar

    2. Production rules

    3. LL(k)

    4. Recursive descent

    5. Factoring, the dangling else

    6. Left recursion

    7. Ambiguous Grammars

    8. A simple calculator

    9. Syntax trees

    10. Adding exponentiation and the unary minus

  10. Testing the Calculator

  11. Adding Assignments and Variables

    1. Using a queue for token lookahead

    2. Updating the syntax analyzer

    3. Updating the lexical analyzer

  12. Building a Recognizer

  13. Appendix EBNF

Whats coming up in Part III?

  1. Building the AST

  2. Symbol Table: (2)

  3. Error Handling

  4. Version Two of the Rhodus Language

  5. Adding Module and Array Support

  6. Math and IO Libraries

  7. Adding Exception Handling

  8. List and Array Libraries

  9. The Virtual Machine: (2)

  10. Code Generation: (2)

  11. Semantic Analysis

  12. The Virtual Machine: (3)

  13. Code Generation: (3)

  14. A Better REPL