Lecture Notes for Advanced Programming II

6 November 2001 - Assignment 3 Code Review


The Test Cases

  1. The empty input.

  2. The input

    x = 1
    y = 1
    z = x + y
    

  3. The input

    x = 1
    y = 1
    z = x + y
    
    x = 1
    y = 1
    z = x + y
    

  4. The input

    x = 1000000000000000000000000
    

  5. The input

    x = 1000000000000000000000000
    y = 1000000000000000000000000
    z = x + y
    


The Results

  1. Empty input - 20 programs worked.

  2. xyz input - 1 program worked, 10 sorta worked.

  3. xyzxyz input - 9 programs sorta worked.

  4. 14 programs worked.

  5. 6 programs sorta worked.


Line Statistics - Newlines

2003004005006007008009001000110013001900
01
1011
2011
30141
401111
501
601
701
80111
9011
Totals136211131121

Statistics derived from all files ending in .cc, .cpp, .CC, .C, and .h.


Line Statistics - Semicolons

000100200300400500600
01
101
20122
30111
40
50
601
701
801111
903121
Totals1842431

Statistics derived from all files ending in .cc, .cpp, .CC, .C, and .h.


Procedure Statistics

totalpages per procedure
procs123456789101112
321
431
6111111
84121
9711
10712
10811
116131
12111
128211
1310111
136151
1444411
158511
20191
241581
28271
3426422

Statistics derived from all files ending in .cc, .cpp, .CC, .C, and .h.


File Statistics

File
Counts
CountAverage
Lines/File
Avg Semicolons
per File
19484217
23341147
36277120
4110834
5116883
6117382
7223388

Statistics derived from all files ending in .cc, .cpp, .CC, .C, and .h.


A Design Problem


Top-Down Decomposition


Small Code Is Good


Details Matter


What's an Equation Set?


What About the Equation Set Code?


What Have We Got?


What's An Equation?


Equation Sets Reconsidered


Program for Change


The Equation Class


Breaking An Equation Into Parts


Fragmenting Code

typedef vector<string> fragments;

fragments fragment_eqn(const string & str) {
  fragments pieces;
  str_citer s = str.begin();
  fragment_expr(s, str.end(), pieces);
  fragment_char("=", s, str.end(), pieces);
  fragment_expr(s, str.end(), pieces);
  return pieces;
  }

void fragment_expr(s, e, pieces) {
  fragment_value(s, e, pieces);
  while (fragment_char("+-*/", s, e, pieces))
    fragment_value(s, e, pieces);
  }

void fragment_value(s, e, pieces) {
  if (!fragment_var(s, e, pieces))
    fragment_number(s, e, pieces);
  }

bool fragment_var(s, e, pieces) {
  return fragment_char("A...Za...z", s, e, pieces);
  }

bool fragment_number(s, e, pieces) {
  if (!skip_space(s, e) || !isdigit(*s))
    return false;
  str_citer n = std::find_if(s, e, is_not_digit);
  pieces.push_back(string(s, n));
  s = n;
  return true;
  }

bool fragment_char(chars, s, e, pieces) {
  if (!skip_space(s, e) || (index(chars, *s) == NULL))
    return false;
  pieces.push_back(string(s, s + 1));
  s++;
  return true;
  }


Fragments and Equations


Can We Simplify Yet?


Can We Simplify Now?


Bottom-Up Expression Design


Other Equation Designs


The Expression Class

class expression {

  public:

    enum type {
      number,
      variable,
      operation
      };

    expression(const tkns_citer & s, const tkns_citer & e);
    expression(const expression &);

   ~expression();
    expression & operator = (const expression &);

    void write(ostream &) const;

  private:

    type expr_t;

    string prep;

    bignum val;

    expression * left;
    expression * right;

  };


Expression Code


Expressions and Equations


A Recap


Looking at Expressions::expressions

expression::expression(
  const tkns_citer & s, const tkns_citer & e) {

  tkns_citer op = std::find_if(s, e, is_muldiv);
  if (op == e)
    op = std::find_if(s, e, is_addsub);

  if (op == e) {
    assert(s->size() > 0);
    assert(s + 1 == e);

    if (isdigit((*s)[0])) {
      expr_t = number;
      val = bignum(s->c_str());
      }
    else {
      assert(isalpha((*s)[0]));
      expr_t = variable;
      }

    prep = *s;
    left = right = 0;
    }

  else {
    expr_t = operation;
    prep = *op;
    left = new expression(s, op);
    op++;
    right = new expression(op, e);
    }
  }


Can We Simplify Now?


Constant Folding Expressions


Constant Folding Equations


Constant Folding Equation Sets

class equation_set {
  private:
    bool fold_constants(void);
  }

bool equation_set::fold_constants(void) {
  bool b = false;
  for (eset_iter i = equations.begin(); i != equations.end(); i++)
    b = i->fold_constants() || b;
  return b;
  }

void equation_set::simplify(void) {
  fold_constants();
  }


Substituting Known Variables


Variable Substitution


But, What About Errors?


This page last modified on 15 November 2001.