pyparsing

Discussion in 'Python' started by =?iso-8859-2?q?Bo=B9tjan_Jerko?=, May 13, 2004.

  1. Hello !

    I am trying to understand pyparsing. Here is a little test program
    to check Optional subclass:

    from pyparsing import Word,nums,Literal,Optional

    lbrack=Literal("[").suppress()
    rbrack=Literal("]").suppress()
    ddot=Literal(":").suppress()
    start = Word(nums+".")
    step = Word(nums+".")
    end = Word(nums+".")

    sequence=lbrack+start+Optional(ddot+step)+ddot+end+rbrack

    tokens = sequence.parseString("[0:0.1:1]")
    print tokens

    tokens1 = sequence.parseString("[1:2]")
    print tokens1

    It works on tokens, but the error message is showed on
    the second string ("[1:2]"). I don't get it. I did use
    Optional for ddot and step so I guess they are optional.

    Any hints what I am doing wrong?

    The versions are pyparsing 1.1.2 and Python 2.3.3.

    Thanks,

    B.
     
    =?iso-8859-2?q?Bo=B9tjan_Jerko?=, May 13, 2004
    #1
    1. Advertising

  2. On Thu, 13 May 2004 08:05:32 +0200, -lj.si
    (Bo¹tjan Jerko) wrote:

    >Hello !
    >
    >I am trying to understand pyparsing. Here is a little test program
    >to check Optional subclass:
    >
    >from pyparsing import Word,nums,Literal,Optional
    >
    >lbrack=Literal("[").suppress()
    >rbrack=Literal("]").suppress()
    >ddot=Literal(":").suppress()
    >start = Word(nums+".")
    >step = Word(nums+".")
    >end = Word(nums+".")
    >
    >sequence=lbrack+start+Optional(ddot+step)+ddot+end+rbrack
    >
    >tokens = sequence.parseString("[0:0.1:1]")
    >print tokens
    >
    >tokens1 = sequence.parseString("[1:2]")
    >print tokens1
    >
    >It works on tokens, but the error message is showed on
    >the second string ("[1:2]"). I don't get it. I did use
    >Optional for ddot and step so I guess they are optional.
    >
    >Any hints what I am doing wrong?
    >
    >The versions are pyparsing 1.1.2 and Python 2.3.3.
    >
    >Thanks,
    >
    >B.

    I don't see anything "obviously" wrong to me, but changing it thusly
    seems to resolve the problem (I added a few intermediate rules to
    make it more obvious):

    pref = lbrack + start
    midf = ddot + step
    suff = ddot + end + rbrack
    sequence = pref + midf + suff | pref + suff

    I've run into "this kind of thing" now and again, and have always
    been able to resolve it by reorganizing my rules.

    --dang
     
    Daniel 'Dang' Griffith, May 13, 2004
    #2
    1. Advertising

  3. =?iso-8859-2?q?Bo=B9tjan_Jerko?=

    Paul McGuire Guest

    "Bo¹tjan Jerko" <-lj.si> wrote in message
    news:-lj.si...
    > Hello !
    >
    > I am trying to understand pyparsing. Here is a little test program
    > to check Optional subclass:
    >
    > from pyparsing import Word,nums,Literal,Optional
    >
    > lbrack=Literal("[").suppress()
    > rbrack=Literal("]").suppress()
    > ddot=Literal(":").suppress()
    > start = Word(nums+".")
    > step = Word(nums+".")
    > end = Word(nums+".")
    >
    > sequence=lbrack+start+Optional(ddot+step)+ddot+end+rbrack
    >
    > tokens = sequence.parseString("[0:0.1:1]")
    > print tokens
    >
    > tokens1 = sequence.parseString("[1:2]")
    > print tokens1
    >
    > It works on tokens, but the error message is showed on
    > the second string ("[1:2]"). I don't get it. I did use
    > Optional for ddot and step so I guess they are optional.
    >
    > Any hints what I am doing wrong?
    >
    > The versions are pyparsing 1.1.2 and Python 2.3.3.
    >
    > Thanks,
    >
    > B.

    Bostjan -

    Here's how pyparsing is processing your input strings:

    [0:0.1:1]
    [ = lbrack
    0 = start
    :0.1 = ddot + step (Optional match)
    : = ddot
    1 = end
    ] = rbrack

    [1:2]
    [ = lbrack
    1 = start
    :2 = ddot + step (Optional match)
    ] = oops! expected ddot -> failure


    Dang Griffith proposed one alternative construct, here's another, perhaps
    more explicit:
    lbrack + ( ( ddot + step + ddot + end ) | (ddot + end) ) + rbrack

    Note that the order of the inner construct is important, so as to not match
    ddot+end before trying ddot+step+ddot+end; '|' is a greedy matching
    operator, creating a MatchFirst object from pyparsing's class library. You
    could avoid this confusion by using '^', which generates an Or object:
    lbrack + ( (ddot + end) ^ ( ddot + step + ddot + end ) ) + rbrack
    This will evaluate both subconstructs, and choose the longer of the two.

    Or you can use another pyparsing helper, the delimited list
    lbrack + delimitedlist( Word(nums+"."), delim=":") + rbrack
    This implicitly suppresses delimiters, so that all you will get back are
    ["1","0.1","1"] in the first case and ["1","2"] in the second.

    Happy pyparsing!
    -- Paul
     
    Paul McGuire, May 13, 2004
    #3
  4. =?iso-8859-2?q?Bo=B9tjan_Jerko?=

    Paul McGuire Guest

    Re: pyparsing (errata)

    > Dang Griffith proposed one alternative construct, here's another, perhaps
    > more explicit:
    > lbrack + ( ( ddot + step + ddot + end ) | (ddot + end) ) + rbrack
    >


    should be:
    lbrack + start + ( ( ddot + step + ddot + end ) | (ddot + end) ) +
    rbrack

    > Note that the order of the inner construct is important, so as to not

    match
    > ddot+end before trying ddot+step+ddot+end; '|' is a greedy matching
    > operator, creating a MatchFirst object from pyparsing's class library.

    You
    > could avoid this confusion by using '^', which generates an Or object:
    > lbrack + ( (ddot + end) ^ ( ddot + step + ddot + end ) ) + rbrack


    should be:
    lbrack + start + ( (ddot + end) ^ ( ddot + step + ddot + end ) ) +
    rbrack

    > This will evaluate both subconstructs, and choose the longer of the two.
    >
    > Or you can use another pyparsing helper, the delimited list
    > lbrack + delimitedlist( Word(nums+"."), delim=":") + rbrack


    at least this one is correct! No, wait, I mis-cased delimitedList!
    should be:
    lbrack + delimitedList( Word(nums+"."), delim=":") + rbrack

    > This implicitly suppresses delimiters, so that all you will get back are
    > ["1","0.1","1"] in the first case and ["1","2"] in the second.
    >
    > Happy pyparsing!
    > -- Paul
    >
    >

    Sorry for the sloppiness,
    -- Paul
     
    Paul McGuire, May 14, 2004
    #4
  5. Re: pyparsing (errata)

    Paul,

    thanks for the explanation.

    Bo¹tjan

    On Fri, 14 May 2004, ._bogus_.com spake:
    >> Dang Griffith proposed one alternative construct, here's another, perhaps
    >> more explicit:
    >> lbrack + ( ( ddot + step + ddot + end ) | (ddot + end) ) +
    >> rbrack
    >>

    >
    > should be:
    > lbrack + start + ( ( ddot + step + ddot + end ) | (ddot + end)
    > ) +
    > rbrack
    >
    >> Note that the order of the inner construct is important, so as to
    >> not

    > match
    >> ddot+end before trying ddot+step+ddot+end; '|' is a greedy matching
    >> operator, creating a MatchFirst object from pyparsing's class
    >> library.

    > You
    >> could avoid this confusion by using '^', which generates an Or
    >> object: lbrack + ( (ddot + end) ^ ( ddot + step + ddot + end )
    >> ) + rbrack

    >
    > should be:
    > lbrack + start + ( (ddot + end) ^ ( ddot + step + ddot + end )
    > ) +
    > rbrack
    >
    >> This will evaluate both subconstructs, and choose the longer of the
    >> two.
    >>
    >> Or you can use another pyparsing helper, the delimited list
    >> lbrack + delimitedlist( Word(nums+"."), delim=":") + rbrack

    >
    > at least this one is correct! No, wait, I mis-cased delimitedList!
    > should be:
    > lbrack + delimitedList( Word(nums+"."), delim=":") + rbrack
    >
    >> This implicitly suppresses delimiters, so that all you will get
    >> back are ["1","0.1","1"] in the first case and ["1","2"] in the
    >> second.
    >>
    >> Happy pyparsing!
    >> -- Paul
    >>
    >>

    > Sorry for the sloppiness,
    > -- Paul
     
    =?iso-8859-2?q?Bo=B9tjan_Jerko?=, May 14, 2004
    #5
    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. Paul McGuire
    Replies:
    0
    Views:
    265
    Paul McGuire
    Dec 24, 2003
  2. Paul McGuire

    ANN: pyparsing 1.0.4 released

    Paul McGuire, Jan 9, 2004, in forum: Python
    Replies:
    0
    Views:
    309
    Paul McGuire
    Jan 9, 2004
  3. Paul McGuire
    Replies:
    1
    Views:
    266
    Dan Dang Griffith
    Apr 28, 2004
  4. Khoa Nguyen

    Pyparsing question

    Khoa Nguyen, May 17, 2004, in forum: Python
    Replies:
    1
    Views:
    448
    Paul McGuire
    May 17, 2004
  5. Paul McGuire

    [ANN] pyparsing 1.2 released

    Paul McGuire, Jun 21, 2004, in forum: Python
    Replies:
    0
    Views:
    277
    Paul McGuire
    Jun 21, 2004
Loading...

Share This Page