[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] /[CS320 Course Materials] /08q.html [08q.txt(Text)] [Search ]
Thu Apr 25 16:30:51 PDT 2013
[Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Grading] [Contact]


    CS320/08 Chapter 7 - Expressions

      Question 1 Definitions

      Describe, with one or two actual examples: Arithmetic expressions, relational expressions, Boolean expressions, conditional expressions (NOT Boolean expressions!), overloaded operators, coercion, functional side effect , and short circuit evaluation

      We will tackle the following questions in groups.

      Question 2 ?:

      a. In C/C++/Java/JavaScript, if B is a boolean expression and E1 and E2 are expressions of type T, what kind of expression is
    1. B ? E1: E2

      b. What type is it?

      c. What does the following C++ program output?

       	#include <iostream>
       	int main()
       	{       cout << ( 3>2 ? "bigger\n" : "smaller\n" );
       	        cout << ( 2>3 ? "bigger\n" : "smaller\n" );
       	        cout << ( 2>2 ? "bigger\n" : "smaller\n" );

      c. Does this type of expression use short circuited evaluation(Y/N)? Why do you think this?

      d. Can you create a C/C++/Java function that can be used as a perfect replacement for the expression (B ? E1: E2) for all expressions B, E1, and E2(Y/N)? Why?(paragraph)

      Question 3 Bitwise operations

      C included 3 binary operators that work with the bits in binary representation of integers. They run incredibly fast because they are implemented as machine operations. They are useful in systems programming and for implementing data where each value represents a set of of elements.

      Here are some questions on these operations:

      a. What are the binary representations of the numbers 5 and 7?

      b. What is output by the following program fragment, and why?

       		cout << (3 & 5) << endl
       		cout << (3 ^ 5) << endl
       		cout << (3 | 5) << endl

      Answers: [ ../samples/c.syntax.html#Bitwise Expressions ]

      Question 4 Overloading

      C++ and Ada permit programmer-defined overloaded operators. Pascal & Java languages overload some operators but do not allow the programmer to define new overloaded symbols. (a) List as many reasons as you can for the Pascal/Java form and (b) as many as you can think of, for the C++/Ada philosophy.

      Question 5 Precedence and associativity

      a. Describe, with examples: operator precedence and operator associativity.

      b. Name one (1) high level programming language that doesn't have them,

      c. Give one (1) reason for having them.

      d. In a certain language, expressions have two operations: multiplication and addition. These are symbolized by "*" and "+" respectively. Precedence: Multiplication is done before addition. Associativity: left to right. Show how to express these rules in a syntax metalanguage like BNF/EBNF/XBNF.

      [ 08.html ]

      Question 5 Small Functional language

      Consider a small language that only has the simplest int expressions and uses functional notation only:
    2. expression::= number| variable | function_call ,
    3. function_call::= function "(" list_of_arguments ")",
    4. list_of_arguments::= argument #( "," argument),
    5. argument::= expression.

      a. Draw class diagram in the UML that fits the above grammar. The diagram should show the classes: Expression, Number, Variable, FunctionCall, and Function. It should also show the relationships between them and show argument as a role_name.

      Click [ ole1a.gif ] to see an answer.

      b. Add operations to express the semantics: All types of expression have a method or function evaluate() that returns an int value. A Function has a method apply( ....) that takes a list of int's and returns an int result.

      Click [ ole1.gif ] to see an answer.

      c. Describe the evaluation of an FunctionCall in terms of a apply(...) in the Function class and evaluating the arguments of the FunctionCall.


      In English: Evaluating an FunctionCall requires that the sub-expressions that make up the operands are evaluated first. Then, the Function is applied to the values obtained by evaluating its arguments:

      In a C++ type language:

       int FunctionCall::evaluate()
       {  // A functionCall has a vector of arguments which we evaluate first.
          for(int i=0; i< argument . size(); i++)
          {  values.push_back(  argument [i] . evaluate()  );
          }//end for
          int return_value = function.apply(value);
          return return_value;
      Note: the vector of values is used just like a stack. Further it is allocated on the stack.... Many language systems evaluate an expression using a run time stack like this example.

      Question 6 on C++

      If i is an integer variable what is i++ in C/Java/C++? Why? What is special about it compared to i+1.

      Project Question

      In your new language: do you have expressions(why?)? What operators will you have(why?)? What are the priorities of the operators(why?)? For operators with the same priority, what kind of associativity do they have (why?)?

    Review -- Think about your answers

( End of document ) <<Contents | Top