Friday, May 3, 2019

SS 1.A - LEX - RECOGNIZE VALID ARITHMETIC EXPRESSION

1 -A) Write a LEX program to recognize valid arithmetic expression. Identifiers in the expression could be only integers and operators could be + and * . Count the identifiers & operators present and print them separately.

lab1.l - PROGRAM

%{
#include<stdio.h>
int v=0,op=0,id=0,flag=0;
%}

%%
[a-zA-Z]+[0-9A-Za-z]* {id++;}
[0-9]+ {id++;}
[\+\-\*/\=] {op++;}
"(" {v++;}
")" {v--;}
";" {flag=1;}
.|\n {return 0;}
%%

int main()
{
        printf("Enter the expression:");
        yylex();
        if((op+1)==id && v==0 && flag==0)
        {
                printf("\n Expression is Valid\n");
                printf("No of identifier = %d \n No of Operators = %d \n",id,op);
        }
        else
                printf("\n Expression is Invalid\n");
return 0;

}

OUTPUT :( click on image to zoom )

No comments:

Post a Comment