[Python-Dev] Python syntax checker ?

Guido van Rossum guido@beopen.com
Wed, 20 Sep 2000 21:17:20 -0500


> Would it be possible to write a Python syntax checker that doesn't
> stop processing at the first error it finds but instead tries
> to continue as far as possible (much like make -k) ?
> 
> If yes, could the existing Python parser/compiler be reused for
> such a tool ?
> 
> I was asked to write a tool which checks Python code and returns
> a list of found errors (syntax error and possibly even some
> lint warnings) instead of stopping at the first error it finds.

I had some ideas for this in the context of CP4E, and I even tried to
implement some, but didn['t get far enough to check it in anywhere.
Then I lost track of the code in the BeOpen move.  (It wasn't very
much.)

I used a completely different approach to parsing: look at the code
from the outside in, e.g. when you see

  def foo(a,b,c):
      print a
      for i in range(b):
          while x:
              print v
      else:
          bah()

you first notice that there's a line starting with a 'def' keyword
followed by some indented stuff; then you notice that the indented
stuff is a line starting with 'print', a line starting with 'for'
followed by more indented stuff, and a line starting with 'else' and
more indented stuff; etc.

This requires tokenization to succeed -- you need to know what are
continuation lines, and what are strings and comments, before you can
parse the rest; but I believe it can be made successful in the light
of quite severe problems.

(No time to elaborate. :-( )

--Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)