pyparsing

Python-Parsing a SQL using pyparsing

你离开我真会死。 提交于 2019-12-04 16:28:15
I want to parse a complex SQL which has (inner join,outer join) and get the table names used in the SQL. I am able to get the table names if it is simple select but if the SQL has inner join ,left join like below then the result is giving only the first table. select * from xyz inner join dhf on df = hfj where z > 100 I am using the program similar what is present in the below link by Paul. http://pyparsing.wikispaces.com/file/view/select_parser.py/158651233/select_parser.py Can someone tell me how to get all the tables used in a SQL like below select * from xyz inner join dhf on df = hfj

Writing grammar rules for context sensitive elements using Pyparsing

给你一囗甜甜゛ 提交于 2019-12-04 16:10:31
I am trying to write a grammar for a set of sentences and using Pyparsing to parse it. These sentences tell what and how to search in a text file, and I need to convert them into corresponding regex search codes. However, there are some elements that are not really context-free and hence, I am finding it difficult to write production rules for them. Basically, my aim is to parse these sentences and then write regexes for them. Some examples of context-sensitive elements found in these sentences - LINE_CONTAINS phrase1 BEFORE {phrase2 AND phrase3} means in the line, phrase1 can come anywhere

List of dictionaries and pyparsing

只谈情不闲聊 提交于 2019-12-04 15:25:48
I'm using pyparsing to construct dictionaries that get appended to a list. When I do this the dictionaries get wrapped in an extra list and there is also an empty dict appended. I have no clue how to fix this. What I want is [{},{},{}] . I am getting [([{}],{})] Why the code from getDict give me what I want and not getDictParse? #! /usr/bin/env python from pyparsing import Literal, NotAny, Word, printables, Optional, Each, Combine, delimitedList, printables, alphanums, nums, White, OneOrMore, Group noParseList = [] parseList = [] def getDict(): return {'duck':'moose','cow':'ewe'} def

“Deparsing” a list using pyparsing

与世无争的帅哥 提交于 2019-12-04 15:24:40
Is it possible to give pyparsing a parsed list and have it return the original string? Owen S. Yes, you can if you've instructed the parser not to throw away any input. You do it with the Combine combinator. Let's say your input is: >>> s = 'abc,def, ghi' Here's a parser that grabs the exact text of the list: >>> from pyparsing import * >>> myList = Word(alphas) + ZeroOrMore(',' + Optional(White()) + Word(alphas)) >>> myList.leaveWhitespace() >>> myList.parseString(s) (['abc', ',', 'def', ',', ' ', 'ghi'], {}) To "deparse": >>> reconstitutedList = Combine(myList) >>> reconstitutedList

Parse line data until keyword with pyparsing

我与影子孤独终老i 提交于 2019-12-04 14:07:11
I'm trying to parse line data and then group them in list. Here is my script: from pyparsing import * data = """START line 2 line 3 line 4 END START line a line b line c END """ EOL = LineEnd().suppress() start = Keyword('START').suppress() + EOL end = Keyword('END').suppress() + EOL line = SkipTo(LineEnd()) + EOL lines = start + OneOrMore(start | end | Group(line)) start.setDebug() end.setDebug() line.setDebug() result = lines.parseString(data) results_list = result.asList() print(results_list) This code was inspired by another stackoverflow question: Matching nonempty lines with pyparsing

Pyparsing problem with operators

随声附和 提交于 2019-12-04 12:21:53
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") OOR = CaselessLiteral("or") ONOT = "-" TERM = (QUOTED | WWORD) EXPRESSION = operatorPrecedence(TERM, [

PyParsing: Is this correct use of setParseAction()?

非 Y 不嫁゛ 提交于 2019-12-04 09:48:36
问题 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 "before: %s" % tokens for index, course_number in enumerate(tokens[1:]): tokens[index + 1] = (tokens[0][0], course_number) print "after: %s" % tokens course = Group(DEPT_CODE + COURSE_NUMBER) # .setResultsName("Course") course_data = (course + ZeroOrMore

change string during pyparsing

家住魔仙堡 提交于 2019-12-04 09:47:16
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? 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): return "boo" + t[0] expr = Literal("A").setParseAction(changeText) + "B" print expr.parseString("A B").asList()

PyParsing: What does Combine() do?

走远了吗. 提交于 2019-12-04 07:52:27
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. 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 = Word(nums) + "." + Word(nums) Then realnum.parseString("3.14") will return a list of 3 tokens: the leading '3',

pyparsing whitespace match issues

北战南征 提交于 2019-12-04 07:34:26
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 robot, 2 and more whitespaces and '\t' are used to split the token, if there are only 1 whitespaces between