Nested compound statements.

N

Neil Cerutti

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?
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top