earley.sch 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. ;;; EARLEY -- Earley's parser, written by Marc Feeley.
  2. ; $Id: earley.sch,v 1.2 1999/07/12 18:05:19 lth Exp $
  3. ; 990708 / lth -- changed 'main' to 'earley-benchmark'.
  4. ;
  5. ; (make-parser grammar lexer) is used to create a parser from the grammar
  6. ; description `grammar' and the lexer function `lexer'.
  7. ;
  8. ; A grammar is a list of definitions. Each definition defines a non-terminal
  9. ; by a set of rules. Thus a definition has the form: (nt rule1 rule2...).
  10. ; A given non-terminal can only be defined once. The first non-terminal
  11. ; defined is the grammar's goal. Each rule is a possibly empty list of
  12. ; non-terminals. Thus a rule has the form: (nt1 nt2...). A non-terminal
  13. ; can be any scheme value. Note that all grammar symbols are treated as
  14. ; non-terminals. This is fine though because the lexer will be outputing
  15. ; non-terminals.
  16. ;
  17. ; The lexer defines what a token is and the mapping between tokens and
  18. ; the grammar's non-terminals. It is a function of one argument, the input,
  19. ; that returns the list of tokens corresponding to the input. Each token is
  20. ; represented by a list. The first element is some `user-defined' information
  21. ; associated with the token and the rest represents the token's class(es) (as a
  22. ; list of non-terminals that this token corresponds to).
  23. ;
  24. ; The result of `make-parser' is a function that parses the single input it
  25. ; is given into the grammar's goal. The result is a `parse' which can be
  26. ; manipulated with the procedures: `parse->parsed?', `parse->trees'
  27. ; and `parse->nb-trees' (see below).
  28. ;
  29. ; Let's assume that we want a parser for the grammar
  30. ;
  31. ; S -> x = E
  32. ; E -> E + E | V
  33. ; V -> V y |
  34. ;
  35. ; and that the input to the parser is a string of characters. Also, assume we
  36. ; would like to map the characters `x', `y', `+' and `=' into the corresponding
  37. ; non-terminals in the grammar. Such a parser could be created with
  38. ;
  39. ; (make-parser
  40. ; '(
  41. ; (s (x = e))
  42. ; (e (e + e) (v))
  43. ; (v (v y) ())
  44. ; )
  45. ; (lambda (str)
  46. ; (map (lambda (char)
  47. ; (list char ; user-info = the character itself
  48. ; (case char
  49. ; ((#\x) 'x)
  50. ; ((#\y) 'y)
  51. ; ((#\+) '+)
  52. ; ((#\=) '=)
  53. ; (else (fatal-error "lexer error")))))
  54. ; (string->list str)))
  55. ; )
  56. ;
  57. ; An alternative definition (that does not check for lexical errors) is
  58. ;
  59. ; (make-parser
  60. ; '(
  61. ; (s (#\x #\= e))
  62. ; (e (e #\+ e) (v))
  63. ; (v (v #\y) ())
  64. ; )
  65. ; (lambda (str) (map (lambda (char) (list char char)) (string->list str)))
  66. ; )
  67. ;
  68. ; To help with the rest of the discussion, here are a few definitions:
  69. ;
  70. ; An input pointer (for an input of `n' tokens) is a value between 0 and `n'.
  71. ; It indicates a point between two input tokens (0 = beginning, `n' = end).
  72. ; For example, if `n' = 4, there are 5 input pointers:
  73. ;
  74. ; input token1 token2 token3 token4
  75. ; input pointers 0 1 2 3 4
  76. ;
  77. ; A configuration indicates the extent to which a given rule is parsed (this
  78. ; is the common `dot notation'). For simplicity, a configuration is
  79. ; represented as an integer, with successive configurations in the same
  80. ; rule associated with successive integers. It is assumed that the grammar
  81. ; has been extended with rules to aid scanning. These rules are of the
  82. ; form `nt ->', and there is one such rule for every non-terminal. Note
  83. ; that these rules are special because they only apply when the corresponding
  84. ; non-terminal is returned by the lexer.
  85. ;
  86. ; A configuration set is a configuration grouped with the set of input pointers
  87. ; representing where the head non-terminal of the configuration was predicted.
  88. ;
  89. ; Here are the rules and configurations for the grammar given above:
  90. ;
  91. ; S -> . \
  92. ; 0 |
  93. ; x -> . |
  94. ; 1 |
  95. ; = -> . |
  96. ; 2 |
  97. ; E -> . |
  98. ; 3 > special rules (for scanning)
  99. ; + -> . |
  100. ; 4 |
  101. ; V -> . |
  102. ; 5 |
  103. ; y -> . |
  104. ; 6 /
  105. ; S -> . x . = . E .
  106. ; 7 8 9 10
  107. ; E -> . E . + . E .
  108. ; 11 12 13 14
  109. ; E -> . V .
  110. ; 15 16
  111. ; V -> . V . y .
  112. ; 17 18 19
  113. ; V -> .
  114. ; 20
  115. ;
  116. ; Starters of the non-terminal `nt' are configurations that are leftmost
  117. ; in a non-special rule for `nt'. Enders of the non-terminal `nt' are
  118. ; configurations that are rightmost in any rule for `nt'. Predictors of the
  119. ; non-terminal `nt' are configurations that are directly to the left of `nt'
  120. ; in any rule.
  121. ;
  122. ; For the grammar given above,
  123. ;
  124. ; Starters of V = (17 20)
  125. ; Enders of V = (5 19 20)
  126. ; Predictors of V = (15 17)
  127. (define (make-parser grammar lexer)
  128. (define (non-terminals grammar) ; return vector of non-terminals in grammar
  129. (define (add-nt nt nts)
  130. (if (member nt nts) nts (cons nt nts))) ; use equal? for equality tests
  131. (let def-loop ((defs grammar) (nts '()))
  132. (if (pair? defs)
  133. (let* ((def (car defs))
  134. (head (car def)))
  135. (let rule-loop ((rules (cdr def))
  136. (nts (add-nt head nts)))
  137. (if (pair? rules)
  138. (let ((rule (car rules)))
  139. (let loop ((l rule) (nts nts))
  140. (if (pair? l)
  141. (let ((nt (car l)))
  142. (loop (cdr l) (add-nt nt nts)))
  143. (rule-loop (cdr rules) nts))))
  144. (def-loop (cdr defs) nts))))
  145. (list->vector (reverse nts))))) ; goal non-terminal must be at index 0
  146. (define (ind nt nts) ; return index of non-terminal `nt' in `nts'
  147. (let loop ((i (- (vector-length nts) 1)))
  148. (if (>= i 0)
  149. (if (equal? (vector-ref nts i) nt) i (loop (- i 1)))
  150. #f)))
  151. (define (nb-configurations grammar) ; return nb of configurations in grammar
  152. (let def-loop ((defs grammar) (nb-confs 0))
  153. (if (pair? defs)
  154. (let ((def (car defs)))
  155. (let rule-loop ((rules (cdr def)) (nb-confs nb-confs))
  156. (if (pair? rules)
  157. (let ((rule (car rules)))
  158. (let loop ((l rule) (nb-confs nb-confs))
  159. (if (pair? l)
  160. (loop (cdr l) (+ nb-confs 1))
  161. (rule-loop (cdr rules) (+ nb-confs 1)))))
  162. (def-loop (cdr defs) nb-confs))))
  163. nb-confs)))
  164. ; First, associate a numeric identifier to every non-terminal in the
  165. ; grammar (with the goal non-terminal associated with 0).
  166. ;
  167. ; So, for the grammar given above we get:
  168. ;
  169. ; s -> 0 x -> 1 = -> 4 e ->3 + -> 4 v -> 5 y -> 6
  170. (let* ((nts (non-terminals grammar)) ; id map = list of non-terms
  171. (nb-nts (vector-length nts)) ; the number of non-terms
  172. (nb-confs (+ (nb-configurations grammar) nb-nts)) ; the nb of confs
  173. (starters (make-vector nb-nts '())) ; starters for every non-term
  174. (enders (make-vector nb-nts '())) ; enders for every non-term
  175. (predictors (make-vector nb-nts '())) ; predictors for every non-term
  176. (steps (make-vector nb-confs #f)) ; what to do in a given conf
  177. (names (make-vector nb-confs #f))) ; name of rules
  178. (define (setup-tables grammar nts starters enders predictors steps names)
  179. (define (add-conf conf nt nts class)
  180. (let ((i (ind nt nts)))
  181. (vector-set! class i (cons conf (vector-ref class i)))))
  182. (let ((nb-nts (vector-length nts)))
  183. (let nt-loop ((i (- nb-nts 1)))
  184. (if (>= i 0)
  185. (begin
  186. (vector-set! steps i (- i nb-nts))
  187. (vector-set! names i (list (vector-ref nts i) 0))
  188. (vector-set! enders i (list i))
  189. (nt-loop (- i 1)))))
  190. (let def-loop ((defs grammar) (conf (vector-length nts)))
  191. (if (pair? defs)
  192. (let* ((def (car defs))
  193. (head (car def)))
  194. (let rule-loop ((rules (cdr def)) (conf conf) (rule-num 1))
  195. (if (pair? rules)
  196. (let ((rule (car rules)))
  197. (vector-set! names conf (list head rule-num))
  198. (add-conf conf head nts starters)
  199. (let loop ((l rule) (conf conf))
  200. (if (pair? l)
  201. (let ((nt (car l)))
  202. (vector-set! steps conf (ind nt nts))
  203. (add-conf conf nt nts predictors)
  204. (loop (cdr l) (+ conf 1)))
  205. (begin
  206. (vector-set! steps conf (- (ind head nts) nb-nts))
  207. (add-conf conf head nts enders)
  208. (rule-loop (cdr rules) (+ conf 1) (+ rule-num 1))))))
  209. (def-loop (cdr defs) conf))))))))
  210. ; Now, for each non-terminal, compute the starters, enders and predictors and
  211. ; the names and steps tables.
  212. (setup-tables grammar nts starters enders predictors steps names)
  213. ; Build the parser description
  214. (let ((parser-descr (vector lexer
  215. nts
  216. starters
  217. enders
  218. predictors
  219. steps
  220. names)))
  221. (lambda (input)
  222. (define (ind nt nts) ; return index of non-terminal `nt' in `nts'
  223. (let loop ((i (- (vector-length nts) 1)))
  224. (if (>= i 0)
  225. (if (equal? (vector-ref nts i) nt) i (loop (- i 1)))
  226. #f)))
  227. (define (comp-tok tok nts) ; transform token to parsing format
  228. (let loop ((l1 (cdr tok)) (l2 '()))
  229. (if (pair? l1)
  230. (let ((i (ind (car l1) nts)))
  231. (if i
  232. (loop (cdr l1) (cons i l2))
  233. (loop (cdr l1) l2)))
  234. (cons (car tok) (reverse l2)))))
  235. (define (input->tokens input lexer nts)
  236. (list->vector (map (lambda (tok) (comp-tok tok nts)) (lexer input))))
  237. (define (make-states nb-toks nb-confs)
  238. (let ((states (make-vector (+ nb-toks 1) #f)))
  239. (let loop ((i nb-toks))
  240. (if (>= i 0)
  241. (let ((v (make-vector (+ nb-confs 1) #f)))
  242. (vector-set! v 0 -1)
  243. (vector-set! states i v)
  244. (loop (- i 1)))
  245. states))))
  246. (define (conf-set-get state conf)
  247. (vector-ref state (+ conf 1)))
  248. (define (conf-set-get* state state-num conf)
  249. (let ((conf-set (conf-set-get state conf)))
  250. (if conf-set
  251. conf-set
  252. (let ((conf-set (make-vector (+ state-num 6) #f)))
  253. (vector-set! conf-set 1 -3) ; old elems tail (points to head)
  254. (vector-set! conf-set 2 -1) ; old elems head
  255. (vector-set! conf-set 3 -1) ; new elems tail (points to head)
  256. (vector-set! conf-set 4 -1) ; new elems head
  257. (vector-set! state (+ conf 1) conf-set)
  258. conf-set))))
  259. (define (conf-set-merge-new! conf-set)
  260. (vector-set! conf-set
  261. (+ (vector-ref conf-set 1) 5)
  262. (vector-ref conf-set 4))
  263. (vector-set! conf-set 1 (vector-ref conf-set 3))
  264. (vector-set! conf-set 3 -1)
  265. (vector-set! conf-set 4 -1))
  266. (define (conf-set-head conf-set)
  267. (vector-ref conf-set 2))
  268. (define (conf-set-next conf-set i)
  269. (vector-ref conf-set (+ i 5)))
  270. (define (conf-set-member? state conf i)
  271. (let ((conf-set (vector-ref state (+ conf 1))))
  272. (if conf-set
  273. (conf-set-next conf-set i)
  274. #f)))
  275. (define (conf-set-adjoin state conf-set conf i)
  276. (let ((tail (vector-ref conf-set 3))) ; put new element at tail
  277. (vector-set! conf-set (+ i 5) -1)
  278. (vector-set! conf-set (+ tail 5) i)
  279. (vector-set! conf-set 3 i)
  280. (if (< tail 0)
  281. (begin
  282. (vector-set! conf-set 0 (vector-ref state 0))
  283. (vector-set! state 0 conf)))))
  284. (define (conf-set-adjoin* states state-num l i)
  285. (let ((state (vector-ref states state-num)))
  286. (let loop ((l1 l))
  287. (if (pair? l1)
  288. (let* ((conf (car l1))
  289. (conf-set (conf-set-get* state state-num conf)))
  290. (if (not (conf-set-next conf-set i))
  291. (begin
  292. (conf-set-adjoin state conf-set conf i)
  293. (loop (cdr l1)))
  294. (loop (cdr l1))))))))
  295. (define (conf-set-adjoin** states states* state-num conf i)
  296. (let ((state (vector-ref states state-num)))
  297. (if (conf-set-member? state conf i)
  298. (let* ((state* (vector-ref states* state-num))
  299. (conf-set* (conf-set-get* state* state-num conf)))
  300. (if (not (conf-set-next conf-set* i))
  301. (conf-set-adjoin state* conf-set* conf i))
  302. #t)
  303. #f)))
  304. (define (conf-set-union state conf-set conf other-set)
  305. (let loop ((i (conf-set-head other-set)))
  306. (if (>= i 0)
  307. (if (not (conf-set-next conf-set i))
  308. (begin
  309. (conf-set-adjoin state conf-set conf i)
  310. (loop (conf-set-next other-set i)))
  311. (loop (conf-set-next other-set i))))))
  312. (define (forw states state-num starters enders predictors steps nts)
  313. (define (predict state state-num conf-set conf nt starters enders)
  314. ; add configurations which start the non-terminal `nt' to the
  315. ; right of the dot
  316. (let loop1 ((l (vector-ref starters nt)))
  317. (if (pair? l)
  318. (let* ((starter (car l))
  319. (starter-set (conf-set-get* state state-num starter)))
  320. (if (not (conf-set-next starter-set state-num))
  321. (begin
  322. (conf-set-adjoin state starter-set starter state-num)
  323. (loop1 (cdr l)))
  324. (loop1 (cdr l))))))
  325. ; check for possible completion of the non-terminal `nt' to the
  326. ; right of the dot
  327. (let loop2 ((l (vector-ref enders nt)))
  328. (if (pair? l)
  329. (let ((ender (car l)))
  330. (if (conf-set-member? state ender state-num)
  331. (let* ((next (+ conf 1))
  332. (next-set (conf-set-get* state state-num next)))
  333. (conf-set-union state next-set next conf-set)
  334. (loop2 (cdr l)))
  335. (loop2 (cdr l)))))))
  336. (define (reduce states state state-num conf-set head preds)
  337. ; a non-terminal is now completed so check for reductions that
  338. ; are now possible at the configurations `preds'
  339. (let loop1 ((l preds))
  340. (if (pair? l)
  341. (let ((pred (car l)))
  342. (let loop2 ((i head))
  343. (if (>= i 0)
  344. (let ((pred-set (conf-set-get (vector-ref states i) pred)))
  345. (if pred-set
  346. (let* ((next (+ pred 1))
  347. (next-set (conf-set-get* state state-num next)))
  348. (conf-set-union state next-set next pred-set)))
  349. (loop2 (conf-set-next conf-set i)))
  350. (loop1 (cdr l))))))))
  351. (let ((state (vector-ref states state-num))
  352. (nb-nts (vector-length nts)))
  353. (let loop ()
  354. (let ((conf (vector-ref state 0)))
  355. (if (>= conf 0)
  356. (let* ((step (vector-ref steps conf))
  357. (conf-set (vector-ref state (+ conf 1)))
  358. (head (vector-ref conf-set 4)))
  359. (vector-set! state 0 (vector-ref conf-set 0))
  360. (conf-set-merge-new! conf-set)
  361. (if (>= step 0)
  362. (predict state state-num conf-set conf step starters enders)
  363. (let ((preds (vector-ref predictors (+ step nb-nts))))
  364. (reduce states state state-num conf-set head preds)))
  365. (loop)))))))
  366. (define (forward starters enders predictors steps nts toks)
  367. (let* ((nb-toks (vector-length toks))
  368. (nb-confs (vector-length steps))
  369. (states (make-states nb-toks nb-confs))
  370. (goal-starters (vector-ref starters 0)))
  371. (conf-set-adjoin* states 0 goal-starters 0) ; predict goal
  372. (forw states 0 starters enders predictors steps nts)
  373. (let loop ((i 0))
  374. (if (< i nb-toks)
  375. (let ((tok-nts (cdr (vector-ref toks i))))
  376. (conf-set-adjoin* states (+ i 1) tok-nts i) ; scan token
  377. (forw states (+ i 1) starters enders predictors steps nts)
  378. (loop (+ i 1)))))
  379. states))
  380. (define (produce conf i j enders steps toks states states* nb-nts)
  381. (let ((prev (- conf 1)))
  382. (if (and (>= conf nb-nts) (>= (vector-ref steps prev) 0))
  383. (let loop1 ((l (vector-ref enders (vector-ref steps prev))))
  384. (if (pair? l)
  385. (let* ((ender (car l))
  386. (ender-set (conf-set-get (vector-ref states j)
  387. ender)))
  388. (if ender-set
  389. (let loop2 ((k (conf-set-head ender-set)))
  390. (if (>= k 0)
  391. (begin
  392. (and (>= k i)
  393. (conf-set-adjoin** states states* k prev i)
  394. (conf-set-adjoin** states states* j ender k))
  395. (loop2 (conf-set-next ender-set k)))
  396. (loop1 (cdr l))))
  397. (loop1 (cdr l)))))))))
  398. (define (back states states* state-num enders steps nb-nts toks)
  399. (let ((state* (vector-ref states* state-num)))
  400. (let loop1 ()
  401. (let ((conf (vector-ref state* 0)))
  402. (if (>= conf 0)
  403. (let* ((conf-set (vector-ref state* (+ conf 1)))
  404. (head (vector-ref conf-set 4)))
  405. (vector-set! state* 0 (vector-ref conf-set 0))
  406. (conf-set-merge-new! conf-set)
  407. (let loop2 ((i head))
  408. (if (>= i 0)
  409. (begin
  410. (produce conf i state-num enders steps
  411. toks states states* nb-nts)
  412. (loop2 (conf-set-next conf-set i)))
  413. (loop1)))))))))
  414. (define (backward states enders steps nts toks)
  415. (let* ((nb-toks (vector-length toks))
  416. (nb-confs (vector-length steps))
  417. (nb-nts (vector-length nts))
  418. (states* (make-states nb-toks nb-confs))
  419. (goal-enders (vector-ref enders 0)))
  420. (let loop1 ((l goal-enders))
  421. (if (pair? l)
  422. (let ((conf (car l)))
  423. (conf-set-adjoin** states states* nb-toks conf 0)
  424. (loop1 (cdr l)))))
  425. (let loop2 ((i nb-toks))
  426. (if (>= i 0)
  427. (begin
  428. (back states states* i enders steps nb-nts toks)
  429. (loop2 (- i 1)))))
  430. states*))
  431. (define (parsed? nt i j nts enders states)
  432. (let ((nt* (ind nt nts)))
  433. (if nt*
  434. (let ((nb-nts (vector-length nts)))
  435. (let loop ((l (vector-ref enders nt*)))
  436. (if (pair? l)
  437. (let ((conf (car l)))
  438. (if (conf-set-member? (vector-ref states j) conf i)
  439. #t
  440. (loop (cdr l))))
  441. #f)))
  442. #f)))
  443. (define (deriv-trees conf i j enders steps names toks states nb-nts)
  444. (let ((name (vector-ref names conf)))
  445. (if name ; `conf' is at the start of a rule (either special or not)
  446. (if (< conf nb-nts)
  447. (list (list name (car (vector-ref toks i))))
  448. (list (list name)))
  449. (let ((prev (- conf 1)))
  450. (let loop1 ((l1 (vector-ref enders (vector-ref steps prev)))
  451. (l2 '()))
  452. (if (pair? l1)
  453. (let* ((ender (car l1))
  454. (ender-set (conf-set-get (vector-ref states j)
  455. ender)))
  456. (if ender-set
  457. (let loop2 ((k (conf-set-head ender-set)) (l2 l2))
  458. (if (>= k 0)
  459. (if (and (>= k i)
  460. (conf-set-member? (vector-ref states k)
  461. prev i))
  462. (let ((prev-trees
  463. (deriv-trees prev i k enders steps names
  464. toks states nb-nts))
  465. (ender-trees
  466. (deriv-trees ender k j enders steps names
  467. toks states nb-nts)))
  468. (let loop3 ((l3 ender-trees) (l2 l2))
  469. (if (pair? l3)
  470. (let ((ender-tree (list (car l3))))
  471. (let loop4 ((l4 prev-trees) (l2 l2))
  472. (if (pair? l4)
  473. (loop4 (cdr l4)
  474. (cons (append (car l4)
  475. ender-tree)
  476. l2))
  477. (loop3 (cdr l3) l2))))
  478. (loop2 (conf-set-next ender-set k) l2))))
  479. (loop2 (conf-set-next ender-set k) l2))
  480. (loop1 (cdr l1) l2)))
  481. (loop1 (cdr l1) l2)))
  482. l2))))))
  483. (define (deriv-trees* nt i j nts enders steps names toks states)
  484. (let ((nt* (ind nt nts)))
  485. (if nt*
  486. (let ((nb-nts (vector-length nts)))
  487. (let loop ((l (vector-ref enders nt*)) (trees '()))
  488. (if (pair? l)
  489. (let ((conf (car l)))
  490. (if (conf-set-member? (vector-ref states j) conf i)
  491. (loop (cdr l)
  492. (append (deriv-trees conf i j enders steps names
  493. toks states nb-nts)
  494. trees))
  495. (loop (cdr l) trees)))
  496. trees)))
  497. #f)))
  498. (define (nb-deriv-trees conf i j enders steps toks states nb-nts)
  499. (let ((prev (- conf 1)))
  500. (if (or (< conf nb-nts) (< (vector-ref steps prev) 0))
  501. 1
  502. (let loop1 ((l (vector-ref enders (vector-ref steps prev)))
  503. (n 0))
  504. (if (pair? l)
  505. (let* ((ender (car l))
  506. (ender-set (conf-set-get (vector-ref states j)
  507. ender)))
  508. (if ender-set
  509. (let loop2 ((k (conf-set-head ender-set)) (n n))
  510. (if (>= k 0)
  511. (if (and (>= k i)
  512. (conf-set-member? (vector-ref states k)
  513. prev i))
  514. (let ((nb-prev-trees
  515. (nb-deriv-trees prev i k enders steps
  516. toks states nb-nts))
  517. (nb-ender-trees
  518. (nb-deriv-trees ender k j enders steps
  519. toks states nb-nts)))
  520. (loop2 (conf-set-next ender-set k)
  521. (+ n (* nb-prev-trees nb-ender-trees))))
  522. (loop2 (conf-set-next ender-set k) n))
  523. (loop1 (cdr l) n)))
  524. (loop1 (cdr l) n)))
  525. n)))))
  526. (define (nb-deriv-trees* nt i j nts enders steps toks states)
  527. (let ((nt* (ind nt nts)))
  528. (if nt*
  529. (let ((nb-nts (vector-length nts)))
  530. (let loop ((l (vector-ref enders nt*)) (nb-trees 0))
  531. (if (pair? l)
  532. (let ((conf (car l)))
  533. (if (conf-set-member? (vector-ref states j) conf i)
  534. (loop (cdr l)
  535. (+ (nb-deriv-trees conf i j enders steps
  536. toks states nb-nts)
  537. nb-trees))
  538. (loop (cdr l) nb-trees)))
  539. nb-trees)))
  540. #f)))
  541. (let* ((lexer (vector-ref parser-descr 0))
  542. (nts (vector-ref parser-descr 1))
  543. (starters (vector-ref parser-descr 2))
  544. (enders (vector-ref parser-descr 3))
  545. (predictors (vector-ref parser-descr 4))
  546. (steps (vector-ref parser-descr 5))
  547. (names (vector-ref parser-descr 6))
  548. (toks (input->tokens input lexer nts)))
  549. (vector nts
  550. starters
  551. enders
  552. predictors
  553. steps
  554. names
  555. toks
  556. (backward (forward starters enders predictors steps nts toks)
  557. enders steps nts toks)
  558. parsed?
  559. deriv-trees*
  560. nb-deriv-trees*))))))
  561. (define (parse->parsed? parse nt i j)
  562. (let* ((nts (vector-ref parse 0))
  563. (enders (vector-ref parse 2))
  564. (states (vector-ref parse 7))
  565. (parsed? (vector-ref parse 8)))
  566. (parsed? nt i j nts enders states)))
  567. (define (parse->trees parse nt i j)
  568. (let* ((nts (vector-ref parse 0))
  569. (enders (vector-ref parse 2))
  570. (steps (vector-ref parse 4))
  571. (names (vector-ref parse 5))
  572. (toks (vector-ref parse 6))
  573. (states (vector-ref parse 7))
  574. (deriv-trees* (vector-ref parse 9)))
  575. (deriv-trees* nt i j nts enders steps names toks states)))
  576. (define (parse->nb-trees parse nt i j)
  577. (let* ((nts (vector-ref parse 0))
  578. (enders (vector-ref parse 2))
  579. (steps (vector-ref parse 4))
  580. (toks (vector-ref parse 6))
  581. (states (vector-ref parse 7))
  582. (nb-deriv-trees* (vector-ref parse 10)))
  583. (nb-deriv-trees* nt i j nts enders steps toks states)))
  584. (define (test k)
  585. (let ((p (make-parser '( (s (a) (s s)) )
  586. (lambda (l) (map (lambda (x) (list x x)) l)))))
  587. (let ((x (p (vector->list (make-vector k 'a)))))
  588. (length (parse->trees x 's 0 k)))))
  589. (define (earley-benchmark . args)
  590. (let ((k (if (null? args) 9 (car args))))
  591. (run-benchmark
  592. "earley"
  593. 1
  594. (lambda () (test k))
  595. (lambda (result)
  596. (display result)
  597. (newline)
  598. #t))))