What's wrong with that comment?

L

Ludwig Miniatur

Hi, I ran into a funny thing when I played with the python parser, to
build a python call graph for learning...

When I have a (special) python program, that runs without any error it
gives an error, when I try to parse it parser.suite. The error is,
when my program has a comment on the last line after an indented
block. Note that there no newline behind "# comment". With a newline
everything is fine.

For example:
#!/usr/bin/env python

from parser import suite, ast2list
fh = file(__file__)
s = fh.read()
fh.close()
ast = suite(s)

while False:
print "hello world"
# comment

Looks like a little bug in parser; but what I don't understand is that
I thought parser was build with the current syntax of python.

So, why can python run the script (an it can if you comment out the
line "ast = suite(s)") but parser can't?

Looking forward for answers...

Ludwig
 
J

Joe P. Cool

Ludwig said:
For example:
#!/usr/bin/env python

from parser import suite, ast2list
fh = file(__file__)
s = fh.read()
fh.close()
ast = suite(s)

while False:
print "hello world"
# comment

Looks like a little bug in parser; but what I don't understand is that
I thought parser was build with the current syntax of python.

I didn't read the grammar but I assume that Python grammar requires a
comment to have the form #.* said:
So, why can python run the script (an it can if you comment out the
line "ast = suite(s)") but parser can't?

The interpreter probably appends a newline after the input stream as a
friendly service :)
 
G

Gabriel Genellina

En Tue, 20 May 2008 16:22:10 -0300, Joe P. Cool
I didn't read the grammar but I assume that Python grammar requires a


The interpreter probably appends a newline after the input stream as a
friendly service :)

Something like that. The last line of source *must* end in a newline (be
it a comment or not); this is a known limitation. See py_compile.py for an
example.
 
L

Ludwig Miniatur

En Tue, 20 May 2008 16:22:10 -0300, Joe P. Cool
<[email protected]> escribió:






Something like that. The last line of source *must* end in a newline (be
it a comment or not); this is a known limitation. See py_compile.py for an
example.

Uoh, you never stop learning...
Thanks for the explanation. I thought something like that, but it made
me some headaches.

Lutz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top