Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » ScannerRSS Feeds

Program of KSCANNER

Posted By: Jay Brown     Category: C++ Programming     Views: 2401

Write a Program of KSCANNER.

Code for Program of KSCANNER in C++ Programming

#include<stdio.h>
#include<process.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>

int IsOperator(char);

void main()
{
    clrscr();
    char curstr[50],instr[50];
    int count=0,counter=0,i=0,j,k,curstate;
    char scantab[6][5]={ 1, 2, 3,-1,-1,
                -1,-1,-1,-1,-1,
                -1, 2, 2, 2,-1,
                -1,-1, 3,-1, 4,
                -1,-1, 5,-1,-1,
                -1,-1, 5,-1,-1};

    printf("Enter The String : ");
    scanf("%[^\n]",instr);

//    printf("%d",strlen(instr));while(instr[count]!='\0')
    {
        counter=0;
        while(instr[count++]==' ')
        {
        }
        count--;

        while(instr[count]!=' ' && instr[count]!='\0')
        {
            curstr[counter++]=instr[count++];
//            printf("%c",curstr[counter-1]);

        }
        curstr[counter]='\0';
//        printf("%s",curstr);

        j=0;
        curstate=0;
//        printf("%d",strlen(curstr));\        while(j<strlen(curstr))
        {
            if (IsOperator(curstr[j])>0)
                k=0;
            elseif (isalpha(curstr[j]))
                k=1;
            elseif (isdigit(curstr[j]))
                k=2;
            elseif(curstr[j]=='_')
                k=3;
            elseif(curstr[j]=='.')
                k=4;
            else
            {
                printf("Error In Input...");
                getch();
                exit;
            }

            curstate=scantab[curstate][k];
            //printf("%d",curstate);
            j++;
        }

        if (curstate==1)
            printf(" op ");
        elseif (curstate==2)
            printf(" id ");
        elseif (curstate==3)
            printf(" int ");
        elseif (curstate==5)
            printf(" real ");
        elseif(curstate==-1)
            printf(" error ");
    }
    getch();
}

int IsOperator(char ch)
{
    if (ch=='+')
        return 1;
    elseif (ch=='-')
        return 2;
    elseif (ch=='*')
        return 3;
    elseif (ch=='/')
        return 4;
    elseif (ch=='=')
        return 5;
    elsereturn 0;
}
  
Share: 


Didn't find what you were looking for? Find more on Program of KSCANNER Or get search suggestion and latest updates.

Jay Brown
Jay Brown author of Program of KSCANNER is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!