pyparsing

change string during pyparsing

雨燕双飞 提交于 2019-12-06 04:20:54
问题 In my pyparsing code I have the following expressions: exp1 = Literal("foo") + Suppress(Literal("=")) + Word(alphanums+'_-') exp2 = Literal("foo") + Suppress(Literal("!=")) + Word(alphanums+'_-') exp = Optional(exp1) & Optional(exp2) I want to change foo in exp2 to bar, so that I can distinguish between = and != in the parsed data. Is this possible? 回答1: Karl Knechtel's comment is valid, but if you want to change a matched token, you can do this in a parse action. def changeText(s,l,t):

PyParsing: What does Combine() do?

不想你离开。 提交于 2019-12-06 02:35:19
问题 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 + is for non-terminals. But I'm not sure. 回答1: Combine has 2 effects: it concatenates all the tokens into a single string it requires the matching tokens to all be adjacent with no intervening whitespace If you create an expression like realnum =

pyparsing whitespace match issues

本小妞迷上赌 提交于 2019-12-06 01:39:06
问题 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 with name\tnode \t is tab, and in robotframework, it is transparently transfered to 2 " "(In fact, it just call str.replace('\t', ' ') to replace the tab, but it will modified the actually length of each line, len('\t') is 1 but len(' ') is 2.). In

PyParsing lookaheads and greedy expressions

瘦欲@ 提交于 2019-12-05 16:31:20
问题 I'm writing a parser for a query language using PyParsing, and I've gotten stuck on (what I believe to be) an issue with lookaheads. One clause type in the query is intended to split strings into 3 parts (fieldname,operator, value) such that fieldname is one word, operator is one or more words, and value is a word, a quoted string, or a parenthesized list of these. My data look like author is william author is 'william shakespeare' author is not shakespeare author is in (william,'the bard'

Parsing a TCL-like text

旧巷老猫 提交于 2019-12-05 14:34:30
I have a configuration text that looks like this: text=""" key1 value1 key2 { value1 value2 } key3 subkey1 { key1 1 key2 2 key3 { value1 } } BLOBKEY name { dont { # comment parse { me } } } key3 subkey2 { key1 value1 } """ The values are plain strings or quoted strings. The keys are just alphanum strings. I know before hand that key2 and key3.subkey1.key4 will hold sets, so I can treat those paths differently. Likewise, I know that BLOBKEY will contain an "escaped" configuration section. The goal is to convert it into a dictionary that looks like this: {'key1': 'value1', 'key2': set(['value1',

context in pyparsing parse actions besides globals

馋奶兔 提交于 2019-12-05 01:50:50
问题 I'd like to be able to parse two (or any number) of expressions, each with their own set of variable definitions or other context. There doesn't seem to be an obvious way to associate a context with a particular invocation of pyparsing.ParseExpression.parseString() . The most natural way seems to be to use an instancemethod of some class as the parse actions. The problem with this approach is that the grammar must be redefined for each parse context (for instance, in the class's __init__ ),

pyparsing one query format to another one

廉价感情. 提交于 2019-12-05 01:32:27
问题 I am at a loss. I have been trying to get this to work for days now. But I am not getting anywhere with this, so I figured I'd consult you guys here and see if someone is able to help me! I am using pyparsing in an attempt to parse one query format to another one. This is not a simple transformation but actually takes some brains :) The current query is the following: ("breast neoplasms"[MeSH Terms] OR breast cancer[Acknowledgments] OR breast cancer[Figure/Table Caption] OR breast cancer

Does Pyparsing Support Context-Sensitive Grammars?

旧巷老猫 提交于 2019-12-05 00:31:50
问题 Forgive me if I have the incorrect terminology; perhaps just getting the "right" words to describe what I want is enough for me to find the answer on my own. I am working on a parser for ODL (Object Description Language), an arcane language that as far as I can tell is now used only by NASA PDS (Planetary Data Systems; it's how NASA makes its data available to the public). Fortunately, PDS is finally moving to XML, but I still have to write software for a mission that fell just before the

pyparsing: how to get token location?

ぃ、小莉子 提交于 2019-12-04 19:25:32
I have a simple pyparsing grammar that matches numbers separated by spaces: from pyparsing import * NUMBER = Word( nums ) STATEMENT = ZeroOrMore( NUMBER ) print( STATEMENT.parseString( "1 2 34" ) ) Given 1 2 34 test string it returns 3 strings that are parsed tokens. But how do I find the location of each token in the original string? I need it for "kind of" syntax highlighting. Add this parse action to NUMBER: NUMBER.setParseAction(lambda locn,tokens: (locn,tokens[0])) Parse actions can be passed the tokens that were parsed for a given expression, the location of the parse, and the original

How do I get PyParsing set up on the Google App Engine?

若如初见. 提交于 2019-12-04 19:04:07
I saw on the Google App Engine documentation that http://www.antlr.org/ Antlr3 is used as the parsing third party library. But from what I know Pyparsing seems to be the easier to use and I am only aiming to parse some simple syntax. Is there an alternative? Can I get pyparsing working on the App Engine? "Just do it"!-) Get pyparsing.py, e.g. from here , and put it in your app engine app's directory; now you can just import pyparsing in your app code and use it. For example, tweak the greeting.py from here to be: from pyparsing import Word, alphas greet = Word( alphas ) + "," + Word( alphas )