CS 313 - Homework 1 - Syntax and grammars

Due: Wednesday 2/22 at 11:15am

This assignment consists of written questions to be done individually, and a programming assignment, which you can do with a partner (or by yourself). Please type the written answers and submit them either in .txt or .pdf format. Where you are asked to draw trees you can either draw them using "ASCII art" or snap a picture of a drawing with your cell phone, include in a word document with your other typed answers, and save as pdf. Please submit your homework online (see instructions below).
  1. Read Sethi Chapters 1 and 2. A copy of the book is available in MBH 632 (please do not remove from the room).

  2. (Sethi problems 2.1, 2.2, 2.3) For each of the following infix expressions, write them in (1) prefix, (2) postfix, and (3) draw the expression tree (aka abstract syntax tree). Treat "sqrt" as an operator with one argument.
    1. a * b + c
    2. a * (b + c)
    3. a * b + c * d
    4. a * (b + c) * d
    5. (b/2 + sqrt((b/2)*(b/2) - a * c)) / a

  3. Convert the following prefix (a, c, e) and postfix (b, d, f) expressions to infix. Be sure to add parentheses where necessary. Treat "sqrt" as an operator with one argument.
    1. + * 2 a * 3 b
    2. 2 a + b 3 + *
    3. + sqrt 4 * sqrt 4 b
    4. 8 8 8 + sqrt sqrt -
    5. - a - b - c d
    6. c d / b a + *

  4. (Sethi problem 2.4c) Give a context-free grammar describing real numbers in which either the integer part or the fractional part can be empty, but not both. Thus, the grammar must allow 31., 3.1, and .13, but not a decimal point by itself.

  5. (Sethi problem 2.6 c,d) Given the expression grammar
    E ::= E + T | E - T | T
    T ::= T * F | T / F | F
    F ::= number | name | ( E )
    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):
    1. 2 + 3 * 5
    2. (2 + 3) * 5

  6. Programming problem (please work in pairs): Mad Lib.

    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):

    <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
    If we generated strings from this grammar at random, we might get stories like the following:
    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 .
    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.

    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:

    <noun>  0.6   ::= man
    <noun>  0.2   ::= cow
    <noun>  0.2   ::= car
    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:
    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.

Online submission

Upload your files on the HW 1 submission page by the beginning of class on Wed.

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.