[SimpleParse] Re: More Simpleparse advice
Brought to you by:
mcfletch
From: Mike C. F. <mcf...@ro...> - 2003-08-17 01:00:38
|
Paul Paterson wrote: >Mike, > >Thanks to your advice last time my VB grammar is now working very well. >Simpleparse is just amazing! However, I wondered if you had any advice >on how to handle parsing errors. When I throw some badly formed VB at >the grammar then the parser quite rightly reports an error. The issue is >that, since the grammar is deeply nested, the error is reported at the >outermost structure and it takes a long time to work out which is the >offending part. > >Have you had this issue before? > >Here's an simple example ... The grammar for a subroutine looks like >this: > >sub_definition ::= > label_definition?, (scope, wsp*)?, "Sub", wsp+, identifier, > > formal_param_list, line_end, block?, label_definition?, >"End Sub" > >And the "block" looks like this, > >block ::= > block_content+ > >block_content ::= > ?-block_terminator, line > >And the "line" looks like ... you get the idea (it's deeply nested!) > >Now if I throw some VB that looks like, > >Sub A() > For I = ... > While ... > If .. > <a bad line here> > End If > Wend > Next I >End Sub > >... And <a bad line here> doesn't parse, then the "line" doesn't match >and so I get a report that the "sub_definition" doesn't match. This is >entirely correct, but quite hard for the end-user to work out what is >going wrong in a long subroutine. > You are probably looking for a construct like so: block := blockstart, !, blockcontent?, blockend or more concretely... ifblock := 'If', condition, !, blockcontent, 'End',ts,'If' That is, once you hit a block-start, you must hit a block-end or there's a syntax error, so you include the cut and a syntax error will be raised when the system can't find the rest of the construct. Similarly, a line in a block is something like: blockcontent := lines+ line := !, line_content, line_end that is, if you are in a place where you need a line and there are no other options to match, and you can't match a line, that's a syntax error, so the error will get reported as coming from the start of the line. You need to be careful that you really do put the cuts (!) in after you have finally determined that a construct *must* match in all cases where it gets checked. There's no way to stop propagation of the error once it's raised. >I thought of skipping a line and then continuing but this is fraught >with pitfalls since a logical line can span multiple actual lines. Is >there a more robust way? Have I designed my grammar to be too nested? > Nesting is the "normal" approach for EBNF grammars. >Thanks for your patience in reading this long note - is there a more >appropriate place to ask questions like this? I don't want to clog your >inbox! > Clogging my inbox isn't a significant issue, I get > 1000 emails a day what with various lists and the like :) . SimpleParse users is the "proper" place to direct questions, so that other users can benefit from the discussion, I've copied it in this response. Have fun, Mike _______________________________________ Mike C. Fletcher Designer, VR Plumber, Coder http://members.rogers.com/mcfletch/ |