pyparsing

Why is ordered choice in pyparsing failing for my use case?

不问归期 提交于 2019-12-23 03:06:17
问题 >>> g = MatchFirst( Literal("scoobydoo"), Literal("scooby") ) >>> g.parseString( "scooby" ) pyparsing.ParseException: Expected "scoobydoo" (at char 0), (line:1, col:1) Is the ParseException thrown because the scooby has already been consumed in the character stream & thus the parser cannot backtrack ? I'm looking for a detailed implementation explanation for this. At the moment, I consider this a bug because why would the parser short-circuit the matching since it has not search all the

Pyparsing problem with operators

末鹿安然 提交于 2019-12-21 19:52:59
问题 I did a grammar with pyparsing, and I have a problem. The grammar tries to parse a search query (with operator precedence, parenthesis, etc), and I need for spaces to work like the and operator. For example, this works fine: (word and word) or word But this fails: (word word) or word And I want the second query to works like the first one. My actual grammar is: WWORD = printables.replace("(", "").replace(")", "") QUOTED = quotedString.setParseAction(removeQuotes) OAND = CaselessLiteral("and")

Access parsed elements using Pyparsing

我的梦境 提交于 2019-12-21 06:16:23
问题 I have a bunch of sentences which I need to parse and convert to corresponding regex search code. Examples of my sentences - LINE_CONTAINS phrase one BEFORE {phrase2 AND phrase3} AND LINE_STARTSWITH Therefore we -This means in the line, phrase one comes somewhere before phrase2 and phrase3 . Also, the line must start with Therefore we LINE_CONTAINS abc {upto 4 words} xyz {upto 3 words} pqr -This means I need to allow upto 4 words between the first 2 phrases and upto 3 words between last 2

Parse mathematical expressions with pyparsing

二次信任 提交于 2019-12-21 02:19:09
问题 I'm trying to parse a mathematical expression using pyparsing. I know i could just copy the example calculator from pyparsing site, but i want to understand it so i can add to it later. And i'm here because i tried to understand the example, and i couldn't, so i tried my best, and i got to this: symbol = ( pp.Literal("^") | pp.Literal("*") | pp.Literal("/") | pp.Literal("+") | pp.Literal("-") ) operation = pp.Forward() atom = pp.Group( pp.Literal("(").suppress() + operation + pp.Literal(")")

Can't get pyparsing Dict() to return nested dictionary

[亡魂溺海] 提交于 2019-12-18 19:46:13
问题 I'm trying to parse strings of the form: 'foo(bar:baz;x:y)' I'd like the results to be returned in form of a nested dictionary, i.e. for the above string, the results should look like this: { 'foo' : { 'bar' : 'baz', 'x' : 'y' } } Despite numerous combinations of Dict() and Group() I can't get it to work. My (one of the versions of) grammar looks like this: import pyparsing as pp field_name = pp.Word( pp.alphanums ) field_value = pp.Word( pp.alphanums ) colon = pp.Suppress( pp.Literal( ':' )

How do I parse indents and dedents with pyparsing?

人盡茶涼 提交于 2019-12-18 11:12:03
问题 Here is a subset of the Python grammar: single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE stmt: simple_stmt | compound_stmt simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE small_stmt: pass_stmt pass_stmt: 'pass' compound_stmt: if_stmt if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT (You can read the full grammar in the Python SVN repository: http://svn.python.org/.../Grammar) I am trying to use this

Pyparsing: Parsing semi-JSON nested plaintext data to a list

社会主义新天地 提交于 2019-12-18 05:14:19
问题 I have a bunch of nested data in a format that loosely resembles JSON: company="My Company" phone="555-5555" people= { person= { name="Bob" location="Seattle" settings= { size=1 color="red" } } person= { name="Joe" location="Seattle" settings= { size=2 color="blue" } } } places= { ... } There are many different parameters with varying levels of depth--this is just a very small subset. It also might be worth noting that when a new sub-array is created that there is always an equals sign

Parsing text usng Combine is not returning any results

空扰寡人 提交于 2019-12-14 02:29:30
问题 I am new to pyparsing. I am attempting to parse some text but don't really understand how pyparsing is behaving. from pyparsing import * number = Word(nums) yearRange = Combine(number+"-"+number) copyright = Literal("Copyright (C)")+yearRange+Literal("CA. All Rights Reserved.") copyrightCombine = Combine(copyright) date = Combine(Word(nums)+"/"+Word(nums)+"/"+Word(nums)) time = Combine(Word(nums)+":"+Word(nums)+":"+Word(nums)) dateTime = Combine(date+time) pageNumber = Suppress(Literal("PAGE"

Tcl file parser for PYTHON

半城伤御伤魂 提交于 2019-12-13 18:28:47
问题 I have a .tcl file. Is there any parser available which directly extracts data from .tcl file ? I don't want to use REGEX for this task. Is pyparsing will work for this problem ? I am using Python 2.7 回答1: .tcl files are not data files, they are programming scripts, written in the Tcl programming language. The Tcl language is extremely flexible in form and style, which makes writing a general-purpose parser a substantial project, whether in pyparsing or any other package. I encourage people,

parsing file with curley brakets

谁都会走 提交于 2019-12-13 16:03:14
问题 I need to parse a file with information separated by curly brackets, for example: Continent { Name Europe Country { Name UK Dog { Name Fiffi Colour Gray } Dog { Name Smut Colour Black } } } Here is what I have tried in Python from io import open from pyparsing import * import pprint def parse(s): return nestedExpr('{','}').parseString(s).asList() def test(strng): print strng try: cfgFile = file(strng) cfgData = "".join( cfgFile.readlines() ) list = parse( cfgData ) pp = pprint.PrettyPrinter(2