Thread: problem with creating lexical analysis

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    2

    problem with creating lexical analysis

    hello everyone i could use some help right now i'm desperate and now where to go
    i have been asked to create a lexical analyzer with c++ as a project and i have no idea how to do it and i was told to make the program read from txt file and categories every lexemes as identifiers or special sympol or comments etc this is an example they gave me and i'm supposed to continue it
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    void main ()
    {
    int counter = 0;
    string word;
    char letter;
    char  letters[]={'a' , 'b' , 'c' , 'd'  ,'e' , 'f' , 'g' , 'h' , 'i'   'j' ,  'k' , 'l' , 'm'  ,'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' ,  'v'   ,'w' , 'x' , 'y' , 'z' ,'A' , 'B' , 'C' , 'D'  ,'E' , 'F' , 'G' ,  'H' ,  'I' ,J' , 'K' , 'L' , 'M'  ,'N' , 'O' , 'P' , 'Q' , 'R' , 'S' ,  'T' ,  'U' , 'V'  ,'W' , 'X' , 'Y' ,Z }
    string RW []={"void" , "main", "iostream" , "string" }
    char digits[]= {'1' ,'2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ,}
    ifstream myfile;
    myfile.open("source.txt");


    please i really need help

  2. #2
    Guest
    Guest
    Quote Originally Posted by dexter00 View Post
    (...) and categories every lexemes as identifiers or special sympol or comments etc this is an example they gave me and i'm supposed to continue it
    Do you need to identify these by line, or individual character, or...? If you could give a more detailed description of your assignment, it would really help. Posting an example of what source.txt might contain would also be most helpful.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    The digits array seems to be missing zero at the end. And what you call lexeme is just a space delimited sequence of characters?

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    2
    really thx for replying
    about the source.txt is just the name of the file u read from
    and sorry about the 0 digit
    i need to get the output as this example
    Code:
    lexeme
    token type
    sum
    Identifier
    =
    Assignment operator
    i
    Identifier
    +
    Addition operator
    2
    Number
    ; void
    Statement delimiter reserved word
    like i write on txt file and the lexical defines what kind lexeme it is and give error massage if it's not defined by user

  5. #5
    Guest
    Guest
    I think I get what the desired output is (except for where void comes into the picture), but what exactly is inside the text file? Is it C++ code, one line, multiple lines? Clear instructions should be the starting point for any such effort. If you have a written assignment, then just paste that here.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There are several possible approaches to implementing this program, so you need to take a step back and come up with a plan (perhaps after re-reading your notes, books or what you can find online).

    For example, you might decide to implement a state machine of some sort: you start by reading a character, then based on the current state and value of that character, you decide if a complete lexeme has been formed and so this is the start of a new one, or if you should continue reading the next character to determine the current lexeme. Once it has been determined that a complete lexeme has been formed, you can then store it somewhere along with its category (e.g., in a std:air). If so, you need to decide on the states, e.g., currently reading an identifier, or a number, etc. For you to do this, you need to be clear to what are these categories.
    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. Beam or Frame Analysis for Structural Analysis
    By greenmania in forum C Programming
    Replies: 3
    Last Post: 05-05-2010, 05:40 PM
  2. Lexical Analyzer
    By ammad in forum C++ Programming
    Replies: 8
    Last Post: 11-18-2009, 06:59 PM
  3. Lexical analyzer for C
    By nishkarsh in forum C Programming
    Replies: 4
    Last Post: 08-26-2008, 08:05 AM
  4. Text analysis problem
    By haz115 in forum C Programming
    Replies: 3
    Last Post: 04-27-2002, 09:52 AM