Error rules and completion events

9 views
Skip to first unread message

Jeffrey Kegler

unread,
May 16, 2013, 12:45:05 PM5/16/13
to Marpa Parser Mailing LIst
A few months ago, Peter Stuifzand, in his pioneering with Marpa, tried error rules.  An error rule is where you detect errors by including a rule for something that you do *NOT* want the application to do.  For example, if you don't want to allow an assignment in a conditional, you might try this rule:
<bad if statement> ::= 'if' '(' <assignment expression ')' <block or statement> action => admonish_user
This was another of Peter's good ideas, but I was forced to tell him that this was not the easiest way to detect errors.  Marpa did not quite have all the hooks needed to make this technique convenient.

With completion events, I think that situation has changed.  You could define a completion event for the above
event assignment_in_conditional = completed <bad if statement>
and throw a fatal error with a nice targeted error message when it you see the event.  This could do a lot to take the mystery out of error reports.

-- jeffrey










Peter Stuifzand

unread,
May 18, 2013, 11:35:18 AM5/18/13
to marpa-...@googlegroups.com
Thanks for reminding me. I hadn't thought of the connection to completion events yet. I have written a simple example that uses an error rule and completion events to warn users of their mistake. When you know what kind of mistakes people make, this can be an easy and effective way to warn them about it.

The code: https://gist.github.com/pstuifzand/5604827

In the example I use $line from line_column() which happens to point to the right spot. I guess I can use g1_location_to_span to find the exact line and column where the expression was found.

--
Peter Stuifzand | peterstuifzand.nl | @pstuifzand


--
You received this message because you are subscribed to the Google Groups "marpa parser" group.
To unsubscribe from this group and stop receiving emails from it, send an email to marpa-parser...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Durand Jean-Damien

unread,
May 18, 2013, 12:34:39 PM5/18/13
to marpa-...@googlegroups.com
About error rules there is another case? Suppose we have a G0 rule like that:

# Skip bad character
ANYTHING_ELSE ~ [.]
:discard ~ ANYTHING_ELSE

Obviously we would like to trigger a warning if the grammar decides to skip a bad character.
This is quite elegant way to go "further" with a incorrect source. As a use case, the C grammar have that, c.f. at the end of this lex file.
But there is no way to trigger something on it because this is not a lexeme.
I wonder if opening events, or pause, to all of G0 is possible and/or the best way to achieve this.

Regards, Jean-Damien.

Jeffrey Kegler

unread,
May 18, 2013, 4:24:33 PM5/18/13
to marpa-...@googlegroups.com
I think what you are after can be accomplished by

1.)  Change ANYTHING_ELSE from :discard to pause => before.
2.)  When the pause occurs, trigger the warning.
3.)  Then resume() *after* the lexeme.  You can use pause_span() to find out where it ends.

-- jeffrey

Durand Jean-Damien wrote:

Durand Jean-Damien

unread,
May 18, 2013, 5:34:25 PM5/18/13
to marpa-...@googlegroups.com
But when ANYTHING_ELSE is not a lexeme, pause is not allowed
Reply all
Reply to author
Forward
0 new messages