DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Untracked file

Line Code
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
;;; ***** BEGIN LICENSE BLOCK *****
;;; Version: MPL 1.1/GPL 2.0/LGPL 2.1
;;;
;;; The contents of this file are subject to the Mozilla Public License Version
;;; 1.1 (the "License"); you may not use this file except in compliance with
;;; the License. You may obtain a copy of the License at
;;; http://www.mozilla.org/MPL/
;;;
;;; Software distributed under the License is distributed on an "AS IS" basis,
;;; WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
;;; for the specific language governing rights and limitations under the
;;; License.
;;;
;;; The Original Code is the Language Design and Prototyping Environment.
;;;
;;; The Initial Developer of the Original Code is
;;; Netscape Communications Corporation.
;;; Portions created by the Initial Developer are Copyright (C) 1999-2002
;;; the Initial Developer. All Rights Reserved.
;;;
;;; Contributor(s):
;;;   Waldemar Horwat <waldemar@acm.org>
;;;
;;; Alternatively, the contents of this file may be used under the terms of
;;; either the GNU General Public License Version 2 or later (the "GPL"), or
;;; the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
;;; in which case the provisions of the GPL or the LGPL are applicable instead
;;; of those above. If you wish to allow use of your version of this file only
;;; under the terms of either the GPL or the LGPL, and not to allow others to
;;; use your version of this file under the terms of the MPL, indicate your
;;; decision by deleting the provisions above and replace them with the notice
;;; and other provisions required by the GPL or the LGPL. If you do not delete
;;; the provisions above, a recipient may use your version of this file under
;;; the terms of any one of the MPL, the GPL or the LGPL.
;;;
;;; ***** END LICENSE BLOCK *****

;;;
;;; LALR(1) and LR(1) parametrized grammar utilities
;;;
;;; Waldemar Horwat (waldemar@acm.org)
;;;


;;; ------------------------------------------------------------------------------------------------------
;;; UTILITIES

(declaim (inline identifier?))
(defun identifier? (form)
  (and form (symbolp form) (not (keywordp form))))

