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.
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:
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:
Now consider the following grammar rules which define a more complex 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.
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,
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.
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.
What value is returned by the code:
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.
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.