Lex program to recognize a valid arithmetic expression.

//Lex program to recognize a valid arithmetic expression.

%{

#include<stdio.h>

int p=0,q=0,r=0,s=0,ob=0,cb=0;
int flagp=0,flagq=0,flagr=0,flags=0;
%}

%%
[a-zA-z]+ printf("\n %s is an identifier\n",yytext);
[+] {p++;flagp=1;}
[-] {q++;flagq=1;}
[*] {r++;flagr=1;}
[/] {s++;flags=1;}
[(] ob++;
[)] cb++;
%%

void main()
{
printf("Enter expression:");
yylex();
if(ob==cb)
printf("\nvalid expression\n");
else
printf("invalid expression\n");
printf("Addition=%d\tSubtract=%d\nMultiply=%d\tDivide=%d\n",p,q,r,s);
printf("\n Operators used:\n");
if(flagp==1)
printf("+\t");
if (flagq==1)
printf("-\t");
if(flagr==1)
printf("*\t");
if(flags==1)
printf("/\n");
}

//output of this code


No comments:

Post a Comment

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...