Step1: Start
Step2: Read the arithmetic expression from keyboard
Step3: Check the condition for end of input. If it is not end of input, proced to Step4, else go to Step9.
Step4: Read the expression token by token
Step5: Initialization of operand, opnd=0
Step6: Check whether the input character is operator. If it is an operator proceed to Step7 else go to Step8
Step7: Increment corresponding operator count, a[i]=a[i]+1 and go back to Step3
Step8: Increment operand count, opnd=opnd+1 and go back to Step3
Step9: Check the condition whether the expression is valid. If it is valid proceed to step 10 else go to Step11
Step10: Print the message as valid expression and go to Step12
Step11: Print the message as invalid expression
Step12: Stop
Explanation:
In this program, we check whether given arithmetic expression is valid or not, and check the number of each arithmetic operators used (+,-,*,/).
%{
int a[]={0,0,0,0},i,valid=1,opnd=0;
%}
%x OPER
%%
- Declaration section. Here we define an array 'a' which has 4 size, each to store arithmetic operators count.
- i is the index variable for this array.
- valid is a variable which tells whether expression is valid or not. 1 indicates valid, 0 indicates invalid. initially let it be 1.
- opnd is variable to count total number of operands.
- We define a state OPER, using %x. we enter this state whenever we encounter a operand. You'll get to know about this later.
whenever we encounter a identifier, we enter OPER state and increase opnd by 1.
<OPER>"+" { if(valid) { valid=0;i=0;} else ext();}
<OPER>"-" { if(valid) { valid=0;i=1;} else ext();}
<OPER>"*" { if(valid) { valid=0;i=2;} else ext();}
<OPER>"/" { if(valid) { valid=0;i=3;} else ext();}
while in OPER state, if we encounter any of the four basic arithmetic operators, we check a condition.
if(valid==1) make valid as 0 and i= operator's respective index, to increment the count. valid=0 because, we just encountered a operator. an expression cant end with an operator. We will make valid 1 again when we next encounter an operand. we will see that later.
if(valid was not equal to 1), it means the previous character read was also an operator, again we encounter a operator, it means we encountered 2 variables back to back, which isn't valid. so we exit.
<OPER>[a-zA-Z0-9]+ { opnd++;
if(valid==0)
{
valid=1; a[i]++;
}
else
ext();
}
Now if we again encounter an operand, we increase the operand count by 1.
Now, if valid==0, this means previous character read was an operator so this allowed. make valid 1 again, and increment the count of the previous read operator, whose index was assigned in i when operator got encountered.
else, exit.
<OPER>"\n" { if(valid==0)
ext();
else
return 0;
}
.\n ext();
%%
while in operator state, if we encounter newline character, this is end of expression. if expression is not valid at this point, we exit displaying invalid message.
else we return successfully because valid=1.
If we encounter, newline at the beginning of input, we exit displaying "invalid" message.
ext()
{
printf(" Invalid Expression \n");
exit(0);
}
this is the function to display error message and exit, we were talking about in the above explanation.
main()
{
printf(" Type the arithmetic Expression \n");
yylex();
printf(" Valid Arithmetic Expression \n");
printf(" No. of Operands/Identifiers : %d \n ",opnd);
printf(" No. of Additions : %d \n No. of Subtractions : %d \n",a[0],a[1]);
printf(" No. of Multiplications : %d \n No. of Divisions : %d \n",a[2],a[3]);
}
Main function. Ask for expression. scan it. call the parser, if returned successfully, print all appropriate values.
Done!
Share your views about this article!