(deftype identifier () '(satisfies identifier?))


; Make sure that form is one of the following:
;   A symbol
;   An integer
;   A float
;   A character
;   A string
;   A list of zero or more forms that also satisfy ensure-proper-form;
;     the list cannot be dotted.
; Return the form.
(defun ensure-proper-form (form)
  (labels
    ((ensure-list-form (form)
       (or (null form)
           (and (consp form)
                (progn
                  (ensure-proper-form (car form))
                  (ensure-list-form (cdr form)))))))
    (unless
      (or (symbolp form)
          (integerp form)
          (floatp form)
          (characterp form)
          (stringp form)
          (ensure-list-form form))
      (error "Bad form: ~S" form))
    form))


;;; ------------------------------------------------------------------------------------------------------
;;; TERMINALS

; A terminal is any of the following:
;   A symbol that is neither nil nor a keyword
;   A string;
;   A character;
;   An integer.
(defun terminal? (x)
  (and x
       (or (and (symbolp x) (not (keywordp x)))
           (stringp x)
           (characterp x)
           (integerp x))))

; The following terminals are reserved and may not be used in user input:
;   $$   Marker for end of token stream
(defconstant *end-marker* '$$)
(defconstant *end-marker-terminal-number* 0)

(deftype terminal () '(satisfies terminal?))
(deftype user-terminal () `(and terminal (not (eql ,*end-marker*))))


; Emit markup for a terminal.  subscript is an optional integer.
(defun depict-terminal (markup-stream terminal &optional subscript)
  (cond
   ((characterp terminal)
    (depict-char-style (markup-stream :character-literal)
      (depict-character markup-stream terminal)))
   ((and terminal (symbolp terminal))
    (let ((name (symbol-name terminal)))
      (if (and (> (length name) 0) (char= (char name 0) #\$))
        (depict-char-style (markup-stream :terminal)
          (depict markup-stream (subseq (symbol-upper-mixed-case-name terminal) 1)))
        (depict-char-style (markup-stream :terminal-keyword)
          (depict markup-stream (string-downcase name))))))
   (t (error "Don't know how to emit markup for terminal ~S" terminal)))
  (when subscript
    (depict-char-style (markup-stream :subscript)
      (depict-char-style (markup-stream :terminal-sub)
        (depict-integer markup-stream subscript)))))



;;; ------------------------------------------------------------------------------------------------------
;;; NONTERMINAL PARAMETERS

(declaim (inline nonterminal-parameter?))
(defun nonterminal-parameter? (x)
  (symbolp x))
(deftype nonterminal-parameter () 'symbol)


; Return true if this nonterminal parameter is a constant.
(declaim (inline nonterminal-attribute?))
(defun nonterminal-attribute? (parameter)
  (and (symbolp parameter) (not (keywordp parameter))))
(deftype nonterminal-attribute () '(and symbol (not keyword)))


(defun depict-nonterminal-attribute (markup-stream attribute)
  (depict-char-style (markup-stream :nonterminal-attribute)
    (depict markup-stream (symbol-lower-mixed-case-name attribute))))


; Return true if this nonterminal parameter is a variable.
(declaim (inline nonterminal-argument?))
(defun nonterminal-argument? (parameter)
  (keywordp parameter))
(deftype nonterminal-argument () 'keyword)


(defparameter *special-nonterminal-arguments*
  '(:alpha :beta :gamma :delta :epsilon :zeta :eta :theta :iota :kappa :lambda :mu :nu
    :xi :omicron :pi :rho :sigma :tau :upsilon :phi :chi :psi :omega))

(defun depict-nonterminal-argument (markup-stream argument)
  (depict-char-style (markup-stream :nonterminal-argument)
    (let ((argument (symbol-abbreviation argument)))
      (depict markup-stream
              (if (member argument *special-nonterminal-arguments*)
                argument
                (symbol-upper-mixed-case-name argument))))))


;;; ------------------------------------------------------------------------------------------------------
;;; ATTRIBUTED NONTERMINALS

; An attributed-nonterminal is a specific instantiation of a generic-nonterminal.
(defstruct (attributed-nonterminal (:constructor allocate-attributed-nonterminal (symbol attributes))
                                   (:copier nil)
                                   (:predicate attributed-nonterminal?))
  (symbol nil :type keyword :read-only t)    ;The name of the attributed nonterminal
  (attributes nil :type list :read-only t))  ;Ordered list of nonterminal attributes


; Make an attributed nonterminal with the given symbol and attributes.  If there
; are no attributes, return the symbol as a plain nonterminal.
; Nonterminals are eq whenever they have identical symbols and attribute lists.
(defun make-attributed-nonterminal (symbol attributes)
  (assert-type symbol keyword)
  (assert-type attributes (list nonterminal-attribute))
  (if attributes
    (let ((generic-nonterminals (get symbol 'generic-nonterminals)))
      (or (cdr (assoc attributes generic-nonterminals :test #'equal))
          (let ((attributed-nonterminal (allocate-attributed-nonterminal symbol attributes)))
            (setf (get symbol 'generic-nonterminals)
                  (acons attributes attributed-nonterminal generic-nonterminals))
            attributed-nonterminal)))
    symbol))


(defmethod print-object ((attributed-nonterminal attributed-nonterminal) stream)
  (print-unreadable-object (attributed-nonterminal stream)
    (format stream "a ~@_~W~{ ~:_~W~}"
            (attributed-nonterminal-symbol attributed-nonterminal)
            (attributed-nonterminal-attributes attributed-nonterminal))))


;;; ------------------------------------------------------------------------------------------------------
;;; GENERIC NONTERMINALS

; A generic-nonterminal is a parametrized nonterminal that can expand into two or more
; attributed-nonterminals.
(defstruct (generic-nonterminal (:constructor allocate-generic-nonterminal (symbol parameters))
                                (:copier nil)
                                (:predicate generic-nonterminal?))
  (symbol nil :type keyword :read-only t)    ;The name of the generic nonterminal
  (parameters nil :type list :read-only t))  ;Ordered list of nonterminal attributes or arguments


; Make a generic nonterminal with the given symbol and parameters.  If none of
; the parameters is an argument, make an attributed nonterminal instead.  If there
; are no parameters, return the symbol as a plain nonterminal.
; Nonterminals are eq whenever they have identical symbols and parameter lists.
(defun make-generic-nonterminal (symbol parameters)
  (assert-type symbol keyword)
  (if parameters
    (let ((generic-nonterminals (get symbol 'generic-nonterminals)))
      (or (cdr (assoc parameters generic-nonterminals :test #'equal))
          (progn
            (assert-type parameters (list nonterminal-parameter))
            (let ((generic-nonterminal (if (every #'nonterminal-attribute? parameters)
                                         (allocate-attributed-nonterminal symbol parameters)
                                         (allocate-generic-nonterminal symbol parameters))))
              (setf (get symbol 'generic-nonterminals)
                    (acons parameters generic-nonterminal generic-nonterminals))
              generic-nonterminal))))
    symbol))


(defmethod print-object ((generic-nonterminal generic-nonterminal) stream)
  (print-unreadable-object (generic-nonterminal stream)
    (format stream "g ~@_~W~{ ~:_~W~}"
            (generic-nonterminal-symbol generic-nonterminal)
            (generic-nonterminal-parameters generic-nonterminal))))


;;; ------------------------------------------------------------------------------------------------------
;;; NONTERMINALS

;;; A nonterminal is a keyword or an attributed-nonterminal.
(declaim (inline nonterminal?))
(defun nonterminal? (x)
  (or (keywordp x) (attributed-nonterminal? x)))

; The following nonterminals are reserved and may not be used in user input:
;   :%   Nonterminal that expands to the start nonterminal

(defconstant *start-nonterminal* :%)

(deftype nonterminal () '(or keyword attributed-nonterminal))
(deftype user-nonterminal () `(and nonterminal (not (eql ,*start-nonterminal*))))


;;; ------------------------------------------------------------------------------------------------------
;;; GENERAL NONTERMINALS

;;; A general-nonterminal is a nonterminal or a generic-nonterminal.
(declaim (inline general-nonterminal?))
(defun general-nonterminal? (x)
  (or (nonterminal? x) (generic-nonterminal? x)))

(deftype general-nonterminal () '(or nonterminal generic-nonterminal))


; Return the list of parameters in the general-nonterminal.  The list is empty if the
; general-nonterminal is a plain nonterminal.
(defun general-nonterminal-parameters (general-nonterminal)
  (cond
   ((attributed-nonterminal? general-nonterminal) (attributed-nonterminal-attributes general-nonterminal))
   ((generic-nonterminal? general-nonterminal) (generic-nonterminal-parameters general-nonterminal))
   (t (progn
        (assert-true (keywordp general-nonterminal))
        nil))))


; Emit markup for a general-nonterminal.  subscript is an optional integer.
; link should be one of:
;   :reference   if this is a reference of this general-nonterminal;
;   :external    if this is an external reference of this general-nonterminal;
;   a string     if this is an external reference of this general-nonterminal to the given html file;
;   :definition  if this is a definition of this general-nonterminal;
;   nil          if this use of the general-nonterminal should not be cross-referenced.
(defun depict-general-nonterminal (markup-stream general-nonterminal link &optional subscript)
  (labels
    ((depict-nonterminal-name (markup-stream symbol)
       (let ((name (symbol-upper-mixed-case-name symbol)))
         (depict-link (markup-stream link "N-" name t)
           (depict-char-style (markup-stream :nonterminal)
             (depict markup-stream name)))))
     
     (depict-nonterminal-parameter (markup-stream parameter)
       (if (nonterminal-attribute? parameter)
         (depict-nonterminal-attribute markup-stream parameter)
         (depict-nonterminal-argument markup-stream parameter)))
     
     (depict-parametrized-nonterminal (markup-stream symbol parameters)
       (depict-nonterminal-name markup-stream symbol)
       (depict-char-style (markup-stream :superscript)
         (depict-list markup-stream #'depict-nonterminal-parameter parameters
                      :separator ","))))
    
    (cond
     ((keywordp general-nonterminal)
      (depict-nonterminal-name markup-stream general-nonterminal))
     ((attributed-nonterminal? general-nonterminal)
      (depict-parametrized-nonterminal markup-stream
                                       (attributed-nonterminal-symbol general-nonterminal)
                                       (attributed-nonterminal-attributes general-nonterminal)))
     ((generic-nonterminal? general-nonterminal)
      (depict-parametrized-nonterminal markup-stream
                                       (generic-nonterminal-symbol general-nonterminal)
                                       (generic-nonterminal-parameters general-nonterminal)))
     (t (error "Bad nonterminal ~S" general-nonterminal)))
    (when subscript
      (depict-char-style (markup-stream :subscript)
        (depict-char-style (markup-stream :nonterminal-sub)
          (depict-integer markup-stream subscript))))))



;;; ------------------------------------------------------------------------------------------------------
;;; GRAMMAR SYMBOLS

;;; A grammar-symbol is either a terminal or a nonterminal.
(deftype grammar-symbol () '(or terminal nonterminal))
(deftype user-grammar-symbol () '(or user-terminal user-nonterminal))

;;; A general-grammar-symbol is either a terminal or a general-nonterminal.
(deftype general-grammar-symbol () '(or terminal general-nonterminal))

; Return true if x is a general-grammar-symbol.  x can be any object.
(defun general-grammar-symbol? (x)
  (or (terminal? x) (general-nonterminal? x)))


; Return true if the two grammar symbols are the same symbol.
(declaim (inline grammar-symbol-=))
(defun grammar-symbol-= (grammar-symbol1 grammar-symbol2)
  (eql grammar-symbol1 grammar-symbol2))
; A version of grammar-symbol-= suitable for being the test function for hash tables.
(defparameter *grammar-symbol-=* #'eql)


; Return the general-grammar-symbol's symbol.  Return it unchanged if it is not
; an attributed or generic nonterminal.
(defun general-grammar-symbol-symbol (general-grammar-symbol)
  (cond
   ((attributed-nonterminal? general-grammar-symbol) (attributed-nonterminal-symbol general-grammar-symbol))
   ((generic-nonterminal? general-grammar-symbol) (generic-nonterminal-symbol general-grammar-symbol))
   (t (assert-type general-grammar-symbol (or keyword terminal)))))


; Return the list of arguments in the general-grammar-symbol.  The list is empty if the
; general-grammar-symbol is not a generic nonterminal.
(defun general-grammar-symbol-arguments (general-grammar-symbol)
  (and (generic-nonterminal? general-grammar-symbol)
       (remove-if (complement #'nonterminal-argument?) (generic-nonterminal-parameters general-grammar-symbol))))


; Return the general-grammar-symbol expanded into source form that can be interned to yield the same
; general-grammar-symbol.
(defun general-grammar-symbol-source (general-grammar-symbol)
  (cond
   ((attributed-nonterminal? general-grammar-symbol)
    (cons (attributed-nonterminal-symbol general-grammar-symbol) (attributed-nonterminal-attributes general-grammar-symbol)))
   ((generic-nonterminal? general-grammar-symbol)
    (cons (generic-nonterminal-symbol general-grammar-symbol) (generic-nonterminal-parameters general-grammar-symbol)))
   (t (assert-type general-grammar-symbol (or keyword terminal)))))


; Emit markup for a general-grammar-symbol.  subscript is an optional integer.
; link should be one of:
;   :reference   if this is a reference of this general-grammar-symbol;
;   :external    if this is an external reference of this general-grammar-symbol;
;   a string     if this is an external reference of this general-grammar-symbol to the given html file;
;   :definition  if this is a definition of this general-grammar-symbol;
;   nil          if this use of the general-grammar-symbol should not be cross-referenced.
(defun depict-general-grammar-symbol (markup-stream general-grammar-symbol link &optional subscript)
  (if (general-nonterminal? general-grammar-symbol)
    (depict-general-nonterminal markup-stream general-grammar-symbol link subscript)
    (depict-terminal markup-stream general-grammar-symbol subscript)))


; Styled text can include (:grammar-symbol <grammar-symbol-source> [<subscript>] [<link>]) as long as
; *styled-text-grammar-parametrization* is bound around the call to depict-styled-text.
(defvar *styled-text-grammar-parametrization*)

(defun depict-grammar-symbol-styled-text (markup-stream grammar-symbol-source &optional subscript link)
  (depict-general-grammar-symbol markup-stream
                                 (grammar-parametrization-intern *styled-text-grammar-parametrization* grammar-symbol-source)
                                 (or link :reference)
                                 subscript))

(setf (styled-text-depictor :grammar-symbol) #'depict-grammar-symbol-styled-text)


;;; ------------------------------------------------------------------------------------------------------
;;; GRAMMAR PARAMETRIZATIONS

; A grammar parametrization holds the rules for converting nonterminal arguments into nonterminal attributes.
(defstruct (grammar-parametrization (:constructor allocate-grammar-parametrization (argument-attributes))
                                    (:predicate grammar-parametrization?))
  (argument-attributes nil :type hash-table :read-only t))   ;Hash table of nonterminal-argument -> list of nonterminal-attributes


(defun make-grammar-parametrization ()
  (allocate-grammar-parametrization (make-hash-table :test #'eq)))


; Return true if the two grammar-parametrizations are the same.
(defun grammar-parametrization-= (grammar-parametrization1 grammar-parametrization2)
  (hash-table-= (grammar-parametrization-argument-attributes grammar-parametrization1)
                (grammar-parametrization-argument-attributes grammar-parametrization2)
                :test #'equal))


; Declare that nonterminal arguments with the given name can hold any of the
; given nonterminal attributes given.  At least one attribute must be provided.
(defun grammar-parametrization-declare-argument (grammar-parametrization argument attributes)
  (assert-type argument nonterminal-argument)
  (assert-type attributes (list nonterminal-attribute))
  (assert-true attributes)
  (when (gethash argument (grammar-parametrization-argument-attributes grammar-parametrization))
    (error "Duplicate parametrized grammar argument ~S" argument))
  (setf (gethash argument (grammar-parametrization-argument-attributes grammar-parametrization)) attributes))


; Return the attributes to which the given argument may expand.
(defun grammar-parametrization-lookup-argument (grammar-parametrization argument)
  (assert-non-null (gethash argument (grammar-parametrization-argument-attributes grammar-parametrization))))


; Create a plain, attributed, or generic grammar symbol from the specification in grammar-symbol-source.
; If grammar-symbol-source is not a cons, it is a plain grammar symbol.  If it is a list, its first element
; must be a keyword that is a nonterminal's symbol and the other elements must be nonterminal
; parameters.
; Return two values:
;   the grammar symbol
;   a list of arguments used in the grammar symbol.
; If allowed-arguments is given, check that each argument is in the allowed-arguments list;
; if not, allow any arguments declared in grammar-parametrization but do not allow duplicates.
(defun grammar-parametrization-intern (grammar-parametrization grammar-symbol-source &optional (allowed-arguments nil allow-duplicates))
  (if (consp grammar-symbol-source)
    (progn
      (assert-type grammar-symbol-source (cons keyword (list nonterminal-parameter)))
      (let* ((symbol (car grammar-symbol-source))
             (parameters (cdr grammar-symbol-source))
             (arguments (remove-if (complement #'nonterminal-argument?) parameters)))
        (mapl #'(lambda (arguments)
                  (let ((argument (car arguments)))
                    (if allow-duplicates
                      (unless (member argument allowed-arguments :test #'eq)
                        (error "Undefined nonterminal argument ~S" argument))
                      (progn
                        (unless (gethash argument (grammar-parametrization-argument-attributes grammar-parametrization))
                          (error "Undeclared nonterminal argument ~S" argument))
                        (when (member argument (cdr arguments) :test #'eq)
                          (error "Duplicate nonterminal argument ~S" argument))))))
              arguments)
        (values (make-generic-nonterminal symbol parameters) arguments)))
    (values (assert-type grammar-symbol-source (or keyword terminal)) nil)))


; Call f on each possible binding permutation of the given arguments concatenated with the bindings in
; bound-argument-alist.  f takes one argument, an association list that maps arguments to attributes.
(defun grammar-parametrization-each-permutation (grammar-parametrization f arguments &optional bound-argument-alist)
  (if arguments
    (let ((argument (car arguments))
          (rest-arguments (cdr arguments)))
      (dolist (attribute (grammar-parametrization-lookup-argument grammar-parametrization argument))
        (grammar-parametrization-each-permutation grammar-parametrization f rest-arguments (acons argument attribute bound-argument-alist))))
    (funcall f bound-argument-alist)))


; If general-grammar-symbol is a generic-nonterminal, return one possible binding permutation of its arguments;
; otherwise return nil.
(defun nonterminal-sample-bound-argument-alist (grammar-parametrization general-grammar-symbol)
  (when (generic-nonterminal? general-grammar-symbol)
    (grammar-parametrization-each-permutation
     grammar-parametrization
     #'(lambda (bound-argument-alist) (return-from nonterminal-sample-bound-argument-alist bound-argument-alist))
     (general-grammar-symbol-arguments general-grammar-symbol))))


; If the grammar symbol is a generic nonterminal, convert it into an attributed nonterminal
; by instantiating its arguments with the corresponding attributes from the bound-argument-alist.
; If the grammar symbol is already an attributed or plain nonterminal, return it unchanged.
(defun instantiate-general-grammar-symbol (bound-argument-alist general-grammar-symbol)
  (if (generic-nonterminal? general-grammar-symbol)
    (make-attributed-nonterminal
     (generic-nonterminal-symbol general-grammar-symbol)
     (mapcar #'(lambda (parameter)
                 (if (nonterminal-argument? parameter)
                   (let ((binding (assoc parameter bound-argument-alist :test #'eq)))
                     (if binding
                       (cdr binding)
                       (error "Unbound nonterminal argument ~S" parameter)))
                   parameter))
             (generic-nonterminal-parameters general-grammar-symbol)))
    (assert-type general-grammar-symbol grammar-symbol)))


; If the grammar symbol is a generic nonterminal parametrized on argument, substitute
; attribute for argument in it and return the modified grammar symbol.  Otherwise, return it unchanged.
(defun general-grammar-symbol-substitute (attribute argument general-grammar-symbol)
  (assert-type attribute nonterminal-attribute)
  (assert-type argument nonterminal-argument)
  (if (and (generic-nonterminal? general-grammar-symbol)
           (member argument (generic-nonterminal-parameters general-grammar-symbol) :test #'eq))
    (make-generic-nonterminal
     (generic-nonterminal-symbol general-grammar-symbol)
     (substitute attribute argument (generic-nonterminal-parameters general-grammar-symbol) :test #'eq))
    (assert-type general-grammar-symbol general-grammar-symbol)))


; If the general grammar symbol is a generic nonterminal, return a list of all possible attributed nonterminals
; that can be instantiated from it; otherwise, return a one-element list containing the given general grammar symbol.
(defun general-grammar-symbol-instances (grammar-parametrization general-grammar-symbol)
  (if (generic-nonterminal? general-grammar-symbol)
    (let ((instances nil))
      (grammar-parametrization-each-permutation
       grammar-parametrization
       #'(lambda (bound-argument-alist)
           (push (instantiate-general-grammar-symbol bound-argument-alist general-grammar-symbol) instances))
       (general-grammar-symbol-arguments general-grammar-symbol))
      (nreverse instances))
    (list (assert-type general-grammar-symbol grammar-symbol))))


; Return true if grammar-symbol can be obtained by calling instantiate-general-grammar-symbol on
; general-grammar-symbol.
(defun general-nonterminal-is-instance? (grammar-parametrization general-grammar-symbol grammar-symbol)
  (or (grammar-symbol-= general-grammar-symbol grammar-symbol)
      (and (generic-nonterminal? general-grammar-symbol)
           (attributed-nonterminal? grammar-symbol)
           (let ((parameters (generic-nonterminal-parameters general-grammar-symbol))
                 (attributes (attributed-nonterminal-attributes grammar-symbol)))
             (and (= (length parameters) (length attributes))
                  (every #'(lambda (parameter attribute)
                             (or (eq parameter attribute)
                                 (and (nonterminal-argument? parameter)
                                      (member attribute (grammar-parametrization-lookup-argument grammar-parametrization parameter) :test #'eq))))
                         parameters
                         attributes))))))