parse.lisp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. ;; parse.lisp -- Proposition string parsing subroutines
  2. ;; Copyright (C) 2024 Alexander Rosenberg
  3. ;;
  4. ;; This program is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. (in-package :truth-table/base)
  17. (defun whitespace-p (char)
  18. "Return nil unless CHAR is whitespace."
  19. (member char '(#\newline #\space) :test 'eq))
  20. (defun paren-p (char)
  21. "Return nil unless CHAR is a parenthesis."
  22. (member char '(#\( #\))))
  23. (defun delim-p (char)
  24. "Return nil unless CHAR is either whitespace or a parenthesis."
  25. (or (whitespace-p char) (paren-p char)))
  26. (defun symbol-char-p (char)
  27. "Return nil until CHAR is a valid character for use in proposition variable
  28. names."
  29. (or (alpha-char-p char) (eq char #\_) (digit-char-p char)))
  30. (defun replace-in-string (str chars new-char)
  31. "Replace all instances of any of CHARS in STR with NEW-CHAR."
  32. (coerce (loop with lchars = (if (atom chars)
  33. (list chars)
  34. chars)
  35. for char across str
  36. when (member char lchars)
  37. collect new-char
  38. else
  39. collect char)
  40. 'string))
  41. (define-condition proposition-parse-error (error)
  42. ((position :initarg :position
  43. :accessor parse-error-position
  44. :initform nil)
  45. (proposition :initarg :proposition
  46. :accessor parse-error-proposition
  47. :initform nil)
  48. (message :initarg :message
  49. :accessor parse-error-message
  50. :initform nil))
  51. (:report (lambda (con stream)
  52. (with-slots (position proposition message) con
  53. (format stream
  54. "parse error~@[ at column ~d~]~@[: ~a~]~
  55. ~@[:~% ~a~@[~% ~a^~]~]"
  56. (when position
  57. (1+ position))
  58. message
  59. (when proposition
  60. (replace-in-string proposition
  61. '(#\newline #\return)
  62. #\space))
  63. (when position
  64. (make-string position
  65. :initial-element #\space))))))
  66. (:documentation "Condition representing an error during parsing of a
  67. proposition."))
  68. (defparameter *operator-symbol-table*
  69. '((open-paren "(")
  70. (close-paren ")")
  71. (and "/\\" "and" "&&" "&" "∧" ".")
  72. (nand "nand" "↑" "⊼" "~&" "~&&" "!&" "!&&")
  73. (or "\\/" "or" "||" "|" "∥" "+" "∨")
  74. (nor "nor" "↓" "⊽" "~|" "~||" "!|" "!||")
  75. (xor "xor" "⊕" "⊻" "↮" "≢" "^" "!=")
  76. (not "¬" "~" "!" "not")
  77. (implies "->" ">" "=>" "⇒" "⟹" "→" "⊃" "implies")
  78. (converse "<-" "<" "<=" "←" "⇐" "⟸" "⊂" "converse")
  79. (iff "<->" "<>" "<=>" "⇔" "↔" "≡" "iff" "=" "==" "xnor" "⊙"))
  80. "Alist table of operator symbols and their possible string representations.")
  81. (defun alpha-string-p (str)
  82. "Return t if STR is only alphabetical characters."
  83. (not (find-if-not 'alpha-char-p str)))
  84. (defparameter *longest-non-alpha-operator*
  85. (apply 'max (mapcar
  86. (lambda (entry)
  87. (apply 'max
  88. (mapcar 'length
  89. (remove-if 'alpha-string-p (cdr entry)))))
  90. *operator-symbol-table*))
  91. "The longest operator in `*operator-symbol-table*' such that `alpha-string-p'
  92. returns t.")
  93. (defparameter *operator-descriptions* ;; noindent 30
  94. `((open-paren ("open parenthesis")
  95. ,(format nil "Used in combination with a close parenthesis to denote ~
  96. some terms to be evaluated before the surrounding terms.")
  97. (((and (or true true) false) . false)
  98. ((or true (and true false)) . true)))
  99. (close-paren ("close parenthesis")
  100. "Used to close a group started with an open parenthesis."
  101. ()) ;; no examples for closed paren
  102. (and ("and" "conjunction")
  103. ,(format nil "Evaluate to true only if the expressions to the left and ~
  104. right evaluate to true.")
  105. (((and true true) . true)
  106. ((and true false) . false)))
  107. (nand ("nand" "non-conjunction")
  108. ,(format nil "Evaluate to true unless the expressions to the left and ~
  109. right are both true. This is the negation of the 'and' operator.")
  110. (((nand true true) . false)
  111. ((nand true false) . true)
  112. ((nand false false) . true)))
  113. (or ("or" "disjunction")
  114. ,(format nil "Evaluate to true if the expression to the left is true, the ~
  115. expression to the right is true, or both the left and right expressions are ~
  116. true.")
  117. (((or true true) . true)
  118. ((or true false) . true)
  119. ((or false false) . false)))
  120. (nor ("nor" "non-disjunction")
  121. ,(format nil "Evaluate to true if the expressions to the left and right ~
  122. are both false. This is the negation of the 'or' operator.")
  123. (((nor true true) . false)
  124. ((nor true false) . false)
  125. ((nor false false) . true)))
  126. (xor ("exclusive or" "exclusive disjunction") ;; noindent 30
  127. ,(format nil "Evaluate to true if the expression to the left is true or ~
  128. if the expression to the right is true, but not if both of them are true.")
  129. (((xor true true) . false)
  130. ((xor true false) . true)))
  131. (not ("not" "negation")
  132. ,(format nil "Evaluate to false if the expression to the left evaluates ~
  133. to true, and evaluate to true if the expression to the left evaluates to ~
  134. false. This is a unary operator (it only applies to the expression following ~
  135. it).")
  136. (((not true) . false)
  137. ((not false) . true)))
  138. (implies ("implies" "conditional")
  139. ,(format nil "Evaluate to false if the expression to the left evaluates ~
  140. to true and the expressions to the right evaluates to false. Otherwise, ~
  141. evaluate to true.")
  142. (((implies true true) . true)
  143. ((implies true false) . false)
  144. ((implies false true) . true)
  145. ((implies false false) . true)))
  146. (converse ("converse")
  147. ,(format nil "Evaluate to false if the expression to the right evaluates ~
  148. to true and the expression to the left evaluates to false. Otherwise, evaluate ~
  149. to true. This is the 'implies' operator with its arguments flipped.")
  150. (((implies true true) . true)
  151. ((implies true false) . true)
  152. ((implies false true) . false)
  153. ((implies false false) . true)))
  154. (iff ("biconditional" "equivalent")
  155. ,(format nil "Evaluate to true if the expressions to the left and rigt ~
  156. evaluate to the same value. That is, they are both true or both false.")
  157. (((iff true true) . true)
  158. ((iff true false) . false)
  159. ((iff false false) . true))))
  160. "Alist table of operator symbols and their descriptions. The format of this
  161. list is SYMBOL NAMES DESCRIPTION (&rest (EXAMPLE LEFT . EXAMPLE RIGHT)). These
  162. are useful for use in things like syntax explanation messages.")
  163. (defun operator-symbol (oper-str)
  164. "Return the symbol for OPER-STR, or nil if it is not a know operator."
  165. (loop for (oper-sym . strs) in *operator-symbol-table*
  166. when (member oper-str strs :test 'equalp)
  167. do (return oper-sym)))
  168. (defun operator-precedence (oper)
  169. "Return the precedence for OPER."
  170. (case oper
  171. (not 1)
  172. (and 2)
  173. (implicit-and 2)
  174. (nand 2)
  175. (xor 3)
  176. (or 4)
  177. (nor 4)
  178. (implies 5)
  179. (converse 5)
  180. (iff 6)
  181. (open-paren most-positive-fixnum)
  182. (t nil)))
  183. (defun unary-p (oper)
  184. "Return whether OPER is a unary operator or not."
  185. (eq oper 'not))
  186. (defparameter *operand-symbol-table*
  187. '((true "t" "true" "⊤" "1")
  188. (false "f" "false" "⊥" "0"))
  189. "Alist mapping operand symbols (true and false) to their textual
  190. representations.")
  191. (defun interpret-operand (oper-str)
  192. "Return a symbol representing OPER-STR, or the string itself if it represents
  193. a variable."
  194. (dolist (entry *operand-symbol-table*)
  195. (when (member oper-str (cdr entry) :test 'equalp)
  196. (return-from interpret-operand (car entry))))
  197. ;; check if OPER-STR is a valid variable name
  198. (if (or (zerop (length oper-str))
  199. (find-if-not 'symbol-char-p oper-str))
  200. nil
  201. oper-str))
  202. (defun string-first-char-safe (str)
  203. "Return the first character of STR, or nil if it is empty."
  204. (unless (zerop (length str))
  205. (elt str 0)))
  206. (defun try-find-operator-for-token (token)
  207. "Return the operator symbol for TOKEN, if it is an operator. As a second
  208. value, return the matched portion of TOKEN. If no match is found, return
  209. (values nil nil)."
  210. (loop for len downfrom (min *longest-non-alpha-operator* (length token)) to 1
  211. for cur-test = (subseq token 0 len)
  212. for oper-sym = (operator-symbol cur-test)
  213. when oper-sym do
  214. (return-from try-find-operator-for-token
  215. (values oper-sym cur-test)))
  216. (values nil nil))
  217. (defun next-symbol-token (str &key multi-char-names)
  218. "Return the next token from STR that is not a paren. If MULTI-CHAR-NAMES is
  219. non-nil, allow names to be more than one character long."
  220. (loop with mode = (if (symbol-char-p (elt str 0))
  221. 'alpha
  222. 'sym)
  223. for char across str
  224. for chari from 0
  225. while (or (and (eq mode 'alpha) (symbol-char-p char))
  226. (and (eq mode 'sym) (not (symbol-char-p char))
  227. (not (delim-p char))))
  228. collect char into token
  229. finally
  230. (let ((str (coerce token 'string)))
  231. (return
  232. (string
  233. (cond
  234. ;; operator
  235. ((multiple-value-bind (sym match)
  236. (try-find-operator-for-token str)
  237. (declare (ignorable sym))
  238. match))
  239. ;; multi-char variable, constant (true or false)
  240. ((or multi-char-names
  241. (symbolp (interpret-operand str)))
  242. str)
  243. ;; single letter variable (multi-char-names is off)
  244. (t (first token))))))))
  245. (defun next-token (str &key multi-char-names)
  246. "Return a list of the next token in STR and how much whitespace it had."
  247. (let ((whitespace-chars 0))
  248. (loop for char across str
  249. while (whitespace-p char) do
  250. (setq whitespace-chars (1+ whitespace-chars)))
  251. (setq str (subseq str whitespace-chars))
  252. (let ((next-char (string-first-char-safe str)))
  253. (cond
  254. ((not next-char)
  255. (list nil whitespace-chars))
  256. ((paren-p next-char)
  257. (list (string next-char) whitespace-chars))
  258. (t
  259. (let ((token (next-symbol-token str :multi-char-names multi-char-names)))
  260. (list token whitespace-chars)))))))
  261. (defmacro dotokens ((var pos-var str &optional (retval nil retvalp))
  262. (&key multi-char-names &allow-other-keys)
  263. &body body)
  264. "Execute BODY once with VAR bound to each token in STR. Optionally, return
  265. RETVAL. The position of each token will be stored in POS-VAR. If
  266. MULTI-CHAR-NAMES is enabled, allow multiple characters in variable names."
  267. (let ((stream-var (gensym))
  268. (token-start-var (gensym))
  269. (token-var (gensym))
  270. (read-chars-var (gensym))
  271. (whitespace-var (gensym)))
  272. `(loop for ,stream-var = ,str then (subseq ,str ,read-chars-var)
  273. for (,token-var ,whitespace-var)
  274. = (next-token ,stream-var :multi-char-names ,multi-char-names)
  275. for ,token-start-var = ,whitespace-var
  276. then (+ ,read-chars-var ,whitespace-var)
  277. for ,read-chars-var = (+ ,whitespace-var (length ,token-var))
  278. then (+ ,read-chars-var ,whitespace-var (length ,token-var))
  279. while ,token-var do
  280. (let ((,var ,token-var)
  281. (,pos-var ,token-start-var))
  282. (declare (ignorable ,pos-var))
  283. ,@body)
  284. finally
  285. (return ,(when retvalp
  286. retval)))))
  287. (defun interpret-token (token)
  288. "Return a list of the form (type value), where type is one of: `operator' or
  289. `operand', and value is the tokens value, as returned by `operator-symbol' or
  290. `interpret-operand'. If the token is of an unknown type, return a list of (nil
  291. nil)."
  292. (let ((operator-value (operator-symbol token)))
  293. (if operator-value
  294. (list 'operator operator-value)
  295. (let ((operand-value (interpret-operand token)))
  296. (if operand-value
  297. (list 'operand operand-value)
  298. (list nil nil))))))
  299. (defun parse-proposition-string (prop-str &key (implicit-and t) multi-char-names)
  300. "Parse PROP-STR, which is a proposition string.
  301. The return value is the values set of the parsed string, and the list of all
  302. found variables."
  303. (let ((found-vars '())
  304. (operators '())
  305. (operands '())
  306. (paren-operands (list 0)))
  307. (labels ((peek-operator ()
  308. (destructuring-bind (&optional value . pos) (car operators)
  309. (values value pos)))
  310. (pop-operator ()
  311. (destructuring-bind (&optional value . pos) (pop operators)
  312. (values value pos)))
  313. (convert-implicit-and (this-name pos)
  314. (multiple-value-bind (value top-pos) (peek-operator)
  315. (when (eq value 'implicit-and)
  316. (unless implicit-and
  317. (cerror "Insert implicit AND operator"
  318. 'proposition-parse-error
  319. :position pos
  320. :proposition prop-str
  321. :message
  322. (format nil "expected binary operator, found ~a"
  323. this-name)))
  324. (pop-operator)
  325. (push (cons 'and top-pos) operators))))
  326. (apply-one-operator ()
  327. (multiple-value-bind (oper pos) (pop-operator)
  328. (when (not oper)
  329. (error 'proposition-parse-error :message "no more operators"
  330. :position pos
  331. :proposition prop-str))
  332. (let ((oper-args (if (unary-p oper) 1 2))
  333. (cur-operands (list (pop operands))))
  334. (when (not (car cur-operands))
  335. (error 'proposition-parse-error
  336. :position pos
  337. :proposition prop-str
  338. :message
  339. (format nil "~A expects ~D arguments, found none"
  340. oper oper-args)))
  341. (when (= oper-args 2)
  342. (push (pop operands) cur-operands)
  343. (when (not (car cur-operands))
  344. (error 'proposition-parse-error
  345. :position pos
  346. :proposition prop-str
  347. :message
  348. (format nil "~A expects ~D arguments, found 1"
  349. oper oper-args))))
  350. (push (cons oper cur-operands) operands))))
  351. (apply-lower-precedent (prec)
  352. (loop for top-oper = (peek-operator)
  353. while (and top-oper
  354. (<= (or (operator-precedence top-oper)
  355. most-positive-fixnum)
  356. prec)
  357. (not (unary-p top-oper)))
  358. do (apply-one-operator)))
  359. (apply-all-unary ()
  360. (loop while (unary-p (peek-operator))
  361. do
  362. (apply-one-operator)))
  363. (push-operator (oper token-pos)
  364. (apply-lower-precedent (operator-precedence oper))
  365. (push (cons oper token-pos) operators)))
  366. (dotokens (token-str token-pos prop-str)
  367. (:multi-char-names multi-char-names)
  368. (destructuring-bind (type value) (interpret-token token-str)
  369. (cond
  370. ;; unknown token
  371. ((not type)
  372. (error 'proposition-parse-error
  373. :position token-pos
  374. :proposition prop-str
  375. :message "unknown token"))
  376. ;; operand
  377. ((eq type 'operand)
  378. (convert-implicit-and "operand" token-pos)
  379. (unless (member value '(true false))
  380. (pushnew value found-vars :test 'equal))
  381. (push value operands)
  382. (incf (car paren-operands))
  383. (apply-all-unary)
  384. (convert-implicit-and "operand" token-pos)
  385. (push-operator 'implicit-and token-pos))
  386. ;; open parenthesis
  387. ((eq value 'open-paren)
  388. (convert-implicit-and "open parenthesis" token-pos)
  389. (push (cons value token-pos) operators)
  390. (push 0 paren-operands))
  391. ;; close parenthesis
  392. ((eq value 'close-paren)
  393. (when (eq (peek-operator) 'implicit-and)
  394. (pop-operator))
  395. (loop while (not (eq (peek-operator) 'open-paren))
  396. when (null operators) do
  397. (error 'proposition-parse-error
  398. :position token-pos
  399. :proposition prop-str
  400. :message "no matching open parenthesis")
  401. do (apply-one-operator))
  402. (when (zerop (pop paren-operands))
  403. (error 'proposition-parse-error
  404. ;; open paren position
  405. :position (cdr (pop operators))
  406. :proposition prop-str
  407. :message "empty parenthesis"))
  408. ;; remove open paren
  409. (pop-operator)
  410. (apply-all-unary)
  411. (convert-implicit-and "close parenthesis" token-pos)
  412. (push-operator 'implicit-and token-pos))
  413. ;; operator
  414. (t
  415. (if (eq (peek-operator) 'implicit-and)
  416. (if (unary-p value)
  417. (convert-implicit-and "unary operator" token-pos)
  418. (pop-operator))
  419. (unless (unary-p value)
  420. (error 'proposition-parse-error
  421. :position token-pos
  422. :proposition prop-str
  423. :message "expected operand, found operator")))
  424. (push-operator value token-pos)))))
  425. ;; remove implicit-and
  426. (when (eq 'implicit-and (peek-operator))
  427. (pop-operator))
  428. (loop for (top-oper . top-pos) = (car operators)
  429. while top-oper
  430. when (eq top-oper 'open-paren) do
  431. (error 'proposition-parse-error
  432. :message "no matching closing parenthesis"
  433. :proposition prop-str
  434. :position top-pos)
  435. do
  436. (apply-one-operator))
  437. ;; return variables in the order we found them
  438. (values (car operands) (nreverse found-vars)))))