[SimpleParse] Re: SimpleParse debugging question
Brought to you by:
mcfletch
From: Mike C. F. <mcf...@ro...> - 2003-12-22 18:26:32
|
bru...@mi... wrote: > Mike, > > I'm trying out SimpleParse 2.0. The samples worked fine. I'm now > working on a very simple grammar and input file. Function > parser.parse() returns (0,[],N). No error message was output. How does > one debug in this system? Are there any error messages that can be > turned on in some way? I have no idea whether the problem is in my > grammar or input file, or both. > This is a question that's been coming up more often lately, so I'll respond on the simpleparse-users list as well as directly. The most reliable way to *build* a grammar for SimpleParse is to use test-driven development (aka TDD), so that you build, and test parsers for each production against the expected results for a given piece of text. You can see examples of this approach in the tests subdirectory. The easiest way to *debug* a completed grammar is to use the cut/ErrorOnFail directive (spelled "!") liberally throughout your grammar. This directive tells the parser "if you start parsing this production, and wind up not finishing, then raise a SyntaxError, rather than continuing to parse". For instance: ifStatement := 'if', !, ts, ':', suite says, if the parser encounters an 'if' (when looking for an ifStatement) and then fails to find some space, a colon, and then a suite of lines, this is an error condition which should be signaled to the user via a syntax error. You need to be careful that you add them only where there is a true SyntaxError on failure, i.e.: ifStatement := if1 / if2 if1 := 'if', !, ts, ':', suite if2 := 'if', !, ts, ':', statement Would likely not have the effect you want, as if2 could never match (if1 would always raise a SyntaxError in any case where if2 would match). You can read a little more about the cut directive here: http://simpleparse.sourceforge.net/simpleparse_grammars.html and see a real-world example of it's use in the examples/vrml_erronfail.py module, which is an error-on-fail-annotated version of the vrml.py module in the same directory. HTH, Mike _______________________________________ Mike C. Fletcher Designer, VR Plumber, Coder http://members.rogers.com/mcfletch/ |