STREAM Grammar Header Lab
Draft v0.1 packs a log fact into a fixed-width header — 5 characters, or 10 extended. This lab explores the v0.2 question: what if the numbers in the header do their packing work as numbers, read by a small parser — decimal runs, base-36 runs, and factored durations like t1d — so the header becomes a tiny grammar instead of a fixed bitfield, and the time delta has no 4,095 ceiling?
1 · The grammar at a glance
A grammar header starts with # — a character outside the v0.1 alphabet, so a reader can tell grammar form from fixed form at the first character — then three packed characters, then a number run the parser reads by its first character, then an optional extension. A space ends the header and the payload follows.
| Token | Form | What it carries |
|---|---|---|
| # | 1 character, always | Grammar marker. Not in the v0.1 alphabet (A–Z a–z 0–9 -_), so the two forms can never be confused. |
| packed | 3 base-64 characters, 18 bits | Position 7 · phase 2 · modality 1 · direction 1 · role presence 4 · estimate 1 · change 1 · extended-follows 1 — the v0.1 short fields except the time delta, which moves out into the run. |
| delta run | Variable length | Time delta. First character chooses the reading: a digit starts a decimal run (digits until the first non-digit, no ceiling); z starts a base-36 run (0–9 A–Z); t starts a factored run of number+unit pairs (d=86400, h=3600, m=60, s=1). No run at all means delta 0. |
| + | 1 character, only if extended-follows = 1 | Extension marker. Sits where the delta run ended — the parser knows the run is over because a non-digit (or a unit boundary) stopped it. |
| absence | 2 base-64 characters, 12 bits | Absence kind per role, 3 bits each (subject, object, kind, measure): 0 present · 1 not-yet · 2 unknown · 3 withheld · 4 not-applicable · 5 refused · 6 ambiguous · 7 reserved. |
| check | 1 base-64 character, 6 bits | XOR-fold of the packed value, absence value, delta and index. It sits at a fixed offset — before the index run — on purpose: the index run is variable-length and must be last so it ends at the space, while the check stays findable without parsing the run first. Short headers carry no check character, exactly as in v0.1. |
| index run | Variable length, last | Dictionary index, as a decimal or base-36 run under the same mode rules as the delta — never factored. |
| (space) | 1 character | Ends the header. Everything after it is payload. |
Parser walkthrough — #Uph5+ACc7
| Step | Text | The parser reads |
|---|---|---|
| 1 | # | Grammar marker — this is grammar form, not v0.1 fixed form. |
| 2 | Uph | 3 packed characters = 18 bits: position 41, phase 1 (begin), modality 0, direction 0, roles subject+object present, estimate 0, change 0, extended-follows 1. |
| 3 | 5 | First character is a digit, so a decimal run: digits until the first non-digit. The next character is +, so the run ends here — delta = 5. |
| 4 | + | Extension marker — expected, because extended-follows was 1. |
| 5 | AC | 2 absence characters = 12 bits: subject present, object present, kind present, measure unknown (2). |
| 6 | c | Check character at its fixed offset: value 28. The parser later folds the packed, absence, delta and index values and compares. |
| 7 | 7 | Index run: a digit, so decimal — index = 7. It runs to the end of the header. |
| 8 | (space) | Ends the header; the payload follows. Check verifies: stored 28 = computed 28. This is preset B below. |
2 · Live encoder
Set the fields; the grammar header, its token breakdown, and the v0.1 fixed-width header for the same fields update live. The encoder's AUTO run mode picks the shorter of decimal vs z+base-36, decimal on ties; factored is a manual choice.
Extension fields
Absence kind per role — meaningful when that role is absent. 0 present · 1 not-yet · 2 unknown · 3 withheld · 4 not-applicable · 5 refused · 6 ambiguous · 7 reserved.
Token breakdown
| Token | Text | Reading |
|---|
3 · Decoder
Paste a grammar header (a payload after a space is fine — the header ends at the space). Malformed headers produce a readable error, never a crash. Extended headers have their check character verified.
Token trace
| Token | Text | Reading |
|---|---|---|
| Nothing decoded yet. | ||
4 · Presets side by side
The three reference presets, in both forms. Grammar forms here use AUTO run mode unless a form is named.
| Preset | Draft v0.1 | Grammar (G1) |
|---|
The prefix property
The grammar header's first two characters are identical to the v0.1 header's first two — QT…, Up…, I5…. The third character differs only in its lowest bit: v0.1 spends that bit on the top bit of its 12-bit delta, while the grammar spends it on extended-follows (the rest of the delta having moved out into the run). Same head, one bit re-purposed — the two forms are cousins, not strangers.
5 · The trade — what a delta costs
Drag the delta and compare run lengths. Decimal is plain and ceiling-free but spends a whole character on ~3.32 bits; base-36 spends the z on a denser run; factored wins only when the duration is round in days, hours or minutes.
| Encoding of the delta | Run | Characters | Verdict |
|---|
| Delta 4096 — the overflow case | Decimal run "4096" costs 4 characters and has no ceiling. That one row is the whole argument: 4096 is the first value v0.1's 12-bit delta cannot hold at all, and the grammar holds it for four plain digits. |
6 · Overflow worked example — delta 86,400 (24 hours)
Preset A's fields with a delta of 86,400 seconds. v0.1 cannot hold this — its 12-bit delta caps at 4,095 — so in v0.1 this line would need an absolute time in the payload or a new segment. The grammar holds it three ways; each form below is encoded and decoded live, here on the page.
| Run mode | Grammar header | Chars | Live round-trip |
|---|
Factored wins here — #QTot1d at 7 characters — because 86,400 is exactly one day. That is the factored run's whole niche: round durations compress; arbitrary ones do not.
7 · Self-test
Runs on load and on the button: the three presets in every run mode, a few hundred random round-trips (short and extended, all run modes), single-character corruptions of extended headers, and malformed headers that must be rejected with readable errors. Exact counts are shown — a failure would be shown as a failure.
- Running…
8 · Honest limits
What the grammar gives up, stated plainly. None of this is hidden in the sections above; it is gathered here so the trade is on one screen.
- Variable length breaks fixed-width filtering. In v0.1 a reader can skip a line by character count alone and slice fields out of fixed positions. A grammar header has no fixed width, so a filter must parse the header instead of slicing bits — still cheap, still payload-free, but it is a parser, with a parser's failure modes, not a ruler.
- Decimal is wasteful per bit. A decimal digit carries about 3.32 bits; a packed-alphabet character carries 6. Ordinary small deltas therefore cost more characters in grammar form — preset A is 6 characters as #QTo60 against 5 as v0.1's QToB4.
- It needs a version marker and defined failures. The
#is that marker, and the decoder above defines the failure mode for every malformed shape it can meet — no leading#, truncated packed characters, characters outside the alphabet,zortwith nothing after them, a bad unit in a factored run, a missing or unexpected+section, a check mismatch, trailing characters after the index run, a number above 253. A grammar without those definitions would fail silently; this one fails out loud. - The check is an XOR fold, not a CRC. It catches every single-character error in an extended header, but compensating multi-character damage can cancel in the fold — the same caveat v0.1 carries, unchanged.
- It only wins past the ceiling, or on round durations. For ordinary small deltas the grammar form is longer than v0.1. It earns its length past delta 4,095 — where v0.1 cannot go at all — and for factored durations that land on whole days, hours or minutes. Everywhere else, the fixed form is shorter. That is the trade, and it may not be worth it; this lab exists so that judgement is made on working numbers, not on taste.