[Python-Dev] Python syntax checker ?

M.-A. Lemburg mal@lemburg.com
Thu, 21 Sep 2000 12:42:46 +0200

Guido van Rossum wrote:
> > 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 is similar to my initial idea: syntax checking should continue
(or possibly restart) at the next found "block" after an error.

E.g. in Thomas' case:

if 1:


    <tons more code using 4-space indent>

the checker should continue at forever() possibly by restarting
checking at that line.

> 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.

Looks like this is highly non-trivial job...

Marc-Andre Lemburg
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/