Wednesday, 17 September 2014

LEX PROGRAM TO FIND VALID ARITHMETIC EXPRESSION

%{
#include<stdio.h>
int op=0,s=0,b=0,f=0,l=0,m=0,k=0,pf=0,mf=0,sf=0,df=0,flag=0,bf=0;
%}
%%
([0-9]+)([a-zA-Z]+) {l=l+2; printf("* \n"); op++;}
([a-zA-Z]+)([0-9]+) {l=l+2; printf("* \n"); op++;}
[0-9a-zA-Z]+ {m=m+1;}
[+] {printf("+ \n");if(pf==0){op++;pf=1;}}
[-] {printf("- \n");if(mf==0){op++;mf=1;}}
[*] {printf("* \n");if(sf==0){op++;sf=1;}}
[/] {printf("/ \n");if(pf==0){op++;df=1;}}
[=] {flag=1; printf("= found");}
[(] {b++;bf=1;}
[)] {b--;if(bf!=1)b=555;}
[[] {s++;bf=1;}
[]] {s--;if(bf!=1)b=555;}
[{] {f++;bf=1;}
[}] {f--;if(bf!=1)b=555;}
%%
main()
{
printf("enter a expression \n ");
yylex();
if((b==0)&&(s==0)&&(f==0)&&(op>=1))
{
  if(flag==1)
         {
           printf("it is an equiation \n");
         }
   else
         { printf("valid exp \n");
           printf("number of operators=%d and no of identifiers=%d \n",op,m+l);
         }
 }
else
{
printf("invalid exp");
}
}


__________________________________________________

Write your program and check with following expressions 
(a+b+c)   identifiers=3 operators=1
)a+b(      invalid
25q+p    identifiers=3 operators=2
q25+p     identifiers=3 operators=2

if it works then your program is almost efficient in giving output.

STEP1: --> ([0-9]+)([a-zA-Z]+)   matches expressions like 35a
--> ([a-zA-Z]+)([0-9]+)  matches expressions like a32
--> [0-9a-zA-Z]+ matches 1 or more characters present square bracket.
--> [+] matches + operator
--> [-] matches - operator
--> [*] matches * operator
--> [/] matches / operator

--> 
 [(] {b++;bf=1;}    and
[)] {b--;if(bf!=1)b=555;}   If ')' is encountered before '(' then b will be set to higher value and if condition in the main() function fails hence the given expression is in-valid.ex: )a+b(



STEP2: -->  
[ [ ] {s++;bf=1;}
[ ] ] {s--;if(bf!=1)b=555;}     If ']' is encountered before '[' then b will be set  to higher value and if condition in main() function fails hence the given expression becomes in-valid.ex: ]a*b[



([0-9]+)([a-zA-Z]+) {l=l+2; printf("* \n"); op++;}
([a-zA-Z]+)([0-9]+) {l=l+2; printf("* \n"); op++;}

these two instructions are written before the instruction 
[0-9a-zA-Z]+ {m=m+1;} because
the y.tab.h created during the compilation will assign the tokens in the order you have given the rules in rule section and patterns also matched in the precedence of how they are tabulated in y.tab.h. for more information....
 




http://www.csee.umbc.edu/courses/331/papers/compactGuideLexYacc.pdf
 


No comments:

Post a Comment

Total Pageviews

108281