Thread: How to identify a reserved word in a text file lexical analyzer

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    34

    How to identify a reserved word in a text file lexical analyzer

    For identifying a digit isdigit()
    For identifying a letter isalpha()

    Now the problem is how do I identify if the input is a reserved word? It just read the word as a letter coz of the isalpha() function. Any idea? Thanks. I'll post some codes if needed.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Read the word then compare that word with your list of reserved words?


    Jim

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    34
    Quote Originally Posted by jimblumberg View Post
    Read the word then compare that word with your list of reserved words?


    Jim
    Where do I list the reserved words? Would an array will do? Coz when comparing it in a if statement of course it will not be accepted because it is a reserved word.

    for an instance in a for loop:
    Code:
    if (lexeme[i] == "for")
    {
         statements....
    }
    in error will occur...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Joemar P. Gava
    Where do I list the reserved words? Would an array will do?
    Yes, an array might work.

    Quote Originally Posted by Joemar P. Gava
    Coz when comparing it in a if statement of course it will not be accepted because it is a reserved word.
    That does not make sense. When you ask "how to identify a reserved word in a text file lexical analyzer", the term "reserved word" has to do which whatever the lexical analyser will be analysing. This has nothing to do with the reserved words of C, unless you happen to be writing a lexical analyser for C.

    Quote Originally Posted by Joemar P. Gava
    for an instance in a for loop:
    Code:
    if (lexeme[i] == "for")
    {
         statements....
    }
    in error will occur...
    Rubbish. Have you actually tried to compile your code? There is a mistake, but it is not an error that will prevent you from compiling the code. The mistake is that you used operator == to compare two strings, but this will result in pointer comparison, whereas you should be using strcmp or something similiar to compare the strings.

    Perhaps you are thinking that because for is a reserved identifier in C, there will be a problem. Nope, there is no problem because "for" is a string literal, not an identifier, so the fact that for is a reserved identifier is not relevant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Identify and count word sequences within a text file
    By Freem in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2012, 12:37 PM
  2. Lexical Analyzer
    By ammad in forum C++ Programming
    Replies: 8
    Last Post: 11-18-2009, 06:59 PM
  3. simple lexical analyzer
    By ^Son_Gokou08 in forum C Programming
    Replies: 6
    Last Post: 08-25-2009, 07:55 AM
  4. Scanner? Lexical analyzer? Tokenizer?
    By audinue in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-23-2008, 11:32 PM
  5. Lexical analyzer for C
    By nishkarsh in forum C Programming
    Replies: 4
    Last Post: 08-26-2008, 08:05 AM