earley.k 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. / Inspired by [[https://codeberg.org/effbiae/earley/src/branch/main]]
  2. / Well written tutorial here: [[https://loup-vaillant.fr/tutorials/earley-parsing/]]
  3. / Earley item: (rule; dot position; origin; id)
  4. empty:3#,!0
  5. id:{(1+#'x`p`i`i)/y}
  6. dot:{@[x[`t]@n;&x[`p;1+y[0]]=n:1+y[1]+x[`p;y[0]];:;" "]}
  7. aug:{,/1((id[x];dot[x])@\:)\y}
  8. data:{R:,/',/+'((*:;1_"|"\"|",)@'":"\)'" "\x
  9. `p`t`i`bp!(+\0,#'R;,/R;y,"\0";(,0N 0N)!,())}
  10. pred:{[d;s;c;t]w:&(-1_d[`t]d[`p])=/:?c;(s;++(*|w;0;s+&#*w);!0)}
  11. scan:{[d;s;c;t]w:&d[`i;s]=/:c;(s+1;@[3#t[s]@\:w;1;+;1];,'s,/:w)}
  12. comp:{[d;s;c;t]$[~#c:&^c;:(s;empty;!0);]
  13. f:(f@;::)@'w:&(d[`t;d[`p;t[s;0;c]]])=t[;4]f:t[s;2;c]
  14. (s;{[x;y;z;w]y,'@[3#x[z;;w];1;+;1]}[t]/[empty].f;+(+f;s,/:c@w 0))}
  15. stp:{add[y 0;y 1]@[r:z[y 0;x;y[1;x;4];y 1];1;aug[y 0]]}
  16. add:{n:(z[1])@\:&((!#i)=i?i)&w:^(y[z 0;3])?i:z[1;3]
  17. $[~#*|nxt:(+(z[0];i); z[2]);;x[`bp]:{@[x;y;?,;,z]}/[x`bp].nxt]
  18. (x;@[y;z 0;,';n])}
  19. rec0:{{(d;st;s):$[n:#**|x 1;(x[0];x[1],,empty,(!0;"");1+x 2);:x]
  20. ({y stp[x]/(pred;scan;comp)}[s]/(d;st)),s}/(x;,aug[x;y];-1)}
  21. rec1:{s:++(&=/1*:\x[`t]x`p;0;0);rec0[x;s]}
  22. rec:{-1_rec1 data[x;y]}
  23. cmbos:{!/:/({y,x[y]?z}[x].'!y;1_'(,0N){,/x,/:\:,'y}/y)}
  24. dsp:{[d;t]rules:{?[x[`t]y+!z;1;":"]}[d].'+-1 1_'1(-':)\d[`p]
  25. itms:{x[z;1]{?[y;2+x;"."]}'y@x[z;0]}[t;rules]'!-1+#t
  26. (rules;itms,''"/",/:/:$-1_t[;2])}
  27. tree:{$[^*z;:0N;]
  28. ls:.[y[0];]'-1_c:1_|(*x@)\z
  29. (y[1]. z;(c~'0N)'[c:o[x;y]'*'1_'x@1_c;ls])}
  30. trees:{?,/(cmbos[y[;3]]x[`bp]_0N 0N)tree[;(y[;4];z);]/:\:lst,/:&~y[lst:-2+#y;2]}
  31. apt:{(dp;nd):+{$[~`A~@y;,(x;y);(,(x;*y)),,/o[x+1]'y 1]}[0;x];(nd;tree.p@dp)}
  32. /
  33. / back pointers:
  34. / gives all possible choices of back pointers
  35. cmbos[r[1;;3]]r[0;`bp]_0N 0N
  36. / display
  37. dsp[d;t]
  38. tree[bp;start]