CS 433 - Homework 4 - 3/3/08

Declaration resolution: types and variables

Due: Monday, 3/10/08, at 2pm

In this assignment, you will implement the first part of the declaration resolution phase for the OMG compiler: the creation of declaration descriptors and their proper insertion into the various linked data structures that we discussed in class. For now, to keep the assignment manageable, we will focus only on the declaration of global and local variables, and skip the declaration of functions and parameters.D

Starting with this assignment, you can work in groups of two. If you prefer, you can continue to work by yourself. Those who want to work in groups should pair up as soon as possible and get started early. I'll leave it up to you how to split up the work and whether to maintain one or two copies of the code. You might want to tell your partner your password. Whatever mode of collaboration you choose, however, make sure that both of your names appear in a comment at the top, and only submit one copy (on paper as well as using "make submit").


Start by copying my directory ~schar/cs433/hw4, and add your code from the previous homeworks as shown below. (Note: Since all programming assignments in this course build on each other, it is important that you are confident that your existing code is correct. If you have doubts, you may want to substitute my sample solutions. At the very least, you should always compare your solutions with the sample solutions.)

    ~> cd cs433
    ~/cs433> cp -r ~schar/cs433/hw4 .
    ~/cs433> cd hw3
    ~/cs433/hw3> cp symtab.c scanner.l parser.y ../hw4
    ~/cs433/hw3> cd ../hw4
    ~/cs433/hw4> ls
    Makefile  main.c    printtree.c  scanner.l  symtab.h  syntree.c
    listp     parser.y  resolve.c    symtab.c   syntax.h  syntree.h
Your task is to implement the declaration resolution of variables in the main program as discussed in class. This involves adding code to the file resolve.c - see the comments in this file for more detail. Note that I've supplied a new version of symtab.h, as well as slightly modified versions of syntree.h, Makefile, main.c, and printtree.c. I'm again including the "listp" script for printing a file with line numbers. Be sure to study the file symtab.h so you understand the new data structures for declaration descriptors. Use the usual sequence of "make depend" and "make" to compile everything. You chould also make the following two changes to properly initialize the new "decl" and "declstack" pointers during the scanning phase:

  1. Add the following line to the function "STinsert" in symtab.c:
          node->declstack = NULL; /* NEW HW 4: initially empty stack of decldescs. */
    
  2. Add the following line to the function "create_ident_node" in scanner.l:
          yylval.treenode->ident.decl = NULL;  /* NEW HW 4: not resolved yet */
    

When you have completed resolve.c, your program should work as follows:

~/cs433/hw4> ./listp ~schar/cs433/omg/testhw4.omg
1 # testhw4.omg - test program for hw 4
2 glob boo a, b;
3 omg() {
4     num b, c;
5     a := yes;
6     b := 1;
7     num a := 2;
8     {
9         num[0..9] c;
10         b := 3;
11         c[0] := 4;
12     }
13     c := 5;
14 }

~/cs433/hw4> ./omg -t ~schar/cs433/omg/testhw4.omg
#line   2: program (2)
#line   2:    globlist
#line   2:       vardecl (2)
#line   2:          type = boo
#line   2:          decllist
#line   2:             decl (2)
#line   2:                ident = a : Declared at line 2
#                         NULL node
#line   2:             decl (2)
#line   2:                ident = b : Declared at line 2
#                         NULL node
#line   3:    funlist
#line   3:       fundecl (4)
#                   NULL node
#line   3:          ident = omg : Unresolved identifier
#                   NULL node
#line   3:          body (1)
#line   4:             stmtlist
#line   4:                vardecl (2)
#line   4:                   type = num
#line   4:                   decllist
#line   4:                      decl (2)
#line   4:                         ident = b : Declared at line 4
#                                  NULL node
#line   4:                      decl (2)
#line   4:                         ident = c : Declared at line 4
#                                  NULL node
#line   5:                assign (2)
#line   5:                   ident = a : Declared at line 2
#line   5:                   const = 1
#line   6:                assign (2)
#line   6:                   ident = b : Declared at line 4
#line   6:                   const = 1
#line   7:                vardecl (2)
#line   7:                   type = num
#line   7:                   decllist
#line   7:                      decl (2)
#line   7:                         ident = a : Declared at line 7
#line   7:                         const = 2
#line   8:                body (1)
#line   9:                   stmtlist
#line   9:                      vardecl (2)
#line   9:                         arraytype (3)
#line   9:                            type = num
#line   9:                            const = 0
#line   9:                            const = 9
#line   9:                         decllist
#line   9:                            decl (2)
#line   9:                               ident = c : Declared at line 9
#                                        NULL node
#line  10:                      assign (2)
#line  10:                         ident = b : Declared at line 4
#line  10:                         const = 3
#line  11:                      assign (2)
#line  11:                         array (2)
#line  11:                            ident = c : Declared at line 9
#line  11:                            const = 0
#line  11:                         const = 4
#line  13:                assign (2)
#line  13:                   ident = c : Declared at line 4
#line  13:                   const = 5
You should also test your code on other OMG programs of your own design, including some with global variables. Also test all the different types of statements and expressions. Make sure you generate useful error messages for multiple declarations or undeclared variables. Do not add your test programs to your Makefile (i.e., don't submit them electronically), but do hand in a printout of one of your tests that demonstrates that declarations are resolved correctly and that proper error messages are generated. Note that the current version of resolve.c doesn't process function names and parameters, so expect to get "undeclared" errors for all parameters in any of the "old" OMG programs (yours and mine).


What to hand in

Hand in a printout of your completed file resolve.c, as well as a printout of a test case as discussed above. Also submit your program electronically by typing "make submit". Remember that I only need one submission per group.

Please note on your submission how much time it took you (use the average time per person if you are working in a group).