Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b21f10d31d
Fetching contributors…

Cannot retrieve contributors at this time

file 266 lines (236 sloc) 9.528 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
;;; -*- Mode: lisp; Syntax: ansi-common-lisp; Base: 10; Package: bnf-parser; -*-

#|
<DOCUMENTATION>
<DESCRIPTION>
this file comprises the methods for <code>atn-canonic-form</code>, which rewrites
S-expression forms for ATN nodes to node instances. it accomplishes the 'same' thing as
<a href='ATN-macros.lisp'>ATN-macros</a>, but does not depend on macro evaluation.
</DESCRIPTION>
<COPYRIGHT YEAR='2001' AUTHOR='james adam anderson' MARK='(C)'
href='file://bnfp/sysdcl.lisp' />
<CHRONOLOGY>
<DELTA DATE='19980203' AUTHOR='BB' />
<DELTA DATE='19980325' AUTHOR='BB' />
<DELTA DATE='20010205' AUTHOR='JAA'>
added ATN-BUILTIN-PREDICATE-CATEGORY to denote builtin, primitives</DELTA>
<DELTA DATE='20010303' AUTHOR='JAA'>
parameterized the node classes</DELTA>
<DELTA DATE='20010331' AUTHOR='JAA'>
added explicit failure control path</DELTA>
</CHRONOLOGY>
</DOCUMENTATION>
|#

(in-package"BNFP")

;;;
;;; Netze
;;;

