5 ms·
There are multiple problems that missing delimiters introduce for the parser when trying to recover. It depends heavily on the language's valid grammar how much
by estebank 5y ago
There are multiple problems that missing delimiters introduce for the parser when trying to recover. It depends heavily on the language's valid grammar how much you can infer about malformed code to identify likely places where the missing delimiter should go.
One thing you can do (that Rust does) is keep a stack of opened delimiters and their positions, and pop them as you find the closing one. If you encounter a mismatch or any other parse error, you look through the open delimiters to match for indentation level or valid alternatives. The more "flag posts" or redundancy the grammar has, the more likely you are to be able to recover from malformed code. If you are in a language with a sparse grammar, recovery is less feasible.