Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:52:16+00:00 2026-05-31T19:52:16+00:00

I am using pyparsing to parse a hex string and I am searching for

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:52:17+00:00Added an answer on May 31, 2026 at 7:52 pm

    Looks like you’ll need a setParseAction for each of the rules.

    From parsing to object hierarchy: “Attach parse actions to each expression, but here is the trick: use a class instead of a function. The class’s init method will get called, and return an instance of that class. “

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pythonistas: Suppose you want to parse the following string using Pyparsing: 'ABC_123_SPEED_X 123' were
I'm using pyparsing to parse vcd (value change dump) files. Essentially, I want to
I'm using pyparsing to parse HTML. I'm grabbing all embed tags, but in some
I'm writing a parser for a query language using PyParsing, and I've gotten stuck
I'm using pyparsing to parse documents containing text in which the line ends vary
I have written a large (to me) grammar using pyparsing to parse H248 message,
I am trying to parse text using pyparsing. My function is shown below. Firstly,
I am using HTML Agility Pack to parse html content. I am using parsing
I am using feedparser for parsing from XML file.But I couldn't parse <geo:lat> ,
I've been considering using Haskell's Parsec parsing library to parse a subset of Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.