Basic idea behind error recovery in recursive-descent parsers

I see that modern SW basically dominated by recursive descent parsers. These parsers try grammar production and back track to an alternative one in case of failure. Suppose BNF is

program = (declaration)* "begin" (statement)*
declaration = type_declaration | variable_declaration | function_declaration
type_declaration = "range" name "to" name | "enum" (name)*
variable_declaration = name ":" type_name
statement = ...


and suppose that user has entered user entered "range 10 to '15'" when declaration expected. The parser has failed to match the first type pattern but it also failed to match other alternatives and declarations and failed to parse the program. How do you recover? Should you track the "best match", which would be location of the "range 10 to '15'" and look for keywords met afterwards or what? Which mechanisms are used in practice, in popular parser combinators and generators like JavaCC?

• "I see that modern SW basically dominated by recursive descent parsers" -- really? My impression was that most real-world parsers in compilers used LR variants. – Raphael Jan 13 '16 at 15:12
• You are asking lots of questions. Please restrict yourself to one per post. – Raphael Jan 13 '16 at 15:13
• @Raphael Which many questions? – Valentin Tihomirov Jan 13 '16 at 15:19
• I count five. "How do you recover?", "Does my idea make sense?", plus three reference requests (which we are probably not the right site for) in the last sentence. – Raphael Jan 13 '16 at 15:20
• As you seem interested in PEG and not if CFG, arxiv.org/pdf/1405.6646.pdf seems relevant. – AProgrammer Jan 13 '16 at 19:33

Another is to add productions for typical errors in some constructs, e.g. handle a b as if it was a * b in case of expressions in a compiler for newbies.
• Can you explain what are we trying to match? Your sync token is mine keyword, as I understand. I have presented a demo grammar. Suppose that ; must follow every declaration and statement. How can you find what can follow the ; and exploit it automatically in PEG, which simply tries every alternative and says "nothing was found to match the program" if you make a slight error? – Valentin Tihomirov Jan 13 '16 at 13:02