pyparsing

How to parse groups with operator and brackets

情到浓时终转凉″ 提交于 2020-07-22 05:34:06
问题 First I would like to say that I am just starting using pyparsing , and I need some help with the following. So here is the context: I have a file with text lines. Each line can have one sequence or can be a set of sequences combined with the concurrent operator || . The representation can either be (seq1)||(seq2)||... etc or simply seq1 . A sequence seq_ is a set of events, starting with a question followed by one or more answers, the sequence order is defined by the order of the answers on

How to get the original text back from a pyparsing token

南笙酒味 提交于 2020-07-19 04:22:09
问题 I have a text of the form name(sum(value1,sum(value2,value3)), "sumname") and pyparsing returns the appropiate tokens, however, I am interested in getting the real text back and I cannot find how. I have tried setParseAction with a function, but since it only returns string and location, I cannot cope with the trailing part. like, I will only get: "sum(value2,value3)), "sumname")" "sum(value1,sum(value2,value3)), "sumname")" "name(sum(value1,sum(value2,value3)), "sumname")" And this is not

Parsing logical sentence very slow with pyparsing

二次信任 提交于 2020-06-22 11:22:08
问题 I try to use pyparsing to parse logical expressions such as these x FALSE NOT x (x + y <= 5) AND (y >= 10) OR NOT (z < 100 OR w) (A=True OR NOT (G < 8) => S = J) => ((P = A) AND not (P = 1) AND (B = O)) => (S = T) ((P = T) AND NOT (K =J) AND (B = F)) => (S = O) AND ((P = T) OR (k and b => (8 + z <= 10)) AND NOT (a + 9 <= F)) => (7 = a + z) The code I wrote below seems to work OK -- but it is very slow (e.g. the last example above takes a few seconds). Did I structure the grammar in some

Translating an EBNF grammar to pyparsing give error

故事扮演 提交于 2020-06-17 07:07:11
问题 I am making a parser to convert a simple DSL into elasticsearch query. some of the possible queries are: response:success response:success AND extension:php OR extension:css response:sucess AND (extension:php OR extension:css) time >= 2020-01-09 time >= 2020-01-09 AND response:success OR os:windows NOT reponse:success response:success AND NOT os:windows I have written the following EBNF grammar for this : <expr> ::= <or> <or> ::= <and> (" OR " <and>)* <and> ::= <unary> ((" AND ") <unary>)*

Can't pickle Pyparsing expression with setParseAction() method. Needed for multiprocessing

我的梦境 提交于 2020-05-29 09:44:53
问题 My original issue is that I am trying to do the following: def submit_decoder_process(decoder, input_line): decoder.process_line(input_line) return decoder self.pool = Pool(processes=num_of_processes) self.pool.apply_async(submit_decoder_process, [decoder, input_line]).get() decoder is a bit involved to describe here, but the important thing is that decoder is an object that is initialized with PyParsing expression that calls setParseAction(). This fails pickle that multiprocessing uses and

How can I break a string into nested tokens?

。_饼干妹妹 提交于 2020-04-07 06:55:28
问题 I have strings made up of Boolean terms and equations, like so x=1 AND (x=2 OR x=3) AND NOT (x=4 AND x=5) AND (x=5) AND y=1 I would like to break up the x into groups that were separated by AND , while respecting parentheses as grouping operators. For example the result for the string above would be [['x=1'], ['x=2', 'x=3'], ['x=4'], ['x=5'], ['x=5']] x=2 and x=3 are in the same group because they are grouped by () and not separated by an AND . The last equation was ignored because it does

pyparsing语法解析心得

冷暖自知 提交于 2020-03-02 07:56:56
一直想总结一年来开发维护导表工具的心得,却因为懒或者项目紧而长期搁置着。最近一个milestone结束之后,有了短暂的空闲调整期,正好趁着这段时间系统得整理一下,也算是一种备份,方便以后查找。 开发起始,花了一定的时间调研寻找一个好的语法解析器,因为在表格安全性检查过程中需要解析各种形式灵活的检查规则,所以需要一个类似lex/yacc这样具有强大语言解析功能,但语法规则又可以轻量级配置的解析器,最后选择了一种近似上下文无关CFG(context-free-grammar)的语言PEG(parsing-expression-grammar),作为我们编写检查规则的基础文法。 PEG文法 要了解PEG文法的演变,首先得从CFG上下文无关文法讲起。对CFG文法的理解一直都停留在理论层面上,而语言{a n b n , n >= 1}(例子1)则是绝大多数人首先能够想起的一个上下文无关文法的典型例子。对于CFG的详细定义可以从一般计算理论书上找到,这里就不再累赘。就我个人理解,上下文无关语言就是对有限确定集合中的元素,按照有限的递归级联合并规则组合而成的语言,譬如例子1中的语言所对应的有限确定集合就是 V = {a, b}, 有限递归级联合并规则就是 S <-- ab S <-- aSb 这两条规则产生的语言S 就等于{a n b n , n >= 1}。而语言{a n b n c n ,

Trouble doing simple parse in pyparsing

老子叫甜甜 提交于 2020-02-03 23:45:51
问题 I'm having some basic problem using pyparsing. Below is the test program and the output of the run. aaron-mac:sql aaron$ more s.py from pyparsing import * n = Word(alphanums) a = Group( n | Group( n + OneOrMore( Suppress(",") + n ))) p = Group( a + Suppress(".") ) print a.parseString("first") print a.parseString("first,second") print p.parseString("first.") print p.parseString("first,second.") aaron-mac:sql aaron$ python s.py [['first']] [['first']] [[['first']]] Traceback (most recent call

Parse function call with PyParsing

妖精的绣舞 提交于 2020-01-23 10:47:12
问题 I'm trying to parse a simple language. The trouble comes with parsing function calls. I'm trying to tell it that a function call is an expression followed by left parenthesis, argument list, and right parenthesis. I have something like this: expr = Forward() iden = Word(alphas+'_', alphanums+'_') integer = Word(nums) binop = operatorPrecedence(expr, ...) # irrevelant call = expr + Literal('(') + delimitedList(expr) + Literal(')') expr << call | integer | iden The problem is obvious: expr is

Parsing JSON like format with pyparsing

帅比萌擦擦* 提交于 2020-01-23 08:12:26
问题 I have two somewhat related questions regarding parsing a JSON like data format using pyparsing . The goal is to parse this data and convert the result to JSON. 1) The first type data looks like mystr = """ DataName = { fieldA = { fieldB = 10 fieldC = "absf" } } DataName = { fieldA = { fieldB = 11 fieldC = "bsf" } } """ I'm wondering what the best way to set up the grammar is, in order to parse mystr into a list of dictionaries that would look like expected_result = [{"DataName": {"fieldA": {