Q0 Get code : In the usual place : svn export http://unbox.org/wisp/var/timm/10/310/src/lisp/2/c For each of the following questions, add lines like this to "run" printf "\n===== 2/c/q1 ========================\n\n" cat myCodeThatAnswersThisQuestion.lisp execute : where "execute" is the "test" command shown below. Q1 Write : Adapt your LISP grammar to handle attributed grammars. For such grammars, : some of the productions are not "(a -> RHS)" but "(a = RHS)". For : such rules, the rhs is a set of single atoms; e.g : (Article -> the a). terminal. If that rule is selected, : then an attribute is fixed to one of those values. : : This lets us fix strings and reuse them. To see why that is useful, : consider the following grammar : : (Sentence -> (Nounphrase Verbphrase)) : (Nounphrase -> (Boy)) : (Nounphrase -> (Girl)) : (Boy -> john ajit) : (Girl -> pima barkha) : (Verbphrase -> (Verb Modlist Adverb with Nounphrase)) : (Verb -> runs walks ) : (Modlist -> () (very Modlist)) : (Adverb -> (quickly slowly)))) : : Note that now we can select two boys at different parts of the : story. : : But, in the following grammar, once we pick a heroine/ hero then we : always use their name (note the use of "=" in the following). : : (Sentence -> (Nounphrase Verbphrase)) : (Nounphrase -> (Boy)) : (Nounphrase -> (Girl)) : (Boy = john ajit) : (Girl = pima barkha) : (Verbphrase -> (Verb Modlist Adverb with Nounphrase)) : (Verb -> runs walks ) : (Modlist -> () (very Modlist)) : (Adverb -> (quickly slowly)))) : Sentence -> Nounphrase Verbphrase Note : Of course, the grammar still is "broken" since in this story, : the someone can travel with themselves. : e.g. "pima runs slowly with pima" : What we need is a better : constraint language in our grammar such that we can specify that : someone cannot travel with themselves. We will build such a grammar, : later in the semester. Hint Working memory : Keep a working memory of settings in a hashtable. To create a hash table (let ((mem (make-hash-table))) ... ) To put something into a hash table (setf (gethash name mem) something) To test is something is NOT in a hash table (unless (eq :missing (gethash name mem :missing)) ;; handle missing item ) Before rewriting a terminal : change the generation code such that before it looks up a definition : if checks for "if (not (eq :missing (gethash name mem :missing)))". : And, if it is there, just return it. After rewriting a terminal : On return from generating a terminal, if the result is something : defined in as a Setting, then you have to set this Setting. Reading : You will also have to change how story.awk reads in productions. : Now, if we see "Variable = terminal", then we have to: : 1) read it as if it was "Variable -> terminal" : 2) add it to the hash table Test : nice -n 1 clisp -q attr.lisp : This code should generate enough stories till the same boy's : name appears twice.