Advanced Programming I Code Review for Programming Assignment 4

Advanced Programming I Lecture Notes

30 March 2007 • Assignment 4 Code Review


Outline

Background: XML

Background: LDAP

The Problem

  1. Read in a tree T.

  2. Read in a sequence of path patterns.

  3. For each path pattern p read

    1. Find all matches to p in T.

    2. Print out the matches.

A Solution

int main()
  std::string emsg
  nary_tree t = input_tree(std::cin, emsg)
  if not emsg.empty()
    std::cerr << "! " << emsg << ".\n"
    exit(EXIT_FAILURE)

  path_pattern pp
  while input_path_pattern(std::cin, pp)
    output_matches(
      std::cout, match_paths(t, pp))

Wish List

  • Data structures:
    nary-tree
    path-pattern
    path-matches
    

  • Procedures:
    input-tree()
    input-path-pattern()
    match-paths()
    output-matches()
    

  • Guideline: find procedures, then ADTs.

  • If you can find the procedures, you can find the ADTs.

  • The procedures give you the ADT interfaces.

Tree Input

Indentation Illustrated

Indentation Cases

Inputting A Tree

nary-tree 
tree-input(istream & is, string & emsg)

  node-data nd
  read-node-data(is, nd, emsg)

  const unsigned indent = nd.indent
  nary-tree t = nary-tree(nd.label)

  while peek-node-data(is, nd, emsg) and 
        nd.indent > indent
    t.add-child(tree-input(is, emsg))

  return t

Indentation Errors

nary-tree input-tree(istream & is)

  if ¬peek-node-data(is, nd)
    return nary-tree()

  const nary-tree t = tree-input(is)

  if peek-node-data(is, nd)
    emsg = "Root with a sibling or parent"

  return t

Path Matching

Matching Paths

Wildcard Matching

Partial Matching

Complete Matching

Data Structures

Testing

Results

Empty Input

Empty Input Don'ts

*** Empty tree with path patterns Input (between the --- lines): --- red * --- Expected output (between the --- lines): --- --- Actual output (between the --- lines): --- ! Error! No input! ---

Don't Prompt.

Prompting Don'ts

*** Two-node tree with no two-node match Input (between the --- lines): --- red blue blue red --- Expected output (between the --- lines): --- --- Actual output (between the --- lines): --- [none] ---

Write Useful Comments

Obey the Rule of Three

The Miscreants

References


This page last modified on 31 March 2006.

This work is covered by a
Creative Commons License.