Re: [Pyparsing] More useful parse errors
Brought to you by:
ptmcg
From: Paul M. <pa...@al...> - 2006-08-14 22:56:00
|
Chris - I have struggled with this behavior since version 0.1 of pyparsing. I have made a faint stab at addressing this using the ParseFatalException: when a parse action detects some semantic incorrectness in input it can raise ParseFatalException which will stop all parsing and report the exact location. But since pyparsing uses exceptions as signals to proceed to the next grammar expression, it's hard to know in advance which exceptions are really, well, exceptional. If anyone can dig up some research on how other recursive-descent parsers address this error-handling issue, I may be able to fold it into a future release. But I can't delve into this kind of research on my own at the moment (pyparsing doesn't pay like my day job does, and isn't likely to anytime soon). -- Paul > -----Original Message----- > From: pyp...@li... > [mailto:pyp...@li...] On > Behalf Of Chris Lambacher > Sent: Friday, August 11, 2006 4:59 PM > To: pyp...@li... > Subject: [Pyparsing] More useful parse errors > > Hi, > > I am using pyparsing to parse CIM mof files. It is a rather > lengthy grammar > with lots of instances of OneOrMore. The top level structure > is a like this: > > bnf = OneOrMore(rest_of_grammar) + StringEnd() > > This raises an exception when there is a parse error, but the > error is not > flagged at the point where the error occurs. Since everyone > here should be > familiar with Python grammar, I will provide an example of > what would happen > if we had a pyparsing grammar for python and attempted to > parse something with > a syntax error: > > > class blah(object): > pass > > class error_says_here(object): > def __init__(self): > self.a = 1 > self.b = 2 > > def error_really_here(self: > pass > > In this case the error would flag as having occurred at > error_says_here rather > than error_really_here. > > Is there a way to work around this behavior? I have not yet > come across an > example of a grammar that looks like it does a really good > job of error > handling. Does the architecture of pyparsing make it easy to > write a parser > for data that you know is syntacticly correct, but difficult > or impossible to > write a parser that can give useful error reports to the user > in the face of > syntax errors? > > Traditional parsers would know all possible tokens that could > be next and if > it encountered something that wasn't one of the above tokens, > it would flag an > error at the location. pyparsing in contrast says, will this > parse, no, ok > will this parse, no, ok, will this parse? The result is that > it can throw > away large portions of valid syntax to eventually end > immediately after parsing > the last full rest_of_grammar from our OneOrMore(rest_of_grammar) + > StringEnd() idiom. > > It may be possible for pyparsing to precompute what the > possible next tokens > are, which may have the side effect of making pyparsing more > efficient since > it won't have so much trial and error. > > -Chris > > > -------------------------------------------------------------- > ----------- > Using Tomcat but need to do more? Need to support web > services, security? > Get stuff done quickly with pre-integrated technology to make > your job easier > Download IBM WebSphere Application Server v.1.0.1 based on > Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057& > dat=121642 > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > > |