468,496 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

Creating a symbol table in c++

13
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.
Mar 21 '08 #1
3 12470
weaknessforcats
9,207 Expert Mod 8TB
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.
Mar 21 '08 #2
rolan
13
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?
Apr 1 '08 #3
weaknessforcats
9,207 Expert Mod 8TB
Just about any C++ textbook will have this. Probably, you can get the same thing from Google.
Apr 2 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Thomas Korimort | last post: by
1 post views Thread by linq936 | last post: by
1 post views Thread by s88 | last post: by
4 posts views Thread by curious | last post: by
2 posts views Thread by chandanlinster | last post: by
3 posts views Thread by cgable2003 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.