Thread: [Pyparsing] setResultsName: request for clarification
Brought to you by:
ptmcg
From: stefaan.himpe <ste...@gm...> - 2008-05-01 15:55:27
|
Hello, I have difficulties in understanding how to use setResultsName correctly. The problem is illustrated in the code below: In the first version, r.BODY is empty. In the second version, it contains what I expected it to contain. But I am not sure what the difference is between these two versions? Thanks for any insights. Best regards, Stefaan. --- import pyparsing as p s = """ SECTION { // { if (1) { dosomething; } else { if (0) then { a; } else { b } } printf ( "{" ); /* { { { */ { nog; iets; } } """ # VERSION 1 section_body = p.Combine(p.nestedExpr("{","}", ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) section_body.setParseAction(p.keepOriginalText) section_body.setResultsName("BODY") grammar = p.CaselessKeyword("SECTION").suppress() + section_body r = grammar.parseString(s) print r.BODY # VERSION 2 section_body = p.Combine(p.nestedExpr("{","}", ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) section_body.setParseAction(p.keepOriginalText) grammar = p.CaselessKeyword("SECTION").suppress() + \ section_body.setResultsName("BODY") r = grammar.parseString(s) print r.BODY |
From: Ralph C. <ra...@in...> - 2008-05-01 16:09:53
|
Hi Stefaan, > I have difficulties in understanding how to use setResultsName > correctly. The problem is illustrated in the code below: In the first > version, r.BODY is empty. In the second version, it contains what I > expected it to contain. But I am not sure what the difference is > between these two versions? > > # VERSION 1 > section_body = p.Combine(p.nestedExpr("{","}", > ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) > section_body.setParseAction(p.keepOriginalText) > section_body.setResultsName("BODY") > grammar = p.CaselessKeyword("SECTION").suppress() + section_body > r = grammar.parseString(s) > print r.BODY > > # VERSION 2 > section_body = p.Combine(p.nestedExpr("{","}", > ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) > section_body.setParseAction(p.keepOriginalText) > grammar = p.CaselessKeyword("SECTION").suppress() + \ > section_body.setResultsName("BODY") > r = grammar.parseString(s) > print r.BODY I would guess the intention is you can do address = ... command = address.setResultsName('from') + \ address.setResultsName('to') So your `version 1' is having little effect. Cheers, Ralph. |
From: Stefaan H. <ste...@gm...> - 2008-05-01 16:27:22
|
Hello Ralph, > I would guess the intention is you can do > > address = ... > command = address.setResultsName('from') + \ > address.setResultsName('to') > > So your `version 1' is having little effect. Thank you! I hadn't looked at it this way. What to me seemed like totally inconsistent behaviour makes kind of sense now. As I understand it now, giving a resultsname only has an effect when using a parser as opposed to when defining one. Thanks! Stefaan. |
From: Eike W. <eik...@gm...> - 2008-05-01 16:18:29
|
On Thursday 01 May 2008 17:54, stefaan.himpe wrote: > Hello, > > I have difficulties in understanding how to use setResultsName > correctly. The problem is illustrated in the code below: > In the first version, r.BODY is empty. In the second version, it > contains what I expected it to contain. But I am not sure what the > difference is between these two versions? setResultsName creates a copy of the object. The original is left unchanged. > > Thanks for any insights. > > Best regards, > Stefaan. > > --- > > import pyparsing as p > > s = """ > SECTION > { > // { > if (1) { dosomething; } else { if (0) then { a; } else { b } } > printf ( "{" ); /* { > { > { > */ > { nog; iets; } > } > """ > > # VERSION 1 > section_body = p.Combine(p.nestedExpr("{","}", > ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) > section_body.setParseAction(p.keepOriginalText) This statement has no effect. The parser with the name attached is forgotten. > section_body.setResultsName("BODY") > grammar = p.CaselessKeyword("SECTION").suppress() + section_body > r = grammar.parseString(s) > print r.BODY > > > # VERSION 2 > section_body = p.Combine(p.nestedExpr("{","}", > ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) > section_body.setParseAction(p.keepOriginalText) > grammar = p.CaselessKeyword("SECTION").suppress() + \ > section_body.setResultsName("BODY") > r = grammar.parseString(s) > print r.BODY > Kind regards, Eike. |
From: stefaan.himpe <ste...@gm...> - 2008-05-01 16:40:38
|
> setResultsName creates a copy of the object. The original is left > unchanged. Hello Eike, Thanks a lot! Now I completely understand. Best regards, Stefaan. |