CSC 107 Fall 2003

Lab 2: Functions and Grammars

In an earlier in-class exercise, you executed
JavaScript code that 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 that call library functions and each other, and so build complexity out of
simplicity.

Random Parts of Speech

Chapter 5 introduced the random.js
library, which contains several useful functions for generating random values. For
example, the RandomOneOf function randomly selects an item from a list of
options. Consider the following JavaScript function:

This function uses RandomOneOf 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:
Create a Web page named grammar.html and enter the noun function
definition in the HEAD of that page. In the BODY of the page, call the
noun function and display the returned value.

Load this page 10 times and list the displayed values below:

What is displayed each time the following JavaScript statement is executed?
Explain.

document.write(noun() + " " + noun());

EXERCISE 2:
Similar to the noun function above, define additional functions called
verb and article that 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.

Important: Each function should return its randomly generated word, NOT
write it!

Make the appropriate calls to test these functions in your grammar.html
page.

Grammar Rules

If you recall from your grade school days, sentences can be parsed according to
grammar rules -- syntactic rules that 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

EXERCISE 3:
Define a function called sentence that returns a sentence of the above
form. Your function should have no inputs (similar to noun,
article, and verb), and should work by calling each of the
previously defined functions and concatenating the resulting words with spaces in
between. Note that the format of a sentence is not random, it will always consist of
an adjective followed by a noun followed by a verb. The random appearance of
sentences is due to the fact that the individual words are randomly generated by the
other functions.

Important: Your function should return the sentence, NOT
write it!

Once you have defined your function, modify the body of your
grammar.html page to call this function and display the return value. Load
the page 10 times and list the displayed sentences below:

EXERCISE 4:
Now consider the following grammar rules that define a more complex sentence
structure.
sentence

Update your page to generate sentences based on these grammar rules. In
particular, you must define new functions called adjective,
preposition, nounPhrase, verbPhrase, and
prepPhrase that return the appropriate type of word or phrase, and also
modify sentence to call these functions appropriately.

For example, the call sentence() might produce "the big man by a tree
hit a green ball above some woman" or "some nice woman beside the ball liked a big
man by the house". Once you have defined/modified the necessary functions, load the
page 10 times and list the displayed sentences below:

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 (13). In practice, sentences and phrases
can be defined with optional parts. For example, the adjective and
prepositional phrase in a noun phrase are not necessary, since "the man", "the nice
man", "the man by the tree", and "the nice man by the tree" are equally valid.
Similarly, a verb phrase does not always require a noun phrase after the verb. For
example, consider the following generalized grammar rules:

sentence
Here, the notation [PART_OF_SPEECH] is used to denote an optional part of
speech. Note: The brackets used in grammar rules should not be confused with
JavaScript brackets, which are used to represent lists in JavaScript code.

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.

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
RandomOneOf function will also prove useful here. In particular, whenever
you want an optional part of speech, you can call the RandomOneOf function
with two options to select from: the part of speech, and the empty string. For
example, the following will either return an adjective (as a result of the call
adjective() or the empty string.

RandomOneOf([ adjective(), "" ])

EXERCISE 6:
Modify the nounPhrase and verbPhrase functions to match the new
grammar rules. In particular, you should include calls to RandomOneOf so
that the specified parts of speech are optional. Using the new versions of these
functions, load the page 20 times and list the displayed sentences below:

EXERCISE 7:
Referring to your 20 sentences from the previous exercise, answer the following
questions:

What was the shortest sentence you obtained? Is its length close to the
minimum length that you predicted?

What was the longest sentence you obtained? Is its length close to the
maximum length that you predicted?

How many times did a noun appear in the 20 sentences? Is this close to what
you would expect? Explain.

How many times did an adjective appear in the 20 sentences? Is this close to
what you would expect? Explain.