Creating a symbol table in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rolan
    New Member
    • Jan 2008
    • 13

    Creating a symbol table in c++

    This is for a class assignment, and I need some help getting my head wrapped around the problem.

    The problem is to create a symbol table for a c++ program. I know what a symbol table is, but figuring out how to create one is my difficulty.

    The only way I can think to create a symbol table is to have a list of valid keywords which I then use to search through the source code. This would enable me to find the declarations of variables, however, it would require me to:
    1) search through the entire file for every keyword

    2) To search through any header files for classes which would need to be added to the list of keywords

    3)I would have to create a list of all keywords from the header files or I would need to have a list of all keywords in the c++ language. (or just have a keyword list tailored to the program I parse for identifiers)


    I have to write a program which creates a symbol table of a c++ program, so I want to make sure i'm approaching this problem in the right way before I create a detailed algorithm.

    Any ideas/help or comments are appreciated.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is a simple word search problem where you build a tree that has a leaf for each different word in a text file. At the end of the text file the tree contains all of the words used in the file.

    Just do the same thing with your C++ program. The only difference is that you will start out with a tree of C++ keywords to determine iof the token read from the program is a symbol. Like == is not a symbol.

    There is an article in the HowTos forum about the State Design pattern that uses a parser as the example. There is code there also.

    I would start off by ignoring include files. Do that as a second step after you get the program itself parsed.

    Be careful not to bite off too much or you will be writing a full-blown C++ parser and you will be there for a very long time, and worse, it's already been done.

    Comment

    • rolan
      New Member
      • Jan 2008
      • 13

      #3
      Thanks for your guidance. Just one question, which may be laziness speaking, is there somewhere I could find a list of the commonly used keywords for c++? (or at least the standard keywords) Or will I just have to tailor the list of keywords to the program I try to parse?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Just about any C++ textbook will have this. Probably, you can get the same thing from Google.

        Comment

        Working...