aboutsummaryrefslogtreecommitdiff
path: root/forest-examples.md
blob: e22edefe1f43a49c327859e697ceb405d2e63175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Reference derivation forests (hand-verified)

These are ground-truth cases for comparing against the parser's forest
output.  Every case was derived by hand from the grammar, so treat a
mismatch as a bug in the parser (or in the oracle), never as a reason
to edit this file silently.

## Notation

Trees are S-expressions: `(Symbol child child ...)`. Terminals are
bare tokens.  An empty derivation is written `eps`.

A packed forest is a set of nodes `Symbol[start,end]`. Each node has
one or more packed alternatives; each alternative is a sequence of
children, where a child is a node or a terminal. Ambiguity = a node
with more than one alternative.  A node is identified by (symbol,
start, end), and identical nodes must be shared.

The tree set represented by a forest is obtained by choosing one
alternative per node, recursively, from the root node `S[0,n]`.

---

## Case 1: ambiguous, no epsilon

Grammar:

```
S -> S S | a
```

Input: `aaa` (tokens `a a a`, n = 3)

Packed forest (6 nodes, 5 packed alternatives in total):

```
S[0,3]:  { S[0,1] S[1,3] }  |  { S[0,2] S[2,3] }
S[0,2]:  { S[0,1] S[1,2] }
S[1,3]:  { S[1,2] S[2,3] }
S[0,1]:  { a }
S[1,2]:  { a }
S[2,3]:  { a }
```

Tree set (exactly 2 trees):

```
(S (S a) (S (S a) (S a)))
(S (S (S a) (S a)) (S a))
```

Tree counts by input length (binary trees with n leaves =
Catalan(n-1)):

| n | trees |
|---|-------|
| 1 | 1     |
| 2 | 1     |
| 3 | 2     |
| 4 | 5     |
| 5 | 14    |

Forest size should stay polynomial: for n = 20 the forest must NOT
enumerate the 1,767,263,190 trees. Number of nodes is at most O(n^2),
packed alternatives at most O(n^3).

---

## Case 2: ambiguous expression grammar

Grammar:

```
E -> E + E | n
```

Input: `n + n + n`

Packed forest:

```
E[0,5]:  { E[0,1] + E[2,5] }  |  { E[0,3] + E[4,5] }
E[0,3]:  { E[0,1] + E[2,3] }
E[2,5]:  { E[2,3] + E[4,5] }
E[0,1]:  { n }
E[2,3]:  { n }
E[4,5]:  { n }
```

Here positions are token indices: `n`=0..1, `+`=1..2, `n`=2..3, `+`=3..4, `n`=4..5.

Tree set (exactly 2 trees):

```
(E (E n) + (E (E n) + (E n)))
(E (E (E n) + (E n)) + (E n))
```

---

## Case 3: nullable nonterminals

Grammar:

```
S -> A A
A -> a | eps
```

Input: `a` (n = 1)

Packed forest:

```
S[0,1]:  { A[0,1] A[1,1] }  |  { A[0,0] A[0,1] }
A[0,1]:  { a }
A[1,1]:  { eps }
A[0,0]:  { eps }
```

Tree set (exactly 2 trees; the two are distinct because the `a` is
consumed by a different `A`):

```
(S (A a) (A eps))
(S (A eps) (A a))
```

Input: `` (empty, n = 0). Exactly 1 tree:

```
(S (A eps) (A eps))
```

Watch out: the empty input must give exactly ONE tree, not two. Both
`A`s derive eps in only one way each.

---

## Case 4: right recursion with epsilon, unambiguous

Grammar:

```
S -> a S | eps
```

Input: `a a`

Packed forest (no ambiguity, every node has exactly one alternative):

```
S[0,2]:  { a S[1,2] }
S[1,2]:  { a S[2,2] }
S[2,2]:  { eps }
```

Tree set (exactly 1 tree):

```
(S a (S a (S eps)))
```

---

## Case 5: cyclic grammars (infinite ambiguity), represented by forest cycles

Policy for this repo: **infinitely many derivations are represented by
a cycle in the forest.** The forest stays finite; its tree set is
infinite. Nothing is cut or pruned.

Consequences for reading the notation:

- A node's alternative may contain the node itself, or a chain of
  nodes leading back to it.
- The tree set is obtained by choosing one alternative **per
  occurrence** of a node in the tree being built, not once per node
  globally. This matters here: the same node appears several times on
  one root-to-leaf path, and each occurrence may choose differently.
- Since every child sequence covers its parent's span exactly, a cycle
  can only pass through nodes with the **same span** `[start, end]`,
  and only through derivations that consume nothing extra (unit rules,
  or rules whose other symbols are nullable).

Because the tree set is infinite, compare **bounded** tree
sets. Define the height of a tree as the maximum number of nonterminal
nodes on any root-to-leaf path. Let `trees_h` be the set of trees with
height <= h. The parser's forest, unpacked with bound h, must equal
the oracle's `trees_h` for every h tested.

### Case 5a: self-loop

Grammar:

```
S -> S | a
```

Input: `a`

Forest (one node, two alternatives, one of them cyclic):

```
S[0,1]:  { S[0,1] }  |  { a }
```

Bounded tree sets (exactly h trees for each h):

```
h = 1:  (S a)
h = 2:  (S a)  (S (S a))
h = 3:  (S a)  (S (S a))  (S (S (S a)))
h = 4:  (S a)  (S (S a))  (S (S (S a)))  (S (S (S (S a))))
```

### Case 5b: nullable self-loop

Grammar:

```
A -> A | eps
```

Input: `` (empty, n = 0)

Forest:

```
A[0,0]:  { A[0,0] }  |  { eps }
```

Bounded tree sets (exactly h trees for each h):

```
h = 1:  (A eps)
h = 2:  (A eps)  (A (A eps))
h = 3:  (A eps)  (A (A eps))  (A (A (A eps)))
```

### Case 5c: two-node cycle

Grammar:

```
S -> A | a
A -> S
```

Input: `a`

Forest (a cycle through two nodes with the same span):

```
S[0,1]:  { A[0,1] }  |  { a }
A[0,1]:  { S[0,1] }
```

Bounded tree sets (each trip around the cycle adds 2 to the height):

```
h = 1:  (S a)
h = 2:  (S a)
h = 3:  (S a)  (S (A (S a)))
h = 4:  (S a)  (S (A (S a)))
h = 5:  (S a)  (S (A (S a)))  (S (A (S (A (S a)))))
```

### Case 5d: cycle combined with ambiguity

Grammar:

```
S -> S S | S | a
```

Input: `a a`

Do not hand-write the expected forest for this one. It is the stress
test: the oracle (bounded by h) is the ground truth. Checks to make:

- The forest is finite and its node count is polynomial in n.
- Every cycle passes only through nodes with identical span (see
  invariants in `CLAUDE.md`).
- For h = 1, 2, 3, 4 the unpacked tree set equals the oracle's
  `trees_h`.
- Nodes with span `[0,2]` are shared: `S[0,2]` must exist exactly
  once.

---

## Suggested extra cases to add once the above pass

- Left recursion: `S -> S a | a`, input `aaaa` (1 tree).
- Hidden left recursion: `S -> A S a | a ; A -> eps`.
- Nullable in the middle: `S -> a A b ; A -> eps | A c`.
- Highly ambiguous with epsilon: `S -> S S | a | eps`, input `aa` (`S
  -> S S` with `S -> eps` is cyclic, so this exercises Case 5
  behavior; compare bounded tree sets only).