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

  • Home
  • SEARCH
  • 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 8789903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:31:35+00:00 2026-06-13T22:31:35+00:00

Given the following minimal working example: from pyparsing import * latex_h = QuotedString($)(‘latex’) reg_text

  • 0

Given the following minimal working example:

from pyparsing import *
latex_h  = QuotedString("$")('latex')
reg_text = Word(alphas)('text')
grammar  = OneOrMore( latex_h | reg_text )('line')

sol = grammar.parseString('''dog $x^2$ cat''')
print sol.dump()

I expected the output to look like:

['dog', 'x^2', 'cat']
- line: ['dog', 'x^2', 'cat']
  - text: dog
  - latex: x^2
  - text: cat

but instead I got:

['dog', 'x^2', 'cat']
- latex: x^2
- line: ['dog', 'x^2', 'cat']
  - latex: x^2
  - text: cat
- text: cat

I don’t understand why in the parse tree did the dog get left behind? In addition, why are two elements of text, latex outside the line?

  • 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-06-13T22:31:36+00:00Added an answer on June 13, 2026 at 10:31 pm

    As Russell Borogove says, named results must be unique when on the same parsing level. You can’t have a “line” with two or more named elements of the same type (e.g. two “text” or two “latex”), since they’ll both use the same key in the underlying dictionary. I’ll defer to Paul McGuire regarding the listAllMatches solution in the latest PyParsing, seeing as he wrote it and all 🙂

    You can also work around this by attaching parsing actions to “latex_h” or “reg_text”, however this won’t help if the “latex_h” element requires knowledge of any sibling “reg_text” elements. In which case you will probably need to break up your grammar a bit further, or use a tree-based approach to parsing (working from the lowest element up to the root, by parse actions and/or by iterating through the list of results), rather than a dictionary-based approach.

    It’s important to note that the the parse tree did not leave “dog” behind. It was parsed correctly, it’s just that the parsed result did not get assigned to a dictionary. You can access the parsed value like so: sol.line[0]

    As for why ‘latex’ and ‘cat’ appear outside of the ‘line’, you need to put the OneOrMore definition within a Group().

    Here’s an example, which applies a parse action to the reg_text element at the time it’s parsed (rather than when any parent element, like grammar is parsed). It does not solve the ‘named result’ issue you are having, but without context on what you are trying to achieve with your parser I can’t suggest a solution.

    from pyparsing import *
    latex_h  = QuotedString("$")('latex')
    reg_text = Word(alphas)('text')
    grammar  = Group(OneOrMore( latex_h | reg_text ))('line')
    
    def parse_reg_text(s, loc, toks):
        if toks.text == 'dog':
            return "atomic " + toks.text
        else:
            return "ninja " + toks.text
    
    reg_text.setParseAction(parse_reg_text)
    
    sol = grammar.parseString('''dog $x^2$ cat $y^3$''')
    print sol.dump()
    

    This gives the following output:

    [['atomic dog', 'x^2', 'ninja cat', 'y^3']]
    - line: ['atomic dog', 'x^2', 'ninja cat', 'y^3']
      - latex: y^3
      - text: ninja cat
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Consider the following minimal example. #include <vector> class Data { std::vector<int>& v; public: Data(std::vector<int>&
i have following code from graph theory topics,kruskal algorithm for minimal spanning tree #include<iostream>
Given following Ruby statements: (Read input and store each word in array removing spaces
I should convert some code from VB to C#. Given following lines of VB
Given the following functional dependencies how would I compute the minimal cover: A ->
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like
Installing egit on indigo im given following options : I've just checked 'Eclipse Egit'
Given the following code: $(.force-selection).blur(function() { var value = $('matched-item').val(); //check if the input's

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.