title: Recursive descent parser C program
body: <p>I have the following grammar and need to write a recursive descent parser in C</p>
  
  <pre><code>E-&gt;E+E|E*E|(E)|i
  </code></pre>
  
  <p>I used left-factoring to get the following grammar</p>
  
  <pre><code>E-&gt;EX|Y
  X-&gt;+E|*E
  Y-&gt;(E)|i
  </code></pre>
  
  <p>Now, I eliminated left recursion to get the following grammar</p>
  
  <pre><code>E-&gt;YE`
  X-&gt;+E|*E
  Y-&gt;(E)|i
  E`-&gt;XE`|e 
  </code></pre>
  
  <p>e denotes epsilon</p>
  
  <p>Now I have written C program for this grammar but I get a segmenation fault</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  static char c[10];
  int j=0;
  int main()
  {
      printf("Enter a string\n");
      scanf("%s",c);
      E();
      if(c[j]=='$')
          printf("Valid string\n");
      else
          printf("Invalid string\n");
      return 0;
  }
  E()
  {
      Y();
      Eprime();
      return;
  }
  X()
  {
      if(c[j]=='+')
      {
          j++;
          E();
      }
      else if(c[j]=='*')
      {
          j++;
          E();
      }
      return;
  }
  Y()
  {
      if(c[j]=='(')
      {
          j++;
          E();
          if(c[j]==')')
              j++;
      }
      else if(c[j]=='i')
          j++;
      return;
  }
  
  Eprime()
  {
      X();
      Eprime();
      return;
  }
  </code></pre>