draw a parse tree for each of the following expression (starting with nonterminal E; you can substitute the terminal symbol "number" for any actual number):E ::= E + T | E - T | T T ::= T * F | T / F | F F ::= number | name | ( E )
A Mad Lib is a word game where random words are inserted into a story template, with funny results. We can create such nonsensical stories using a simple context-free grammar (CFG) for a small subset of English, and choosing productions at random.
For instance, consider the following grammar in Backus-Naur form (BNF):
If we generated strings from this grammar at random, we might get stories like the following:<story> ::= <sentence> <sentence> <sentence> the end . <sentence> ::= <subject> <verb> <object> . <subject> ::= <article> <optional_adjective> <noun> <object> ::= <subject> <article> ::= the | a <noun> ::= man | cow | car <verb> ::= loved | missed | hated | killed <optional_adjective> ::= big | green | shiny | EPSILON
Your task is to implement this idea in a programming language of your choice (Python, Java, ...). Ideally you can write a better grammar that results in more interesting stories. First, define your grammar in BNF format as in the example above, and save it in a text file. Then, implement the grammar in a program that chooses productions at random. You can either hard-code the grammar (hint: use a function for each nonterminal), or, if you are looking for a challenge (and some extra credit), write a program that reads that text file containing the grammar and then generates sentences accordingly.a big man loved the big man . the cow missed the car . the big man missed the green man . the end . a green car missed a big man . a shiny cow killed the shiny man . the big man hated a big man . the end . a green cow loved the green car . a big car missed the cow . the green car missed the green man . the end .
Be creative! Anything goes, from programs writing limericks to presidential speeches. The only constraint is that you use a CFG. Possible extensions are adding more grammar rules (adverbs, prepositions, pronouns, ...), or writing recursive rules that result in stories or sentences with varying length. Another idea is to specify probabilities for different rules for the same nonterminal, resulting in a probabilistic or stochastic grammar. For instance, if you want to use the noun "man" more often than "cow" or "car", you could specify the rules as follows:
Submit your grammar text file, your program, and sample output produced by your program. If you work in a team, be sure that both names appear in a comment, and submit only one copy. Create a zip archive madlib.zip containing all three files and upload it as described below. You can create the zip file from the command line as follows:<noun> 0.6 ::= man <noun> 0.2 ::= cow <noun> 0.2 ::= car
zip madlib.zip madlib.py grammar.txt output.txt
I will post an edited volume of your stories (and the generating grammars) on the course page.
For more inspiration, here are some entertaining entries submitted the last time the course was taught.
For the written problems (1-5), upload either a text file written.txt or a pdf file written.pdf. For problem 6, one of the team members should upload madlib.zip.