From e110a6ff4073799d00c7d3400ac022f758a03e40 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Wed, 18 Aug 2021 00:03:59 +0900 Subject: Set look-ahead symbols to items before generating a SLR(1) parsing table --- grammar/lalr1.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'grammar/lalr1.go') diff --git a/grammar/lalr1.go b/grammar/lalr1.go index fc15942..f1b8149 100644 --- a/grammar/lalr1.go +++ b/grammar/lalr1.go @@ -42,7 +42,24 @@ func genLALR1Automaton(lr0 *lr0Automaton, prods *productionSet, first *firstSet) } if p.isEmpty() { - state.emptyProdItems = append(state.emptyProdItems, item) + var reducibleItem *lrItem + for _, it := range state.emptyProdItems { + if it.id != item.id { + continue + } + + reducibleItem = it + break + } + if reducibleItem == nil { + return nil, fmt.Errorf("reducible item not found: %v", item.id) + } + if reducibleItem.lookAhead.symbols == nil { + reducibleItem.lookAhead.symbols = map[symbol]struct{}{} + } + for a := range item.lookAhead.symbols { + reducibleItem.lookAhead.symbols[a] = struct{}{} + } propDests = append(propDests, &stateAndLRItem{ kernelID: state.id, -- cgit v1.2.3