Python Grammar (was Re: A TYPICAL NEWBIE MISTAKE?)
Dirck Blaskey
dirck.IsGettingTooMuchSpam at pacbell.net
Tue May 16 14:16:46 EDT 2000
Just van Rossum <just at letterror.com> wrote in message
news:l03102801b546da85b825@[193.78.237.167]...
> At 1:22 AM -0700 16-05-2000, Dirck Blaskey wrote:
> >> Theoretically, a colon is only necessary in things like
> >>
> >> if yes: print "yes"
> >
> >Oddly enough, the parser doesn't really need the colon here either.
> >It can manage to figure out where the if expression ends without it.
>
> Erm, how would you parse this?
>
> if yes ()
>
> That's either two expressions or just one...
Ok, let's Try It in the colopted Python and see how it would parse this:
Python 1.5.2 (#0, Mar 28 2000, 00:16:01) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def yes():
... print 'yes'
... return 1
...
>>> if yes(): print 'if 1'
...
yes
if 1
>>> if yes () print 'if 2'
...
yes
if 2
>>> if yes ()
...
File "<stdin>", line 2
^
SyntaxError: invalid syntax
>>>
>>> if yes: ()
...
()
It looks like the parser will keep eating as long as the code looks like an
expression,
which is a problem if the statement looks like part of the expression.
The parser will complain if it eats everything and there's no statement
left.
Andrew Dalke <dalke at acm.org> wrote in message
news:8fr5nt$le7$1 at nntp9.atl.mindspring.net...
> ...
> Oh, oh! I've got one!
>
> Is "if 1 - 1 - 2" the same as
> "if 1: -1 - 2" or
> "if 1-1: -2" or
> "if 1-1-2:"
>...
> Another is:
>
> if "spam" " eggs" " viking"
>
>...
Let's Try these as well:
>>> if 1 - 1 - 2
...
File "<stdin>", line 2
^
SyntaxError: invalid syntax
>>> if "spam" " eggs" " viking"
...
File "<stdin>", line 2
^
SyntaxError: invalid syntax
>>>
In this case, the parser chokes, but on the other hand,
it's not really code, is it? Like, ah, what does it do? :-)
((Warning W8019 t.py 2: Code has no effect in function t))
A \n fixes the statements (but they still don't do much).
(Oh no! A club soda stain! Some red wine will take that right out. :-)
d
More information about the Python-list
mailing list