Guest User

Untitled

a guest
Jul 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (require 'clojure.set)
  2. (require ['clojure.string :as 'string])
  3.  
  4.  
  5. (declare 'lex-table)
  6. (declare 'parse-table)
  7. (declare 'parse-single-token)
  8.  
  9.  
  10.  
  11. ;; Parser tables to load
  12. ;;(load-file "clojure/infix2-table.clj")
  13. (load-file "english.clj")
  14.  
  15. (defmacro dbg[x]
  16.   `(let [x# ~x]
  17.      (println "dbg:" '~x "=" x#)
  18.      x#))
  19.  
  20. (defmacro applyp [p & x]
  21.   ;; apply a predicate macro to a list, like 'and' or 'or',
  22.   ;; because I don't know the proper way to do it
  23.   `(eval (list* '~p ~@x)))
  24.  
  25. (defn ntrue? [n & x]
  26.   ;; Count the number of truths
  27.   (cond (<= n 0) true
  28.         (empty? x) false
  29.         :else (apply ntrue?
  30.                      (if (first x)
  31.                        (- n 1)
  32.                        n)
  33.                      (rest x))))
  34.  
  35.  
  36. (defn opify-vec [v]
  37.   ;; match up lexed bits with the actual op names
  38.   (some #(when (first %) %)
  39.         (map-indexed #(vector %2 (nth (keys lex-table) %1)) (rest v))))
  40.  
  41.  
  42. (defn replace-word [w]
  43.   (if (= :word (second w))
  44.     (vector (first w) (check-trie (first w) dict))
  45.     w))
  46.    
  47.  
  48. (defn lexify [s l-table]
  49.   ;; Build a vector of lexed tokens
  50.   (let [pat (re-pattern (str "(" (string/join ")|("  (vals l-table)) ")"))]
  51.     (map replace-word        
  52.          (map opify-vec (re-seq pat s)))))
  53.  
  54. (defn is-compound? [e p-table]
  55.   ;; Is the token a parser token?
  56.   (contains? p-table e))
  57.  
  58. (defn is-simple? [e p-table]
  59.   ;; Is the token a lexer token?
  60.   (not (is-compound? e p-table)))
  61.  
  62. (defn parse-token-vector [l-vec tok-entry p-table]
  63.   ;; Take the remainder of a sentence a vector of tokens and return the parsings
  64.   ;;  (dbg l-vec)
  65.   (let [tok-vec (first tok-entry)
  66.         tok-fun (second tok-entry)
  67.         ans (reduce
  68.              (fn [old-res new-tok]
  69.                (if (old-res :parse?)
  70.                  (merge-with #(concat %2 %1)
  71.                              (parse-single-token (get old-res :rest) [new-tok] p-table)
  72.                              {:value (old-res :value)})
  73.                  {:parse? false :exit :token}))
  74.              {:parse? true :rest l-vec :value []}
  75.              tok-vec)]
  76.     (if (get ans :parse?)
  77.       (assoc ans :value (vector (tok-fun (get ans :value))))
  78.       ans)))
  79.  
  80.  
  81.  
  82. (defn parse-single-token [l-vec token-vec p-table]
  83.   ;; Take the remainder of a sentence and a vector with a token in it, return the parsings
  84.   ;;(dbg l-vec)
  85.   (let [cur-tok (first token-vec)]  
  86.     (cond (and (empty? l-vec) (nil? cur-tok)) {:parse? true :rest [] :exit :finished}
  87.           (and (empty? l-vec) (not (nil? cur-tok))) {:parse? false :exit :run-out}
  88.           (and (not (empty? l-vec)) (nil? cur-tok)) {:parse? true :rest l-vec}
  89.           (is-simple? cur-tok p-table) (if (= cur-tok (second (first l-vec)))
  90.                                          {:parse? true :rest (rest l-vec) :value (vector (ffirst l-vec))}
  91.                                          {:parse? false :exit :wrong-simpleton})
  92.           :else        
  93.           (let [init-p (map #(parse-token-vector l-vec % p-table)
  94.                             (get p-table cur-tok))
  95.                 parsings (filter #(get % :parse?) init-p)
  96.                 fin (filter #(empty? (get % :rest)) parsings)]
  97.  
  98.             (cond (zero? (count parsings))  {:parse? false :exit :no-compounds}
  99.                   (not (empty? fin)) (first fin)
  100.                   :else (first parsings))))))
  101.  
  102. (defn parse [s]
  103.   (let [ans (parse-single-token (lexify s lex-table) (vector (first parse-table)) (second parse-table))]
  104.     (if (and (ans :parse?)
  105.              (zero? (count (ans :rest))))
  106.       (first (get ans :value))
  107.       nil)))
Add Comment
Please, Sign In to add comment