I've written a Marpa-powered utility that reports mismatched brackets <https://gist.github.com/jeffreykegler/b6bfeeadfcedeade6519> in the set () {} []. It was written as a demo, but is quite useable as it is -- you may find it more accurate than the native compiler/interpreter for your language.

It works using the Ruby Slippers. It uses the new rejection events to stop parsing whenever the parser rejects all lexemes, so that the application can dummy up a new "Ruby Slippers" token. It makes its best guess as to how to correct the problem and proceeds. This makes the parse "self-correcting", allowing the utility to accurately diagnose a series of mismatched parentheses. Traditional parsers are usually hopeless after the first mismatch, and not any too clever about the first one.

To be sure, ultimately it comes down to the programmer's intent, but I think you'll find this utility is highly accurate in most practical situations. The formatting of the reports is (I hope) helpful. For the string '((([))', the diagnostics are:

* Line 1, column 1: Opening '(' never closed, problem detected at end of string
((([))
^
====================
* Line 1, column 4: Missing close ], problem detected at line 1, column 5
((([))
   ^^
Marpa is left-eidetic -- it knows everything about the parse to the left of the current position, and the utility exploits this. For the string '[({({x[]x{}x()x)})]', it spots the extra closing ')' at column 16, but it's also able to tell *why* it's wrong. It cannot accept the ')' at column 16, without first closing the set of curly braces started at column 5. Maybe your compiler is giving you this sort of feedback, but I doubt it.

* Line 1, column 5: Missing close }, problem detected at line 1, column 16
[({({x[]x{}x()x)})]
    ^          ^

While this utility is already useful, I think of it as a prototype for what could be done. This problem -- matching delimiters -- is in fact very general, and I believe these techniques have very wide application.

-- jeffrey

--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to