Anonymous user
Parse EBNF: Difference between revisions
Parsing EBNF pattern
(→{{header|Tcl}}: Add in-progress, incomplete Ruby.) |
(Parsing EBNF pattern) |
||
Line 12:
=={{header|PicoLisp}}==
<lang PicoLisp>(def 'expr 'ebnf '(term ((PLUS | MINUS) term) *))
(def 'term 'ebnf '(factor ((MULT | DIV) factor) *))
(def 'factor 'ebnf '(NUMBER))</lang>
<lang PicoLisp>(de matchEbnf (Pat)
((asoq Pat '((PLUS . +) (MINUS . -) (MULT . *) (DIV . /)))
((== 'NUMBER Pat)
(cond
@ )
((and (= "-" (car *Lst)) (num? (cadr *Lst)))
(setq *Lst (cddr *Lst))
(- @) ) ) )
((get Pat 'ebnf) (parseLst @))
((atom Pat))
(T
(loop
(T (matchEbnf (pop 'Pat)) @)
(NIL Pat)
(NIL (== '| (pop 'Pat)))
(NIL Pat) ) ) ) )
(let
(
(NIL Pat)
(if (n== '* (cadr Pat))
(
(loop
(NIL *Lst)
(NIL (matchEbnf (car Pat)))
(setq X (list @ X (or (matchEbnf P) (throw)))) )
(setq Pat (cddr Pat)) ) )
X ) )
(de
(let
(catch NIL
(
▲ X ) )
▲ (let X (pop '*L)
▲ (cond
▲ ((num? X) X)
▲ ((= "+" X) (term))
▲ ((= "-" X) (pack "-" (term)))
▲ ((= "(" X) (prog1 (aggr) (pop '*L)))) ) ) )
Output:
<pre>: (
->
=={{header|Ruby}}==
|