C program to simulate lexical analyzer for validating operators.

Aim:- Write a C program to simulate lexical analyzer for validating operators.

C Program Code:

             
#include<stdio.h>
#include<conio.h>
void main()
{
    char a[80];
    int i, flag = 1;
    clrscr();
    printf("\nEnter String: ");
    gets(a);
    for(i = 0; i < strlen(a); i++)
    {
        if(strcmp(a) == 0 || strlen(a)  > 255)
        {
            flag = 1;
        }
        else if((a[0] >= 'a' && a[0] <= 'z') || (a[0] >= 'A' && a[0] <= 'Z'))
        {
            if(a[1] == '+' || a[1] == '-' || a[1] == '*' || a[1] == '/' || a[1] == '%')
            {
                if((a[2] >= 'a' && a[2] <= 'z')||(a[2] >= 'A' && a[2] <= 'Z') || a[2] == '_')
                {
                    flag = 0;
                }
            }
        } 
        else
        {
            flag = 1;
        }
    }
    if(flag == 0)
    {
        printf("\n%s is Valid Operator...", a);
    }
    else
    {
        printf("\n%s is Invalid Operator...", a);
    }
    getch();
}
                        
            
        

Output:

Post a Comment

0 Comments