Hi,
I'm fairly new to the pyparsing and the mailing list, but I have
browsed through the email archives but I can't seem find how to parse
statements in any order.
#### Start Code ####
from pyparsing import *
Ident = Word(alphas, alphanums+"_")
type_name = Ident.setResultsName("name")
type = Group(Suppress(Keyword("type", caseless=True))+
type_name).setResultsName("type")
types = Group(ZeroOrMore(type)).setResultsName("types")
type2_name = Ident.setResultsName("name")
type2 = Group(Suppress(Keyword("type2", caseless=True))+
type_name).setResultsName("type2")
types2 = Group(ZeroOrMore(type2)).setResultsName("types2")
content = Each([types, types2])
grammar = And([content]).setResultsName("content")
parser = grammar.parseString( testString )
parser.asXML()
####
It successfully parses the following input:
type IDL
type ODL
type2 Ika
type2 Nfkb
and produces
<content>
<types>
<type>
<name>IDL</name>
<type>
<type>
<name>ODL</name>
<type>
</types>
<types2>
<type2>
<name>IDL</name>
<type2>
<type2>
<name>Nfkb</name>
<type2>
</types2>
</content>
But does not parse correctly the next input, which is basically the
types swapped over:
type2 Ika
type2 Nfkb
type IDL
type ODL
It produces the following without errors; It misses the second set of types:
<content>
<types>
</types>
<types2>
<type2>
<name>IDL</name>
<type2>
<type2>
<name>Nfkb</name>
<type2>
</types2>
</content>
Ideally I'd like the parser to pick up both sets of types and place
them in their respective collection(namely types and types): e.g it
should be able to process the second input and the following and be
able to produce the first output or similar:
type2 Ika
type IDL
type2 Nfkb
type ODL
Any help would greatly be appreciated.
Regards
Chiraj
|