Context-Free Grammar That Verifies Addition
🌱 What Does It Mean to Verify Addition?
Imagine you have a string like:
101 + 10 = 111
We want a grammar that accepts the string only if the addition is correct.
So:
- If the sum is correct → grammar generates it
- If the sum is wrong → grammar cannot generate it
This is like having a very strict teacher checking your homework.
📌 Representing Binary Numbers
Binary numbers work just like decimal numbers, but with only two digits:
- 0
- 1
For example:
101means 510means 2111means 7
So the equation above is valid:
101 (5) + 10 (2) = 111 (7)
Now we want a CFG that recognizes only correct equations of this form.
🔍 How Can a Grammar Check Addition?
Instead of adding numbers the way we do on paper, the grammar works differently.
It checks the addition from right to left, just like you do when you add numbers normally. But instead of “carrying digits,” the grammar uses different nonterminals to represent whether:
- A carry came from the right
- No carry is present
Think of it as two “modes”:
- C0 → Addition with no carry
- C1 → Addition with a carry
These modes help us verify every column of the sum.
🧠 Key Insight
Each digit of the equation follows this pattern:
a + b = c (with or without carry)
Where a, b, and c are bits (0 or 1).
The CFG needs rules that match every possible valid combination.
🧩 The Context-Free Grammar
Let’s define the grammar piece by piece.
🎯 Start Symbol
S → C0
We begin without any carry.
⭐ Case 1: Addition Without Carry (C0)
These are valid column operations without a carry:
- 0 + 0 = 0
- 1 + 0 = 1
- 0 + 1 = 1
- 1 + 1 = 0 (but now we produce a carry → moves to C1)
In grammar form:
C0 → 0 C0 0 | 1 C0 1 | 0 C0 1 | 1 C0 0 C1
C0 → ε
⭐ Case 2: Addition With Carry (C1)
Now we include the extra +1:
- 0 + 0 + 1 = 1
- 1 + 0 + 1 = 0 (with new carry)
- 0 + 1 + 1 = 0 (with carry)
- 1 + 1 + 1 = 1 (with carry)
Grammar rules:
C1 → 0 C1 1 | 1 C1 0 | 0 C1 0 C1 | 1 C1 1 C1
C1 → 1 (if final result has carry)
Each rule checks a correct binary addition step.
🎨 Simple Diagram to Show Idea
Here’s a conceptual diagram of how the grammar works:
+-------------+
| S |
+------^------+
|
No Carry
|
+-----+
| C0 |
+--+--+
|
---------------------------------
| | |
Match Switch to End
(a+b=c) Carry Mode ε
|
+-----+
| C1 |
+--+--+
|
---------------------------------
| | |
Match Stay in Carry Extra
(a+b+carry=c) 1
The two “worlds” C0 and C1 work together just like real addition.
🎯 Why This Grammar Is So Interesting
Most people assume grammars only generate strings, but here they:
- Validate math
- Track carries elegantly
- Use structural patterns rather than numeric logic
This is a wonderful example of how computation and language theory blend together.
🪄 Example of a Valid String
Take this one:
101 + 10 = 111
The grammar successfully matches each column from right to left, shifting between C0 and C1 as needed.
Try something incorrect like:
101 + 10 = 110
No series of grammar productions can generate this, so it gets rejected.
