896+ Capstone Project is Available: Whatsapp: +91-7011258995, Email: sharecodepoint@gmail.com

Program to recognize a valid arithmetic expression and identify the identifiers and operators present. Print them separately. | Compiler Design Lab Programs | Lex Programs


Compiler Design Program :

%{
 #include<stdio.h>
 #include<string.h>
 int noprt=0, nopnd=0, valid=1, top=-1, m, l=0, j=0;
 char opnd[10][10], oprt[10][10], a[100];
%}
%%
“(“ { top++; a[top]=’(‘ ; }
“{“ { top++; a[top]=’{‘ ; }
“[“ { top++; a[top]=’[‘ ; }
“)” { if(a[top]!=’(‘)
 {
 valid=0; return;
}
else
 top--;
 }
“}” { if(a[top]!=’{‘)
 {
 valid=0; return;
}
else
 top--;
 }
“]” { if(a[top]!=’[‘)
 {
 valid=0; return;
}
else
 top--;
 }
“+”|”-“|”*”|”/” { noprt++;
 strcpy(oprt[l], yytext);
l++;
 }
[0-9]+|[a-zA-Z][a-zA-Z0-9_]* {nopnd++; 
strcpy(opnd[j],yytext);
 j++;
 }
%%
int yywrap()
{
return 1;
}
main()
{
 int k;
 printf(“Enter the expression.. at end press ^d\n”);
 yylex();
 if(valid==1 && i==-1 && (nopnd-noprt)==1)
 {
 printf(“The expression is valid\n”);
 printf(“The operators are\n”);
 for(k=0;k<l;k++)
 Printf(“%s\n”,oprt[k]);
 for(k=0;k<l;k++)
 Printf(“%s\n”,opnd[k]);
 }
 else
 Printf(“The expression is invalid”);
}

Sharecodepoint

Sharecodepoint is the junction where every essential thing is shared for college students in the well-defined packets of codes. We are focused on providing you the best material package like Question papers, MCQ'S, and one NIGHT STUDY MATERIAL. facebook twitter youtube instagram

Post a Comment

Previous Post Next Post

Contact Form