Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Recursive descent parser in PARLANSE, a C-like langauge with LISP syntax: [70 lines, 1376 characters not counting indent-by-4 needed by SO] EDIT: Rules changed, somebody insisted on floating point numbers, fixed. No library calls except the float conversion, input and print. [now 94 lines, 1825 characters]</p> <pre><code>(define main (procedure void) (local (;; (define f (function float void)) (= [s string] (append (input) "$")) (= [i natural] 1) (define S (lambda f (let (= v (P)) (value (loop (case s:i) "+" (;; (+= i) (+= v (P) );; "-" (;; (+= i) (-= v (P) );; else (return v) )case )loop v )value )define (define P (lambda f (let (= v (T)) (value (loop (case s:i) "*" (;; (+= i) (= v (* v (T)) );; "/" (;; (+= i) (= v (/ v (T)) );; else (return v) )case )loop v )value )define (define T (lambda f (let (= v (O)) (value (loop (case s:i) "^" (;; (+= i) (= v (** v (T)) );; else (return v) )case )loop v )value )define (define O (lambda f (let (= v +0) (value (case s:i) "(" (;; (+= i) (= v (E)) (+= i) );; "-" (;; (+= i) (= v (- 0.0 (O))) );; else (= v (StringToFloat (F)) )value v )let )define (define F (lambda f) (let (= n (N)) (value (;; (ifthen (== s:i ".") (;; (+= i) (= n (append n ".")) (= n (concatenate n (N))) );; )ifthen (ifthen (== s:i "E") (;; (+= i) (= n (append n "E")) (ifthen (== s:i "-") (;; (+= i) (= n (append n "-")) (= n (concatenate n (N))) );; );; )ifthen );; n )let )define (define N (lambda (function string string) (case s:i (any "0" "1" "2" "3" "4" "5" "6" "7" "8" "9") (value (+= i) (append ? s:(-- i)) )value else ? )case )define );; (print (S)) )local )define </code></pre> <p>Assumes a well-formed expression, float numbers with at least one leading digit, exponents optional as Enn or E-nnn. Not compiled and run.</p> <p>Pecularities: the definition f is essentially signature typedef. The lambdas are the parsing functions, one per grammar rule. A function F is called by writing (F args). PARLANSE functions are lexically scoped, so each function has implicit access to the expression s to be evaluated and a string scanning index i. </p> <p>The grammar implemented is:</p> <pre><code>E = S $ ; S = P ; S = S + P ; P = T ; P = P * T ; T = O ; T = O ^ T ; O = ( S ) ; O = - O ; O = F ; F = digits {. digits} { E {-} digits} ; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload