Thread: Program Runs off Screen / Lexical Analyzer

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    58

    Program Runs off Screen / Lexical Analyzer

    Can anyone tell me why my program: once it is executed, runs down the screen and it doesnt let me scroll up!!! no idea why its doing this. very frustrating. i tried changing with my properties on my console window...but nothing. maybe it is the code.

    here is my code: it is 2 files

    cLex.c
    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    
    void keyw(char *p);
    
    int i=0,id=0,kw=0,num=0,op=0;
    
    char keys[32][10]={"auto","break","case","char","const","continue","default",
    "do","double","else","enum","extern","float","for","goto",
    "if","int","long","register","return","short","signed",
    "sizeof","static","struct","switch","typedef","union",
    "unsigned","void","volatile","while"};
    
    main()
        {
        	char ch,str[25],seps[15]=" \t\n,;(){}[]#\"<>",oper[]="!%^&*-+=~|.<>/?";
        	int j;
        	char fname[50];
        	FILE *f1;
        	//clrscr();
        printf("enter file path (drive:\\fold\\filename)\n");
        scanf("%s",fname);
        f1 = fopen(fname,"r");
        //f1 = fopen("Input","r");
        	if(f1==NULL)
    
    
            	{
            	 printf("file not found");
            	 exit(0);
            	}
            	while((ch=fgetc(f1))!=EOF)
    
    
                	{
                for(j=0;j<=14;j++)
    
    
                    {
                    if(ch==oper[j])
    
    
                        {
                        printf("%c is an operator\n",ch);
                        op++;
                        str[i]='\0';
                        keyw(str);
                    }
    
                }
    
                for(j=0;j<=14;j++)
    
    
                    {
                    if(i==-1)
                    break;
                    if(ch==seps[j])
    
    
                        {
                        if(ch=='#')
    
    
                            {
                            while(ch!='>')
    
    
                                {
                                printf("%c",ch);
                                ch=fgetc(f1);
                            }
    
                            printf("%c is a header file\n",ch);
                            i=-1;
                            break;
                        }
    
                        if(ch=='"')
    
    
                            {
                            do
    
    
                                {
                                ch=fgetc(f1);
                                printf("%c",ch);
                            }while(ch!='"');
    
                            printf("\b is an argument\n");
                            i=-1;
                            break;
                        }
    
                        str[i]='\0';
                        keyw(str);
                    }
    
                }
    
                if(i!=-1)
    
    
                    {
                    str[i]=ch;
                    i++;
                }
    
                else
                i=0;
                	}
                printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers: %d\n",kw,id,op,num);
                //getch();
            }
    
            void keyw(char *p)
    
    
                {
                int k,flag=0;
                for(k=0;k<=31;k++)
    
    
                    {
                    if(strcmp(keys[k],p)==0)
    
    
                        {
                        printf("%s is a keyword\n",p);
                        kw++;
                        flag=1;
                        break;
                    }
    
                }
    
                if(flag==0)
    
    
                    {
                    if(isdigit(p[0]))
    
    
                        {
                        printf("%s is a number\n",p);
                        num++;
                    }
    
                    else
    
    
                        {
                        //if(p[0]!=13&&p[0]!=10)
                        if(p[0]!='\0')
    
    
                            {
                            printf("%s is an identifier\n",p);
                            id++;
                        }
    
                    }
    
                }
    
                i=-1;
            }
    Last edited by pantherman34; 05-06-2010 at 11:25 AM.

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    UPDATE: i need help! i need to be able to recognize comments that begin with /* and read the whole comment until it ends with */ which spreads multiple lines of code. It needs to be recognized as a comment, and reported as a comment, not just skipped in this analyzer! how can i do this?!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Set a flag when you're in a comment read, and read until you get a matching */.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If I had a program that ran off the screen, I would attach some kind of leash to it, linked to a gearing mechanism and generator, and use it to produce power.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    thanks for the info. also - how would i implement that if the program finds a preprocessor directive, (ie. #define) it disregards all the code on that line after the #define, but includes it with it.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    Quote Originally Posted by brewbuck View Post
    If I had a program that ran off the screen, I would attach some kind of leash to it, linked to a gearing mechanism and generator, and use it to produce power.
    rofl

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why is this so confusing for you? Do something like:
    Code:
    while( fgets( buf, BUFSIZ, inputfile ) != NULL )
    {
        switch( flags )
        {
            case SOMEFLAG:
                ...look at the string to see if it's what I need...
                ...do something with it if it is...
                ...handle errors if it isn't...
            break;
    
            case ANOTHERFLAG:
                ...more of the same...
        }
    }
    You check for a #define the same way you check for anything else.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2010, 01:35 PM
  2. Lexical Analyzer
    By ammad in forum C++ Programming
    Replies: 8
    Last Post: 11-18-2009, 06:59 PM
  3. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  4. Making Program Launch Full Screen
    By mrtechguy in forum C++ Programming
    Replies: 19
    Last Post: 03-15-2006, 02:06 AM