python
69 lines · 9 steps
A recursive descent arithmetic parser
A hand-written parser tokenizes an arithmetic string and evaluates it with one function per precedence level.
Explained by
highlit
1class Parser:
2 def __init__(self, text):
3 self.tokens = self._tokenize(text)
4 self.pos = 0
5
6 def _tokenize(self, text):
7 tokens = []
8 i = 0
9 while i < len(text):
10 c = text[i]
11 if c.isspace():
12 i += 1
13 elif c in "+-*/()":
14 tokens.append(c)
15 i += 1
16 elif c.isdigit() or c == ".":
17 j = i
18 while j < len(text) and (text[j].isdigit() or text[j] == "."):
19 j += 1
20 tokens.append(float(text[i:j]))
21 i = j
22 else:
23 raise ValueError(f"Unexpected character: {c!r}")
24 return tokens
25
26 def _peek(self):
27 return self.tokens[self.pos] if self.pos < len(self.tokens) else None
28
29 def _advance(self):
30 tok = self.tokens[self.pos]
31 self.pos += 1
32 return tok
33
34 def parse(self):
35 value = self._expr()
36 if self.pos != len(self.tokens):
37 raise ValueError("Unexpected trailing input")
38 return value
39
40 def _expr(self):
41 value = self._term()
42 while self._peek() in ("+", "-"):
43 op = self._advance()
44 rhs = self._term()
45 value = value + rhs if op == "+" else value - rhs
46 return value
47
48 def _term(self):
49 value = self._factor()
50 while self._peek() in ("*", "/"):
51 op = self._advance()
52 rhs = self._factor()
53 value = value * rhs if op == "*" else value / rhs
54 return value
55
56 def _factor(self):
57 tok = self._peek()
58 if tok == "(":
59 self._advance()
60 value = self._expr()
61 if self._advance() != ")":
62 raise ValueError("Expected closing parenthesis")
63 return value
64 if tok == "-":
65 self._advance()
66 return -self._factor()
67 if isinstance(tok, float):
68 return self._advance()
69 raise ValueError(f"Unexpected token: {tok!r}")
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Splitting tokenizing from parsing keeps each stage simple and independently testable.
- 2One parse function per precedence level makes operator precedence fall out of the call structure.
- 3A shared position cursor with peek and advance lets recursive functions cooperate over one token stream.
Related explainers
python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect app = FastAPI()
Building a WebSocket chat with FastAPI
websockets
broadcast
connection-management
Intermediate
9 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
python
import random from typing import Iterator, List
How reservoir sampling picks k items
reservoir-sampling
streaming
randomness
Intermediate
5 steps
python
import secrets from django.contrib.auth import authenticate, login from django.core.cache import cache
Two-factor login with OTP in Django
two-factor-auth
one-time-passwords
caching
Intermediate
9 steps
python
import re from functools import total_ordering from typing import Optional
Parsing and comparing semantic versions
regex
operator-overloading
sorting
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-recursive-descent-arithmetic-parser-explained-python-c26a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.