Questions tagged [pyparsing]

The pyparsing module is an alternative approach to creating and executing simple grammars, vs. the traditional lex/yacc approach, or the use of regular expressions. The pyparsing module provides a library of classes that client code uses to construct the grammar directly in Python code.

Pyparsing is a Python module for creating and executing simple grammars. This is an alternative approach to the traditional lex/yacc or regular expressions. The pyparsing module provides a library of classes that client code uses to construct the grammar directly in Python code.

981 questions
8
votes
2 answers

Recursive expressions with pyparsing

I'm trying to figure out how to do a left-associative expression where recursive (not-enclosed in anything) expressions are possible. For example, I'd like to do: expr + OP + expr that parses 2 operations like 1 x 2 x 3 into (expr OP expr) OP expr…
viraptor
  • 33,322
  • 10
  • 107
  • 191
8
votes
1 answer

Define the variable grammar C# using pyparsing

How to find all the variables in the source C# code? Here's my non-working grammar, which does not work because ZeroOrMore (VarDef4) Protected = Literal("protected") Private = Literal("private") Public = Literal("public") Modification =…
farabiDev
  • 81
  • 3
8
votes
3 answers

Parsing Snort Logs with PyParsing

Having a problem with parsing Snort logs using the pyparsing module. The problem is with separating the Snort log (which has multiline entries, separated by a blank line) and getting pyparsing to parse each entry as a whole chunk, rather than read…
Sam Halicke
  • 194
  • 2
  • 9
8
votes
1 answer

PyParsing: What does Combine() do?

What is the difference between: foo = TOKEN1 + TOKEN2 and foo = Combine(TOKEN1 + TOKEN2) Thanks. UPDATE: Based on my experimentation, it seems like Combine() is for terminals, where you're trying to build an expression to match on, whereas plain…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
8
votes
3 answers

Distinguish matches in pyparsing

I want to parse some words and some numbers with pyparsing. Simple right. from pyparsing import * A = Word(nums).setResultsName('A') B = Word(alphas).setResultsName('B') expr = OneOrMore(A | B) result = expr.parseString("123 abc 456 7 d") print…
Jakube
  • 3,353
  • 3
  • 23
  • 40
8
votes
2 answers

the trick to nested structures in pyparsing

I am struggling to parse nested structures with PyParsing. I've searched many of the 'nested' example uses of PyParsing, but I don't see how to fix my problem. Here is what my internal structure looks like: texture_unit optionalName { texture…
cyrf
  • 5,127
  • 6
  • 25
  • 42
8
votes
2 answers

Getting a pure list out of 'pyparsing.ParseResults'

I'm currently trying to get a result from pyparsing as a pure list so I can flatten it. I read in the documentation that ParseResults can also be converted to an ordinary list of strings by calling asList(). Note that this will strip the results of…
Dietmar Winkler
  • 942
  • 5
  • 11
7
votes
2 answers

Print a tree of pyparsing result

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,…
Nine
  • 118
  • 1
  • 6
7
votes
1 answer

Pyparsing, parsing the contents of php function comment blocks using nested parsers

AKA "Add sub-nodes constructed from the results of a Parser.parseAction to the parent parse tree" I'm trying to parse PHP files using PyParsing (Which rules IMHO) whereby the function definitions have been annotated with JavaDoc style annotations.…
Bryan Hunt
  • 3,685
  • 2
  • 24
  • 36
7
votes
3 answers

How to parse JSON-XML hybrid file in Python

I would like to parse a file with the following syntax (but with an indefinite number of nesting) with Python: { "str2" 123.0 { 1 2 3 4 5 6 6 "str" "str" 43 "str" 4543 } { …
norok2
  • 25,683
  • 4
  • 73
  • 99
7
votes
5 answers

Is there a library similar to pyparsing in Java?

I need to quickly build a parser for a very simplified version of a html-like markup language in Java. In python, I would use pyparsing library to do this. Is there something similar for Java? Please, don't suggest libraries already out there for…
VoY
  • 5,479
  • 2
  • 37
  • 45
7
votes
1 answer

pyparsing: named results?

I am writing a parser to parse mathematical expressions, which contain variables among other things. I want a list of all the captured variables. But I am only getting the last captured variable. Below is a minimal example to show the problem. …
Tima
  • 113
  • 6
7
votes
4 answers

PyParsing: Is this correct use of setParseAction()?

I have strings like this: "MSE 2110, 3030, 4102" I would like to output: [("MSE", 2110), ("MSE", 3030), ("MSE", 4102)] This is my way of going about it, although I haven't quite gotten it yet: def makeCourseList(str, location, tokens): print…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
7
votes
1 answer

pyparsing whitespace match issues

I tried to use pyparsing to parse robotframework, which is a text based DSL. The sytnax is like following ( sorry, but I think it's a little hard for me to describe it in BNF). a single line in robotframework may looks like: Library\tSSHClient …
python
  • 1,870
  • 4
  • 24
  • 35
7
votes
1 answer

Incremental but complete parsing with PyParsing?

I'm using PyParsing to parse some rather large text files with a C-like format (braces and semicolons and all that). PyParsing works just great, but it is slow and consumes a very large amount of memory due to the size of my files. Because of this,…
Dan Lenski
  • 76,929
  • 13
  • 76
  • 124
1 2
3
65 66