Printing a piping result tree

I use pyparsing to parse a hexadecimal string, and I'm looking for an automatic way to print a parser tree.

The closest approach is a team dump, but it prints a lot of duplicated information.

For instance:

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 the result of result.dump () was something like

  - 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 

A cute print is optional, pretending to be a tree structure.

Is there a way to make this fingerprint, or do I need to have setParseAction for all the rules?

+6
source share
2 answers

It looks like you will need setParseAction for each of the rules.

From parsing an object hierarchy : "Attach parsing actions to each expression, but here's the trick: use a class instead of a function. The class method init is called and returns an instance of that class."

+4
source

Prefer to add an answer instead of editing a question so that a lot of code ..

Not perfect, levels do not get right, and classes can be dropped if I can get resultName from printAction. Maybe I should create a new question: - /

If someone uses it and improves, tell me how :)

 #!/usr/bin/python from pyparsing import * #Word, Optional, OneOrMore, Group, ParseException data = Forward() level = 0 arrayExpr = Forward() def data_array(s,l,t): n = int(t[0], 16) arrayExpr << ( n * data) return t[0] class TreeChild(object): def __init__(self,t): self.args = t def __str__(self): ret = " %s: " % self.name return ' ' * level + ret + self.args[0] + "\n" class TreeBranch(object): def __init__(self,t): self.args = t def __str__(self): global level level = level + 1 childs = " ".join(map(str,self.args)) level = level - 1 ret = " %s: " % self.name + '\n' return ' ' * level + ret + childs + "\n" class Frame(TreeBranch): name = 'frame' class Char(TreeChild): name = 'char' class Boolean(TreeChild): name = 'boolean' class Id(TreeChild): name = 'id' class Array(TreeBranch): name = 'array' array = Suppress(Word(hexnums, exact=2).setParseAction(data_array)) + arrayExpr data << (Suppress(Literal('01')) + array.setResultsName('array').setParseAction(Array) | Suppress(Literal('03')) + Word(hexnums, exact=2)('char').setParseAction(Char) | Suppress(Literal('04')) + Word(hexnums, exact=2)('boolean').setParseAction(Boolean)) frame = (Word(hexnums, exact=2)('id').setParseAction(Id) \ + data('data'))('frame').setParseAction(Frame) result = frame.parseString("020103030104020302"); print result[0] 
+2
source

Source: https://habr.com/ru/post/911527/


All Articles