Nested compound statements.

Discussion in 'Python' started by Neil Cerutti, Feb 1, 2008.

  1. Neil Cerutti

    Neil Cerutti Guest

    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 <>
     
    Neil Cerutti, Feb 1, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,233
    mfmehdi
    Oct 20, 2006
  2. Replies:
    3
    Views:
    896
    Steve Holden
    Sep 16, 2003
  3. Fredrik Tolf

    Compound statements in expressions

    Fredrik Tolf, Sep 30, 2004, in forum: C Programming
    Replies:
    18
    Views:
    526
    Fredrik Tolf
    Sep 30, 2004
  4. Kurt Euler
    Replies:
    1
    Views:
    179
    Jason Creighton
    Sep 3, 2003
  5. Randy Kramer
    Replies:
    11
    Views:
    387
    Randy Kramer
    Oct 19, 2007
Loading...

Share This Page