This post was published long ago, when I was a student and an amateur blogger. The links might be outdated and content may not be useful anymore. Please read this content keeping its age in mind.
Subject : System Software Laboratory
Branch : Information Science & Engineering
Semester : 6
University : VTU
………………………………………………………………………………………………………………………….
PART – A
………………………………………………………………………………………………………………………….
2) a. Program to recognize a valid arithmetic expression and to recognize
the identifiers and operators present. Print them separately.
%{
#include<stdio.h>
#include<string.h>
#define max 20
int flag,i,j,k,top,b;
char stack[max],ident[max],oper[max],brac[max];
%}
%%
[a-zA-Z0-9] {j++;strcat(ident,yytext);}
[a-zA-Z0-9]+ {flag=1;}
“+” {oper[k++]=’+’;}
“-” {oper[k++]=’-‘;}
“” {oper[k++]=’‘;}
“/” {oper[k++]=’*’;}
“$” {oper[k++]=’$’;}
“^” {oper[k++]=’^’;}
“%” {oper[k++]=’%’;}
“(” { stack[++top]='(‘;brac[b++]='(‘;}
“)” { if (stack[top]=='(‘ && top!=-1) top–;flag=0;brac[b++]=’)’;}
%%
int main()
{
int i=j=k=b=flag=0;
top=-1;
printf(“nEnter the Expression : “);
yylex();
printf(“nThe identifiers are : “);
for(i=0;i<j;i++)
printf(“t%c”,ident[i]);
printf(“nNo.of identifiers are : %d”,j);
printf(“nThe operators are :n”);
for(i=0;i<k;i++)
printf(“t%c”,oper[i]);
printf(“nNo. of operators are :%d”,k);
if(flag==0 && top==-1 && j==k+1)
printf(“nValid expression”);
else
printf(“nInvalid expression”);
return 0;
}
Praveesh says
Thanks a lot buddy…. 🙂
Tapashm42 says
need more program