title: Construction of Lexical Analyzer, String Index Out Of Range in While Loop,
body: <p><strong>The Intro:</strong>
  I'm Learning how to make my own language and steps the necessary to achieve that. I was trying to implement Lexical Analyzer but I'm getting an error even though my logic is right. I want the Program to not read the comment.</p>
  
  <p><strong>The Problem:</strong>
  I'm getting the Error "String Index out of range', when I try to Iterate the words and look for '\n' in the line of the comment.</p>
  
  <p><strong>Python Code:</strong></p>
  
  <pre><code>comment = ['//', '/*', '*/']
  keyw = ["main", "void"]
  br = ['(', ')', '{', '}']
  lineCount = 1
  temp = ''
  flag = False
  f = open('Program.C', 'r')
  Program = f.read()
  #print(Program)
  
  for c in range(len(Program)):
      if Program[c] == ' ':
          continue
      if Program[c] == '\n':
          lineCount = lineCount + 1
          continue
      if Program[c] == '/':
          c = c + 1
          if Program[c] == '/':
              c = c + 1
              while Program[c] != '\n':
                  c = c +1
      if Program[c] in br:
          print(lineCount, "Brackets", Program[c])
      else:
          temp = temp + Program[c]
          print(temp)
          if temp in keyw:
              print(lineCount, "Keyword", temp)
              temp = ''
      print(Program[c])
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>while Program[c] != '\n':
  IndexError: string index out of range
   S
  S
   Sa
  a
   Saa
  a
   Saad
  d
  Process finished with exit code 1
  </code></pre>
  
  <p><strong>Sample Input File:</strong></p>
  
  <pre><code>// Saad
  // Bhai
  </code></pre>