0% found this document useful (0 votes)
102 views1 page

AIM: Write A Lex Program To Validate Arithmetic Expressions and Display A Separate List of The Identifiers and Operators. Program

This document describes a Lex program to validate arithmetic expressions and output separate lists of identifiers and operators. The program uses Lex to tokenize an input expression, storing operands in one array and operators in another. It then checks that parentheses are balanced and the number of operands and operators are equal. If valid, it prints the operands and operators lists, otherwise it prints an invalid expression message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views1 page

AIM: Write A Lex Program To Validate Arithmetic Expressions and Display A Separate List of The Identifiers and Operators. Program

This document describes a Lex program to validate arithmetic expressions and output separate lists of identifiers and operators. The program uses Lex to tokenize an input expression, storing operands in one array and operators in another. It then checks that parentheses are balanced and the number of operands and operators are equal. If valid, it prints the operands and operators lists, otherwise it prints an invalid expression message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

2CEIT701: COMPILER DESIGN PRACTICAL-4

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

You might also like