How to do lexical analysis for c/c++ source file?

0

i have c++ code for Lexical analysis to get result of Expression/String as input and produces output as list of tokens that are: Total Identifier, Total Constants, Total Literals, Total Keywords, Total Operators, Total Delimiters, Invalid Tokens & Used Tokens.
So, i want to know same for a C/C++ file, then how to do?

How am passing the expression for analysis as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
void main()
{
    clrscr();
    char mstr[50];
    printf("\n\n\tEnter String: ");// Here i want file as input
    gets(mstr);
    Lex_Ana oblex(mstr);//Here how to pass all lines to analysis
    oblex.Find_Lex();
    oblex.Rem_Dup();
    oblex.Sep_Val();
    oblex.Display();
    getch();
}

i tried like below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void main()
{
    clrscr();
    ifstream indata;
    char file_name[40], *mainStr,*ch;
    printf("\n\n\tEnter File name: ");
    gets(file_name);
    indata.open(file_name);
    if(indata==NULL)
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }
    else
    {
        while(!indata.eof())
        {
            getline(indata,ch);
            strcat(mainStr,ch);
        }
        Lex_Ana oblex(mainStr);
        oblex.Find_Lex();
        oblex.Rem_Dup();
        oblex.Sep_Val();
        oblex.Display();
        indata.close();
    }
    getch();
}

this is not working..

Compiler: Turbo c++
Any help would be appreciated..

Tags: asked June 7, 2012

Your Answer

Please login to post questions.

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: