pyparsing problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • name

    pyparsing problem

    Hi,

    I try to parse a file with pyparsing and get this output:

    ['generic', 'host-01', 'host alias xyz', '10.0.0.1']
    - alias: host alias xyz
    - host_name: ['host-01']
    - ip_address: ['10.0.0.1']
    - use: ['generic']


    <Hosts>
    <ITEM>generic </ITEM>
    <host_name>ho st-01</host_name>
    <alias>host alias xyz</alias>
    <ip_address>10. 0.0.1</ip_address>
    </Hosts>

    ['generic', 'host-01', 'host alias xyz', '10.0.0.1']

    finished

    What I don't understand is why I get the line

    <ITEM>generic </ITEM>

    and not

    <use>generic</use>

    as there is an associated name in the dump() output.

    Thank you very much in advance for any clue or help you could
    provide.



    The code:
    ---------

    #!/usr/bin/env python

    from pyparsing import *

    sample = """
    define host{
    use generic
    host_name host-01
    alias host alias xyz
    address 10.0.0.1
    }
    """

    # define tokens
    LBRACE,RBRACE = map(Suppress, '{}')
    ipAddress = Combine(Word(nu ms) + ('.' + Word(nums))*3)
    useTemplate = oneOf('generic user')

    # define grammar

    deviceName = Word(alphanums+ '-')
    hostDef = Literal('define ').suppress() + Literal('host') .suppress()
    useLine = Suppress('use') + useTemplate + Suppress(SkipTo (lineEnd))
    host_nameLine = Suppress('host_ name') + deviceName + Suppress(SkipTo
    (lineEnd))
    aliasLine = Suppress('alias ') + SkipTo(lineEnd)
    aliasLine.setPa rseAction(lambd a x: ' '.join(x[0].split()))
    ip_addressLine = Suppress('addre ss') + ipAddress

    use = useLine.setResu ltsName('use')
    host_name = host_nameLine.s etResultsName(' host_name')
    alias = aliasLine.setRe sultsName('alia s')
    ip_address = ip_addressLine. setResultsName( 'ip_address')


    host_stmt = (use + host_name + alias + ip_address)
    host = hostDef + LBRACE + host_stmt + RBRACE

    test_file = OneOrMore(host) + stringEnd


    # test
    x = test_file.parse String(sample)
    print x.dump()
    print
    print x.asXML('Hosts' )
    print
    print x.asList()
    print

    print 'finished'

  • Paul McGuire

    #2
    Re: pyparsing problem

    On Jul 1, 8:02 am, name <x...@y.zwrot e:
    Hi,
    >
    I try to parse a file with pyparsing and get this output:
    >
        ['generic', 'host-01', 'host alias xyz', '10.0.0.1']
        - alias: host alias xyz
        - host_name: ['host-01']
        - ip_address: ['10.0.0.1']
        - use: ['generic']
    >
        <Hosts>
          <ITEM>generic </ITEM>
          <host_name>ho st-01</host_name>
          <alias>host alias xyz</alias>
          <ip_address>10. 0.0.1</ip_address>
        </Hosts>
    >
        ['generic', 'host-01', 'host alias xyz', '10.0.0.1']
    >
        finished
    >
    What I don't understand is why I get the line
    >
        <ITEM>generic </ITEM>
    >
    and not
    >
        <use>generic</use>
    >
    as there is an associated name in the dump() output.
    >
    Thank you very much in advance for any clue or help you could
    provide.
    >
    The code:
    ---------
    >
    #!/usr/bin/env python
    >
    from pyparsing import *
    >
    sample = """
    define host{
      use                   generic
      host_name             host-01
      alias                 host alias xyz
      address                       10.0.0.1}
    >
    """
    >
    # define tokens
    LBRACE,RBRACE = map(Suppress, '{}')
    ipAddress = Combine(Word(nu ms) + ('.' + Word(nums))*3)
    useTemplate = oneOf('generic user')
    >
    # define grammar
    >
    deviceName = Word(alphanums+ '-')
    hostDef = Literal('define ').suppress() + Literal('host') .suppress()
    useLine = Suppress('use') + useTemplate + Suppress(SkipTo (lineEnd))
    host_nameLine = Suppress('host_ name') + deviceName + Suppress(SkipTo
    (lineEnd))
    aliasLine = Suppress('alias ') + SkipTo(lineEnd)
    aliasLine.setPa rseAction(lambd a x: ' '.join(x[0].split()))
    ip_addressLine = Suppress('addre ss') + ipAddress
    >
    use = useLine.setResu ltsName('use')
    host_name = host_nameLine.s etResultsName(' host_name')
    alias = aliasLine.setRe sultsName('alia s')
    ip_address = ip_addressLine. setResultsName( 'ip_address')
    >
    host_stmt = (use + host_name + alias + ip_address)
    host = hostDef + LBRACE + host_stmt  + RBRACE
    >
    test_file = OneOrMore(host) + stringEnd
    >
    # test
    x = test_file.parse String(sample)
    print x.dump()
    print
    print x.asXML('Hosts' )
    print
    print x.asList()
    print
    >
    print 'finished'
    Looks like this is a bug in asXML(). Note that if I reverse the use
    and host_name strings in the input and in your grammar, I get this XML
    output:

    <Hosts>
    <ITEM>host-01</ITEM>
    <use>generic</use>
    <alias>host alias xyz</alias>
    <ip_address>10. 0.0.1</ip_address>
    </Hosts>

    Fortunately, you have provided a nice short test case, which should
    allow me to track down the problem.

    Thanks,
    -- Paul

    Comment

    • name

      #3
      Re: pyparsing problem

      Paul McGuire <ptmcg@austin.r r.comwrote in news:be7af822-70d7-44fb-96fa-
      78b490192334@r6 6g2000hsg.googl egroups.com:
      >
      Looks like this is a bug in asXML(). Note that if I reverse the use
      and host_name strings in the input and in your grammar, I get this XML
      output:
      >
      <Hosts>
      <ITEM>host-01</ITEM>
      <use>generic</use>
      <alias>host alias xyz</alias>
      <ip_address>10. 0.0.1</ip_address>
      </Hosts>
      >
      Fortunately, you have provided a nice short test case, which should
      allow me to track down the problem.
      >
      Thanks,
      -- Paul
      >
      You are welcome! And I would like to thank you for this outstanding tool!

      Thanks,
      Bernhard

      Comment

      • Paul McGuire

        #4
        Re: pyparsing problem

        On Jul 1, 8:02 am, name <x...@y.zwrot e:
        Hi,
        >
        I try to parse a file with pyparsing and get this output:
        >
            ['generic', 'host-01', 'host alias xyz', '10.0.0.1']
            - alias: host alias xyz
            - host_name: ['host-01']
            - ip_address: ['10.0.0.1']
            - use: ['generic']
        >
            <Hosts>
              <ITEM>generic </ITEM>
              <host_name>ho st-01</host_name>
              <alias>host alias xyz</alias>
              <ip_address>10. 0.0.1</ip_address>
            </Hosts>
        >
            ['generic', 'host-01', 'host alias xyz', '10.0.0.1']
        >
            finished
        >
        What I don't understand is why I get the line
        >
            <ITEM>generic </ITEM>
        >
        and not
        >
            <use>generic</use>
        >
        as there is an associated name in the dump() output.
        >
        Thank you very much in advance for any clue or help you could
        provide.
        >
        The code:
        ---------
        >
        #!/usr/bin/env python
        >
        from pyparsing import *
        >
        sample = """
        define host{
          use                   generic
          host_name             host-01
          alias                 host alias xyz
          address                       10.0.0.1}
        >
        """
        >
        # define tokens
        LBRACE,RBRACE = map(Suppress, '{}')
        ipAddress = Combine(Word(nu ms) + ('.' + Word(nums))*3)
        useTemplate = oneOf('generic user')
        >
        # define grammar
        >
        deviceName = Word(alphanums+ '-')
        hostDef = Literal('define ').suppress() + Literal('host') .suppress()
        useLine = Suppress('use') + useTemplate + Suppress(SkipTo (lineEnd))
        host_nameLine = Suppress('host_ name') + deviceName + Suppress(SkipTo
        (lineEnd))
        aliasLine = Suppress('alias ') + SkipTo(lineEnd)
        aliasLine.setPa rseAction(lambd a x: ' '.join(x[0].split()))
        ip_addressLine = Suppress('addre ss') + ipAddress
        >
        use = useLine.setResu ltsName('use')
        host_name = host_nameLine.s etResultsName(' host_name')
        alias = aliasLine.setRe sultsName('alia s')
        ip_address = ip_addressLine. setResultsName( 'ip_address')
        >
        host_stmt = (use + host_name + alias + ip_address)
        host = hostDef + LBRACE + host_stmt  + RBRACE
        >
        test_file = OneOrMore(host) + stringEnd
        >
        # test
        x = test_file.parse String(sample)
        print x.dump()
        print
        print x.asXML('Hosts' )
        print
        print x.asList()
        print
        >
        print 'finished'
        Try downloading the latest pyparsing.py file from the SourceForge SVN
        repository. This bug should be fixed in the latest version.

        -- Paul

        Comment

        Working...