tags:

views:

254

answers:

4
#include<stdio.h>

/* this is a lexer which recognizes constants , variables ,symbols, identifiers , functions , comments and also header files . It stores the lexemes in 3 different files . One file contains all the headers and the comments . Another file will contain all the variables , another will contain all the symbols. */

int main()
{
    int i=0,j;
    char a,b[20],c[30];
    FILE *fp1,*fp2;
    c[0]='"if";
    c[1]="then";
    c[2]="else";
    c[3]="switch";
    c[4]="printf";
    c[5]="scanf";
    c[6]="NULL";
    c[7]="int";
    c[8]="char";
    c[9]="float";
    c[10]="long";
    c[11]="double";
    c[12]="char";
    c[13]="const";
    c[14]="continue";
    c[15]="break";
    c[16]="for";
    c[17]="size of";
    c[18]="register";
    c[19]="short";
    c[20]="auto";
    c[21]="while";
    c[22]="do";
    c[23]="case";
    fp1=fopen("source.txt","r"); //the source file is opened in read only mode which will passed through the lexer
    fp2=fopen("lext.txt","w");  
    //now lets remove all the white spaces and store the rest of the words in a file 


    if(fp1==NULL)
    {
        perror("failed to open source.txt");
        //return EXIT_FAILURE;
    }
    i=0;
    while(!feof(fp1))
    {


        a=fgetc(fp1);

        if(a!=' ')
        {
            b[i]=a;

        }
        else
        {
            for (j=0;j<23;j++)
        {
            if(c[j]==b)
            {
                fprintf(fp2, "%.20s\n", c[j]);
                continue ;
                        }
            b[i]='\0';
            fprintf(fp2, "%.20s\n", b);
            i=0;
            continue;
        }
    //else if 
    //{

        i=i+1;                  

        /*Switch(a)
        {
            case EOF :return eof;
            case '+':sym=sym+1;

            case '-':sym=sym+1;

            case '*':sym=sym+1;

            case '/':sym=sym+1;

            case '%':sym=sym+1;

            case '
        */
    }
fclose(fp1);
fclose(fp2);
return 0;
}

This is my c code for lexical analysis .. its giving warnings and also not writing anything into the lext file ..

+7  A: 

char c[30]; declares an array of 30 char, i.e. 30 byte long chunk of memory. So an assignment like the c[0] = "if"; tries putting a pointer into a char-sized integer.

What you probably want there is char* c[30]; - an array of 30 pointers.

Nikolai N Fetissov
if i make it to pointers it gives me segmentation fault..
mekasperasky
When you use pointers, you allocated memory ?
Andrei Ciobanu
A: 

Also you are comparing strings as:

if(c[j]==b)

you should be using strcmp for this as:

if(! strcmp(c[j],b))

Its sad that you've not followed any of the suggestions on your previous question.

codaddict
+2  A: 

C does not support assignment of arrays - you cannot say things like:

c[0]='"if";

in C. And there seems to be an extraneous quote in your code.

All your posts here this afternoon have been on really basic stuff. Which C textbook are you using where this kind of thing is not covered?

anon
What on earth would make you think there's any chance whatsoever that he's using a textbook? ;-)
Steve Jessop
i am not using any book .. just learning from my mistakes ..
mekasperasky
@Steve Jessop - Apparently, its been a while since you looked at textbooks (though, I have not seen one THAT bad (yet)).
Tim Post
@mekasperasky That wastes both your time and ours.
anon
A: 

As I've said here (another question of yours),

c is a char*, while c[0], c[1], c[2], ... are char. What you are trying to do, is to assign a char* (eg. "if") to a char (eg. c[0]).

Andrei Ciobanu