pyparsing

changing ** operator to power function using parsing?

最后都变了- 提交于 2019-12-02 02:28:39
My requirement is to change ** operator to power function For example 1.Input -"B**2" Output - power(B,2) 2."B**2&&T**2*X" Output - power(B,2) I have wrote following regular expression to address that problem rx=r"([a-zA-Z0-9]+)\*\*([a-zA-Z0-9()]+)" result = regex.sub(rx, r"power(\1,\2)", expression, 0, regex.IGNORECASE | regex.MULTILINE) But above code successfully converting expression similar to the example 1 and example 2, but failed to convert expression like (a+1)**2 or ((a+b)*c)**2 . I realized regular expression is not the best way to handle such scenarios. Instead of that parsing will

What does this use of << mean in Python

大兔子大兔子 提交于 2019-12-02 02:14:07
问题 I ran across this use of '<<' in a Python example using the pyparsing module: whereExpression << whereCondition + ZeroOrMore( ( and_ | or_ ) + whereExpression ) It clearly isn't a binary left shift operator, but I don't find it described in any Python reference. Can someone explain it? Thank you. 回答1: Like any operator, << can be overloaded by classes to define their own behavior. The example you give looks like it's from code using pyparsing. This is a parser library that overloads operators

What does this use of << mean in Python

无人久伴 提交于 2019-12-02 00:14:50
I ran across this use of '<<' in a Python example using the pyparsing module: whereExpression << whereCondition + ZeroOrMore( ( and_ | or_ ) + whereExpression ) It clearly isn't a binary left shift operator, but I don't find it described in any Python reference. Can someone explain it? Thank you. Like any operator, << can be overloaded by classes to define their own behavior. The example you give looks like it's from code using pyparsing . This is a parser library that overloads operators in this way. The << here updates the content of a previously-defined placeholder token. Read the

pyparsing, Each, results name

烈酒焚心 提交于 2019-12-01 23:17:23
I'm trying to use pyparsing to build a little not-quite-sql parser (I don't have from clauses, I don't have any joins, etc). I've been basing my work today on the simpleSQL.py example script included with pyparsing. I'm trying to add the "GROUP BY" and "ORDER BY" clauses to the parser, but trying to match them no matter which comes before the other. I'm using the Each class, and it seems to be matching them, but it does not set the result name from within the Each class. I am either not doing something right, or something is happening I'm not clear on. Again, from the simpleSQL.py example, I

How to match parentheses / brackets in pyparsing

℡╲_俬逩灬. 提交于 2019-12-01 20:38:43
I have a grammar token specified as: list_value = Suppress(oneOf("[ (")) + Group( delimitedList(string_value | int_value))("list") + Suppress(oneOf("] )")) However, this obviously allows (foo, bar] How do I enforce that the lists opening and closing characters must match? You make a list a choice between two rules: one for parentheses and one for square brackets. Thanks for bringing up pyparsing. I like it. My answer for your question is: delim_value = Group(delimitedList(string_value | int_value))("list") list_value = Or( (Suppress("[") + delim_value + Suppress("]"), Suppress("(") + delim

How to validate boolean expression syntax using pyparsing?

最后都变了- 提交于 2019-12-01 11:02:34
I'm using the Pyparsing library to evaluate simple boolean queries like these ones: (True AND True) OR False AND True (True AND (True OR False OR True)) Using the simpleBool script from the examples section ( simpleBool.py ), I've hit a snag when trying to validate the expression syntax. Expressions like the ones below are considered valid even tho they have clear syntax issues: (True AND True) OR False AND True OR OR (True AND (True OR False OR True))(((( Does anyone know how to validate syntax with Pyparsing? Here is the testing script, as requested: # # simpleBool.py # # Example of defining

How to validate boolean expression syntax using pyparsing?

坚强是说给别人听的谎言 提交于 2019-12-01 09:40:32
问题 I'm using the Pyparsing library to evaluate simple boolean queries like these ones: (True AND True) OR False AND True (True AND (True OR False OR True)) Using the simpleBool script from the examples section (simpleBool.py), I've hit a snag when trying to validate the expression syntax. Expressions like the ones below are considered valid even tho they have clear syntax issues: (True AND True) OR False AND True OR OR (True AND (True OR False OR True))(((( Does anyone know how to validate

Pyparsing - where order of tokens in unpredictable

假装没事ソ 提交于 2019-12-01 04:07:51
I want to be able to pull out the type and count of letters from a piece of text where the letters could be in any order. There is some other parsing going on which I have working, but this bit has me stumped! input -> result "abc" -> [['a',1], ['b',1],['c',1]] "bbbc" -> [['b',3],['c',1]] "cccaa" -> [['a',2],['c',3]] I could use search or scan and repeat for each possible letter, but is there a clean way of doing it? This is as far as I got: from pyparsing import * def handleStuff(string, location, tokens): return [tokens[0][0], len(tokens[0])] stype = Word("abc").setParseAction(handleStuff)

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

假如想象 提交于 2019-11-30 18:17:44
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( ':' ) ) expr = pp.Dict( pp.Group( field_name + \ pp.nestedExpr( content = pp.delimitedList( pp.Group( field

Recursive expressions with pyparsing

回眸只為那壹抹淺笑 提交于 2019-11-30 17:39:47
问题 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 result. If I try to prevent expr parsing from infinite recursion, i can do something like: expr -> Group(simple_expr + OP + expr) | simple_expr but then I'd get the expr OP (expr OR expr) result. How do I force left-side binding? Edit: I know about