Programming Assigment 5

Course Project: Phase 1 -- The Symbol Table


  • Background:

    • You must demo to another team by (TBD).
    • This must also work with Scanner and Parser Integration
  • Overview of the Symbol Table:

    Chapter 1 introduces the Symbol Table and how it fits in with the rest of the compiler. Chapter 2 Section 7 (beginning on pg 85) expands on this usage. Other authors have said that ``in strongly-typed languages, where everything must be declared by the programmer before use, the lexical analyzer and the parser, working together, must make sure that every newly declared identifier is entered in the symbol table, and they must ensure that every identifier used subsequently has been declared.'' (Parsons) Therefore, it is obvious that the symbol table is an important part of our compiler project and must be designed, implemented, tested, and working properly for our compiler to be successful.

  • Implementation of the Symbol Table

    Your symbol table may be implemented as a stack of Binary Search Trees. When the scanning begins a BST will be pushed onto the stack for the main program. Every time we enter a procedure a BST will be pushed onto the stack and all of the local variables will be stored there. When a procedure is finished that level of the stack will be popped.

    Some compilers implement Level 0 of the stack as a BST containing all the reserved words for the language, while others place the reserved words directly into the scanner and return the tokens to the parser without consulting the symbol table. I would recommend the latter choice.

    Therefore, your job is to

    • Write a Stack
    • Write a Binary Search Tree
      (If you are a grad student taking CS 660 or an honors student taking CS 460 Honors, it must be a balanced BST).
    • Combine them to form the Symbol table.

    The nodes in the BSTs will contain at least a key (which should be the string identifier for that symbol) and some other arbitrary structure (the data), the contents of which you will decide at a later time.

    Your overall Symbol Table will need to have some basic routines to

    • insert a new symbol
    • search for a symbol
    • write the contents of the symbol table to a file
    • push or pop a new level
    • ...
  • Implementation Notes

    • The structure of your node will change as you learn more, but the key will probably remain the string value of the token.
    • You must return a pointer to the node searched for (including the data) so the parser can manipulate it later. If data is copied (balancing algorithm) you may need to make a data structure pointed to by the node and return a pointer to the data.
    • You must provide a means to search only the top (current) level.
    • Your search routine must provide enough information for callers to determine whether the symbol was found in the top level or some other level or not at all.
    • Your insert routine must provide enough information for callers to determine whether the inserted symbol shadows or conflicts with one already in the table. (and what line the shadowed symbol was on)
    • You must write a driver program to test your symbol table and be able to demonstrate that it works properly.