Assignment 2, Due 9/09/04 12:30 p.m.
(Must be done with your group)

You will need to refer to mystery grammar to answer these questions.

  1. (Skill 3.2) Problem set, page 157, #8

  2. (Skill 2.2) Problem set, page 157, #10

  3. (Skill 2.3) Problem set, Page 158, #11

  4. (Skill 2.3) Problem set, Page 158, #12

  5. Consider the following two grammars for <Expr>.  Note that the first  grammar is part of <Expr>'s grammar in mystery.  <Operator>  and <Operand> in both grammars are the same as <Operator> and <Operand> in mystery's grammar.  You need not look at the definition of <Operand> to answer this question.

    <Expr> →<Operand> | <Expr> <Operator> <Operand>

    <Expr> <Operand> <ExprSuffix>
    <ExprSuffix> <Operator> <Operand> <ExprSuffix> | ε

    1. (Skill 2.2) Intuitively describe the expressions generated by two grammars. 

    2. (Skill 2.4) Do these grammars generate the same or different expressions?  Explain

  6. Consider the following two grammars for <Operand>.  The first grammar is part of <Operand>'s grammar in mystery.  <Expr> in both grammars is the same as <Expr> in mystery's grammar.  You need not look at the definition of <Expr> to answer this question. 

    <Operand> Number | id |  <Operand> [<Expr>]

    <Operand> Number <OperandSuffix> | id <OperandSuffix>
    <OperandSuffix> [<Expr>] <OperandSuffix> | ε

    The square brackets ([...]) are for array references and do not mean "optional" as in EBNF.

    1. (Skill 2.2) Intuitively describe the expressions generated by two grammars. 

    2. (Skill 2.4) Do these grammars generate the same or different expressions?  Explain

  7. (Skill 3.4) Write a program that demonstrates whether + has higher precedence than * or vice versa in the C++ programming language. Make sure that you are checking for precedence in your program and not for left or right associativity.  Submit your program, the output produced by running your program, and an argument indicating how you arrived at the relative precedence of + and * based on the output.

  8. (Skill 2.5) Give the BNF grammar for floating point numbers which are made up of a fraction (e.g., 5.6 or 3.123 or -2.5) followed by an optional exponent (e.g., E10 or E-10).  The exponent, if it exists, is the letter "E" followed by an integer.  For example, the following are floating point numbers: 3.5E3, 3.123E30, -2.5E2, -2.5E-2, 3.5.   The following are not examples of floating point numbers: 3.E3, E3, 3.0E4.5. Your grammar should be complete (i.e., it should not count on a non-terminal that it does not itself define).

  9. (Skill 3.3) Looking at MYSTERY syntax determine if the "+" operator is left or right associative.  Also determine if "+" has higher, lower, or same precedence as ">".  Explain the reasoning behind your answer.

  10. (Skill 3.1) From the syntax of mystery (you will need to look at the full syntax for this question), we see that a mystery program is a block  A mystery program starts by executing this block.

We have designed mystery to be as small as possible yet large enough to illustrate many of the concepts that we will cover in this class.  Thus, mystery is missing many features that you may be used to in your favorite programming language.  On the other hand, you will find later this semester that mystery has some features that are more powerful than features in your favorite programming language.

  1. Write a program that prints out 1 through 100 (inclusive).  You should test your program using this form.  Use your group name as the "User id", enter your code in the "Input" space, and click "Submit".  This will compile and run your program and present any errors or output in a new browser window.  You may use as many attempts as you wish.  As an additional challenge to yourself, try to do this in as few attempts as you can.  Be sure to brag about how many tries it took in your submission!

  2. Write a program that has two procedures, sum and main.  sum takes two integers as arguments and returns their sum.  Main calls sum with two arguments and prints out the result.    Once again, you should use this form to test your program.