;; test grammar for the Earley parser.
;; It contains a grammar rule set named LAB1 and a dictionary for
;; it called LABDICT, and a set of test sentences TESTS
;; after loading this file, create the grammar named FOO with the call:
;; (create-cfg-table 'FOO 'lab1 'root 1)
;; to build a parser that uses the FIRST lookahead calculation,
;; or 
;; (create-cfg-table 'FOO 'lab1 'root 0) to build a parser that doesn't
;; It's silly not to use the lookahead, though
;;
;; To use the parser, the initial call is:
;;
;; (p "some sentence"  :grammar 'FOO :dictionary 'labdict)
;;
;; The sentence may also be given as a list, '(some sentence)
;; The grammar and dictionary name are sticky after this initial call.
;; For tracing info, supply the keyword arg :print-states t
;; To parse a list of sentences, supply the keyword arg :sentence-set followed
;; by the name of the sentence set, e.g., 'TESTS
;;
;; There are a variety of commands for manipulating rule sets, both grammars,
;; dictionaries, and sentences.  If a grammar rule set is altered, one must
;; rebuilt the parser table that uses it.
;; Here is a short list of commands.
;; (list-all)  lists all the rules sets, their types, and sizes
;;
;; (add-rule-set name type)
;;
;; (remove-rule-set name)  this is how you remove a grammar
;;
;; (sort-rules name)
;;
;; (list-rules name &optional (template '*)) list all rules in set name that
;; match a given template
;;
;; (add rule1 rule 2 ...) add rules to the last rule set listed with list-rules
;; 
;; (del n1 n2 n3 ...) deleted the numbered rules from the last rule set
;; listed with list-rules
;;
;; (rpl n1 rule1 rule 2 ...) replace the numbered rules with the new ones
;; in the last rule set listed with list-rules
;; (add-rule name rule)  add rule to rule set name
;;
;; (del-rule name rule) remove the rule from rule set name
;;
;; (add-rule-list name rule-list)  add the rules in the list rule-list to
;; the rule set
;;
;; (del-rule-list name rule-list)  remove the rules in the list rule-list
;; from the rule set name
;;
;; (p sentence &key (template '*) grammar dictionary (use-lookahead t)
;;      print-states file sentence-set)
;; 
;; (retrieve-parses &optional (template '*))  return results of last sentence
;; parse according to template

; Name: lab1  Type: CFG
(add-rule-set 'LAB1 'CFG)
(add-rule-list 'lab1
               '((ROOT ==> S)
                 (ROOT ==> Q)
                 (ROOT ==> S/R R)
                 (ROOT ==> Q/R R)
                 (S ==> NP VP)
                 (S ==> NP VP+TNS)
                 (S ==> NP VP+PASS)
                 (S ==> NP S/NP)
                 (VP ==> AUX VP-TNS)
                 (AUX ==> V+AUX AUX1)
                 (AUX1 ==> V+AUX AUX1)
                 (AUX1 ==>)
                 (VP-FIN ==> *TO VP-TNS)
                 (VP+TNS ==> V1+TNS)
                 (VP-TNS ==> V1-TNS)
                 (VP+TNS ==> V2+TNS NP)
                 (VP-TNS ==> V2-TNS NP) ; simple trans
                 (VP+TNS ==>  V3+TNS NP NP)
                 (VP-TNS ==>  V3-TNS NP NP)
                 (VP+TNS ==> V4+TNS PP)
                 (VP+TNS ==> V6+TNS NP PP+LOC)
                 (VP+TNS ==> V9+TNS SBARTHAT)
                 (VP+TNS ==> V10+TNS SBARQ)
                 (VP+TNS ==> V11+TNS QEMB)
                 (VP+TNS ==> V12+TNS SBARFOR)
                 (VP+TNS ==> V14+TNS VP-FIN)
                 (VP+PASS ==> AUX VP+PASS)
                 (VP+PASS ==> VP+PASS-TNS)
                 (VP+PASS ==> *BE-TNS VP+PASSP)
                 (VP+PASSP ==> V2+PASSP NP/NP)
                 (SBARTHAT ==> *THAT S)
                 (SBARFOR ==> *FOR S-FIN)
                 (SBARQ ==> QCOMP S)
                 (QEMB ==> NP+WH S/NP)
                 (QEMB ==> NP+WH VP+TNS)
                 (QEMB ==> PP+WH S/PP)
                 (QEMB ==> PP+LOC+WH S/PP+LOC)
                 (PP ==> P NP)
                 (PP+LOC ==> P+LOC PP)
                 (PP+LOC ==> P+LOC NP)
                 (NP ==> DET  N)
                 (AP+OPT ==> A AP+OPT)
                 (AP+OPT ==>)
                 (NP ==> NAME)
                 (NP ==> PRONP)
                 (NP ==> NP R) ;; relative
                 (NP+WH ==> PRONP+WH)
                 (R ==> RELPRO VP+TNS)
                 (R ==> S/NP)
                 (R ==> RELPRO S/NP)
                 (R ==> PP+WH+PRO S/PP)
                 (R ==> PP+LOC+WH+PRO S/PP+LOC)
                 (Q ==> NP+WH VP+TNS)
                 (Q ==> NP+WH Q/NP)
                 (Q ==> PP+WH Q/PP)
                 (Q/NP ==> V+AUX NP VP/NP)
                 (Q/NP ==> V+AUX NP VP-TNS/NP)
                 (Q/PP ==> V+AUX NP VP-TNS/PP)
                 (Q/PP ==> V+AUX NP VP/PP)
                 (S/NP ==> NP VP/NP)
                 (S/NP ==> NP VP+TNS/NP)
                 (NP/NP ==>)
                 (SBARTHAT/NP ==> *THAT S/NP)
                 (VP/NP ==> AUX VP-TNS/NP)
                 (VP-TNS/NP ==> V2-TNS NP/NP)
                 (VP+TNS/NP ==> V2+TNS NP/NP)
                 (VP-TNS/NP ==> V4-TNS PP/NP)
                 (VP+TNS/NP ==> V4+TNS PP/NP)
                 (VP-TNS/NP ==> V8-TNS NP/NP VP)
                 (VP+TNS/NP ==> V8+TNS NP/NP VP)
                 (VP-TNS/NP ==> V8-TNS S/NP)  ;think
                 (VP+TNS/NP ==> V8+TNS NP/NP VP+TNS)
                 (VP-TNS/NP ==> V8-TNS NP/NP VP+TNS)))

(add-rule-set 'labdict 'DICTIONARY)
(add-rule-list 'labdict
               '((Fido NAME)
                  (Mary NAME)
                  (man N)
                  (i PRONP)
                  (you PRONP)
                  (the DET)
                  (do V+AUX)
                  (chased V2+TNS)
                  (saw V2+TNS)
                  (think V8+TNS V8-TNS V9-TNS V9+TNS)
                  (returned V1+TNS)
                  (that RELPRO *THAT COMP)
                  (who RELPRO PRONP+WH)))

                 
(add-rule-set 'tests 'SENTENCES)
(add-rule-list 'tests
               '((mary returned)
                 (the man that chased fido returned)
                 (the man who chased fido returned)
                 (who do you think mary saw)
                 (who do you think saw mary)
                 (who saw mary)
                 (the man i think saw mary returned)))
                 
