0
#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 ..

3
  • 1
    The botched syntax highlighting suggests that this code should give a syntax error, not a warning (there's a single quote after c[0]= that should not be there). Commented Apr 18, 2010 at 16:50
  • What line is giving you the warning? Commented Apr 18, 2010 at 16:51
  • from line 10 to 33 and lines 60 and 94 Commented Apr 18, 2010 at 16:52

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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?

3 Comments

What on earth would make you think there's any chance whatsoever that he's using a textbook? ;-)
@Steve Jessop - Apparently, its been a while since you looked at textbooks (though, I have not seen one THAT bad (yet)).
@mekasperasky That wastes both your time and ours.
0

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]).

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.