These are the notes for Low-Level Design (LLD) episode 44, the eleventh and last classic behavioural pattern (patterns about how objects share work and talk to each other). The Interpreter pattern turns a small language, like a cafe owner's offer rules (total > 500 and hour < 17), into a tree of objects that can evaluate itself. We start with a school grammar lesson, watch a text-splitting shortcut give discounts it shouldn't, write the language's grammar, and invent one class per grammar rule. Then we watch recursive evaluation, fall into a typo trap, write a tiny parser that turns text into the tree, grow it into a rule engine, and see when to reach for a parser generator instead. Every program was actually run with javac Main.java && java -cp . Main (Java 21). Outputs are real.
1. The real-life idea: grammar class at school
| In English class | In code |
|---|---|
| Grammar rules: a sentence = subject + verb + object | Grammar: rules saying how sentences of a language are built |
| Single words, like "Asha" or "500": nothing inside | Terminal expression: a leaf, a number or a name (Num, Var) |
| Phrases built from parts, like "tea and samosa" | Non-terminal expression: built from smaller expressions (And, More) |
| Understanding the sentence in today's situation | Interpret with a context: evaluate against today's facts |
"Terminal" because breaking a sentence down ends there.
2. The problem: offers keep changing
Asha changes the cafe's offers every week: "10% off if the total is over 500", then "...and only before 5 pm". Written as Java if statements, every change needs a developer, a rebuild and a new release. She wants to type the offer as text in an admin screen: total > 500 and hour < 17.
The obvious way: split the text on spaces
public class Main {
static boolean check(String offer, int total, int hour) { // read the offer text
String[] w = offer.split(" "); // "total" ">" "500"
int left = w[0].equals("total") ? total : hour;
int right = Integer.parseInt(w[2]);
return w[1].equals(">") ? left > right : left < right;
}
public static void main(String[] args) {
System.out.println("rule 1, 650 at 3pm: " + check("total > 500", 650, 15));
String offer = "total > 500 and hour < 17"; // the owner's new offer
System.out.println("rule 2, 650 at 7pm: " + check(offer, 650, 19));
}
}
Output (real):
rule 1, 650 at 3pm: true
rule 2, 650 at 7pm: true
Rule 2 at 7 pm should be false (after 5 pm). No error, just a wrong answer: the code only read the first three words and silently ignored everything after and. Patching it for and, or, not and brackets inside brackets doesn't scale.
3. Why it failed: text has structure
The offer is a tree, not a flat list of words:
and
/ \
total > 500 hour < 17
/ \ / \
total 500 hour 17
The grammar of our little offer language, in three rules ({ } = repeat zero or more times, | = or):
rule := cmp { "and" cmp }
cmp := value ( ">" | "<" ) value
value := number | name
Idea: one class per grammar rule; build the tree from objects; each object evaluates itself.
4. The fix: a class per grammar rule
import java.util.*;
class Context { // the facts about this bill
private final Map<String, Integer> vars;
Context(Map<String, Integer> vars) { this.vars = vars; }
int get(String name) { return vars.get(name); }
}
interface Value { int get(Context c); } // an expression giving a number
interface Rule { boolean test(Context c); } // an expression giving true/false
record Num(int n) implements Value { // terminal: 500
public int get(Context c) { return n; } }
record Var(String name) implements Value { // terminal: total
public int get(Context c) { return c.get(name); } }
record More(Value a, Value b) implements Rule { // non-terminal: a > b
public boolean test(Context c) { return a.get(c) > b.get(c); } }
record Less(Value a, Value b) implements Rule { // non-terminal: a < b
public boolean test(Context c) { return a.get(c) < b.get(c); } }
record And(Rule a, Rule b) implements Rule { // non-terminal: a and b
public boolean test(Context c) { return a.test(c) && b.test(c); } }
public class Main {
public static void main(String[] args) {
Rule offer = new And(new More(new Var("total"), new Num(500)), // total > 500
new Less(new Var("hour"), new Num(17))); // and hour < 17
System.out.println("650 at 3pm: " + offer.test(new Context(Map.of("total", 650, "hour", 15))));
System.out.println("650 at 7pm: " + offer.test(new Context(Map.of("total", 650, "hour", 19))));
System.out.println("300 at 3pm: " + offer.test(new Context(Map.of("total", 300, "hour", 15))));
}
}
Output (real):
650 at 3pm: true
650 at 7pm: false
300 at 3pm: false
- Context: the facts the sentence is interpreted against (this bill's
totalandhour, by name). - Terminal expressions:
Numreturns its number;Varlooks its name up in the context. - Non-terminal expressions:
More,Less,Andhold smaller expressions and evaluate themselves by first asking their children. - Recursive evaluation: each node asks its children, all the way down to the leaves.
- The tree built in
mainis an abstract syntax tree (AST) (episode 43): the structure of a sentence, without spaces.
5. It has a name: the Interpreter pattern
| Role | In our code | Job |
|---|---|---|
| Abstract expression | Rule, Value |
The interface every node implements, with one interpret method (test / get) |
| Terminal expression | Num, Var |
Leaves: return a value directly |
| Non-terminal expression | More, Less, And |
Hold child expressions; combine their results |
| Context | Context |
The facts the sentence is interpreted against |
| Client | Main |
Builds (or parses) the tree, calls interpret on the root |
Intent (classic definition, via SourceMaking): "Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language." Non-terminals hold children of the same interface type: that's the Composite pattern (episode 32), which lets trees grow as deep as needed.
Recursive evaluation, step by step (650 at 15h)
Main→And.test(ctx): can't answer yet, asks its left child.And→More.test(ctx)→Var("total").get(ctx)→Context.get("total")→650.Morecompares650 > 500→true.Andasks its right child the same way (15 < 17→true), returnstrue && true.
6. The trap: a typo in a name
import java.util.*;
class Context { // the facts about this bill
private final Map<String, Integer> vars;
Context(Map<String, Integer> vars) { this.vars = vars; }
int get(String name) { return vars.get(name); }
}
interface Value { int get(Context c); } // an expression giving a number
interface Rule { boolean test(Context c); } // an expression giving true/false
record Num(int n) implements Value { // terminal: 500
public int get(Context c) { return n; } }
record Var(String name) implements Value { // terminal: total
public int get(Context c) { return c.get(name); } }
record More(Value a, Value b) implements Rule { // non-terminal: a > b
public boolean test(Context c) { return a.get(c) > b.get(c); } }
record Less(Value a, Value b) implements Rule { // non-terminal: a < b
public boolean test(Context c) { return a.get(c) < b.get(c); } }
record And(Rule a, Rule b) implements Rule { // non-terminal: a and b
public boolean test(Context c) { return a.test(c) && b.test(c); } }
public class Main {
public static void main(String[] args) {
Rule offer = new And(new More(new Var("totl") , new Num(500)), // typo: totl
new Less(new Var("hour"), new Num(17))); // and hour < 17
System.out.println("650 at 3pm: " + offer.test(new Context(Map.of("total", 650, "hour", 15))));
System.out.println("650 at 7pm: " + offer.test(new Context(Map.of("total", 650, "hour", 19))));
System.out.println("300 at 3pm: " + offer.test(new Context(Map.of("total", 300, "hour", 15))));
}
}
Output (real):
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object)" is null
at Context.get(Main.java:5)
at Var.get(Main.java:12)
at More.test(Main.java:14)
at And.test(Main.java:18)
at Main.main(Main.java:23)
The map has no totl, so Map.get returns null, and Java crashes converting null to an int (unboxing: turning an Integer object into a plain int). The message mentions Map.get, not the offer, so nobody running the cafe can act on it. Fix: fail fast in the context with a clear message (next section).
7. Parsing: turning text into the tree
"The pattern doesn't address parsing" (SourceMaking). Parsing = turning text into a tree; a parser is the code that does it. For a grammar this small, a hand-written recursive-descent parser (one method per grammar rule, calling each other) is enough.
import java.util.*;
record Context(Map<String, Integer> vars) { // fail fast on unknown names
int get(String n) { Integer v = vars.get(n);
if (v == null) throw new IllegalArgumentException("unknown variable: " + n); return v; } }
interface Value { int get(Context c); } interface Rule { boolean test(Context c); }
record Num(int n) implements Value { public int get(Context c) { return n; } }
record Var(String name) implements Value { public int get(Context c) { return c.get(name); } }
record More(Value a, Value b) implements Rule { public boolean test(Context c) { return a.get(c) > b.get(c); } }
record Less(Value a, Value b) implements Rule { public boolean test(Context c) { return a.get(c) < b.get(c); } }
record And(Rule a, Rule b) implements Rule { public boolean test(Context c) { return a.test(c) && b.test(c); } }
class Parser { // text -> tree
private final String[] t; private int i = 0; Parser(String text) { t = text.trim().split("\\s+"); }
Rule rule() { Rule r = cmp(); // rule := cmp { "and" cmp }
while (i < t.length && t[i].equals("and")) { i++; r = new And(r, cmp()); } return r; }
Rule cmp() { Value a = value(); String op = next(); Value b = value(); // cmp := value op value
if (op.equals(">")) return new More(a, b); if (op.equals("<")) return new Less(a, b);
throw new IllegalArgumentException("expected > or < but got " + op); }
Value value() { String s = next(); // value := number | name
return s.matches("\\d+") ? new Num(Integer.parseInt(s)) : new Var(s); }
String next() { if (i == t.length) throw new IllegalArgumentException("rule ended early"); return t[i++]; }
}
public class Main {
public static void main(String[] args) {
Rule offer = new Parser("total > 500 and hour < 17").rule(); System.out.println(offer);
System.out.println("650 at 3pm: " + offer.test(new Context(Map.of("total", 650, "hour", 15))));
System.out.println("650 at 7pm: " + offer.test(new Context(Map.of("total", 650, "hour", 19))));
}
}
Output (real):
And[a=More[a=Var[name=total], b=Num[n=500]], b=Less[a=Var[name=hour], b=Num[n=17]]]
650 at 3pm: true
650 at 7pm: false
Records print their components, so the first line shows the exact tree the parser built.
Failing fast on bad rules
Text "total > 500 and hour" (unfinished):
Exception in thread "main" java.lang.IllegalArgumentException: rule ended early
at Parser.next(Main.java:20)
at Parser.cmp(Main.java:15)
at Parser.rule(Main.java:14)
at Main.main(Main.java:24)
Text "totl > 500 and hour < 17" (typo):
And[a=More[a=Var[name=totl], b=Num[n=500]], b=Less[a=Var[name=hour], b=Num[n=17]]]
Exception in thread "main" java.lang.IllegalArgumentException: unknown variable: totl
at Context.get(Main.java:4)
at Var.get(Main.java:7)
at More.test(Main.java:8)
at And.test(Main.java:10)
at Main.main(Main.java:25)
In a real admin screen, parse the rule when it is saved, show the message next to the text box, and refuse to save it.
8. A rule engine
A rule engine is a program that checks a list of rules against facts and acts on the ones that match. Rules live in data (a file or a database), not in Java code, so the owner can change them without a release.
import java.util.*;
record Context(Map<String, Integer> vars) { // fail fast on unknown names
int get(String n) { Integer v = vars.get(n);
if (v == null) throw new IllegalArgumentException("unknown variable: " + n); return v; } }
interface Value { int get(Context c); } interface Rule { boolean test(Context c); }
record Num(int n) implements Value { public int get(Context c) { return n; } }
record Var(String name) implements Value { public int get(Context c) { return c.get(name); } }
record More(Value a, Value b) implements Rule { public boolean test(Context c) { return a.get(c) > b.get(c); } }
record Less(Value a, Value b) implements Rule { public boolean test(Context c) { return a.get(c) < b.get(c); } }
record And(Rule a, Rule b) implements Rule { public boolean test(Context c) { return a.test(c) && b.test(c); } }
class Parser { // text -> tree
private final String[] t; private int i = 0; // the words, and where we are
Parser(String text) { t = text.trim().split("\\s+"); }
Rule rule() { Rule r = cmp(); // rule := cmp { "and" cmp }
while (i < t.length && t[i].equals("and")) { i++; r = new And(r, cmp()); } return r; }
Rule cmp() { Value a = value(); String op = next(); Value b = value(); // cmp := value op value
if (op.equals(">")) return new More(a, b); if (op.equals("<")) return new Less(a, b);
throw new IllegalArgumentException("expected > or < but got " + op); }
Value value() { String s = next(); // value := number | name
return s.matches("\\d+") ? new Num(Integer.parseInt(s)) : new Var(s); }
String next() { if (i == t.length) throw new IllegalArgumentException("rule ended early"); return t[i++]; }
}
record Offer(String text, Rule rule, int percent) { } // one rule + its discount
class RuleEngine {
private final List<Offer> offers = new ArrayList<>();
void add(String text, int percent) { offers.add(new Offer(text, new Parser(text).rule(), percent)); } // parse once
Optional<Offer> best(Context bill) { // every matching rule, best discount wins
return offers.stream().filter(o -> o.rule().test(bill)).max(Comparator.comparingInt(Offer::percent));
}
}
public class Main {
public static void main(String[] args) {
RuleEngine engine = new RuleEngine();
engine.add("total > 500 and hour < 17", 10);
engine.add("items > 5", 15);
engine.add("total > 2000", 20);
List<Context> bills = List.of(
new Context(Map.of("total", 650, "hour", 15, "items", 3)),
new Context(Map.of("total", 650, "hour", 19, "items", 7)),
new Context(Map.of("total", 2500, "hour", 12, "items", 9)),
new Context(Map.of("total", 200, "hour", 20, "items", 1)));
for (Context b : bills) {
String result = engine.best(b).map(o -> o.percent() + "% off (" + o.text() + ")").orElse("no offer");
System.out.println(b.vars().get("total") + " at " + b.vars().get("hour") + "h, " + b.vars().get("items") + " items: " + result);
}
}
}
Output (real):
650 at 15h, 3 items: 10% off (total > 500 and hour < 17)
650 at 19h, 7 items: 15% off (items > 5)
2500 at 12h, 9 items: 20% off (total > 2000)
200 at 20h, 1 items: no offer
Each rule is parsed once, when added; every bill only runs test.
9. When to use a parser (generator) instead
| Interpreter pattern | Parser generator | |
|---|---|---|
| Grammar size | A few rules | Dozens of rules, brackets, priorities |
| Classes | One per rule: fine | One per rule: explodes |
| Parser | Hand-written is fine | The tool writes it from a grammar file |
| Speed | Rarely matters | Built for big inputs |
| Example | Offer rules, filters, simple formulas | Programming languages, query languages |
A well-known parser generator is ANTLR: "ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files" (antlr.org). SourceMaking's advice: "When the grammar is very complex, other techniques (such as a parser) are more appropriate", and first decide if a "little language" is worth it at all; sometimes a simple settings screen is enough.
10. Interpreter vs Visitor
| Interpreter | Visitor (episode 43) | |
|---|---|---|
| Where the work lives | In each node (test, get) |
Outside, in visitor classes |
| New node type | Easy: one class | Hard: every visitor changes |
| New operation | Hard: edit every node | Easy: one visitor |
| Typical use | One main operation: evaluate | Many operations on one tree |
Big tools often combine them: the tree evaluates itself, and extra operations (printing, checking) come as visitors.
Common mistakes
| Mistake | What you see | Why | Fix |
|---|---|---|---|
| Splitting the text on spaces | No error: rule 2, 650 at 7pm: true (wrong) |
Text has structure | Grammar + tree of objects |
| Missing name in the context | NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object)" is null |
null unboxed to int |
Context fails fast: unknown variable: totl |
| Accepting half-written rules | IllegalArgumentException: rule ended early while a customer pays |
Parsed too late | Parse and validate when the rule is saved |
| Parsing the rule on every bill | Slow checkout | Repeated work | Parse once, keep the tree |
| Hand-coding a big language | Hundreds of classes, slow, buggy | Grammar too complex | Parser generator such as ANTLR |
| Forgetting operator priority | a or b and c evaluates wrongly |
Grammar has one level | One grammar rule per priority level |
Mini project: the full rule engine
See section 8: Offer, RuleEngine.add (parse once) and RuleEngine.best (all matching rules, highest discount wins).
Practice
1. Add or (lower priority than and) and an = comparison to the grammar and the parser.
Answer
The grammar gets one more level: rule := all { "or" all }, all := cmp { "and" cmp }, cmp := value (">" | "<" | "=") value.
import java.util.*;
record Context(Map<String, Integer> vars) { // fail fast on unknown names
int get(String n) { Integer v = vars.get(n);
if (v == null) throw new IllegalArgumentException("unknown variable: " + n); return v; } }
interface Value { int get(Context c); } interface Rule { boolean test(Context c); }
record Num(int n) implements Value { public int get(Context c) { return n; } }
record Var(String name) implements Value { public int get(Context c) { return c.get(name); } }
record More(Value a, Value b) implements Rule { public boolean test(Context c) { return a.get(c) > b.get(c); } }
record Less(Value a, Value b) implements Rule { public boolean test(Context c) { return a.get(c) < b.get(c); } }
record And(Rule a, Rule b) implements Rule { public boolean test(Context c) { return a.test(c) && b.test(c); } }
record Or(Rule a, Rule b) implements Rule { public boolean test(Context c) { return a.test(c) || b.test(c); } }
record Eq(Value a, Value b) implements Rule { public boolean test(Context c) { return a.get(c) == b.get(c); } }
class Parser { // text -> tree
private final String[] t; private int i = 0; // the words, and where we are
Parser(String text) { t = text.trim().split("\\s+"); }
Rule rule() { Rule r = all(); // rule := all { "or" all }
while (i < t.length && t[i].equals("or")) { i++; r = new Or(r, all()); } return r; }
Rule all() { Rule r = cmp(); // all := cmp { "and" cmp }
while (i < t.length && t[i].equals("and")) { i++; r = new And(r, cmp()); } return r; }
Rule cmp() { Value a = value(); String op = next(); Value b = value(); // cmp := value op value
if (op.equals(">")) return new More(a, b); if (op.equals("<")) return new Less(a, b);
if (op.equals("=")) return new Eq(a, b);
throw new IllegalArgumentException("expected >, < or = but got " + op); }
Value value() { String s = next(); // value := number | name
return s.matches("\\d+") ? new Num(Integer.parseInt(s)) : new Var(s); }
String next() { if (i == t.length) throw new IllegalArgumentException("rule ended early"); return t[i++]; }
}
public class Main {
public static void main(String[] args) {
Rule offer = new Parser("total > 500 and hour < 17 or items = 10").rule();
System.out.println(offer);
System.out.println("650 at 19h, 10 items: " + offer.test(new Context(Map.of("total", 650, "hour", 19, "items", 10))));
System.out.println("650 at 19h, 3 items: " + offer.test(new Context(Map.of("total", 650, "hour", 19, "items", 3))));
System.out.println("650 at 15h, 3 items: " + offer.test(new Context(Map.of("total", 650, "hour", 15, "items", 3))));
}
}
Output (real):
Or[a=And[a=More[a=Var[name=total], b=Num[n=500]], b=Less[a=Var[name=hour], b=Num[n=17]]], b=Eq[a=Var[name=items], b=Num[n=10]]]
650 at 19h, 10 items: true
650 at 19h, 3 items: false
650 at 15h, 3 items: true
and binds tighter than or, so the tree is Or(And(...), Eq(...)).
2. Use a visitor (episode 43) to print a rule tree back as the owner's text.
Answer
import java.util.*;
interface RuleVisitor<R> { R num(int n); R var(String name); R more(Expr a, Expr b); R and(Expr a, Expr b); }
interface Expr { <R> R accept(RuleVisitor<R> v); }
record Num(int n) implements Expr { public <R> R accept(RuleVisitor<R> v) { return v.num(n); } }
record Var(String name) implements Expr { public <R> R accept(RuleVisitor<R> v) { return v.var(name); } }
record More(Expr a, Expr b) implements Expr { public <R> R accept(RuleVisitor<R> v) { return v.more(a, b); } }
record And(Expr a, Expr b) implements Expr { public <R> R accept(RuleVisitor<R> v) { return v.and(a, b); } }
class ToText implements RuleVisitor<String> { // prints the tree back as the owner's text
public String num(int n) { return String.valueOf(n); }
public String var(String name) { return name; }
public String more(Expr a, Expr b) { return a.accept(this) + " > " + b.accept(this); }
public String and(Expr a, Expr b) { return a.accept(this) + " and " + b.accept(this); }
}
public class Main {
public static void main(String[] args) {
Expr rule = new And(new More(new Var("total"), new Num(500)), new More(new Var("items"), new Num(2)));
System.out.println(rule.accept(new ToText()));
}
}
Output (real):
total > 500 and items > 2
3. In total > 500 and hour < 17, which parts are terminal expressions and which are non-terminal?
Answer
Terminals: total, 500, hour, 17 (Var, Num). Non-terminals: the two comparisons (More, Less) and the and (And).
4. Why should the context throw unknown variable instead of returning 0 for a missing name?
Answer
Returning 0 hides the typo: totl > 500 would silently be false for every bill and the offer would never apply. Failing fast shows the mistake immediately, with the name.
Quick quiz
Rule r = new And(
new More(new Var("x"), new Num(2)),
new Less(new Var("x"), new Num(5)));
Context c = new Context(Map.of("x", 5));
System.out.println(r.test(c));
What is printed? (a) true (b) false (c) NullPointerException (d) Compile error
Answer
(b) false (real output). 5 > 2 is true, but 5 < 5 is false, so the And is false.
Interview questions
- What is the Interpreter pattern? A behavioural pattern that represents each grammar rule of a small language as a class, so a sentence becomes a tree of objects that evaluates itself.
- Terminal vs non-terminal expression? Terminal: a leaf with no sub-expressions (a number, a variable). Non-terminal: built from other expressions (
and,>). - What is the context? The external facts the sentence is evaluated against (variable values).
- Does Interpreter include parsing? No. Parsing (text → tree) is a separate step: hand-written for small grammars, a parser generator for big ones.
- How does it relate to Composite? Non-terminals hold children of the same interface: the tree is a Composite; interpret is the recursive operation.
- Interpreter vs Visitor? Interpreter puts evaluation in the nodes; Visitor moves operations outside. Interpreter makes new node types easy; Visitor makes new operations easy.
- When is it a bad idea? Large or fast-changing grammars, performance-critical evaluation, or when a settings screen would do.
- Real uses? Rule engines, search filters, formula fields, feature-flag conditions, regular-expression engines, SQL
WHEREevaluators. - How do you handle operator priority? One grammar rule (and parser method) per priority level, lowest first:
or→and→ comparison. - How would you make it fast? Parse once and cache the tree; validate on save; for heavy use, compile the tree to something cheaper.
Glossary
| Term | Meaning, with an example |
|---|---|
| Behavioural pattern | A design pattern about how objects share work and talk to each other |
| Interpreter pattern | One class per grammar rule of a small language; a sentence becomes a tree that evaluates itself |
| Language | A set of sentences built by rules. Asha's offer rules form a tiny language |
| Grammar | The rules saying how sentences are built: cmp := value (">" \| "<") value |
| Terminal expression | A leaf that can't be broken down: Num(500), Var("total") |
| Non-terminal expression | An expression built from smaller ones: And, More, Less |
| Abstract expression | The interface every node implements: Rule, Value |
| Abstract syntax tree (AST) | The tree structure of a sentence, without spaces or brackets |
| Context | The facts a sentence is interpreted against: total = 650, hour = 15 |
| Interpret / evaluate | Work out a sentence's value in a context: test(ctx) → true |
| Recursive evaluation | Each node evaluates its children first, down to the leaves |
| Composite pattern | Objects that hold children of the same type, forming trees (episode 32) |
| Parsing / parser | Turning text into a tree / the code that does it |
| Recursive-descent parser | A hand-written parser with one method per grammar rule |
| Token | One word of the input: total, >, 500 |
| Parser generator | A tool that writes a parser from a grammar file, such as ANTLR |
| Rule engine | A program that checks a list of rules against facts and acts on matches |
| Fail fast | Stop with a clear error as soon as something is wrong: unknown variable: totl |
| Unboxing | Turning an Integer object into a plain int; fails on null |
NullPointerException |
The error when code uses null as if it were an object |
| Operator priority (precedence) | Which operator binds tighter: and before or, * before + |
| Record | A compact class for plain data that prints its parts: Num[n=500] |
| Little language | A small special-purpose language, like offer rules or a search filter |
Summary
- Text with structure can't be handled by splitting words: write a grammar.
- Interpreter: one class per grammar rule; terminals are leaves, non-terminals hold children; each node interprets itself recursively against a context.
- Parsing is a separate step: a tiny hand-written parser for a tiny grammar; fail fast on bad text and unknown names.
- Rules as data give a rule engine that the business can change without a release.
- For big grammars, use a parser generator (such as ANTLR).
Next episode: Beyond the Gang of Four: Null Object, Repository, Specification (business rules as objects) and MVC.