Parse EBNF: Difference between revisions
Content added Content deleted
m (→{{header|Tcl}}: Note that this is not exactly what the task description calls for, but it is what it probably *should* want) |
(Added PicoLisp) |
||
Line 10: | Line 10: | ||
factor : NUMBER ;</pre> |
factor : NUMBER ;</pre> |
||
=={{header|PicoLisp}}== |
|||
<lang PicoLisp>(de parse (Str) |
|||
(let *L (str Str "") |
|||
(aggr) ) ) |
|||
(de aggr () |
|||
(let X (prod) |
|||
(while (member (car *L) '("+" "-")) |
|||
(setq X (op (if (= "+" (pop '*L)) "PLUS" "MINUS") X (prod))) ) |
|||
X ) ) |
|||
(de prod () |
|||
(let X (term) |
|||
(while (member (car *L) '("*" "/")) |
|||
(setq X (op (if (= "*" (pop '*L)) "MULT" "DIV") X (term))) ) |
|||
X ) ) |
|||
(de term () |
|||
(let X (pop '*L) |
|||
(cond |
|||
((num? X) X) |
|||
((= "+" X) (term)) |
|||
((= "-" X) (pack "-" (term))) |
|||
((= "(" X) (prog1 (aggr) (pop '*L)))) ) ) ) |
|||
(de op (Op Ex1 Ex2) |
|||
(pack Op " " (subx Ex1) " " (subx Ex2)) ) |
|||
(de subx (Ex) |
|||
(if (sub? " " Ex) (pack "{" Ex "}") Ex) ) )</lang> |
|||
Output: |
|||
<pre>: (parse "1-2 - -3 * (4+5)") |
|||
-> "MINUS {MINUS 1 2} {MULT -3 {PLUS 4 5}}" |
|||
: (parse "1-2 - -3 * 4+5") |
|||
-> "PLUS {MINUS {MINUS 1 2} {MULT -3 4}} 5" |
|||
: (parse "(1+2+3)*-4/(1+2)") |
|||
-> "DIV {MULT {PLUS {PLUS 1 2} 3} -4} {PLUS 1 2}"</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |