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 8381105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:37:22+00:00 2026-06-09T16:37:22+00:00

I have the following test code for my PyParsing grammar: from pyparsing import Word,

  • 0

I have the following test code for my PyParsing grammar:

from pyparsing import Word, nums, alphas, delimitedList, Group, oneOf
from pprint import pprint

field = Word(alphas)("field")
operator = oneOf("= +")("operator")
string_value = Word(alphas)("string")
int_value = Word(nums).setParseAction(lambda t: int(t[0]))("int")
value = (string_value | int_value )("value")
expression = Group(field + operator + value)("expression")
grammar = Group(delimitedList(expression, delim="&&"))("expr_list")

def test(s):
    print "Parsing '{0}'".format(s)
    tokenized = grammar.parseString(s)
    for f in tokenized:
        e = f.expression
        pprint(dict(e.items()))

if __name__ == "__main__":
    test("foo=1")
    test("foo=1 && bar=2")
    test("foobar=2 && snakes=4")

Output is quite unexpected – seems that I only get the last expression in tokenized:

Parsing 'foo=1'
{'field': 'foo', 'int': 1, 'operator': '=', 'value': 1}
Parsing 'foo=1 && bar=2'
{'field': 'bar', 'int': 2, 'operator': '=', 'value': 2}
Parsing 'foobar=2 && snakes=4'
{'field': 'snakes', 'int': 4, 'operator': '=', 'value': 4}

How do I fix this?

  • 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-09T16:37:23+00:00Added an answer on June 9, 2026 at 4:37 pm

    Untested, but I think you just need to change:

    expression = (field + operator + value)("expression")
    

    to:

    expression = Group(field + operator + value)("expression")
    

    EDIT: okay, one other change. Your iteration code looks for multiple items named ‘expression’. There are multiple items named ‘expression’ inside the ‘&&’-delimited list. It is simpler not to reference these by their name, but by iterating over the grouped expressions inside ‘expr_list’:

    for f in tokenized['expr_list']: 
        field = f['field']
        op = f['operator']
        value = f['value']
        print field, op, value
    

    I usually use the dump method on parsed results to see just how the data has been grouped and named. If I print out tokenized.dump() I get:

    [[['foo', '=', 1], ['bar', '=', 2]]]
    - expr_list: [['foo', '=', 1], ['bar', '=', 2]]
      - expression: ['bar', '=', 2]
        - field: bar
        - int: 2
        - operator: =
        - value: 2
    

    I can see that I can get at the ‘expr_list’ named value. I also see that there is a sub-level ‘expression’, but as these keys are by default unique like in a dict, there is only a value for the group that was parsed last. But I can access the multiple groups inside ‘expr_list’ – if I look at the 0’th item (using print tokenized['expr_list'][0].dump()), I get:

    ['foo', '=', 1]
    - field: foo
    - int: 1
    - operator: =
    - value: 1
    

    So I can iterate over the groups in the ‘expr_list’ using:

    for f in tokenized['expr_list']: 
        field = f['field']
        op = f['operator']
        value = f['value']
        print field, op, value
    

    and I’ll get:

    foo = 1
    bar = 2
    

    It isn’t necessary to put results names on every level within your grammar – in this case, we got the expressions by iterating through expr_list and didn’t even use expression. And in fact, if you take the Group of the outermost grammar expression, you don’t need ‘expr_list’ either, just iterate for f in tokenized:.

    When trying to tease out the contents of your returned ParseResults, the dump method is probably the best tool.

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

Sidebar

Related Questions

I have the following test code. import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; class MyTask
I have the following code test_A.py which mocks MyClass.mymethod: from unittest import main from
I have following test code executed on Windows: import multiprocessing import time def child()
I have the following test code use Data::Dumper; my $hash = { foo =>
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
I have the following code in test.sh: while getopts f:i: opt; do case $opt
I have the following code: class Test { public static void main(String[] args) {
I have the following code require 'test_helper' class ApplicationControllerTest < ActionController::TestCase test should display
I have following code public class TEST { public static void main(String arg[]){ try
I have following code: //preparing DAO objects FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(test-context.xml); surveyDao =

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.