From ed2c20102659f4c8aef0e88ea604e91fb56f25f6 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Mon, 6 Sep 2021 23:59:43 +0900 Subject: Change semantic action APIs The driver reports whether it recovered from an error to the semantic action APIs via the argument `recovered`. --- driver/parser.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'driver/parser.go') diff --git a/driver/parser.go b/driver/parser.go index 1e7af29..af257e2 100644 --- a/driver/parser.go +++ b/driver/parser.go @@ -85,20 +85,22 @@ ACTION_LOOP: case act < 0: // Shift nextState := act * -1 + recovered := false if p.onError { + p.shiftCount++ + // When the parser performs shift three times, the parser recovers from the error state. - if p.shiftCount < 3 { - p.shiftCount++ - } else { + if p.shiftCount >= 3 { p.onError = false p.shiftCount = 0 + recovered = true } } p.shift(nextState) if p.semAct != nil { - p.semAct.Shift(tok) + p.semAct.Shift(tok, recovered) } tok, err = p.nextToken() @@ -108,9 +110,11 @@ ACTION_LOOP: case act > 0: // Reduce prodNum := act + recovered := false if p.onError && p.gram.ParsingTable.RecoverProductions[prodNum] != 0 { p.onError = false p.shiftCount = 0 + recovered = true } accepted := p.reduce(prodNum) @@ -123,7 +127,7 @@ ACTION_LOOP: } if p.semAct != nil { - p.semAct.Reduce(prodNum) + p.semAct.Reduce(prodNum, recovered) } default: // Error if p.onError { -- cgit v1.2.3