title: Lex program to recognise valid arithmetic expression and also to recognise valid identifies and operators
body: <p>the below program checks the arithmatic expression like a+b a-b it gives the output valid or invalid;</p>
  
  <pre><code>%{
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  int c,d,bo=0,bc=0;
  %}
  operand [a-zA-Z0-9]+
  operator [+\-\/*]
  %%
  //the operand is one count higher than the operator if that fails then its is invalid eg a+b operand is two and operator is 1;
  
  {operator} {d++;printf("%s is an operator \n",yytext);} 
  
  {operand} {c=d+1;printf("%s is an operand \n",yytext);}
  
  "(" {if(bc&lt;=bo)bo++;}
  
  ")" {bc++;}
  
  \n {if(bo==bc&amp;&amp;c&gt;d){printf("valid exp");}else {printf("invalid exp");};exit(0);}
  %%
  void main(){
  yylex();
  }
  </code></pre>
  
  <p>the problem that im am facing is when i check a++b it says valid but when i try a+b- and other values like )a+b(, (a+b(,+a-b++ it gives me right output .only for a++b and a--b or a+-b it fails.
  Im kinda stuck.</p>
  
  <p>this is the if condition for \n,I put it on \n bcz when I press enter it  gives me the output and exits. </p>
  
  <pre><code>if(bo==bc &amp;&amp; c&gt;d)                   //c&gt;d means if operand is greater than operator
      {  printf("valid exp");  }
        else {  
               printf("invalid exp"); } 
  </code></pre>