I am using pyparsing to parse a hex string and I am searching for an automatic way of print the parser tree.
A near approach is command dump but it print a lot of duplicated info.
For example:
from pyparsing import * #Word, Optional, OneOrMore, Group, ParseException
data = Forward()
arrayExpr = Forward()
def data_array(s,l,t):
n = int(t[0], 16)
arrayExpr << ( n * data)
return t[0]
array = Word(hexnums, exact=2).setParseAction(data_array) + arrayExpr
data << (Literal('01') + array.setResultsName('array')
| Literal('03') + Word(hexnums, exact=2)('char')
| Literal('04') + Word(hexnums, exact=2)('boolean'))
frame = (Word(hexnums, exact=2)('id') \
+ data('data'))('frame')
result = frame.parseString("02010203010302");
print result.dump()
The goal is that result of result.dump() was something similar to
- frame: ['02', '01', '03', '03', '01', '04', '02', '03', '02']
- id: 02
- array: ['03', '03', '01', '04', '02', '03', '02']
- char: 01
- boolean: 02
- char: 02
The pretty print isn't mandatory, the pretended is the tree structure.
Is there a way of make this print or I will need to had a setParseAction for all rules ?