Q0 Set up : svn export http://unbox.org/wisp/var/timm/10/310/src/st/3/c : svn add c : svn commit -m "3c ready to go" : For each of the following questions, add lines like this to "run" printf "\n===== 3/c/q1 ========================\n\n" cat myGrammarFilesUsedInThisExample.rules cat myCodeThatAnswersThisQuestion.st execute : where "execute" is the "test" command shown below. Q1 Background : In non-OO, data are dead structs that : get manipulated by puppet masters (the functions). In OO, the data : "structs" (really, classes) are the masters and all functionality : lives inside the appropriate class. : : For example, consider the problem of building the attributed : grammars. In all our other implementations, we stored them : externally in some hash-table or array. In OO, that is wrong-headed. : The fact that we are to cache something, and even the cache value, : can be defined inside Rule. So, code up the attributed : grammar code, and do it the OO-way way: Hints: : Create sub-classes of Rule for SimpleRule and CachedRule. SimpleRule : is the same as Rule but CachedRule has a "cached" value. : : Add some reset code so that you can spin over all the rules and : blast their cache. : : Alter rewriteSome: in Grammar such that the selection code moves : inside Rule. : : After Rule such that SimpleRules do what they currently do while : CacheRule returns the cached value, if it exists. Otherwise, it : finds a rewrite and caches it. : : Alter Tag.st so that when an attributed rule is meet, extra messages : are send to Grammar so it knows to create a Rule Write : Adapt your Smalltalk 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. 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 : Boy -> ajit : Girl -> pima : Girl -> barkha : Verbphrase -> Verb Modlist Adverb with Nounphrase : Verb -> runs : Verb -> walks : Modlist -> : Modlist -> very Modlist : Adverb -> quickly : Adverb -> 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 : Boy = ajit : Girl = pima : Girl = barkha : Verbphrase -> Verb Modlist Adverb travels with Nounphrase : Verb -> runs : Verb -> walks : Modlist -> : Modlist -> very Modlist : Adverb -> quickly : Adverb -> slowly 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. Test : Show that a grammar with the "=" sign can load and selections from : "=" rules remain constant.