Thread: sample?

  1. #1


    how does a compiler scan a program and find/recognise keywords and functions? does anyone have any sample code please? thanks

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    a compiler reads in the input a line at a time, and breaks the input into tokens depending upon whitespace and parsing rules. for instance, ()+- will break into '(', ')', '+', '-' while int x; will break into 'int', 'x', ';'. in the case of c (and probably others), keywords need whitespace about them (or valid tokens) ie intlol will break into 'intlol'. each token is tied to a specific type, such as ADD for '+', and INT for 'int', and the unknown tokens are treated as identifiers. as for recognizing functions, a c function could be defined as a return type keyword followed by an identifier followed by an argument list. the compiler has to take all of the rules into context. while i don't have a nice and simple sample program ready at hand, you might be able to use these:

    .sect signature

  3. #3

    help plz

    i have to make a symbol table containing c keywords. I need to then scan thru a program and list all keywords, identifiers etc within the program....does anyone have any idea as to what kinda info i need to look for and also if u have any examples that would be gr8 thank u

  4. #4
    Registered User
    Join Date
    Nov 2002
    Ah, you do not need anything as complex as a a C compiler. Just read in a line at a time from the file. Then use something like strchr to find all of the words in the line you read in. Then compare each word with your array of keywords and if it matches, do whatever you need to do. Your program does not need to understand C at all, just needs to do some simple string comparison.

    If you do not know how to use strchr or strcmp, go read the man pages. They can be found on the internet if you do not have them installed. has them under documentation section.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Additionally, assuming that you're trying to validate a C program or something similar, you'll want to make your program ignore keywords and symbols which are enclosed in a string.

    int x;
    char s[] = "int x;" /* this 'int x;' is not counted */

    Also, you'll likely want to ignore stuff in comments. For extra credit you could also omit things like:
    #if 0 /*this line would count */
        /* everything here is ignored */
    #endif /* this line would count */
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sample Software Development Proposal ?
    By khpuce in forum A Brief History of
    Replies: 4
    Last Post: 03-24-2008, 09:38 AM
  2. Bjarne's member function binding sample is wrong?
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 03-11-2008, 04:05 AM
  3. MSDN volatile sample
    By George2 in forum C++ Programming
    Replies: 38
    Last Post: 01-05-2008, 06:59 AM
  4. MSDN OLE DB Sample Provider entry point
    By George2 in forum C++ Programming
    Replies: 0
    Last Post: 07-21-2007, 07:30 AM
  5. looking for non-MFC based socket programming sample
    By George2 in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-19-2006, 05:57 AM