(defmethod atn-canonic-form ((object (eql 'defatn)) forms)
   (destructuring-bind (name start &key nodes finally) forms
     (let ((net (make-instance *class.atn* :name name :start start
                               :finally finally)))
        (flet ((mknode (l)
                (let ((node (atn-canonic-form 'defnode l)))
                   (setf (atn-net node) net)
                   node)))
          (setf (atn-nodes net) (mapcar #'mknode nodes))
          net))))

;;;
;;; Knoten
;;;

(defmethod atn-canonic-form ((object (eql 'defnode)) forms)
   (let ((node (make-instance *class.atn-node* :name (first forms))))
      (flet ((edge (l)
              (let ((edge (atn-canonic-form (first l) (rest l))))
                 (setf (atn-node edge) node)
                 edge)))
         (setf (atn-edges node) (mapcar #'edge (rest forms)))
        node)))

;;;
;;; Kanten
;;;

(defmethod atn-canonic-form (test forms)
   (let ((edge (atn-canonic-form (first forms) (rest forms))))
      (setf (atn-test edge) test)
      edge))

(defmethod atn-canonic-form ((object (eql 'push)) forms)
   (destructuring-bind (subnet target fail &rest actions) forms
     (make-instance *class.push-atn-edge*
       :net subnet
       :end target
       :fail fail
       :actions actions)))

(defmethod atn-canonic-form ((object (eql 'pop)) forms)
   (let ((register (first (last forms)))
         (actions (butlast forms)))
     (make-instance *class.pop-atn-edge*
       :register register
       :actions actions)))

(defmethod atn-canonic-form ((object (eql 'fail)) forms)
   (make-instance *class.fail-atn-edge*
     :actions forms))

(defmethod atn-canonic-form ((object (eql 'word)) forms)
  ;; no check as the term may be internal and not in the bnf
  ;; in which case a warning has already appeared
  (destructuring-bind (word target fail . actions) forms
    (make-instance *class.word-atn-edge*
      :word word
      :end target
      :fail fail
      :actions actions)))

(defMethod atn-canonic-form ((object (eql 'or)) forms)
  ;; carry a disjunction over
  (destructuring-bind (edges target fail . actions) forms
    (make-instance *class.or-atn-edge*
      :edges (mapcar #'(lambda (l) (atn-canonic-form (first l) (rest l))) edges)
      :end target
      :fail fail
      :actions actions)))

;; 20010205.jaa
(defmethod atn-canonic-form ((object (eql 'test)) forms)
  (ensure-uncategorialized-lexem (first forms))
  (destructuring-bind (test target fail . actions) forms
    (make-instance *class.test-atn-edge*
      :test test
      :end target
      :fail fail
      :actions actions)))

(defmethod atn-canonic-form ((object (eql 'cat)) forms)
  (destructuring-bind (cat target fail . actions) forms
    (make-instance *class.cat-atn-edge*
      :cat cat
      :end target
      :fail fail
      :actions actions)))

(defmethod atn-canonic-form ((object (eql 'jump)) forms)
  (destructuring-bind (target . actions) forms
    (make-instance *class.jump-atn-edge*
      :end target
      :actions actions)))

(defmethod atn-canonic-form ((object (eql 'cell)) forms)
  (destructuring-bind (name to . actions) forms
    (make-instance *class.cell-atn-edge*
      :name name
      ; :row row
      :end to
      ; :column column
      ; :value value
      :actions actions
      )))

;;;
;;; Lexikon
;;;

(defmethod atn-canonic-form ((object (eql 'deflexicon)) forms)
   (destructuring-bind (&key lexems predicates) forms
     ;; :lexems jedem Lexem wird eine Liste von Kategorien zugeordnet
     ;; :predicates jedem predicate wird eine Liste von Kategorien bzw. Terminalen
     ;; zugeorndet
     ;; :categories jeder category wird eine Liste von terminalen Symbolen zugeordnet
     (let ((lexicon (make-instance *class.atn-lexicon*))
           (*lexems nil)
           (*categories nil))
       (declare (special *categories *lexems))
       (flet ((ensure-defs (cat def)
                (setf (category-elements cat)
                      (mapcar #'ensure-def-object def))
                cat)
              (table-cells-p (def)
                ;;;(inspect (list 'table-cells-p def))
                (if (consp def)
                  (every #'(lambda (x) (typep x 'bnf-table-cell)) def)
                  (typep def 'bnf-table-cell))))
         ;;;(setf (category-elements cat) (atn-canonic-form cat def))
         (setf *lexems
               (delete-duplicates
                (mapcar #'(lambda (x) (atn-canonic-form (make-atn-lexem (first x)) (rest x)))
                        lexems))
               (atn-lexems lexicon)
               *lexems)
         ;(inspect predicates)
         (loop for (cat . def) in predicates
               collect
               (make-instance
                 (if (table-cells-p def)
                   *class.atn-cell-category*
                   *class.atn-derived-category*)
                 :name cat) into cats
               collect def into defs
               finally ;; do ;; allegro loop doesn't recognize addenda to finally
               (progn
               (loop for i from 0
                     for c in *categories
                     do (setf (category-flag-id c) (expt 2 i)))
               (setf *categories (append cats *categories))
               (map nil #'ensure-defs cats defs)
               (setf (atn-predicates lexicon) *categories))))
       lexicon)))

(defun make-atn-lexem (name)
  (declare (special *lexems))
  (or (find name *lexems :key #'atn-name :test #'equal)
      (let ((lexem (make-instance *class.atn-lexem* :name name)))
        (push lexem *lexems)
        lexem)))

(defun ensure-def-object (def)
  (declare (special *categories *lexems))
  (if (consp def)
    (cond ((eq (first def) 'not)
           (make-instance *class.atn-negated-alternatives*
             :elements (mapcar #'ensure-def-object (rest def))))
          ((eq (first def) 'and)
           (make-instance *class.atn-conjunction*
             :elements (mapcar #'ensure-def-object (rest def))))
          (t (error "Syntaxfehler ~A" def)))
    (cond ((find def *categories :key #'category-name :test #'equal))
          ((find def *lexems :key #'atn-name :test #'equal))
          ((typep def 'bnf-tokenset) def)
          ((typep def 'bnf-table-cell) def)
          ((predicate-terminal-p def)
           (make-instance *class.atn-builtin-predicate-category* :name def))
          ((uncategorialized-terminal-p def)
           (ensure-uncategorialized-lexem def))
          (t
           (warn "~A wurde nicht deklariert!" def)
           (make-instance *class.atn-undeclared-category* :name def)))))

(defun ensure-uncategorialized-lexem (name)
  (or (find name *atn-words* :test #'equal :key #'atn-name)
      (let ((lexem (make-instance *class.atn-lexem* :name name)))
        (push lexem *atn-words*)
        lexem)))

(defun uncategorialized-terminal-p (object)
  (and (stringp object)
       (or (null (setf object (make-lisp-predicate-name object :if-does-not-exist nil)))
           (not (fboundp object)))))

(defun predicate-terminal-p (object)
  (and (stringp object)
       (setf object (make-lisp-predicate-name object :if-does-not-exist nil))
       (fboundp object)))


;;; Lexeme

(defmethod atn-canonic-form ((object atn-lexem) form)
  (flet ((ensure-atn-category (cat)
           (declare (special *categories))
           (let ((category (find cat *categories :key #'category-name :test #'equal)))
             (unless category
               (setf category (make-instance *class.atn-primitive-category*
                                :name cat))
               (push category *categories))
             (push object (category-elements category))
             category)))
    (setf (atn-categories object)
          (union (atn-categories object) (mapcar #'ensure-atn-category form))))
  object)

;;; Abgeleitete Kategorien

(defmethod atn-canonic-form ((object atn-derived-category) form)
  (flet ((ensure-element (elt)
           (declare (special *categories))
           (or (find elt *categories :key #'category-name :test #'equal)
               (make-atn-lexem elt))))
    (let ((catname (first form))
          (defs (rest form)))
      (setf (category-name object) catname)
      (cond ((eq (first defs) 'not)
             (change-class object *class.atn-complement-category*)
             (setf (category-elements object)
                   (mapcar #'ensure-element (rest (first defs)))))
            (t (change-class object *class.atn-alternative-category*)
               (setf (category-elements object) (mapcar #'ensure-element defs))))))
  object)

:EOF
Something went wrong with that request. Please try again.