IUCompilerCourse / Essentials-of-Compilation

A book about compiling Racket and Python to x86-64 assembly
1.27k stars 137 forks source link

Earley's Algorithm discrepancy - chart array length #163

Closed jonbrandenburg closed 1 year ago

jonbrandenburg commented 1 year ago

There appears to be a discrepancy in the description of the algorithm.

The chart is an array with one slot for each position in the input string, where
position $0$ is before the first character and position $n$ is
immediately after the last character. So, the array has length $n+1$
for an input string of length $n$.

Unless I am mistaken the length of the array, as described in these two sentences is actually n+2. Else how do you have a slot both before and after an input of length n and end up with n+1?

jsiek commented 1 year ago

Let's say the input string is "hello". Then we have the following situation:

   h    e    l    l    o
0    1    2    3    4    5

so here we have n=5. The array length is 6, which is n+1, not n+2. Does that help?

jonbrandenburg commented 1 year ago

It does indeed. Thank you very much!

So the takeaway I have is that the chart represents positions in the interstitial space between each character in the input string with the start and end of string being special case which makes sense given the nature of the algorithm.

Thanks again!