title: Recursive descent parser implementation in python
body: <p>The objective of the python code(3.5) is to read standard C codes for the following rules:</p>
  
  <pre><code>Program  --&gt; main () { declarations   statement-list } 
  declarations--&gt; data-type identifier-list; declarations |epsilon  
  data-type --&gt; int|char identifier-list --&gt; id|id, identifier-list   
  statement_list --&gt; statement   statement_list| epsilon 
  statement --&gt;    looping-stat 
  looping-stat --&gt; while (expn) {assignment_stat} |
          for    (assignment_stat ; expn ; increment_stat )       { assignment_stat }    
  expn--&gt; factor eprime 
  eprime--&gt;relop factor|epsilon 
  assignment_stat    --&gt; id = factor 
  increment_stat --&gt; id  inc_dec 
  inc_dec --&gt; ++|-- 
  factor --&gt; id|num 
  relop --&gt; = =|!=|&lt;=|&gt;=|&gt;|&lt;
  </code></pre>
  
  <p>I understand that the method is to use consecutive procedure calls (For eg. for main() call declaration() and statements with it). Idea was to read lines from a text into a list and try to parse it. I am confused on  rules like declaration. For example </p>
  
  <blockquote>
    <p>int id;</p>
  </blockquote>
  
  <p>and </p>
  
  <blockquote>
    <p>while(a&lt;100)</p>
  </blockquote>
  
  <p>Any help will be appreciated.</p>
  
  <p>A trial code:</p>
  
  <pre><code>#for input
  def file_input():
      global buffer,lookahead
  
      buffer=list()
      file=open("parser.txt","r")
      lines=file.readlines()
      file.close()
      for line in lines:
          line=line.strip()
          #print(line)
          buffer.append(line)
  
      return(buffer)
  
  #for the while loop
  def while_loop():
      global lookahead,buffer
  
      if "while_loop" in buffer[lookahead]:
          print("Parsing",buffer[lookahead])
          match('while_loop')
          expression()
          assignment()
  
  #matching   
  def match(t):
      global lookahead,buffer
  
      print('matching', t)
      if buffer[lookahead] == "t":
          lookahead = lookahead + 1
      else:
          print('error')
  </code></pre>