Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

token made up of ? amount of smaller tokens?

kecleon
2012-03-03
2013-05-14
  • kecleon
    kecleon
    2012-03-03

    Lets say you have tokens A, B and C. And you want to define a token D which contains unknown amount of these tokens. How do you create such token D?
    By parsign D you might get something which you would by parsing ABBACB or BCCA. I hope I could explain what I want.

     

  • Anonymous
    2012-03-04

    I am not sure what you are expecting for the result. Is this close?

    from pyparsing import Literal, Group, ZeroOrMore
    A = Literal('A')
    B = Literal('B')
    C = Literal('C')
    D = Group(ZeroOrMore(A^B^C))
    print(D.parseString('ABBACB'))
    print(D.parseString('BCCA')
    

    A, B and C could be whatever you want. I just made them literals to go with your example.

    This produces:

    ([(['A', 'B', 'B', 'A', 'C', 'B'], {})], {})
    

    and

    ([(['B', 'C', 'C', 'A'], {})], {})
    
     

  • Anonymous
    2012-03-04

    Or even just this:

    E = ZeroOrMore(A|B|C)
    print(E.parseString('ABBACB'))
    print(E.parseString('BCCA'))
    

    produces:

    ['A', 'B', 'B', 'A', 'C', 'B']
    ['B', 'C', 'C', 'A']