AIM: Write A Lex Program To Validate Arithmetic Expressions and Display A Separate List of The Identifiers and Operators. Program
AIM: Write A Lex Program To Validate Arithmetic Expressions and Display A Separate List of The Identifiers and Operators. Program
AIM: Write a Lex program to validate arithmetic expressions and display a separate list
of the identifiers and operators.
PROGRAM:
%{
#include<stdio.h>
#include<string.h>
int flag=0,i=0,j,k=0,l=0;
char operand[20][20],operator[20][20];
%}
%%
[(]?[)]? {l++;}
[a-zA-Z0-9]+ {flag++; strcpy(operand[i],yytext); i++;}
[-+*/=] {flag--; strcpy(operator[k],yytext); k++;}
%%
int main(int argc, char* argv[])
{
printf("enter an arithmetic expression\n");
yylex();
if((l == 2|| l==0)){
if(flag!=1)
printf("Invalidexpression\n");
else {
printf("Valid expression\n");
printf("The operands are\t");
for(j=0;j<i;j++){
printf("%s\t",operand[j]);}
printf("\nTheoperators are\t");
for(j=0;j<k;j++){
printf("%s\t",operator[j]);}
printf("\n");
}}
else { printf("Invalid expression\n");
}}
int yywrap( ){
return 1;
}
OUTPUT:
8
20012021063 HARIKRUSHN TALAVIYA 7D-2