C++ Programming Blog

 
 
 
#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;
            else if (isalpha(curstr[j]))
                k=1;
            else if (isdigit(curstr[j]))
                k=2;
            else if(curstr[j]==\'_\')
                k=3;
            else if(curstr[j]==\'.\')
                k=4;
            else
            {
                printf(\"Error In Input...\");
                getch();
                exit;
            }

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

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

int IsOperator(char ch)
{
    if (ch==\'+\')
        return 1;
    else if (ch==\'-\')
        return 2;
    else if (ch==\'*\')
        return 3;
    else if (ch==\'/\')
        return 4;
    else if (ch==\'=\')
        return 5;
    else
        return 0;
}

 
 
Didn't find what you were looking for? Find more on Program of KSCANNER