CS 131       Fall 1998

Lab 4: Functions & Grammars

In Lab 1, you executed JavaScript code which generated random sequences of characters, some of which turned out to be words. In this lab, you will take the next step of generating random sentences based on grammar rules. To accomplish this, you will write simple functions which call library functions and each other, and so build complexity out of simplicity.


Random Parts of Speech

In Lab 2, you wrote code for simulating a magic 8-ball. At that time, the code for randomly selecting a response from a list of potential responses was given to you. This code has since been encapsulated into the function randomItem which is contained in the random.js library. Any time a random item (e.g., a word) must be selected from a list of options, the randomItem function can be loaded from the library and called. For example, consider the following JavaScript function:

function noun() // Given : none // Returns: a random noun { return randomItem(["man", "woman", "ball"]); } This function uses randomItem to select at random from a list of three nouns. The call noun() will thus return one of the three nouns each time it is executed.


EXERCISE 1:     Enter this function definition into the JavaScript interpreter. Make 10 calls to the noun function and list the returned values below:






What is displayed by the following JavaScript code? Explain.

noun1 = noun(); noun2 = noun(); document.write(noun1 + " " + noun2);






EXERCISE 2:     Similar to the noun function above, define additional functions called verb and article which return a random verb (e.g., "hit", "liked", "smelled") and article (e.g., "the", "a", "some"), respectively. Feel free to add to the vocabulary of these functions.

Once you have tested your functions, save their definitions in a document named Lab4.






Grammar Rules

If you recall from your grade school days, sentences can be parsed according to grammar rules -- syntactic rules which specify how the different parts of speech can fit together. For example, a simple sentence can be constructed with an article, followed by a noun, followed by a verb, such as "The ball smelled." or "Some woman hit." This can be written as a grammar rule in the following notation:

sentence <--- article + noun + verb


EXERCISE 3:     Define a function called sentence which has no parameters, and which returns a sentence of the above form. Your function should work by calling each of the previously defined functions (article, noun, and verb), and concatenating the resulting words with spaces in between.

Once you have defined your function, make 10 calls to it as follows:

sent = sentence(); document.write(sent); List the returned values below. Save your code in the Lab4 document.







EXERCISE 4:     Now consider the following grammar rules which define a more complex sentence structure. sentence <--- nounPhrase + verbPhrase nounPhrase <--- article + adjective + noun verbPhrase <--- verb + nounPhrase Update your code to generate sentences based on these grammar rules. In particular, you must define new functions called adjective, nounPhrase, and verbPhrase which return the appropriate type of word or phrase, and also modify sentence to generate and return the new sentence structure.

For example, the call sentence() might return "the big man hit a green ball" or "some nice woman liked the big man". Once you have defined/modified the necessary functions, make 10 calls to sentence and list the returned values below. When done, save your code in the Lab4 document.



Optional Parts of Speech

The grammar rules in the previous exercise leave no options when constructing a sentence. Every sentence generated using these rules will have the exact same structure, even the same number of words (7). In practice, sentences and phrases can be defined with optional parts. For example, the adjective in a noun phrase is not necessary, since "the nice man" and "the man" are equally valid. Similarly, a verb phrase does not always require a noun phrase after the verb. When writing grammar rules, optional parts of speech can be denoted using brackets. For example,

sentence <--- nounPhrase + verbPhrase nounPhrase <--- article + [adjective] + noun verbPhrase <--- verb + [nounPhrase] Note: the brackets used to denote optional components of a grammar rule should not be confused with brackets as they are used in JavaScript. In JavaScript, brackets are used to construct a list, as in the list of nouns that appears in the noun function.


EXERCISE 5:     From looking at these grammar rules, what is the fewest number of words possible in a sentence? Similarly, what is the greatest number of words possible in a sentence. Justify your answers.







EXERCISE 6:     In order to generate sentences based on these more flexible grammar rules, you must modify the nounPhrase and verbPhrase functions so that they sometimes include the optional parts of speech and sometimes they don't. The following function, which takes a string as argument and randomly returns either that string or the empty string, will prove useful. function optional(str) // Given : str is a word or phrase // Returns: either str or the empty string "" { return randomItem(["", str]); } Type in this function and make 10 calls with your name as argument. (Reminder: you must enclose a string in quotes, or else it will be treated as a variable.) Is your name returned roughly half the time?





What value is returned by the code:

adj = adjective(); optional(adj); Explain your results.






EXERCISE 7:     Modify nounPhrase and verbPhrase so that they make use of optional to implement the grammar rules above. Using the new versions of these functions, make 20 calls to sentence and list the returned values below. When done, save the new versions of these functions in the Lab4 document.

Note: there should always be exactly one space between words in a sentence.

















EXERCISE 8:     Referring to the results of your 20 calls to sentence from the previous exercise, answer the following questions:





Hand in a printout of the Lab4 document, attached to these sheets.