Homework 5



  1. Suppose you are given the following program:
    int k = 30;
    
    main() {
      int k = 50;
      adder(30);
    }
    
    int adder(int operand) {
      int k = operand + 40;
      printer();
    }
    
    void printer() {
      printf("k = %d\n", k);
    }
    
    What value will be printed for k by printer assuming:

    1. static scoping
    2. dynamic scoping

  2. Assume you have the following snippet of C++ code:
      #include <iostream>
      #include <string>
    
      extern bool edit_mode;
    
      class Button {
        protected:
          string label;
    
          class HandleButtonSelection {
            protected:
              void handleEvent() {
                  cout << label << endl;
                  edit_mode = true;
              }
          }
        public:
          Button(string myLabel) {
            label = myLabel;
          }
    };
    
    Make the following assumptions:

    1. The iostream library contains a declaration of the form:
      outputstream cout;
      
      and a declaration for the outputstream class. outputstream is assigned a scope id of 2. Its category is "type" and its "other" information is "class scope 2".
    2. The string library contains a class declaration for string. string is assigned a scope id of 3. Its category is "type" and its "other" information is "class scope 3".
    3. You can treat bool as a primitive type
    4. You have a symbol table of size 7 and the names hash into the symbol table as follows:
      bool: 2
      string: 5
      edit_mode: 5
      cout: 5
      label: 6
      Button: 6
      handleEvent: 1
      HandleButtonSelection: 3
      myLabel: 0
      
    Using a style similar to that used in the Scott text:

    1. Show the contents of a LeBlanc-Cook style symbol table after the above file has been processed by the compiler.
    2. Show what the scope stack looks like when the compiler is processing the method handleEvent
    3. Show what the scope stack looks like when the compiler is processing the Button constructor method.

  3. Assuming a LeBlanc-Cook style symbol table, propose a strategy for how the compiler might find the symbol table information (e.g., the type) of a complicated reference such as employee->address->street. If it helps to have concrete types to work with, then assume you have the follow declarations:
    struct addr {
      char *street;
      char *city;
      char *state;
      int zipcode;
    }
    
    struct worker {
      char *name;
      struct addr *address;
    }
    
    struct worker *employee;
    
    hint: think about the "with" statement discussed in class and how the strategy used for "with" statements might be adapted to work with this chain of references.

  4. Suppose I have some C++ code that looks as follows:
    class GraphicalObject {
      protected:
        int left;
        int top;
    }
    
    class BoxObject : public GraphicalObject {
      protected:
        int width;
        int height;
    }
    
    class Rectangle : public BoxObject {
      public:
        void draw(GraphicsContext g) {
          g->drawRect(left, top, width, height);
        }
    }
    
    Assume a LeBlanc-Cook style symbol table and algorithm for resolving variable name references. In the draw method, the resolution algorithm must have a way of finding the symbol table records for left, top, width, height. However, since Rectangle is not physically nested within either BoxObject or GraphicalObject, the scope records for BoxObject and GraphicalObject will not be on the scope stack when the draw method for Rectangle gets processed by the compiler. Propose a strategy for how the compiler might find the symbol table information for left, top, width, and height. Your strategy will need to discuss:

    1. the "other" information that must be maintained for a class entry in the symbol table (e.g., for the name GraphicalObject or BoxObject)
    2. how the compiler might be able to use the "other" information to create and push appropriate scope records for BoxObject and GraphicalObject onto the scope stack