Advertisement
Ladies_Man

Сканер и парсер

Dec 26th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.77 KB | None | 0 0
  1.  
  2. (define (list-head s i stek)
  3.    (if (= i 0) stek (list-head (cdr s) (- i 1) (append stek (list (car s))))))
  4.  
  5. (define (findclose s i)
  6.   (if (null? s)
  7.       #f
  8.       (if (eq? (car s) #\)) i (findclose (cdr s) (+ i 1)))))
  9.  
  10. (define (findopen s i)
  11.   (if (or (not i) (= i (- 1)))
  12.       #f
  13.       (if (eq? (list-ref s i) #\() i (findopen s (- i 1)))))
  14.  
  15. (define (convert s)
  16.   (if (string->number s) (string->number s) (string->symbol s)))
  17.  
  18. (define (read-string s)
  19.   (define (skan s i stek st)
  20.     (if (< i 0)
  21.         (if (eq? st "")
  22.             stek
  23.             (cons (convert st)stek))
  24.         (let ((a (string-ref s i)))
  25.           (cond ((or (eq? a #\() (eq? a #\))) (skan s (- i 1) (cons a (if (eq? st "") stek (cons (convert st) stek))) ""))
  26.             ((or (eq? a #\space) (eq? a #\newline) (eq? a #\tab))
  27.                  (if (eq? "" st)
  28.                      (skan s (- i 1) stek "")
  29.                      (skan s (- i 1) (cons (convert st) stek) "")))
  30.             (else (skan s (- i 1) stek (string-append (string a) st)))))))
  31.   (define (list->list s)
  32.     (let* ((r (findclose s 0))
  33.           (l (findopen s r)))
  34.          (if l
  35.              (list->list (append (list-head s l '()) (list (list-head (list-tail s (+ l 1)) (- r l 1) '())) (list-tail s (+ r 1))))
  36.              s)))
  37.   (define (checking s k)
  38.     (if (null? s)
  39.         (zero? k)
  40.         (if (< k 0)
  41.             #f
  42.             (cond ((eq? (car s) #\() (checking (cdr s) (+ k 1)))
  43.                   ((eq? (car s) #\)) (checking (cdr s) (- k 1)))
  44.             (else (checking (cdr s) k))))))
  45.   (define (check s)
  46.     (if (and (> (length s) 1) (not (eq? (car s) #\())) #f (checking s 0)))
  47.   (let ((s (skan s (- (string-length s) 1) '() "")))
  48.   (if (and (check s) (not (null? s))) (car (list->list s)) #f)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement