469,356 Members | 2,681 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Nested compound statements.

The docs say:

A suite can be one or more semicolon-separated simple statements on
the same line as the header, following the header's colon, or it can
be one or more indented statements on subsequent lines. Only the
latter form of suite can contain nested compound statements; the
following is illegal, mostly because it wouldn't be clear to which if
clause a following else clause would belong:

if test1: if test2: print x

What's the rest of the reason? Is it an LL(1) parser limitation?

The error came to my attention through:

with nested(open(args[0], "rb"),
open(args[1], "rb")) as (banner, pfaids):
if outfile_path is None:
report(sys.stdout, reconcile(banner, pfaids))
else: with open(outfile_path, "w") as outfile:
report(outfile, reconcile(banner, pfaids))

Instead I must write:

with nested(open(args[0], "rb"),
open(args[1], "rb")) as (banner, pfaids):
if outfile_path is None:
report(sys.stdout, reconcile(banner, pfaids))
else:
with open(outfile_path, "w") as outfile:
report(outfile, reconcile(banner, pfaids))

There's nothing terribly wrong with it, I guess, but it does look
"hairier" when really it isn't.

Moreover, "invalid syntax" is a bit terse--but probably it's not worth
it to complicate the grammar just for a better error message.

Finally, any ideas for a prettier version of the above snippet?

--
Neil Cerutti <mr***************@gmail.com>
Feb 1 '08 #1
0 1067

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Nils Grimsmo | last post: by
11 posts views Thread by Khaled Afiouni | last post: by
18 posts views Thread by Rhino | last post: by
46 posts views Thread by Neptune | last post: by
10 posts views Thread by nimmi_srivastav | last post: by
3 posts views Thread by Jake Emerson | last post: by
14 posts views Thread by Jan Schmidt | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.