gocc icon indicating copy to clipboard operation
gocc copied to clipboard

parser: Update error-recovery mode implemented in gocc

Open shivansh opened this issue 7 years ago • 3 comments

The following steps are performed when an invalid input symbol is encountered -

  • In case the input reaches an invalid symbol, we save its error attribute and keep discarding all the immediately following error symbols until a valid symbol is found.

  • If the action corresponding to the found valid symbol is reduce, it is performed. In case it is a shift, it is deferred until the saved error attribute is pushed on the stack.

  • Once the saved error attribute is pushed on stack, parsing can resume normally.

  • It should be noted that the current model cannot report multiple errors for invalid symbols occurring one after the other. If such a case arises, the error for only the first invalid symbol is reported. This is also the case with the currently implemented error recovery model in gocc.

Demo for error-recovery example

=== RUN   TestFail
input: a ; b
output: [
	a
Error in S0: INVALID(0,;), Pos(offset=2, line=1, column=3), expected one of: $ id error 
	b
]
--- PASS: TestFail (0.00s)
PASS
ok  	github.com/goccmack/gocc/example/errorrecovery	0.002s
input: a ; ; b
output: [
	a
Error in S0: INVALID(0,;), Pos(offset=2, line=1, column=3), expected one of: $ id error 
	b
]
--- PASS: TestFail (0.00s)
PASS
ok  	github.com/goccmack/gocc/example/errorrecovery	0.002s
input: a ; b ; c
output: [
	a
Error in S0: INVALID(0,;), Pos(offset=2, line=1, column=3), expected one of: $ id error 
	b
Error in S0: INVALID(0,;), Pos(offset=6, line=1, column=6), expected one of: $ id error 
	c
]
--- PASS: TestFail (0.00s)
PASS
ok  	github.com/goccmack/gocc/example/errorrecovery	0.002s

It should be noted that there might be scope of further refactoring the code in this PR. I'll defer doing it until the proposed heuristic has been verified.

shivansh avatar May 10 '18 04:05 shivansh

Am I understanding correctly that this is an attempt to fix https://github.com/goccmack/gocc/issues/76

I wouldn't call this a heuristic, but rather just a possibly better way to do error recovery.

I am sick though, so I probably missed something.

@goccmack this is probably something worth checking out. What do you think?

awalterschulze avatar May 11 '18 17:05 awalterschulze

Am I understanding correctly that this is an attempt to fix #76

Yes.

I wouldn't call this a heuristic, but rather just a possibly better way to do error recovery.

oic, updating the title and PR message.

I am sick though, so I probably missed something.

Get well soon :)

shivansh avatar May 12 '18 02:05 shivansh

@shivansh put his finger on one of the aspects of gocc that didn't receive sufficient attention originally. If I remember correctly I tried to implement error recovery according to the dragon book "Error Recovery in LR Parsing" but under time pressure. I think @shivansh is on the right track.

goccmack avatar May 14 '18 07:05 goccmack